diff options
| author | Matthias Krüger <matthias.krueger@famsik.de> | 2021-12-29 10:17:10 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-12-29 10:17:10 +0100 |
| commit | f044c6c86ccfc517ba336c6a5f45b7d1c1e1b2f2 (patch) | |
| tree | 254a8dd40ec4f7367bcaf51f75ac9e0ee7ff4709 | |
| parent | bee14712ab2993c0c6a8af9c72dbfa9b149b7f8d (diff) | |
| parent | b1529a680a9a593bd0cb01502dc86b7d0e3ee28d (diff) | |
| download | rust-f044c6c86ccfc517ba336c6a5f45b7d1c1e1b2f2.tar.gz rust-f044c6c86ccfc517ba336c6a5f45b7d1c1e1b2f2.zip | |
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
| -rw-r--r-- | compiler/rustc_resolve/src/late.rs | 5 | ||||
| -rw-r--r-- | src/test/ui/match/expr_before_ident_pat.rs | 15 | ||||
| -rw-r--r-- | src/test/ui/match/expr_before_ident_pat.stderr | 15 | ||||
| -rw-r--r-- | src/test/ui/match/issue-92100.rs | 7 | ||||
| -rw-r--r-- | src/test/ui/match/issue-92100.stderr | 9 |
5 files changed, 50 insertions, 1 deletions
diff --git a/compiler/rustc_resolve/src/late.rs b/compiler/rustc_resolve/src/late.rs index 12123c946cc..5098cef11e8 100644 --- a/compiler/rustc_resolve/src/late.rs +++ b/compiler/rustc_resolve/src/late.rs @@ -1603,10 +1603,13 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> { pat_src: PatternSource, bindings: &mut SmallVec<[(PatBoundCtx, FxHashSet<Ident>); 1]>, ) { + // 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); self.resolve_pattern_inner(pat, pat_src, bindings); // This has to happen *after* we determine which pat_idents are variants: self.check_consistent_bindings_top(pat); - visit::walk_pat(self, pat); } /// Resolve bindings in a pattern. This is a helper to `resolve_pattern`. diff --git a/src/test/ui/match/expr_before_ident_pat.rs b/src/test/ui/match/expr_before_ident_pat.rs new file mode 100644 index 00000000000..47db6c3f488 --- /dev/null +++ b/src/test/ui/match/expr_before_ident_pat.rs @@ -0,0 +1,15 @@ +#![feature(half_open_range_patterns)] + +macro_rules! funny { + ($a:expr, $b:ident) => { + match [1, 2] { + [$a, $b] => {} + } + }; +} + +fn main() { + funny!(a, a); + //~^ ERROR cannot find value `a` in this scope + //~| ERROR arbitrary expressions aren't allowed in patterns +} diff --git a/src/test/ui/match/expr_before_ident_pat.stderr b/src/test/ui/match/expr_before_ident_pat.stderr new file mode 100644 index 00000000000..1ac8274ffd5 --- /dev/null +++ b/src/test/ui/match/expr_before_ident_pat.stderr @@ -0,0 +1,15 @@ +error: arbitrary expressions aren't allowed in patterns + --> $DIR/expr_before_ident_pat.rs:12:12 + | +LL | funny!(a, a); + | ^ + +error[E0425]: cannot find value `a` in this scope + --> $DIR/expr_before_ident_pat.rs:12:12 + | +LL | funny!(a, a); + | ^ not found in this scope + +error: aborting due to 2 previous errors + +For more information about this error, try `rustc --explain E0425`. diff --git a/src/test/ui/match/issue-92100.rs b/src/test/ui/match/issue-92100.rs new file mode 100644 index 00000000000..021166b2ba5 --- /dev/null +++ b/src/test/ui/match/issue-92100.rs @@ -0,0 +1,7 @@ +#![feature(half_open_range_patterns)] + +fn main() { + match [1, 2] { + [a.., a] => {} //~ ERROR cannot find value `a` in this scope + } +} diff --git a/src/test/ui/match/issue-92100.stderr b/src/test/ui/match/issue-92100.stderr new file mode 100644 index 00000000000..0f694c587fc --- /dev/null +++ b/src/test/ui/match/issue-92100.stderr @@ -0,0 +1,9 @@ +error[E0425]: cannot find value `a` in this scope + --> $DIR/issue-92100.rs:5:10 + | +LL | [a.., a] => {} + | ^ not found in this scope + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0425`. |
