Skip to content

Commit f5e5eb6

Browse files
committed
Fix an ICE on an invalid binding @ ... in a tuple struct pattern
1 parent d3df851 commit f5e5eb6

File tree

3 files changed

+40
-1
lines changed

3 files changed

+40
-1
lines changed

src/librustc_resolve/late.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -1500,11 +1500,17 @@ impl<'a, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
15001500
pat_src: PatternSource,
15011501
bindings: &mut SmallVec<[(PatBoundCtx, FxHashSet<Ident>); 1]>,
15021502
) {
1503+
let is_tuple_struct_pat = matches!(pat.kind, PatKind::TupleStruct(_, _));
1504+
15031505
// Visit all direct subpatterns of this pattern.
15041506
pat.walk(&mut |pat| {
15051507
debug!("resolve_pattern pat={:?} node={:?}", pat, pat.kind);
15061508
match pat.kind {
1507-
PatKind::Ident(bmode, ident, ref sub) => {
1509+
// In tuple struct patterns ignore the invalid `ident @ ...`.
1510+
// It will be handled as an error by the AST lowering.
1511+
PatKind::Ident(bmode, ident, ref sub)
1512+
if !(is_tuple_struct_pat && sub.as_ref().filter(|p| p.is_rest()).is_some()) =>
1513+
{
15081514
// First try to resolve the identifier as some existing entity,
15091515
// then fall back to a fresh binding.
15101516
let has_sub = sub.is_some();

src/test/ui/issues/issue-74539.rs

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
enum E {
2+
A(u8, u8),
3+
}
4+
5+
fn main() {
6+
let e = E::A(2, 3);
7+
match e {
8+
E::A(x @ ..) => { //~ ERROR `x @` is not allowed in a tuple
9+
x //~ ERROR cannot find value `x` in this scope
10+
}
11+
};
12+
}

src/test/ui/issues/issue-74539.stderr

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
error[E0425]: cannot find value `x` in this scope
2+
--> $DIR/issue-74539.rs:9:13
3+
|
4+
LL | x
5+
| ^ help: a local variable with a similar name exists: `e`
6+
7+
error: `x @` is not allowed in a tuple struct
8+
--> $DIR/issue-74539.rs:8:14
9+
|
10+
LL | E::A(x @ ..) => {
11+
| ^^^^^^ this is only allowed in slice patterns
12+
|
13+
= help: remove this and bind each tuple field independently
14+
help: if you don't need to use the contents of x, discard the tuple's remaining fields
15+
|
16+
LL | E::A(..) => {
17+
| ^^
18+
19+
error: aborting due to 2 previous errors
20+
21+
For more information about this error, try `rustc --explain E0425`.

0 commit comments

Comments
 (0)