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

Conversation

dianne
Copy link
Contributor

@dianne dianne commented May 20, 2025

When checking a pattern with guards in it, GatherLocalsVisitor will visit both the pattern (when type-checking the let, arm, or param containing it) and local declarations in the guard expression (when checking the guard itself). This keeps it from visiting the guard when visiting the pattern, since otherwise it would gather locals from the guard twice, which would lead to a delayed bug: "evaluated expression more than once".

Tracking issue for guard patterns: #129967

When checking a pattern with guards in it, `GatherLocalsVisitor` will
visit both the pattern (when type-checking the let, arm, or param
containing it) and the guard expression (when checking the guard
itself). This keeps it from visiting the guard when visiting the
pattern, since otherwise it would gather locals from the guard twice,
which would lead to a delayed bug: "evaluated expression more than
once".
@rustbot
Copy link
Collaborator

rustbot commented May 20, 2025

r? @nnethercote

rustbot has assigned @nnethercote.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels May 20, 2025
@@ -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.

Copy link
Member

@compiler-errors compiler-errors left a comment

Choose a reason for hiding this comment

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

This makes sense. Since I introduced the regression, I'm happy to approve it. It's kinda a shame we have to special case guards here, but it makes sense, since this is the only case where we have a nested expr within a pattern.

We could alternatively do something like override visit_expr to be a noop, but whatever, this current approach keeps the behavior more localized.

@@ -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
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.

@compiler-errors
Copy link
Member

r? compiler-errors @bors r+ rollup

@bors
Copy link
Collaborator

bors commented May 20, 2025

📌 Commit c343b2a has been approved by compiler-errors

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels May 20, 2025
bors added a commit to rust-lang-ci/rust that referenced this pull request May 20, 2025
…iaskrgr

Rollup of 7 pull requests

Successful merges:

 - rust-lang#139419 (Error on recursive opaque ty in HIR typeck)
 - rust-lang#141236 (Resolved issue with mismatched types triggering ICE in certain scenarios)
 - rust-lang#141253 (Warning added when dependency crate has async drop types, and the feature is disabled)
 - rust-lang#141269 (rustc-dev-guide subtree update)
 - rust-lang#141275 (`gather_locals`: only visit guard pattern guards when checking the guard)
 - rust-lang#141279 (`lower_to_hir` cleanups)
 - rust-lang#141285 (Add tick to `RePlaceholder` debug output)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit ac500ad into rust-lang:master May 20, 2025
6 checks passed
@rustbot rustbot added this to the 1.89.0 milestone May 20, 2025
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request May 20, 2025
Rollup merge of rust-lang#141275 - dianne:gather-guard-pat-locals-once, r=compiler-errors

`gather_locals`: only visit guard pattern guards when checking the guard

When checking a pattern with guards in it, `GatherLocalsVisitor` will visit both the pattern (when type-checking the let, arm, or param containing it) and local declarations in the guard expression (when checking the guard itself). This keeps it from visiting the guard when visiting the pattern, since otherwise it would gather locals from the guard twice, which would lead to a delayed bug: "evaluated expression more than once".

Tracking issue for guard patterns: rust-lang#129967
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants