Skip to content

gather_locals: only visit guard pattern guards when checking the guard #141275

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion compiler/rustc_hir_typeck/src/gather_locals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,12 @@ impl<'a, 'tcx> Visitor<'tcx> for GatherLocalsVisitor<'a, 'tcx> {
);
}
let old_outermost_fn_param_pat = self.outermost_fn_param_pat.take();
intravisit::walk_pat(self, p);
if let PatKind::Guard(subpat, _) = p.kind {
// We'll visit the guard when checking it. Don't gather its locals twice.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I don't understand this. When is the guard expression checked, if not here?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's checked in check_pat_inner; see the check_expr_has_type_or_error call there.

self.visit_pat(subpat);
} else {
intravisit::walk_pat(self, p);
}
self.outermost_fn_param_pat = old_outermost_fn_param_pat;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//@ check-pass
//! Test that `GatherLocalsVisitor` only visits expressions in guard patterns when checking the
//! expressions, and not a second time when visiting the pattern. If locals are declared inside the
//! the guard expression, it would ICE if visited twice ("evaluated expression more than once").

#![feature(guard_patterns)]
#![expect(incomplete_features)]

fn main() {
match (0,) {
// FIXME(guard_patterns): liveness lints don't work yet; this will ICE without the `_`.
(_ if { let _x = false; _x },) => {}
_ => {}
}
}
Loading