Skip to content

Commit 0959f0f

Browse files
committed
Tweak suggestion for missing field in patterns
Account for parser recovered struct and tuple patterns to avoid invalid suggestion. Follow up to rust-lang#81103.
1 parent 7907345 commit 0959f0f

File tree

4 files changed

+19
-30
lines changed

4 files changed

+19
-30
lines changed

compiler/rustc_typeck/src/check/pat.rs

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1525,24 +1525,13 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
15251525
_ => return err,
15261526
},
15271527
[.., field] => {
1528-
// if last field has a trailing comma, use the comma
1529-
// as the span to avoid trailing comma in ultimate
1530-
// suggestion (Issue #78511)
1531-
let tail = field.span.shrink_to_hi().until(pat.span.shrink_to_hi());
1532-
let tail_through_comma = self.tcx.sess.source_map().span_through_char(tail, ',');
1533-
let sp = if tail_through_comma == tail {
1534-
field.span.shrink_to_hi()
1535-
} else {
1536-
tail_through_comma
1537-
};
1538-
(
1539-
match pat.kind {
1540-
PatKind::Struct(_, [_, ..], _) => ", ",
1541-
_ => "",
1542-
},
1543-
"",
1544-
sp,
1545-
)
1528+
// Account for last field having a trailing comma or parse recovery at the tail of
1529+
// the pattern to avoid invalid suggestion (#78511).
1530+
let tail = field.span.shrink_to_hi().with_hi(pat.span.hi());
1531+
match &pat.kind {
1532+
PatKind::Struct(..) => (", ", " }", tail),
1533+
_ => return err,
1534+
}
15461535
}
15471536
};
15481537
err.span_suggestion(

src/test/ui/destructuring-assignment/struct_destructure_fail.stderr

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,12 @@ LL | Struct { a, _ } = Struct { a: 1, b: 2 };
3232
|
3333
help: include the missing field in the pattern
3434
|
35-
LL | Struct { a, b _ } = Struct { a: 1, b: 2 };
36-
| ^^^
35+
LL | Struct { a, b } = Struct { a: 1, b: 2 };
36+
| ^^^^^
3737
help: if you don't care about this missing field, you can explicitly ignore it
3838
|
39-
LL | Struct { a, .. _ } = Struct { a: 1, b: 2 };
40-
| ^^^^
39+
LL | Struct { a, .. } = Struct { a: 1, b: 2 };
40+
| ^^^^^^
4141

4242
error: aborting due to 5 previous errors
4343

src/test/ui/error-codes/E0027.stderr

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ LL | Dog { age: x } => {}
77
help: include the missing field in the pattern
88
|
99
LL | Dog { age: x, name } => {}
10-
| ^^^^^^
10+
| ^^^^^^^^
1111
help: if you don't care about this missing field, you can explicitly ignore it
1212
|
1313
LL | Dog { age: x, .. } => {}
14-
| ^^^^
14+
| ^^^^^^
1515

1616
error[E0027]: pattern does not mention field `age`
1717
--> $DIR/E0027.rs:15:9
@@ -22,11 +22,11 @@ LL | Dog { name: x, } => {}
2222
help: include the missing field in the pattern
2323
|
2424
LL | Dog { name: x, age } => {}
25-
| ^^^^^
25+
| ^^^^^^^
2626
help: if you don't care about this missing field, you can explicitly ignore it
2727
|
2828
LL | Dog { name: x, .. } => {}
29-
| ^^^^
29+
| ^^^^^^
3030

3131
error[E0027]: pattern does not mention field `age`
3232
--> $DIR/E0027.rs:19:9
@@ -37,11 +37,11 @@ LL | Dog { name: x , } => {}
3737
help: include the missing field in the pattern
3838
|
3939
LL | Dog { name: x, age } => {}
40-
| ^^^^^
40+
| ^^^^^^^
4141
help: if you don't care about this missing field, you can explicitly ignore it
4242
|
4343
LL | Dog { name: x, .. } => {}
44-
| ^^^^
44+
| ^^^^^^
4545

4646
error[E0027]: pattern does not mention fields `name`, `age`
4747
--> $DIR/E0027.rs:22:9

src/test/ui/structs/struct-pat-derived-error.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ LL | let A { x, y } = self.d;
1919
help: include the missing fields in the pattern
2020
|
2121
LL | let A { x, y, b, c } = self.d;
22-
| ^^^^^^
22+
| ^^^^^^^^
2323
help: if you don't care about these missing fields, you can explicitly ignore them
2424
|
2525
LL | let A { x, y, .. } = self.d;
26-
| ^^^^
26+
| ^^^^^^
2727

2828
error: aborting due to 3 previous errors
2929

0 commit comments

Comments
 (0)