Skip to content

Make spans for tuple patterns in E0023 more precise #88123

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Aug 27, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
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
  • Loading branch information
camelid committed Aug 25, 2021
commit 8a6501d28831d864a3af6adf2e0bd83a773062ed
10 changes: 6 additions & 4 deletions compiler/rustc_typeck/src/check/pat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -992,15 +992,15 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
let fields_ending = pluralize!(fields.len());

let subpat_spans = if subpats.is_empty() {
vec![pat_span.trim_start(qpath.span()).unwrap_or(pat_span)]
vec![pat_span]
} else {
subpats.iter().map(|p| p.span).collect()
};
let last_subpat_span = *subpat_spans.last().unwrap();
let res_span = self.tcx.def_span(res.def_id());
let def_ident_span = self.tcx.def_ident_span(res.def_id()).unwrap_or(res_span);
let field_def_spans = if fields.is_empty() {
vec![res_span.trim_start(def_ident_span).unwrap_or(res_span)]
vec![res_span]
} else {
fields.iter().map(|f| f.ident.span).collect()
};
Expand All @@ -1021,8 +1021,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
last_subpat_span,
&format!("expected {} field{}, found {}", fields.len(), fields_ending, subpats.len()),
);
err.span_label(qpath.span(), "");
if self.tcx.sess.source_map().is_multiline(def_ident_span.between(field_def_spans[0])) {
if self.tcx.sess.source_map().is_multiline(qpath.span().between(last_subpat_span)) {
err.span_label(qpath.span(), "");
}
if self.tcx.sess.source_map().is_multiline(def_ident_span.between(last_field_def_span)) {
err.span_label(def_ident_span, format!("{} defined here", res.descr()));
}
for span in &field_def_spans[..field_def_spans.len() - 1] {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ LL | struct TupleStruct<S, T>(S, T);
| - - tuple struct has 2 fields
...
LL | TupleStruct(a, a, b) = TupleStruct(1, 2);
| ----------- ^ ^ ^ expected 2 fields, found 3
| ^ ^ ^ expected 2 fields, found 3

error[E0023]: this pattern has 1 field, but the corresponding tuple struct has 2 fields
--> $DIR/tuple_struct_destructure_fail.rs:32:17
Expand All @@ -30,7 +30,7 @@ LL | struct TupleStruct<S, T>(S, T);
| - - tuple struct has 2 fields
...
LL | TupleStruct(_) = TupleStruct(1, 2);
| ----------- ^ expected 2 fields, found 1
| ^ expected 2 fields, found 1
|
help: use `_` to explicitly ignore each field
|
Expand All @@ -48,7 +48,7 @@ LL | SingleVariant(S, T)
| - - tuple variant has 2 fields
...
LL | Enum::SingleVariant(a, a, b) = Enum::SingleVariant(1, 2);
| ------------------- ^ ^ ^ expected 2 fields, found 3
| ^ ^ ^ expected 2 fields, found 3

error[E0023]: this pattern has 1 field, but the corresponding tuple variant has 2 fields
--> $DIR/tuple_struct_destructure_fail.rs:36:25
Expand All @@ -57,7 +57,7 @@ LL | SingleVariant(S, T)
| - - tuple variant has 2 fields
...
LL | Enum::SingleVariant(_) = Enum::SingleVariant(1, 2);
| ------------------- ^ expected 2 fields, found 1
| ^ expected 2 fields, found 1
|
help: use `_` to explicitly ignore each field
|
Expand Down
12 changes: 6 additions & 6 deletions src/test/ui/error-codes/E0023.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ LL | Apple(String, String),
| ------ ------ tuple variant has 2 fields
...
LL | Fruit::Apple(a) => {},
| ------------ ^ expected 2 fields, found 1
| ^ expected 2 fields, found 1
|
help: use `_` to explicitly ignore each field
|
Expand All @@ -19,7 +19,7 @@ LL | Apple(String, String),
| ------ ------ tuple variant has 2 fields
...
LL | Fruit::Apple(a, b, c) => {},
| ------------ ^ ^ ^ expected 2 fields, found 3
| ^ ^ ^ expected 2 fields, found 3

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

error[E0023]: this pattern has 2 fields, but the corresponding tuple variant has 1 field
--> $DIR/E0023.rs:14:23
Expand All @@ -37,21 +37,21 @@ LL | Orange((String, String)),
| ---------------- tuple variant has 1 field
...
LL | Fruit::Orange(a, b) => {},
| ------------- ^ ^ expected 1 field, found 2
| ^ ^ expected 1 field, found 2
|
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:22
--> $DIR/E0023.rs:15:9
|
LL | Banana(()),
| -- tuple variant has 1 field
...
LL | Fruit::Banana() => {},
| -------------^^ expected 1 field, found 0
| ^^^^^^^^^^^^^^^ expected 1 field, found 0
|
help: missing parentheses
|
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/issues/issue-72574-2.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ LL | struct Binder(i32, i32, i32);
| --- --- --- tuple struct has 3 fields
...
LL | Binder(_a, _x @ ..) => {}
| ------ ^^ ^^^^^^^ expected 3 fields, found 2
| ^^ ^^^^^^^ expected 3 fields, found 2
|
help: use `_` to explicitly ignore each field
|
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/match/match-pattern-field-mismatch.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ LL | Rgb(usize, usize, usize),
| ----- ----- ----- tuple variant has 3 fields
...
LL | Color::Rgb(_, _) => { }
| ---------- ^ ^ expected 3 fields, found 2
| ^ ^ expected 3 fields, found 2
|
help: use `_` to explicitly ignore each field
|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ 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:10
--> $DIR/issue-67037-pat-tup-scrut-ty-diff-less-fields.rs:19:9
|
LL | struct P<T>(T); // 1 type parameter wanted
| - tuple struct has 1 field
...
LL | let P() = U {};
| -^^ expected 1 field, found 0
| ^^^ expected 1 field, found 0
|
help: use `_` to explicitly ignore each field
|
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/pattern/issue-74539.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ LL | A(u8, u8),
| -- -- tuple variant has 2 fields
...
LL | E::A(x @ ..) => {
| ---- ^^^^^^ expected 2 fields, found 1
| ^^^^^^ expected 2 fields, found 1
|
help: use `_` to explicitly ignore each field
|
Expand Down
54 changes: 27 additions & 27 deletions src/test/ui/pattern/pat-tuple-field-count-cross.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -121,18 +121,18 @@ error[E0023]: this pattern has 1 field, but the corresponding tuple struct has 0
--> $DIR/pat-tuple-field-count-cross.rs:14:12
|
LL | Z1(x) => {}
| -- ^ expected 0 fields, found 1
| ^ expected 0 fields, found 1
|
::: $DIR/auxiliary/declarations-for-tuple-field-count-errors.rs:2:1
|
LL | pub struct Z1();
| ---------------- tuple struct has 0 fields

error[E0023]: this pattern has 0 fields, but the corresponding tuple struct has 3 fields
--> $DIR/pat-tuple-field-count-cross.rs:18:10
--> $DIR/pat-tuple-field-count-cross.rs:18:9
|
LL | S() => {}
| -^^ expected 3 fields, found 0
| ^^^ expected 3 fields, found 0
|
::: $DIR/auxiliary/declarations-for-tuple-field-count-errors.rs:4:14
|
Expand All @@ -152,7 +152,7 @@ error[E0023]: this pattern has 1 field, but the corresponding tuple struct has 3
--> $DIR/pat-tuple-field-count-cross.rs:19:11
|
LL | S(1) => {}
| - ^ expected 3 fields, found 1
| ^ expected 3 fields, found 1
|
::: $DIR/auxiliary/declarations-for-tuple-field-count-errors.rs:4:14
|
Expand All @@ -172,7 +172,7 @@ error[E0023]: this pattern has 2 fields, but the corresponding tuple struct has
--> $DIR/pat-tuple-field-count-cross.rs:20:11
|
LL | S(xyz, abc) => {}
| - ^^^ ^^^ expected 3 fields, found 2
| ^^^ ^^^ expected 3 fields, found 2
|
::: $DIR/auxiliary/declarations-for-tuple-field-count-errors.rs:4:14
|
Expand All @@ -188,18 +188,18 @@ error[E0023]: this pattern has 4 fields, but the corresponding tuple struct has
--> $DIR/pat-tuple-field-count-cross.rs:21:11
|
LL | S(1, 2, 3, 4) => {}
| - ^ ^ ^ ^ expected 3 fields, found 4
| ^ ^ ^ ^ expected 3 fields, found 4
|
::: $DIR/auxiliary/declarations-for-tuple-field-count-errors.rs:4:14
|
LL | pub struct S(pub u8, pub u8, pub u8);
| ------ ------ ------ tuple struct has 3 fields

error[E0023]: this pattern has 0 fields, but the corresponding tuple struct has 3 fields
--> $DIR/pat-tuple-field-count-cross.rs:24:10
--> $DIR/pat-tuple-field-count-cross.rs:24:9
|
LL | M() => {}
| -^^ expected 3 fields, found 0
| ^^^ expected 3 fields, found 0
|
::: $DIR/auxiliary/declarations-for-tuple-field-count-errors.rs:5:1
|
Expand All @@ -226,7 +226,7 @@ error[E0023]: this pattern has 1 field, but the corresponding tuple struct has 3
--> $DIR/pat-tuple-field-count-cross.rs:25:11
|
LL | M(1) => {}
| - ^ expected 3 fields, found 1
| ^ expected 3 fields, found 1
|
::: $DIR/auxiliary/declarations-for-tuple-field-count-errors.rs:5:1
|
Expand All @@ -253,7 +253,7 @@ error[E0023]: this pattern has 2 fields, but the corresponding tuple struct has
--> $DIR/pat-tuple-field-count-cross.rs:26:11
|
LL | M(xyz, abc) => {}
| - ^^^ ^^^ expected 3 fields, found 2
| ^^^ ^^^ expected 3 fields, found 2
|
::: $DIR/auxiliary/declarations-for-tuple-field-count-errors.rs:5:1
|
Expand All @@ -276,7 +276,7 @@ error[E0023]: this pattern has 4 fields, but the corresponding tuple struct has
--> $DIR/pat-tuple-field-count-cross.rs:27:11
|
LL | M(1, 2, 3, 4) => {}
| - ^ ^ ^ ^ expected 3 fields, found 4
| ^ ^ ^ ^ expected 3 fields, found 4
|
::: $DIR/auxiliary/declarations-for-tuple-field-count-errors.rs:5:1
|
Expand All @@ -294,18 +294,18 @@ error[E0023]: this pattern has 1 field, but the corresponding tuple variant has
--> $DIR/pat-tuple-field-count-cross.rs:36:16
|
LL | E1::Z1(x) => {}
| ------ ^ expected 0 fields, found 1
| ^ expected 0 fields, found 1
|
::: $DIR/auxiliary/declarations-for-tuple-field-count-errors.rs:11:19
|
LL | pub enum E1 { Z0, Z1(), S(u8, u8, u8) }
| ---- tuple variant has 0 fields

error[E0023]: this pattern has 0 fields, but the corresponding tuple variant has 3 fields
--> $DIR/pat-tuple-field-count-cross.rs:39:14
--> $DIR/pat-tuple-field-count-cross.rs:39:9
|
LL | E1::S() => {}
| -----^^ expected 3 fields, found 0
| ^^^^^^^ expected 3 fields, found 0
|
::: $DIR/auxiliary/declarations-for-tuple-field-count-errors.rs:11:27
|
Expand All @@ -325,7 +325,7 @@ error[E0023]: this pattern has 1 field, but the corresponding tuple variant has
--> $DIR/pat-tuple-field-count-cross.rs:40:15
|
LL | E1::S(1) => {}
| ----- ^ expected 3 fields, found 1
| ^ expected 3 fields, found 1
|
::: $DIR/auxiliary/declarations-for-tuple-field-count-errors.rs:11:27
|
Expand All @@ -345,7 +345,7 @@ error[E0023]: this pattern has 2 fields, but the corresponding tuple variant has
--> $DIR/pat-tuple-field-count-cross.rs:41:15
|
LL | E1::S(xyz, abc) => {}
| ----- ^^^ ^^^ expected 3 fields, found 2
| ^^^ ^^^ expected 3 fields, found 2
|
::: $DIR/auxiliary/declarations-for-tuple-field-count-errors.rs:11:27
|
Expand All @@ -361,18 +361,18 @@ error[E0023]: this pattern has 4 fields, but the corresponding tuple variant has
--> $DIR/pat-tuple-field-count-cross.rs:42:15
|
LL | E1::S(1, 2, 3, 4) => {}
| ----- ^ ^ ^ ^ expected 3 fields, found 4
| ^ ^ ^ ^ expected 3 fields, found 4
|
::: $DIR/auxiliary/declarations-for-tuple-field-count-errors.rs:11:27
|
LL | pub enum E1 { Z0, Z1(), S(u8, u8, u8) }
| -- -- -- tuple variant has 3 fields

error[E0023]: this pattern has 0 fields, but the corresponding tuple variant has 3 fields
--> $DIR/pat-tuple-field-count-cross.rs:46:14
--> $DIR/pat-tuple-field-count-cross.rs:46:9
|
LL | E2::S() => {}
| -----^^ expected 3 fields, found 0
| ^^^^^^^ expected 3 fields, found 0
|
::: $DIR/auxiliary/declarations-for-tuple-field-count-errors.rs:14:7
|
Expand All @@ -392,7 +392,7 @@ error[E0023]: this pattern has 1 field, but the corresponding tuple variant has
--> $DIR/pat-tuple-field-count-cross.rs:47:15
|
LL | E2::S(1) => {}
| ----- ^ expected 3 fields, found 1
| ^ expected 3 fields, found 1
|
::: $DIR/auxiliary/declarations-for-tuple-field-count-errors.rs:14:7
|
Expand All @@ -412,7 +412,7 @@ error[E0023]: this pattern has 2 fields, but the corresponding tuple variant has
--> $DIR/pat-tuple-field-count-cross.rs:48:15
|
LL | E2::S(xyz, abc) => {}
| ----- ^^^ ^^^ expected 3 fields, found 2
| ^^^ ^^^ expected 3 fields, found 2
|
::: $DIR/auxiliary/declarations-for-tuple-field-count-errors.rs:14:7
|
Expand All @@ -428,18 +428,18 @@ error[E0023]: this pattern has 4 fields, but the corresponding tuple variant has
--> $DIR/pat-tuple-field-count-cross.rs:49:15
|
LL | E2::S(1, 2, 3, 4) => {}
| ----- ^ ^ ^ ^ expected 3 fields, found 4
| ^ ^ ^ ^ expected 3 fields, found 4
|
::: $DIR/auxiliary/declarations-for-tuple-field-count-errors.rs:14:7
|
LL | S(u8, u8, u8),
| -- -- -- tuple variant has 3 fields

error[E0023]: this pattern has 0 fields, but the corresponding tuple variant has 3 fields
--> $DIR/pat-tuple-field-count-cross.rs:52:14
--> $DIR/pat-tuple-field-count-cross.rs:52:9
|
LL | E2::M() => {}
| -----^^ expected 3 fields, found 0
| ^^^^^^^ expected 3 fields, found 0
|
::: $DIR/auxiliary/declarations-for-tuple-field-count-errors.rs:15:5
|
Expand All @@ -466,7 +466,7 @@ error[E0023]: this pattern has 1 field, but the corresponding tuple variant has
--> $DIR/pat-tuple-field-count-cross.rs:53:15
|
LL | E2::M(1) => {}
| ----- ^ expected 3 fields, found 1
| ^ expected 3 fields, found 1
|
::: $DIR/auxiliary/declarations-for-tuple-field-count-errors.rs:15:5
|
Expand All @@ -493,7 +493,7 @@ error[E0023]: this pattern has 2 fields, but the corresponding tuple variant has
--> $DIR/pat-tuple-field-count-cross.rs:54:15
|
LL | E2::M(xyz, abc) => {}
| ----- ^^^ ^^^ expected 3 fields, found 2
| ^^^ ^^^ expected 3 fields, found 2
|
::: $DIR/auxiliary/declarations-for-tuple-field-count-errors.rs:15:5
|
Expand All @@ -516,7 +516,7 @@ error[E0023]: this pattern has 4 fields, but the corresponding tuple variant has
--> $DIR/pat-tuple-field-count-cross.rs:55:15
|
LL | E2::M(1, 2, 3, 4) => {}
| ----- ^ ^ ^ ^ expected 3 fields, found 4
| ^ ^ ^ ^ expected 3 fields, found 4
|
::: $DIR/auxiliary/declarations-for-tuple-field-count-errors.rs:15:5
|
Expand Down
Loading