Skip to content

Commit 20a26ad

Browse files
committed
implement resolution of guard condition expressions
1 parent c3b93b1 commit 20a26ad

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

compiler/rustc_ast/src/ast.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -607,7 +607,7 @@ impl Pat {
607607
/// Walk top-down and call `it` in each place where a pattern occurs
608608
/// starting with the root pattern `walk` is called on. If `it` returns
609609
/// false then we will descend no further but siblings will be processed.
610-
pub fn walk(&self, it: &mut impl FnMut(&Pat) -> bool) {
610+
pub fn walk<'s>(&'s self, it: &mut impl FnMut(&'s Pat) -> bool) {
611611
if !it(self) {
612612
return;
613613
}

compiler/rustc_resolve/src/late.rs

+18-2
Original file line numberDiff line numberDiff line change
@@ -750,7 +750,14 @@ impl<'ra: 'ast, 'ast, 'tcx> Visitor<'ast> for LateResolutionVisitor<'_, 'ast, 'r
750750
fn visit_pat(&mut self, p: &'ast Pat) {
751751
let prev = self.diag_metadata.current_pat;
752752
self.diag_metadata.current_pat = Some(p);
753-
visit::walk_pat(self, p);
753+
754+
match p.kind {
755+
// We visit only the subpattern, allowing the condition to be resolved later in `resolve_pat`.
756+
PatKind::Guard(ref subpat, _) => self.visit_pat(subpat),
757+
// Otherwise, we just walk the pattern.
758+
_ => visit::walk_pat(self, p),
759+
}
760+
754761
self.diag_metadata.current_pat = prev;
755762
}
756763
fn visit_local(&mut self, local: &'ast Local) {
@@ -3737,7 +3744,7 @@ impl<'a, 'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
37373744
/// See the implementation and `fresh_binding` for more details.
37383745
fn resolve_pattern_inner(
37393746
&mut self,
3740-
pat: &Pat,
3747+
pat: &'ast Pat,
37413748
pat_src: PatternSource,
37423749
bindings: &mut SmallVec<[(PatBoundCtx, FxHashSet<Ident>); 1]>,
37433750
) {
@@ -3797,6 +3804,15 @@ impl<'a, 'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
37973804
// Prevent visiting `ps` as we've already done so above.
37983805
return false;
37993806
}
3807+
PatKind::Guard(ref subpat, ref cond) => {
3808+
self.with_rib(ValueNS, RibKind::Normal, |this| {
3809+
this.resolve_pattern_inner(subpat, pat_src, bindings);
3810+
this.resolve_expr(cond, None);
3811+
});
3812+
3813+
// Prevent visiting `pat` as we've already done so above.
3814+
return false;
3815+
}
38003816
_ => {}
38013817
}
38023818
true

0 commit comments

Comments
 (0)