Skip to content

Commit

Permalink
Make spans for tuple patterns in E0023 more precise
Browse files Browse the repository at this point in the history
As suggested in rust-lang#86307.
  • Loading branch information
camelid committed Aug 17, 2021
1 parent d83da1d commit da25af2
Show file tree
Hide file tree
Showing 10 changed files with 96 additions and 48 deletions.
6 changes: 4 additions & 2 deletions compiler/rustc_typeck/src/check/pat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -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)>`.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
|
Expand All @@ -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
|
Expand Down
30 changes: 20 additions & 10 deletions src/test/ui/error-codes/E0023.stderr
Original file line number Diff line number Diff line change
@@ -1,57 +1,67 @@
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
|
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
|
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
|
Expand Down
6 changes: 4 additions & 2 deletions src/test/ui/issues/issue-72574-2.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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
|
Expand Down
6 changes: 4 additions & 2 deletions src/test/ui/match/match-pattern-field-mismatch.stderr
Original file line number Diff line number Diff line change
@@ -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
|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
|
Expand Down
6 changes: 4 additions & 2 deletions src/test/ui/pattern/issue-74539.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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
|
Expand Down
12 changes: 8 additions & 4 deletions src/test/ui/pattern/pat-tuple-overfield.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
42 changes: 28 additions & 14 deletions src/test/ui/pattern/pat-tuple-underfield.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,31 @@ 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
|
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
|
Expand All @@ -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
|
Expand All @@ -58,27 +64,31 @@ 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
|
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
|
Expand All @@ -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
|
Expand All @@ -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
|
Expand Down
Loading

0 comments on commit da25af2

Please sign in to comment.