Visit patterns' literal expressions before binding new idents
This commit is contained in:
parent
77497c74f9
commit
b1529a680a
5 changed files with 50 additions and 1 deletions
|
@ -1603,10 +1603,13 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
|
||||||
pat_src: PatternSource,
|
pat_src: PatternSource,
|
||||||
bindings: &mut SmallVec<[(PatBoundCtx, FxHashSet<Ident>); 1]>,
|
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);
|
self.resolve_pattern_inner(pat, pat_src, bindings);
|
||||||
// This has to happen *after* we determine which pat_idents are variants:
|
// This has to happen *after* we determine which pat_idents are variants:
|
||||||
self.check_consistent_bindings_top(pat);
|
self.check_consistent_bindings_top(pat);
|
||||||
visit::walk_pat(self, pat);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Resolve bindings in a pattern. This is a helper to `resolve_pattern`.
|
/// Resolve bindings in a pattern. This is a helper to `resolve_pattern`.
|
||||||
|
|
15
src/test/ui/match/expr_before_ident_pat.rs
Normal file
15
src/test/ui/match/expr_before_ident_pat.rs
Normal file
|
@ -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
|
||||||
|
}
|
15
src/test/ui/match/expr_before_ident_pat.stderr
Normal file
15
src/test/ui/match/expr_before_ident_pat.stderr
Normal file
|
@ -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`.
|
7
src/test/ui/match/issue-92100.rs
Normal file
7
src/test/ui/match/issue-92100.rs
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
#![feature(half_open_range_patterns)]
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
match [1, 2] {
|
||||||
|
[a.., a] => {} //~ ERROR cannot find value `a` in this scope
|
||||||
|
}
|
||||||
|
}
|
9
src/test/ui/match/issue-92100.stderr
Normal file
9
src/test/ui/match/issue-92100.stderr
Normal file
|
@ -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`.
|
Loading…
Add table
Reference in a new issue