Skip to content

Commit da25af2

Browse files
committedAug 17, 2021
Make spans for tuple patterns in E0023 more precise
As suggested in #86307.
1 parent d83da1d commit da25af2

10 files changed

+96
-48
lines changed
 

‎compiler/rustc_typeck/src/check/pat.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -990,10 +990,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
990990
) {
991991
let subpats_ending = pluralize!(subpats.len());
992992
let fields_ending = pluralize!(fields.len());
993+
let fields_span = pat_span.trim_start(qpath.span()).unwrap_or(pat_span);
993994
let res_span = self.tcx.def_span(res.def_id());
994995
let mut err = struct_span_err!(
995996
self.tcx.sess,
996-
pat_span,
997+
fields_span,
997998
E0023,
998999
"this pattern has {} field{}, but the corresponding {} has {} field{}",
9991000
subpats.len(),
@@ -1003,9 +1004,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
10031004
fields_ending,
10041005
);
10051006
err.span_label(
1006-
pat_span,
1007+
fields_span,
10071008
format!("expected {} field{}, found {}", fields.len(), fields_ending, subpats.len(),),
10081009
)
1010+
.span_label(qpath.span(), format!("this {}", res.descr()))
10091011
.span_label(res_span, format!("{} defined here", res.descr()));
10101012

10111013
// Identify the case `Some(x, y)` where the expected type is e.g. `Option<(T, U)>`.

‎src/test/ui/destructuring-assignment/tuple_struct_destructure_fail.stderr

+16-8
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,26 @@ LL | Enum::SingleVariant(a, .., b, ..) = Enum::SingleVariant(0, 1);
1515
| previously used here
1616

1717
error[E0023]: this pattern has 3 fields, but the corresponding tuple struct has 2 fields
18-
--> $DIR/tuple_struct_destructure_fail.rs:30:5
18+
--> $DIR/tuple_struct_destructure_fail.rs:30:16
1919
|
2020
LL | struct TupleStruct<S, T>(S, T);
2121
| ------------------------------- tuple struct defined here
2222
...
2323
LL | TupleStruct(a, a, b) = TupleStruct(1, 2);
24-
| ^^^^^^^^^^^^^^^^^^^^ expected 2 fields, found 3
24+
| -----------^^^^^^^^^ expected 2 fields, found 3
25+
| |
26+
| this tuple struct
2527

2628
error[E0023]: this pattern has 1 field, but the corresponding tuple struct has 2 fields
27-
--> $DIR/tuple_struct_destructure_fail.rs:32:5
29+
--> $DIR/tuple_struct_destructure_fail.rs:32:16
2830
|
2931
LL | struct TupleStruct<S, T>(S, T);
3032
| ------------------------------- tuple struct defined here
3133
...
3234
LL | TupleStruct(_) = TupleStruct(1, 2);
33-
| ^^^^^^^^^^^^^^ expected 2 fields, found 1
35+
| -----------^^^ expected 2 fields, found 1
36+
| |
37+
| this tuple struct
3438
|
3539
help: use `_` to explicitly ignore each field
3640
|
@@ -42,22 +46,26 @@ LL | TupleStruct(..) = TupleStruct(1, 2);
4246
| ~~
4347

4448
error[E0023]: this pattern has 3 fields, but the corresponding tuple variant has 2 fields
45-
--> $DIR/tuple_struct_destructure_fail.rs:34:5
49+
--> $DIR/tuple_struct_destructure_fail.rs:34:24
4650
|
4751
LL | SingleVariant(S, T)
4852
| ------------------- tuple variant defined here
4953
...
5054
LL | Enum::SingleVariant(a, a, b) = Enum::SingleVariant(1, 2);
51-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected 2 fields, found 3
55+
| -------------------^^^^^^^^^ expected 2 fields, found 3
56+
| |
57+
| this tuple variant
5258

5359
error[E0023]: this pattern has 1 field, but the corresponding tuple variant has 2 fields
54-
--> $DIR/tuple_struct_destructure_fail.rs:36:5
60+
--> $DIR/tuple_struct_destructure_fail.rs:36:24
5561
|
5662
LL | SingleVariant(S, T)
5763
| ------------------- tuple variant defined here
5864
...
5965
LL | Enum::SingleVariant(_) = Enum::SingleVariant(1, 2);
60-
| ^^^^^^^^^^^^^^^^^^^^^^ expected 2 fields, found 1
66+
| -------------------^^^ expected 2 fields, found 1
67+
| |
68+
| this tuple variant
6169
|
6270
help: use `_` to explicitly ignore each field
6371
|

‎src/test/ui/error-codes/E0023.stderr

+20-10
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,67 @@
11
error[E0023]: this pattern has 1 field, but the corresponding tuple variant has 2 fields
2-
--> $DIR/E0023.rs:11:9
2+
--> $DIR/E0023.rs:11:21
33
|
44
LL | Apple(String, String),
55
| --------------------- tuple variant defined here
66
...
77
LL | Fruit::Apple(a) => {},
8-
| ^^^^^^^^^^^^^^^ expected 2 fields, found 1
8+
| ------------^^^ expected 2 fields, found 1
9+
| |
10+
| this tuple variant
911
|
1012
help: use `_` to explicitly ignore each field
1113
|
1214
LL | Fruit::Apple(a, _) => {},
1315
| +++
1416

1517
error[E0023]: this pattern has 3 fields, but the corresponding tuple variant has 2 fields
16-
--> $DIR/E0023.rs:12:9
18+
--> $DIR/E0023.rs:12:21
1719
|
1820
LL | Apple(String, String),
1921
| --------------------- tuple variant defined here
2022
...
2123
LL | Fruit::Apple(a, b, c) => {},
22-
| ^^^^^^^^^^^^^^^^^^^^^ expected 2 fields, found 3
24+
| ------------^^^^^^^^^ expected 2 fields, found 3
25+
| |
26+
| this tuple variant
2327

2428
error[E0023]: this pattern has 2 fields, but the corresponding tuple variant has 1 field
25-
--> $DIR/E0023.rs:13:9
29+
--> $DIR/E0023.rs:13:20
2630
|
2731
LL | Pear(u32),
2832
| --------- tuple variant defined here
2933
...
3034
LL | Fruit::Pear(1, 2) => {},
31-
| ^^^^^^^^^^^^^^^^^ expected 1 field, found 2
35+
| -----------^^^^^^ expected 1 field, found 2
36+
| |
37+
| this tuple variant
3238

3339
error[E0023]: this pattern has 2 fields, but the corresponding tuple variant has 1 field
34-
--> $DIR/E0023.rs:14:9
40+
--> $DIR/E0023.rs:14:22
3541
|
3642
LL | Orange((String, String)),
3743
| ------------------------ tuple variant defined here
3844
...
3945
LL | Fruit::Orange(a, b) => {},
40-
| ^^^^^^^^^^^^^^^^^^^ expected 1 field, found 2
46+
| -------------^^^^^^ expected 1 field, found 2
47+
| |
48+
| this tuple variant
4149
|
4250
help: missing parentheses
4351
|
4452
LL | Fruit::Orange((a, b)) => {},
4553
| + +
4654

4755
error[E0023]: this pattern has 0 fields, but the corresponding tuple variant has 1 field
48-
--> $DIR/E0023.rs:15:9
56+
--> $DIR/E0023.rs:15:22
4957
|
5058
LL | Banana(()),
5159
| ---------- tuple variant defined here
5260
...
5361
LL | Fruit::Banana() => {},
54-
| ^^^^^^^^^^^^^^^ expected 1 field, found 0
62+
| -------------^^ expected 1 field, found 0
63+
| |
64+
| this tuple variant
5565
|
5666
help: missing parentheses
5767
|

‎src/test/ui/issues/issue-72574-2.stderr

+4-2
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,15 @@ LL | Binder(_a, _x @ ..) => {}
1919
= note: only allowed in tuple, tuple struct, and slice patterns
2020

2121
error[E0023]: this pattern has 2 fields, but the corresponding tuple struct has 3 fields
22-
--> $DIR/issue-72574-2.rs:6:9
22+
--> $DIR/issue-72574-2.rs:6:15
2323
|
2424
LL | struct Binder(i32, i32, i32);
2525
| ----------------------------- tuple struct defined here
2626
...
2727
LL | Binder(_a, _x @ ..) => {}
28-
| ^^^^^^^^^^^^^^^^^^^ expected 3 fields, found 2
28+
| ------^^^^^^^^^^^^^ expected 3 fields, found 2
29+
| |
30+
| this tuple struct
2931
|
3032
help: use `_` to explicitly ignore each field
3133
|

‎src/test/ui/match/match-pattern-field-mismatch.stderr

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
error[E0023]: this pattern has 2 fields, but the corresponding tuple variant has 3 fields
2-
--> $DIR/match-pattern-field-mismatch.rs:10:11
2+
--> $DIR/match-pattern-field-mismatch.rs:10:21
33
|
44
LL | Rgb(usize, usize, usize),
55
| ------------------------ tuple variant defined here
66
...
77
LL | Color::Rgb(_, _) => { }
8-
| ^^^^^^^^^^^^^^^^ expected 3 fields, found 2
8+
| ----------^^^^^^ expected 3 fields, found 2
9+
| |
10+
| this tuple variant
911
|
1012
help: use `_` to explicitly ignore each field
1113
|

‎src/test/ui/pattern/issue-67037-pat-tup-scrut-ty-diff-less-fields.stderr

+4-2
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,15 @@ LL | let P() = U {};
1010
found struct `P<_>`
1111

1212
error[E0023]: this pattern has 0 fields, but the corresponding tuple struct has 1 field
13-
--> $DIR/issue-67037-pat-tup-scrut-ty-diff-less-fields.rs:19:9
13+
--> $DIR/issue-67037-pat-tup-scrut-ty-diff-less-fields.rs:19:10
1414
|
1515
LL | struct P<T>(T); // 1 type parameter wanted
1616
| --------------- tuple struct defined here
1717
...
1818
LL | let P() = U {};
19-
| ^^^ expected 1 field, found 0
19+
| -^^ expected 1 field, found 0
20+
| |
21+
| this tuple struct
2022
|
2123
help: use `_` to explicitly ignore each field
2224
|

‎src/test/ui/pattern/issue-74539.stderr

+4-2
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,15 @@ LL | E::A(x @ ..) => {
1919
= note: only allowed in tuple, tuple struct, and slice patterns
2020

2121
error[E0023]: this pattern has 1 field, but the corresponding tuple variant has 2 fields
22-
--> $DIR/issue-74539.rs:8:9
22+
--> $DIR/issue-74539.rs:8:13
2323
|
2424
LL | A(u8, u8),
2525
| --------- tuple variant defined here
2626
...
2727
LL | E::A(x @ ..) => {
28-
| ^^^^^^^^^^^^ expected 2 fields, found 1
28+
| ----^^^^^^^^ expected 2 fields, found 1
29+
| |
30+
| this tuple variant
2931
|
3032
help: use `_` to explicitly ignore each field
3133
|

‎src/test/ui/pattern/pat-tuple-overfield.stderr

+8-4
Original file line numberDiff line numberDiff line change
@@ -22,22 +22,26 @@ LL | (1, 2, .., 3, 4) => {}
2222
found tuple `(_, _, _, _)`
2323

2424
error[E0023]: this pattern has 4 fields, but the corresponding tuple struct has 3 fields
25-
--> $DIR/pat-tuple-overfield.rs:10:9
25+
--> $DIR/pat-tuple-overfield.rs:10:10
2626
|
2727
LL | struct S(u8, u8, u8);
2828
| --------------------- tuple struct defined here
2929
...
3030
LL | S(1, 2, 3, 4) => {}
31-
| ^^^^^^^^^^^^^ expected 3 fields, found 4
31+
| -^^^^^^^^^^^^ expected 3 fields, found 4
32+
| |
33+
| this tuple struct
3234

3335
error[E0023]: this pattern has 4 fields, but the corresponding tuple struct has 3 fields
34-
--> $DIR/pat-tuple-overfield.rs:12:9
36+
--> $DIR/pat-tuple-overfield.rs:12:10
3537
|
3638
LL | struct S(u8, u8, u8);
3739
| --------------------- tuple struct defined here
3840
...
3941
LL | S(1, 2, .., 3, 4) => {}
40-
| ^^^^^^^^^^^^^^^^^ expected 3 fields, found 4
42+
| -^^^^^^^^^^^^^^^^ expected 3 fields, found 4
43+
| |
44+
| this tuple struct
4145

4246
error: aborting due to 4 previous errors
4347

‎src/test/ui/pattern/pat-tuple-underfield.stderr

+28-14
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,31 @@ LL | E::S => {}
88
| ^^^^ help: use the tuple variant pattern syntax instead: `E::S(_, _)`
99

1010
error[E0023]: this pattern has 1 field, but the corresponding tuple struct has 2 fields
11-
--> $DIR/pat-tuple-underfield.rs:9:9
11+
--> $DIR/pat-tuple-underfield.rs:9:10
1212
|
1313
LL | struct S(i32, f32);
1414
| ------------------- tuple struct defined here
1515
...
1616
LL | S(x) => {}
17-
| ^^^^ expected 2 fields, found 1
17+
| -^^^ expected 2 fields, found 1
18+
| |
19+
| this tuple struct
1820
|
1921
help: use `_` to explicitly ignore each field
2022
|
2123
LL | S(x, _) => {}
2224
| +++
2325

2426
error[E0023]: this pattern has 1 field, but the corresponding tuple struct has 2 fields
25-
--> $DIR/pat-tuple-underfield.rs:14:9
27+
--> $DIR/pat-tuple-underfield.rs:14:10
2628
|
2729
LL | struct S(i32, f32);
2830
| ------------------- tuple struct defined here
2931
...
3032
LL | S(_) => {}
31-
| ^^^^ expected 2 fields, found 1
33+
| -^^^ expected 2 fields, found 1
34+
| |
35+
| this tuple struct
3236
|
3337
help: use `_` to explicitly ignore each field
3438
|
@@ -40,13 +44,15 @@ LL | S(..) => {}
4044
| ~~
4145

4246
error[E0023]: this pattern has 0 fields, but the corresponding tuple struct has 2 fields
43-
--> $DIR/pat-tuple-underfield.rs:20:9
47+
--> $DIR/pat-tuple-underfield.rs:20:10
4448
|
4549
LL | struct S(i32, f32);
4650
| ------------------- tuple struct defined here
4751
...
4852
LL | S() => {}
49-
| ^^^ expected 2 fields, found 0
53+
| -^^ expected 2 fields, found 0
54+
| |
55+
| this tuple struct
5056
|
5157
help: use `_` to explicitly ignore each field
5258
|
@@ -58,27 +64,31 @@ LL | S(..) => {}
5864
| ++
5965

6066
error[E0023]: this pattern has 1 field, but the corresponding tuple variant has 2 fields
61-
--> $DIR/pat-tuple-underfield.rs:27:9
67+
--> $DIR/pat-tuple-underfield.rs:27:13
6268
|
6369
LL | S(i32, f32),
6470
| ----------- tuple variant defined here
6571
...
6672
LL | E::S(x) => {}
67-
| ^^^^^^^ expected 2 fields, found 1
73+
| ----^^^ expected 2 fields, found 1
74+
| |
75+
| this tuple variant
6876
|
6977
help: use `_` to explicitly ignore each field
7078
|
7179
LL | E::S(x, _) => {}
7280
| +++
7381

7482
error[E0023]: this pattern has 1 field, but the corresponding tuple variant has 2 fields
75-
--> $DIR/pat-tuple-underfield.rs:32:9
83+
--> $DIR/pat-tuple-underfield.rs:32:13
7684
|
7785
LL | S(i32, f32),
7886
| ----------- tuple variant defined here
7987
...
8088
LL | E::S(_) => {}
81-
| ^^^^^^^ expected 2 fields, found 1
89+
| ----^^^ expected 2 fields, found 1
90+
| |
91+
| this tuple variant
8292
|
8393
help: use `_` to explicitly ignore each field
8494
|
@@ -90,13 +100,15 @@ LL | E::S(..) => {}
90100
| ~~
91101

92102
error[E0023]: this pattern has 0 fields, but the corresponding tuple variant has 2 fields
93-
--> $DIR/pat-tuple-underfield.rs:38:9
103+
--> $DIR/pat-tuple-underfield.rs:38:13
94104
|
95105
LL | S(i32, f32),
96106
| ----------- tuple variant defined here
97107
...
98108
LL | E::S() => {}
99-
| ^^^^^^ expected 2 fields, found 0
109+
| ----^^ expected 2 fields, found 0
110+
| |
111+
| this tuple variant
100112
|
101113
help: use `_` to explicitly ignore each field
102114
|
@@ -108,13 +120,15 @@ LL | E::S(..) => {}
108120
| ++
109121

110122
error[E0023]: this pattern has 2 fields, but the corresponding tuple struct has 4 fields
111-
--> $DIR/pat-tuple-underfield.rs:50:9
123+
--> $DIR/pat-tuple-underfield.rs:50:15
112124
|
113125
LL | struct Point4(i32, i32, i32, i32);
114126
| ---------------------------------- tuple struct defined here
115127
...
116128
LL | Point4( a , _ ) => {}
117-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ expected 4 fields, found 2
129+
| ------^^^^^^^^^^^^^^^^^^^^ expected 4 fields, found 2
130+
| |
131+
| this tuple struct
118132
|
119133
help: use `_` to explicitly ignore each field
120134
|

‎src/test/ui/pattern/pattern-error-continue.stderr

+4-2
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,15 @@ LL | A::B(_) => (),
2626
| ~
2727

2828
error[E0023]: this pattern has 3 fields, but the corresponding tuple variant has 2 fields
29-
--> $DIR/pattern-error-continue.rs:17:9
29+
--> $DIR/pattern-error-continue.rs:17:13
3030
|
3131
LL | B(isize, isize),
3232
| --------------- tuple variant defined here
3333
...
3434
LL | A::B(_, _, _) => (),
35-
| ^^^^^^^^^^^^^ expected 2 fields, found 3
35+
| ----^^^^^^^^^ expected 2 fields, found 3
36+
| |
37+
| this tuple variant
3638

3739
error[E0308]: mismatched types
3840
--> $DIR/pattern-error-continue.rs:22:9

0 commit comments

Comments
 (0)
Please sign in to comment.