Skip to content

Commit 059acfc

Browse files
committed
implement resolution of guard condition expressions
1 parent 74da69e commit 059acfc

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
@@ -758,7 +758,14 @@ impl<'ra: 'ast, 'ast, 'tcx> Visitor<'ast> for LateResolutionVisitor<'_, 'ast, 'r
758758
fn visit_pat(&mut self, p: &'ast Pat) {
759759
let prev = self.diag_metadata.current_pat;
760760
self.diag_metadata.current_pat = Some(p);
761-
visit::walk_pat(self, p);
761+
762+
match p.kind {
763+
// We visit only the subpattern, allowing the condition to be resolved later in `resolve_pat`.
764+
PatKind::Guard(ref subpat, _) => self.visit_pat(subpat),
765+
// Otherwise, we just walk the pattern.
766+
_ => visit::walk_pat(self, p),
767+
}
768+
762769
self.diag_metadata.current_pat = prev;
763770
}
764771
fn visit_local(&mut self, local: &'ast Local) {
@@ -3751,7 +3758,7 @@ impl<'a, 'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
37513758
/// See the implementation and `fresh_binding` for more details.
37523759
fn resolve_pattern_inner(
37533760
&mut self,
3754-
pat: &Pat,
3761+
pat: &'ast Pat,
37553762
pat_src: PatternSource,
37563763
bindings: &mut SmallVec<[(PatBoundCtx, FxHashSet<Ident>); 1]>,
37573764
) {
@@ -3811,6 +3818,15 @@ impl<'a, 'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
38113818
// Prevent visiting `ps` as we've already done so above.
38123819
return false;
38133820
}
3821+
PatKind::Guard(ref subpat, ref cond) => {
3822+
self.with_rib(ValueNS, RibKind::Normal, |this| {
3823+
this.resolve_pattern_inner(subpat, pat_src, bindings);
3824+
this.resolve_expr(cond, None);
3825+
});
3826+
3827+
// Prevent visiting `pat` as we've already done so above.
3828+
return false;
3829+
}
38143830
_ => {}
38153831
}
38163832
true

0 commit comments

Comments
 (0)