Skip to content

Commit e268fcd

Browse files
committed
implement resolution of guard condition expressions
1 parent b6e3630 commit e268fcd

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

compiler/rustc_ast/src/ast.rs

Lines changed: 1 addition & 1 deletion
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

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -732,7 +732,14 @@ impl<'a: 'ast, 'ast, 'tcx> Visitor<'ast> for LateResolutionVisitor<'a, '_, 'ast,
732732
fn visit_pat(&mut self, p: &'ast Pat) {
733733
let prev = self.diag_metadata.current_pat;
734734
self.diag_metadata.current_pat = Some(p);
735-
visit::walk_pat(self, p);
735+
736+
match p.kind {
737+
// We visit only the subpattern, allowing the condition to be resolved later in `resolve_pat`.
738+
PatKind::Guard(ref subpat, _) => self.visit_pat(subpat),
739+
// Otherwise, we just walk the pattern.
740+
_ => visit::walk_pat(self, p),
741+
}
742+
736743
self.diag_metadata.current_pat = prev;
737744
}
738745
fn visit_local(&mut self, local: &'ast Local) {
@@ -3645,7 +3652,7 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
36453652
/// See the implementation and `fresh_binding` for more details.
36463653
fn resolve_pattern_inner(
36473654
&mut self,
3648-
pat: &Pat,
3655+
pat: &'ast Pat,
36493656
pat_src: PatternSource,
36503657
bindings: &mut SmallVec<[(PatBoundCtx, FxHashSet<Ident>); 1]>,
36513658
) {
@@ -3705,6 +3712,15 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
37053712
// Prevent visiting `ps` as we've already done so above.
37063713
return false;
37073714
}
3715+
PatKind::Guard(ref subpat, ref cond) => {
3716+
self.with_rib(ValueNS, RibKind::Normal, |this| {
3717+
this.resolve_pattern_inner(subpat, pat_src, bindings);
3718+
this.resolve_expr(cond, None);
3719+
});
3720+
3721+
// Prevent visiting `pat` as we've already done so above.
3722+
return false;
3723+
}
37083724
_ => {}
37093725
}
37103726
true

0 commit comments

Comments
 (0)