Skip to content

Commit 8a6501d

Browse files
committed
Adjust spans
* Highlight the whole pattern if it has no fields * Highlight the whole definition if it has no fields * Only highlight the pattern name if the pattern is multi-line * Determine whether a pattern is multi-line based on distance from name to last field, rather than first field
1 parent 19f4510 commit 8a6501d

13 files changed

+211
-85
lines changed

compiler/rustc_typeck/src/check/pat.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -992,15 +992,15 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
992992
let fields_ending = pluralize!(fields.len());
993993

994994
let subpat_spans = if subpats.is_empty() {
995-
vec![pat_span.trim_start(qpath.span()).unwrap_or(pat_span)]
995+
vec![pat_span]
996996
} else {
997997
subpats.iter().map(|p| p.span).collect()
998998
};
999999
let last_subpat_span = *subpat_spans.last().unwrap();
10001000
let res_span = self.tcx.def_span(res.def_id());
10011001
let def_ident_span = self.tcx.def_ident_span(res.def_id()).unwrap_or(res_span);
10021002
let field_def_spans = if fields.is_empty() {
1003-
vec![res_span.trim_start(def_ident_span).unwrap_or(res_span)]
1003+
vec![res_span]
10041004
} else {
10051005
fields.iter().map(|f| f.ident.span).collect()
10061006
};
@@ -1021,8 +1021,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
10211021
last_subpat_span,
10221022
&format!("expected {} field{}, found {}", fields.len(), fields_ending, subpats.len()),
10231023
);
1024-
err.span_label(qpath.span(), "");
1025-
if self.tcx.sess.source_map().is_multiline(def_ident_span.between(field_def_spans[0])) {
1024+
if self.tcx.sess.source_map().is_multiline(qpath.span().between(last_subpat_span)) {
1025+
err.span_label(qpath.span(), "");
1026+
}
1027+
if self.tcx.sess.source_map().is_multiline(def_ident_span.between(last_field_def_span)) {
10261028
err.span_label(def_ident_span, format!("{} defined here", res.descr()));
10271029
}
10281030
for span in &field_def_spans[..field_def_spans.len() - 1] {

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ LL | struct TupleStruct<S, T>(S, T);
2121
| - - tuple struct has 2 fields
2222
...
2323
LL | TupleStruct(a, a, b) = TupleStruct(1, 2);
24-
| ----------- ^ ^ ^ expected 2 fields, found 3
24+
| ^ ^ ^ expected 2 fields, found 3
2525

2626
error[E0023]: this pattern has 1 field, but the corresponding tuple struct has 2 fields
2727
--> $DIR/tuple_struct_destructure_fail.rs:32:17
@@ -30,7 +30,7 @@ LL | struct TupleStruct<S, T>(S, T);
3030
| - - tuple struct has 2 fields
3131
...
3232
LL | TupleStruct(_) = TupleStruct(1, 2);
33-
| ----------- ^ expected 2 fields, found 1
33+
| ^ expected 2 fields, found 1
3434
|
3535
help: use `_` to explicitly ignore each field
3636
|
@@ -48,7 +48,7 @@ LL | SingleVariant(S, T)
4848
| - - tuple variant has 2 fields
4949
...
5050
LL | Enum::SingleVariant(a, a, b) = Enum::SingleVariant(1, 2);
51-
| ------------------- ^ ^ ^ expected 2 fields, found 3
51+
| ^ ^ ^ expected 2 fields, found 3
5252

5353
error[E0023]: this pattern has 1 field, but the corresponding tuple variant has 2 fields
5454
--> $DIR/tuple_struct_destructure_fail.rs:36:25
@@ -57,7 +57,7 @@ LL | SingleVariant(S, T)
5757
| - - tuple variant has 2 fields
5858
...
5959
LL | Enum::SingleVariant(_) = Enum::SingleVariant(1, 2);
60-
| ------------------- ^ expected 2 fields, found 1
60+
| ^ expected 2 fields, found 1
6161
|
6262
help: use `_` to explicitly ignore each field
6363
|

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

+6-6
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ LL | Apple(String, String),
55
| ------ ------ tuple variant has 2 fields
66
...
77
LL | Fruit::Apple(a) => {},
8-
| ------------ ^ expected 2 fields, found 1
8+
| ^ expected 2 fields, found 1
99
|
1010
help: use `_` to explicitly ignore each field
1111
|
@@ -19,7 +19,7 @@ LL | Apple(String, String),
1919
| ------ ------ tuple variant has 2 fields
2020
...
2121
LL | Fruit::Apple(a, b, c) => {},
22-
| ------------ ^ ^ ^ expected 2 fields, found 3
22+
| ^ ^ ^ expected 2 fields, found 3
2323

2424
error[E0023]: this pattern has 2 fields, but the corresponding tuple variant has 1 field
2525
--> $DIR/E0023.rs:13:21
@@ -28,7 +28,7 @@ LL | Pear(u32),
2828
| --- tuple variant has 1 field
2929
...
3030
LL | Fruit::Pear(1, 2) => {},
31-
| ----------- ^ ^ expected 1 field, found 2
31+
| ^ ^ expected 1 field, found 2
3232

3333
error[E0023]: this pattern has 2 fields, but the corresponding tuple variant has 1 field
3434
--> $DIR/E0023.rs:14:23
@@ -37,21 +37,21 @@ LL | Orange((String, String)),
3737
| ---------------- tuple variant has 1 field
3838
...
3939
LL | Fruit::Orange(a, b) => {},
40-
| ------------- ^ ^ expected 1 field, found 2
40+
| ^ ^ expected 1 field, found 2
4141
|
4242
help: missing parentheses
4343
|
4444
LL | Fruit::Orange((a, b)) => {},
4545
| + +
4646

4747
error[E0023]: this pattern has 0 fields, but the corresponding tuple variant has 1 field
48-
--> $DIR/E0023.rs:15:22
48+
--> $DIR/E0023.rs:15:9
4949
|
5050
LL | Banana(()),
5151
| -- tuple variant has 1 field
5252
...
5353
LL | Fruit::Banana() => {},
54-
| -------------^^ expected 1 field, found 0
54+
| ^^^^^^^^^^^^^^^ expected 1 field, found 0
5555
|
5656
help: missing parentheses
5757
|

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ LL | struct Binder(i32, i32, i32);
2525
| --- --- --- tuple struct has 3 fields
2626
...
2727
LL | Binder(_a, _x @ ..) => {}
28-
| ------ ^^ ^^^^^^^ expected 3 fields, found 2
28+
| ^^ ^^^^^^^ expected 3 fields, found 2
2929
|
3030
help: use `_` to explicitly ignore each field
3131
|

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ LL | Rgb(usize, usize, usize),
55
| ----- ----- ----- tuple variant has 3 fields
66
...
77
LL | Color::Rgb(_, _) => { }
8-
| ---------- ^ ^ expected 3 fields, found 2
8+
| ^ ^ expected 3 fields, found 2
99
|
1010
help: use `_` to explicitly ignore each field
1111
|

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ 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:10
13+
--> $DIR/issue-67037-pat-tup-scrut-ty-diff-less-fields.rs:19:9
1414
|
1515
LL | struct P<T>(T); // 1 type parameter wanted
1616
| - tuple struct has 1 field
1717
...
1818
LL | let P() = U {};
19-
| -^^ expected 1 field, found 0
19+
| ^^^ expected 1 field, found 0
2020
|
2121
help: use `_` to explicitly ignore each field
2222
|

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ LL | A(u8, u8),
2525
| -- -- tuple variant has 2 fields
2626
...
2727
LL | E::A(x @ ..) => {
28-
| ---- ^^^^^^ expected 2 fields, found 1
28+
| ^^^^^^ expected 2 fields, found 1
2929
|
3030
help: use `_` to explicitly ignore each field
3131
|

src/test/ui/pattern/pat-tuple-field-count-cross.stderr

+27-27
Original file line numberDiff line numberDiff line change
@@ -121,18 +121,18 @@ error[E0023]: this pattern has 1 field, but the corresponding tuple struct has 0
121121
--> $DIR/pat-tuple-field-count-cross.rs:14:12
122122
|
123123
LL | Z1(x) => {}
124-
| -- ^ expected 0 fields, found 1
124+
| ^ expected 0 fields, found 1
125125
|
126126
::: $DIR/auxiliary/declarations-for-tuple-field-count-errors.rs:2:1
127127
|
128128
LL | pub struct Z1();
129129
| ---------------- tuple struct has 0 fields
130130

131131
error[E0023]: this pattern has 0 fields, but the corresponding tuple struct has 3 fields
132-
--> $DIR/pat-tuple-field-count-cross.rs:18:10
132+
--> $DIR/pat-tuple-field-count-cross.rs:18:9
133133
|
134134
LL | S() => {}
135-
| -^^ expected 3 fields, found 0
135+
| ^^^ expected 3 fields, found 0
136136
|
137137
::: $DIR/auxiliary/declarations-for-tuple-field-count-errors.rs:4:14
138138
|
@@ -152,7 +152,7 @@ error[E0023]: this pattern has 1 field, but the corresponding tuple struct has 3
152152
--> $DIR/pat-tuple-field-count-cross.rs:19:11
153153
|
154154
LL | S(1) => {}
155-
| - ^ expected 3 fields, found 1
155+
| ^ expected 3 fields, found 1
156156
|
157157
::: $DIR/auxiliary/declarations-for-tuple-field-count-errors.rs:4:14
158158
|
@@ -172,7 +172,7 @@ error[E0023]: this pattern has 2 fields, but the corresponding tuple struct has
172172
--> $DIR/pat-tuple-field-count-cross.rs:20:11
173173
|
174174
LL | S(xyz, abc) => {}
175-
| - ^^^ ^^^ expected 3 fields, found 2
175+
| ^^^ ^^^ expected 3 fields, found 2
176176
|
177177
::: $DIR/auxiliary/declarations-for-tuple-field-count-errors.rs:4:14
178178
|
@@ -188,18 +188,18 @@ error[E0023]: this pattern has 4 fields, but the corresponding tuple struct has
188188
--> $DIR/pat-tuple-field-count-cross.rs:21:11
189189
|
190190
LL | S(1, 2, 3, 4) => {}
191-
| - ^ ^ ^ ^ expected 3 fields, found 4
191+
| ^ ^ ^ ^ expected 3 fields, found 4
192192
|
193193
::: $DIR/auxiliary/declarations-for-tuple-field-count-errors.rs:4:14
194194
|
195195
LL | pub struct S(pub u8, pub u8, pub u8);
196196
| ------ ------ ------ tuple struct has 3 fields
197197

198198
error[E0023]: this pattern has 0 fields, but the corresponding tuple struct has 3 fields
199-
--> $DIR/pat-tuple-field-count-cross.rs:24:10
199+
--> $DIR/pat-tuple-field-count-cross.rs:24:9
200200
|
201201
LL | M() => {}
202-
| -^^ expected 3 fields, found 0
202+
| ^^^ expected 3 fields, found 0
203203
|
204204
::: $DIR/auxiliary/declarations-for-tuple-field-count-errors.rs:5:1
205205
|
@@ -226,7 +226,7 @@ error[E0023]: this pattern has 1 field, but the corresponding tuple struct has 3
226226
--> $DIR/pat-tuple-field-count-cross.rs:25:11
227227
|
228228
LL | M(1) => {}
229-
| - ^ expected 3 fields, found 1
229+
| ^ expected 3 fields, found 1
230230
|
231231
::: $DIR/auxiliary/declarations-for-tuple-field-count-errors.rs:5:1
232232
|
@@ -253,7 +253,7 @@ error[E0023]: this pattern has 2 fields, but the corresponding tuple struct has
253253
--> $DIR/pat-tuple-field-count-cross.rs:26:11
254254
|
255255
LL | M(xyz, abc) => {}
256-
| - ^^^ ^^^ expected 3 fields, found 2
256+
| ^^^ ^^^ expected 3 fields, found 2
257257
|
258258
::: $DIR/auxiliary/declarations-for-tuple-field-count-errors.rs:5:1
259259
|
@@ -276,7 +276,7 @@ error[E0023]: this pattern has 4 fields, but the corresponding tuple struct has
276276
--> $DIR/pat-tuple-field-count-cross.rs:27:11
277277
|
278278
LL | M(1, 2, 3, 4) => {}
279-
| - ^ ^ ^ ^ expected 3 fields, found 4
279+
| ^ ^ ^ ^ expected 3 fields, found 4
280280
|
281281
::: $DIR/auxiliary/declarations-for-tuple-field-count-errors.rs:5:1
282282
|
@@ -294,18 +294,18 @@ error[E0023]: this pattern has 1 field, but the corresponding tuple variant has
294294
--> $DIR/pat-tuple-field-count-cross.rs:36:16
295295
|
296296
LL | E1::Z1(x) => {}
297-
| ------ ^ expected 0 fields, found 1
297+
| ^ expected 0 fields, found 1
298298
|
299299
::: $DIR/auxiliary/declarations-for-tuple-field-count-errors.rs:11:19
300300
|
301301
LL | pub enum E1 { Z0, Z1(), S(u8, u8, u8) }
302302
| ---- tuple variant has 0 fields
303303

304304
error[E0023]: this pattern has 0 fields, but the corresponding tuple variant has 3 fields
305-
--> $DIR/pat-tuple-field-count-cross.rs:39:14
305+
--> $DIR/pat-tuple-field-count-cross.rs:39:9
306306
|
307307
LL | E1::S() => {}
308-
| -----^^ expected 3 fields, found 0
308+
| ^^^^^^^ expected 3 fields, found 0
309309
|
310310
::: $DIR/auxiliary/declarations-for-tuple-field-count-errors.rs:11:27
311311
|
@@ -325,7 +325,7 @@ error[E0023]: this pattern has 1 field, but the corresponding tuple variant has
325325
--> $DIR/pat-tuple-field-count-cross.rs:40:15
326326
|
327327
LL | E1::S(1) => {}
328-
| ----- ^ expected 3 fields, found 1
328+
| ^ expected 3 fields, found 1
329329
|
330330
::: $DIR/auxiliary/declarations-for-tuple-field-count-errors.rs:11:27
331331
|
@@ -345,7 +345,7 @@ error[E0023]: this pattern has 2 fields, but the corresponding tuple variant has
345345
--> $DIR/pat-tuple-field-count-cross.rs:41:15
346346
|
347347
LL | E1::S(xyz, abc) => {}
348-
| ----- ^^^ ^^^ expected 3 fields, found 2
348+
| ^^^ ^^^ expected 3 fields, found 2
349349
|
350350
::: $DIR/auxiliary/declarations-for-tuple-field-count-errors.rs:11:27
351351
|
@@ -361,18 +361,18 @@ error[E0023]: this pattern has 4 fields, but the corresponding tuple variant has
361361
--> $DIR/pat-tuple-field-count-cross.rs:42:15
362362
|
363363
LL | E1::S(1, 2, 3, 4) => {}
364-
| ----- ^ ^ ^ ^ expected 3 fields, found 4
364+
| ^ ^ ^ ^ expected 3 fields, found 4
365365
|
366366
::: $DIR/auxiliary/declarations-for-tuple-field-count-errors.rs:11:27
367367
|
368368
LL | pub enum E1 { Z0, Z1(), S(u8, u8, u8) }
369369
| -- -- -- tuple variant has 3 fields
370370

371371
error[E0023]: this pattern has 0 fields, but the corresponding tuple variant has 3 fields
372-
--> $DIR/pat-tuple-field-count-cross.rs:46:14
372+
--> $DIR/pat-tuple-field-count-cross.rs:46:9
373373
|
374374
LL | E2::S() => {}
375-
| -----^^ expected 3 fields, found 0
375+
| ^^^^^^^ expected 3 fields, found 0
376376
|
377377
::: $DIR/auxiliary/declarations-for-tuple-field-count-errors.rs:14:7
378378
|
@@ -392,7 +392,7 @@ error[E0023]: this pattern has 1 field, but the corresponding tuple variant has
392392
--> $DIR/pat-tuple-field-count-cross.rs:47:15
393393
|
394394
LL | E2::S(1) => {}
395-
| ----- ^ expected 3 fields, found 1
395+
| ^ expected 3 fields, found 1
396396
|
397397
::: $DIR/auxiliary/declarations-for-tuple-field-count-errors.rs:14:7
398398
|
@@ -412,7 +412,7 @@ error[E0023]: this pattern has 2 fields, but the corresponding tuple variant has
412412
--> $DIR/pat-tuple-field-count-cross.rs:48:15
413413
|
414414
LL | E2::S(xyz, abc) => {}
415-
| ----- ^^^ ^^^ expected 3 fields, found 2
415+
| ^^^ ^^^ expected 3 fields, found 2
416416
|
417417
::: $DIR/auxiliary/declarations-for-tuple-field-count-errors.rs:14:7
418418
|
@@ -428,18 +428,18 @@ error[E0023]: this pattern has 4 fields, but the corresponding tuple variant has
428428
--> $DIR/pat-tuple-field-count-cross.rs:49:15
429429
|
430430
LL | E2::S(1, 2, 3, 4) => {}
431-
| ----- ^ ^ ^ ^ expected 3 fields, found 4
431+
| ^ ^ ^ ^ expected 3 fields, found 4
432432
|
433433
::: $DIR/auxiliary/declarations-for-tuple-field-count-errors.rs:14:7
434434
|
435435
LL | S(u8, u8, u8),
436436
| -- -- -- tuple variant has 3 fields
437437

438438
error[E0023]: this pattern has 0 fields, but the corresponding tuple variant has 3 fields
439-
--> $DIR/pat-tuple-field-count-cross.rs:52:14
439+
--> $DIR/pat-tuple-field-count-cross.rs:52:9
440440
|
441441
LL | E2::M() => {}
442-
| -----^^ expected 3 fields, found 0
442+
| ^^^^^^^ expected 3 fields, found 0
443443
|
444444
::: $DIR/auxiliary/declarations-for-tuple-field-count-errors.rs:15:5
445445
|
@@ -466,7 +466,7 @@ error[E0023]: this pattern has 1 field, but the corresponding tuple variant has
466466
--> $DIR/pat-tuple-field-count-cross.rs:53:15
467467
|
468468
LL | E2::M(1) => {}
469-
| ----- ^ expected 3 fields, found 1
469+
| ^ expected 3 fields, found 1
470470
|
471471
::: $DIR/auxiliary/declarations-for-tuple-field-count-errors.rs:15:5
472472
|
@@ -493,7 +493,7 @@ error[E0023]: this pattern has 2 fields, but the corresponding tuple variant has
493493
--> $DIR/pat-tuple-field-count-cross.rs:54:15
494494
|
495495
LL | E2::M(xyz, abc) => {}
496-
| ----- ^^^ ^^^ expected 3 fields, found 2
496+
| ^^^ ^^^ expected 3 fields, found 2
497497
|
498498
::: $DIR/auxiliary/declarations-for-tuple-field-count-errors.rs:15:5
499499
|
@@ -516,7 +516,7 @@ error[E0023]: this pattern has 4 fields, but the corresponding tuple variant has
516516
--> $DIR/pat-tuple-field-count-cross.rs:55:15
517517
|
518518
LL | E2::M(1, 2, 3, 4) => {}
519-
| ----- ^ ^ ^ ^ expected 3 fields, found 4
519+
| ^ ^ ^ ^ expected 3 fields, found 4
520520
|
521521
::: $DIR/auxiliary/declarations-for-tuple-field-count-errors.rs:15:5
522522
|

0 commit comments

Comments
 (0)