1
Fork 0

Make spans for tuple patterns in E0023 more precise

As suggested in #86307.
This commit is contained in:
Noah Lev 2021-08-17 13:55:31 -07:00
parent d83da1d05d
commit da25af2940
10 changed files with 96 additions and 48 deletions

View file

@ -990,10 +990,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
) {
let subpats_ending = pluralize!(subpats.len());
let fields_ending = pluralize!(fields.len());
let fields_span = pat_span.trim_start(qpath.span()).unwrap_or(pat_span);
let res_span = self.tcx.def_span(res.def_id());
let mut err = struct_span_err!(
self.tcx.sess,
pat_span,
fields_span,
E0023,
"this pattern has {} field{}, but the corresponding {} has {} field{}",
subpats.len(),
@ -1003,9 +1004,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
fields_ending,
);
err.span_label(
pat_span,
fields_span,
format!("expected {} field{}, found {}", fields.len(), fields_ending, subpats.len(),),
)
.span_label(qpath.span(), format!("this {}", res.descr()))
.span_label(res_span, format!("{} defined here", res.descr()));
// Identify the case `Some(x, y)` where the expected type is e.g. `Option<(T, U)>`.

View file

@ -15,22 +15,26 @@ LL | Enum::SingleVariant(a, .., b, ..) = Enum::SingleVariant(0, 1);
| previously used here
error[E0023]: this pattern has 3 fields, but the corresponding tuple struct has 2 fields
--> $DIR/tuple_struct_destructure_fail.rs:30:5
--> $DIR/tuple_struct_destructure_fail.rs:30:16
|
LL | struct TupleStruct<S, T>(S, T);
| ------------------------------- tuple struct defined here
...
LL | TupleStruct(a, a, b) = TupleStruct(1, 2);
| ^^^^^^^^^^^^^^^^^^^^ expected 2 fields, found 3
| -----------^^^^^^^^^ expected 2 fields, found 3
| |
| this tuple struct
error[E0023]: this pattern has 1 field, but the corresponding tuple struct has 2 fields
--> $DIR/tuple_struct_destructure_fail.rs:32:5
--> $DIR/tuple_struct_destructure_fail.rs:32:16
|
LL | struct TupleStruct<S, T>(S, T);
| ------------------------------- tuple struct defined here
...
LL | TupleStruct(_) = TupleStruct(1, 2);
| ^^^^^^^^^^^^^^ expected 2 fields, found 1
| -----------^^^ expected 2 fields, found 1
| |
| this tuple struct
|
help: use `_` to explicitly ignore each field
|
@ -42,22 +46,26 @@ LL | TupleStruct(..) = TupleStruct(1, 2);
| ~~
error[E0023]: this pattern has 3 fields, but the corresponding tuple variant has 2 fields
--> $DIR/tuple_struct_destructure_fail.rs:34:5
--> $DIR/tuple_struct_destructure_fail.rs:34:24
|
LL | SingleVariant(S, T)
| ------------------- tuple variant defined here
...
LL | Enum::SingleVariant(a, a, b) = Enum::SingleVariant(1, 2);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected 2 fields, found 3
| -------------------^^^^^^^^^ expected 2 fields, found 3
| |
| this tuple variant
error[E0023]: this pattern has 1 field, but the corresponding tuple variant has 2 fields
--> $DIR/tuple_struct_destructure_fail.rs:36:5
--> $DIR/tuple_struct_destructure_fail.rs:36:24
|
LL | SingleVariant(S, T)
| ------------------- tuple variant defined here
...
LL | Enum::SingleVariant(_) = Enum::SingleVariant(1, 2);
| ^^^^^^^^^^^^^^^^^^^^^^ expected 2 fields, found 1
| -------------------^^^ expected 2 fields, found 1
| |
| this tuple variant
|
help: use `_` to explicitly ignore each field
|

View file

@ -1,11 +1,13 @@
error[E0023]: this pattern has 1 field, but the corresponding tuple variant has 2 fields
--> $DIR/E0023.rs:11:9
--> $DIR/E0023.rs:11:21
|
LL | Apple(String, String),
| --------------------- tuple variant defined here
...
LL | Fruit::Apple(a) => {},
| ^^^^^^^^^^^^^^^ expected 2 fields, found 1
| ------------^^^ expected 2 fields, found 1
| |
| this tuple variant
|
help: use `_` to explicitly ignore each field
|
@ -13,31 +15,37 @@ LL | Fruit::Apple(a, _) => {},
| +++
error[E0023]: this pattern has 3 fields, but the corresponding tuple variant has 2 fields
--> $DIR/E0023.rs:12:9
--> $DIR/E0023.rs:12:21
|
LL | Apple(String, String),
| --------------------- tuple variant defined here
...
LL | Fruit::Apple(a, b, c) => {},
| ^^^^^^^^^^^^^^^^^^^^^ expected 2 fields, found 3
| ------------^^^^^^^^^ expected 2 fields, found 3
| |
| this tuple variant
error[E0023]: this pattern has 2 fields, but the corresponding tuple variant has 1 field
--> $DIR/E0023.rs:13:9
--> $DIR/E0023.rs:13:20
|
LL | Pear(u32),
| --------- tuple variant defined here
...
LL | Fruit::Pear(1, 2) => {},
| ^^^^^^^^^^^^^^^^^ expected 1 field, found 2
| -----------^^^^^^ expected 1 field, found 2
| |
| this tuple variant
error[E0023]: this pattern has 2 fields, but the corresponding tuple variant has 1 field
--> $DIR/E0023.rs:14:9
--> $DIR/E0023.rs:14:22
|
LL | Orange((String, String)),
| ------------------------ tuple variant defined here
...
LL | Fruit::Orange(a, b) => {},
| ^^^^^^^^^^^^^^^^^^^ expected 1 field, found 2
| -------------^^^^^^ expected 1 field, found 2
| |
| this tuple variant
|
help: missing parentheses
|
@ -45,13 +53,15 @@ LL | Fruit::Orange((a, b)) => {},
| + +
error[E0023]: this pattern has 0 fields, but the corresponding tuple variant has 1 field
--> $DIR/E0023.rs:15:9
--> $DIR/E0023.rs:15:22
|
LL | Banana(()),
| ---------- tuple variant defined here
...
LL | Fruit::Banana() => {},
| ^^^^^^^^^^^^^^^ expected 1 field, found 0
| -------------^^ expected 1 field, found 0
| |
| this tuple variant
|
help: missing parentheses
|

View file

@ -19,13 +19,15 @@ LL | Binder(_a, _x @ ..) => {}
= note: only allowed in tuple, tuple struct, and slice patterns
error[E0023]: this pattern has 2 fields, but the corresponding tuple struct has 3 fields
--> $DIR/issue-72574-2.rs:6:9
--> $DIR/issue-72574-2.rs:6:15
|
LL | struct Binder(i32, i32, i32);
| ----------------------------- tuple struct defined here
...
LL | Binder(_a, _x @ ..) => {}
| ^^^^^^^^^^^^^^^^^^^ expected 3 fields, found 2
| ------^^^^^^^^^^^^^ expected 3 fields, found 2
| |
| this tuple struct
|
help: use `_` to explicitly ignore each field
|

View file

@ -1,11 +1,13 @@
error[E0023]: this pattern has 2 fields, but the corresponding tuple variant has 3 fields
--> $DIR/match-pattern-field-mismatch.rs:10:11
--> $DIR/match-pattern-field-mismatch.rs:10:21
|
LL | Rgb(usize, usize, usize),
| ------------------------ tuple variant defined here
...
LL | Color::Rgb(_, _) => { }
| ^^^^^^^^^^^^^^^^ expected 3 fields, found 2
| ----------^^^^^^ expected 3 fields, found 2
| |
| this tuple variant
|
help: use `_` to explicitly ignore each field
|

View file

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

View file

@ -19,13 +19,15 @@ LL | E::A(x @ ..) => {
= note: only allowed in tuple, tuple struct, and slice patterns
error[E0023]: this pattern has 1 field, but the corresponding tuple variant has 2 fields
--> $DIR/issue-74539.rs:8:9
--> $DIR/issue-74539.rs:8:13
|
LL | A(u8, u8),
| --------- tuple variant defined here
...
LL | E::A(x @ ..) => {
| ^^^^^^^^^^^^ expected 2 fields, found 1
| ----^^^^^^^^ expected 2 fields, found 1
| |
| this tuple variant
|
help: use `_` to explicitly ignore each field
|

View file

@ -22,22 +22,26 @@ LL | (1, 2, .., 3, 4) => {}
found tuple `(_, _, _, _)`
error[E0023]: this pattern has 4 fields, but the corresponding tuple struct has 3 fields
--> $DIR/pat-tuple-overfield.rs:10:9
--> $DIR/pat-tuple-overfield.rs:10:10
|
LL | struct S(u8, u8, u8);
| --------------------- tuple struct defined here
...
LL | S(1, 2, 3, 4) => {}
| ^^^^^^^^^^^^^ expected 3 fields, found 4
| -^^^^^^^^^^^^ expected 3 fields, found 4
| |
| this tuple struct
error[E0023]: this pattern has 4 fields, but the corresponding tuple struct has 3 fields
--> $DIR/pat-tuple-overfield.rs:12:9
--> $DIR/pat-tuple-overfield.rs:12:10
|
LL | struct S(u8, u8, u8);
| --------------------- tuple struct defined here
...
LL | S(1, 2, .., 3, 4) => {}
| ^^^^^^^^^^^^^^^^^ expected 3 fields, found 4
| -^^^^^^^^^^^^^^^^ expected 3 fields, found 4
| |
| this tuple struct
error: aborting due to 4 previous errors

View file

@ -8,13 +8,15 @@ LL | E::S => {}
| ^^^^ help: use the tuple variant pattern syntax instead: `E::S(_, _)`
error[E0023]: this pattern has 1 field, but the corresponding tuple struct has 2 fields
--> $DIR/pat-tuple-underfield.rs:9:9
--> $DIR/pat-tuple-underfield.rs:9:10
|
LL | struct S(i32, f32);
| ------------------- tuple struct defined here
...
LL | S(x) => {}
| ^^^^ expected 2 fields, found 1
| -^^^ expected 2 fields, found 1
| |
| this tuple struct
|
help: use `_` to explicitly ignore each field
|
@ -22,13 +24,15 @@ LL | S(x, _) => {}
| +++
error[E0023]: this pattern has 1 field, but the corresponding tuple struct has 2 fields
--> $DIR/pat-tuple-underfield.rs:14:9
--> $DIR/pat-tuple-underfield.rs:14:10
|
LL | struct S(i32, f32);
| ------------------- tuple struct defined here
...
LL | S(_) => {}
| ^^^^ expected 2 fields, found 1
| -^^^ expected 2 fields, found 1
| |
| this tuple struct
|
help: use `_` to explicitly ignore each field
|
@ -40,13 +44,15 @@ LL | S(..) => {}
| ~~
error[E0023]: this pattern has 0 fields, but the corresponding tuple struct has 2 fields
--> $DIR/pat-tuple-underfield.rs:20:9
--> $DIR/pat-tuple-underfield.rs:20:10
|
LL | struct S(i32, f32);
| ------------------- tuple struct defined here
...
LL | S() => {}
| ^^^ expected 2 fields, found 0
| -^^ expected 2 fields, found 0
| |
| this tuple struct
|
help: use `_` to explicitly ignore each field
|
@ -58,13 +64,15 @@ LL | S(..) => {}
| ++
error[E0023]: this pattern has 1 field, but the corresponding tuple variant has 2 fields
--> $DIR/pat-tuple-underfield.rs:27:9
--> $DIR/pat-tuple-underfield.rs:27:13
|
LL | S(i32, f32),
| ----------- tuple variant defined here
...
LL | E::S(x) => {}
| ^^^^^^^ expected 2 fields, found 1
| ----^^^ expected 2 fields, found 1
| |
| this tuple variant
|
help: use `_` to explicitly ignore each field
|
@ -72,13 +80,15 @@ LL | E::S(x, _) => {}
| +++
error[E0023]: this pattern has 1 field, but the corresponding tuple variant has 2 fields
--> $DIR/pat-tuple-underfield.rs:32:9
--> $DIR/pat-tuple-underfield.rs:32:13
|
LL | S(i32, f32),
| ----------- tuple variant defined here
...
LL | E::S(_) => {}
| ^^^^^^^ expected 2 fields, found 1
| ----^^^ expected 2 fields, found 1
| |
| this tuple variant
|
help: use `_` to explicitly ignore each field
|
@ -90,13 +100,15 @@ LL | E::S(..) => {}
| ~~
error[E0023]: this pattern has 0 fields, but the corresponding tuple variant has 2 fields
--> $DIR/pat-tuple-underfield.rs:38:9
--> $DIR/pat-tuple-underfield.rs:38:13
|
LL | S(i32, f32),
| ----------- tuple variant defined here
...
LL | E::S() => {}
| ^^^^^^ expected 2 fields, found 0
| ----^^ expected 2 fields, found 0
| |
| this tuple variant
|
help: use `_` to explicitly ignore each field
|
@ -108,13 +120,15 @@ LL | E::S(..) => {}
| ++
error[E0023]: this pattern has 2 fields, but the corresponding tuple struct has 4 fields
--> $DIR/pat-tuple-underfield.rs:50:9
--> $DIR/pat-tuple-underfield.rs:50:15
|
LL | struct Point4(i32, i32, i32, i32);
| ---------------------------------- tuple struct defined here
...
LL | Point4( a , _ ) => {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ expected 4 fields, found 2
| ------^^^^^^^^^^^^^^^^^^^^ expected 4 fields, found 2
| |
| this tuple struct
|
help: use `_` to explicitly ignore each field
|

View file

@ -26,13 +26,15 @@ LL | A::B(_) => (),
| ~
error[E0023]: this pattern has 3 fields, but the corresponding tuple variant has 2 fields
--> $DIR/pattern-error-continue.rs:17:9
--> $DIR/pattern-error-continue.rs:17:13
|
LL | B(isize, isize),
| --------------- tuple variant defined here
...
LL | A::B(_, _, _) => (),
| ^^^^^^^^^^^^^ expected 2 fields, found 3
| ----^^^^^^^^^ expected 2 fields, found 3
| |
| this tuple variant
error[E0308]: mismatched types
--> $DIR/pattern-error-continue.rs:22:9