Skip to content

only resolve top-level guard patterns' guards once #141267

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 21, 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
4 changes: 3 additions & 1 deletion compiler/rustc_resolve/src/late.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3901,7 +3901,9 @@ impl<'a, 'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
// We walk the pattern before declaring the pattern's inner bindings,
// so that we avoid resolving a literal expression to a binding defined
// by the pattern.
visit::walk_pat(self, pat);
// NB: `Self::visit_pat` must be used rather than `visit::walk_pat` to avoid resolving guard
// patterns' guard expressions multiple times (#141265).
self.visit_pat(pat);
self.resolve_pattern_inner(pat, pat_src, bindings);
// This has to happen *after* we determine which pat_idents are variants:
self.check_consistent_bindings(pat);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//! Regression test for <https://github.com/rust-lang/rust/issues/141265>.
//! Make sure expressions in top-level guard patterns are only resolved once.

fn main() {
for
else if b 0 {}
//~^ ERROR expected identifier, found keyword `else`
//~| ERROR missing `in` in `for` loop
//~| ERROR cannot find value `b` in this scope
//~| ERROR guard patterns are experimental
//~| ERROR `{integer}` is not an iterator
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
error: expected identifier, found keyword `else`
--> $DIR/only-resolve-top-level-guard-expr-once-ice-141265.rs:6:5
|
LL | else if b 0 {}
| ^^^^ expected identifier, found keyword

error: missing `in` in `for` loop
--> $DIR/only-resolve-top-level-guard-expr-once-ice-141265.rs:6:14
|
LL | else if b 0 {}
| ^
|
help: try adding `in` here
|
LL | else if b in 0 {}
| ++

error[E0425]: cannot find value `b` in this scope
--> $DIR/only-resolve-top-level-guard-expr-once-ice-141265.rs:6:13
|
LL | else if b 0 {}
| ^ not found in this scope

error[E0658]: guard patterns are experimental
--> $DIR/only-resolve-top-level-guard-expr-once-ice-141265.rs:6:13
|
LL | else if b 0 {}
| ^
|
= note: see issue #129967 <https://github.com/rust-lang/rust/issues/129967> for more information
= help: add `#![feature(guard_patterns)]` to the crate attributes to enable
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
= help: consider using match arm guards

error[E0277]: `{integer}` is not an iterator
--> $DIR/only-resolve-top-level-guard-expr-once-ice-141265.rs:6:15
|
LL | else if b 0 {}
| ^ `{integer}` is not an iterator
|
= help: the trait `Iterator` is not implemented for `{integer}`
= note: if you want to iterate between `start` until a value `end`, use the exclusive range syntax `start..end` or the inclusive range syntax `start..=end`
= note: required for `{integer}` to implement `IntoIterator`

error: aborting due to 5 previous errors

Some errors have detailed explanations: E0277, E0425, E0658.
For more information about an error, try `rustc --explain E0277`.
Loading