Skip to content

Commit f044c6c

Browse files
authored
Rollup merge of #92237 - compiler-errors:issue-92100, r=cjgillot
Visit expressions in-order when resolving pattern bindings [edited:] Visit the pattern's sub-expressions before defining any bindings. Otherwise, we might get into a case where a Lit/Range expression in a pattern has a qpath pointing to a Ident pattern that is defined after it, causing an ICE when lowering to HIR. I have a more detailed explanation in the issue linked. Fixes #92100
2 parents bee1471 + b1529a6 commit f044c6c

File tree

5 files changed

+50
-1
lines changed

5 files changed

+50
-1
lines changed

compiler/rustc_resolve/src/late.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -1603,10 +1603,13 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
16031603
pat_src: PatternSource,
16041604
bindings: &mut SmallVec<[(PatBoundCtx, FxHashSet<Ident>); 1]>,
16051605
) {
1606+
// We walk the pattern before declaring the pattern's inner bindings,
1607+
// so that we avoid resolving a literal expression to a binding defined
1608+
// by the pattern.
1609+
visit::walk_pat(self, pat);
16061610
self.resolve_pattern_inner(pat, pat_src, bindings);
16071611
// This has to happen *after* we determine which pat_idents are variants:
16081612
self.check_consistent_bindings_top(pat);
1609-
visit::walk_pat(self, pat);
16101613
}
16111614

16121615
/// Resolve bindings in a pattern. This is a helper to `resolve_pattern`.
+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#![feature(half_open_range_patterns)]
2+
3+
macro_rules! funny {
4+
($a:expr, $b:ident) => {
5+
match [1, 2] {
6+
[$a, $b] => {}
7+
}
8+
};
9+
}
10+
11+
fn main() {
12+
funny!(a, a);
13+
//~^ ERROR cannot find value `a` in this scope
14+
//~| ERROR arbitrary expressions aren't allowed in patterns
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
error: arbitrary expressions aren't allowed in patterns
2+
--> $DIR/expr_before_ident_pat.rs:12:12
3+
|
4+
LL | funny!(a, a);
5+
| ^
6+
7+
error[E0425]: cannot find value `a` in this scope
8+
--> $DIR/expr_before_ident_pat.rs:12:12
9+
|
10+
LL | funny!(a, a);
11+
| ^ not found in this scope
12+
13+
error: aborting due to 2 previous errors
14+
15+
For more information about this error, try `rustc --explain E0425`.

src/test/ui/match/issue-92100.rs

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#![feature(half_open_range_patterns)]
2+
3+
fn main() {
4+
match [1, 2] {
5+
[a.., a] => {} //~ ERROR cannot find value `a` in this scope
6+
}
7+
}

src/test/ui/match/issue-92100.stderr

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
error[E0425]: cannot find value `a` in this scope
2+
--> $DIR/issue-92100.rs:5:10
3+
|
4+
LL | [a.., a] => {}
5+
| ^ not found in this scope
6+
7+
error: aborting due to previous error
8+
9+
For more information about this error, try `rustc --explain E0425`.

0 commit comments

Comments
 (0)