Skip to content

Commit 9eac386

Browse files
committed
Auto merge of #57047 - euclio:field-structured-suggestions, r=estebank
use structured suggestions for nonexistent fields r? @estebank
2 parents 6efaef6 + dfc326d commit 9eac386

18 files changed

+50
-35
lines changed

src/librustc_typeck/check/mod.rs

+17-7
Original file line numberDiff line numberDiff line change
@@ -3422,8 +3422,12 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
34223422
if let Some(suggested_field_name) =
34233423
Self::suggest_field_name(def.non_enum_variant(),
34243424
&field.as_str(), vec![]) {
3425-
err.span_label(field.span,
3426-
format!("did you mean `{}`?", suggested_field_name));
3425+
err.span_suggestion_with_applicability(
3426+
field.span,
3427+
"a field with a similar name exists",
3428+
suggested_field_name.to_string(),
3429+
Applicability::MaybeIncorrect,
3430+
);
34273431
} else {
34283432
err.span_label(field.span, "unknown field");
34293433
let struct_variant_def = def.non_enum_variant();
@@ -3550,8 +3554,12 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
35503554
if let Some(field_name) = Self::suggest_field_name(variant,
35513555
&field.ident.as_str(),
35523556
skip_fields.collect()) {
3553-
err.span_label(field.ident.span,
3554-
format!("field does not exist - did you mean `{}`?", field_name));
3557+
err.span_suggestion_with_applicability(
3558+
field.ident.span,
3559+
"a field with a similar name exists",
3560+
field_name.to_string(),
3561+
Applicability::MaybeIncorrect,
3562+
);
35553563
} else {
35563564
match ty.sty {
35573565
ty::Adt(adt, ..) => {
@@ -5195,13 +5203,15 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
51955203
if let Some(adt_def) = adt_def {
51965204
match adt_def.adt_kind() {
51975205
AdtKind::Enum => {
5198-
err.note("did you mean to use one of the enum's variants?");
5206+
err.help("did you mean to use one of the enum's variants?");
51995207
},
52005208
AdtKind::Struct |
52015209
AdtKind::Union => {
5202-
err.span_label(
5210+
err.span_suggestion_with_applicability(
52035211
span,
5204-
format!("did you mean `Self {{ /* fields */ }}`?"),
5212+
"use curly brackets",
5213+
String::from("Self { /* fields */ }"),
5214+
Applicability::HasPlaceholders,
52055215
);
52065216
}
52075217
}

src/test/ui/did_you_mean/issue-36798.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error[E0609]: no field `baz` on type `Foo`
22
--> $DIR/issue-36798.rs:7:7
33
|
44
LL | f.baz; //~ ERROR no field
5-
| ^^^ did you mean `bar`?
5+
| ^^^ help: a field with a similar name exists: `bar`
66

77
error: aborting due to previous error
88

src/test/ui/did_you_mean/issue-42599_available_fields_note.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error[E0560]: struct `submodule::Demo` has no field named `inocently_mispellable
22
--> $DIR/issue-42599_available_fields_note.rs:16:39
33
|
44
LL | Self { secret_integer: 2, inocently_mispellable: () }
5-
| ^^^^^^^^^^^^^^^^^^^^^ field does not exist - did you mean `innocently_misspellable`?
5+
| ^^^^^^^^^^^^^^^^^^^^^ help: a field with a similar name exists: `innocently_misspellable`
66

77
error[E0560]: struct `submodule::Demo` has no field named `egregiously_nonexistent_field`
88
--> $DIR/issue-42599_available_fields_note.rs:21:39
@@ -16,7 +16,7 @@ error[E0609]: no field `inocently_mispellable` on type `submodule::Demo`
1616
--> $DIR/issue-42599_available_fields_note.rs:32:41
1717
|
1818
LL | let innocent_field_misaccess = demo.inocently_mispellable;
19-
| ^^^^^^^^^^^^^^^^^^^^^ did you mean `innocently_misspellable`?
19+
| ^^^^^^^^^^^^^^^^^^^^^ help: a field with a similar name exists: `innocently_misspellable`
2020

2121
error[E0609]: no field `egregiously_nonexistent_field` on type `submodule::Demo`
2222
--> $DIR/issue-42599_available_fields_note.rs:35:42

src/test/ui/error-codes/ex-E0612.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error[E0609]: no field `1` on type `Foo`
22
--> $DIR/ex-E0612.rs:5:6
33
|
44
LL | y.1; //~ ERROR no field `1` on type `Foo`
5-
| ^ did you mean `0`?
5+
| ^ help: a field with a similar name exists: `0`
66

77
error: aborting due to previous error
88

src/test/ui/issues/issue-47073-zero-padded-tuple-struct-indices.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error[E0609]: no field `00` on type `Verdict`
22
--> $DIR/issue-47073-zero-padded-tuple-struct-indices.rs:8:30
33
|
44
LL | let _condemned = justice.00;
5-
| ^^ did you mean `0`?
5+
| ^^ help: a field with a similar name exists: `0`
66

77
error[E0609]: no field `001` on type `Verdict`
88
--> $DIR/issue-47073-zero-padded-tuple-struct-indices.rs:10:31

src/test/ui/issues/issue-4736.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error[E0560]: struct `NonCopyable` has no field named `p`
22
--> $DIR/issue-4736.rs:4:26
33
|
44
LL | let z = NonCopyable{ p: () }; //~ ERROR struct `NonCopyable` has no field named `p`
5-
| ^ field does not exist - did you mean `0`?
5+
| ^ help: a field with a similar name exists: `0`
66

77
error: aborting due to previous error
88

src/test/ui/issues/issue-56199.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,27 @@ error: the `Self` constructor can only be used with tuple or unit structs
44
LL | let _ = Self;
55
| ^^^^
66
|
7-
= note: did you mean to use one of the enum's variants?
7+
= help: did you mean to use one of the enum's variants?
88

99
error: the `Self` constructor can only be used with tuple or unit structs
1010
--> $DIR/issue-56199.rs:8:17
1111
|
1212
LL | let _ = Self();
1313
| ^^^^
1414
|
15-
= note: did you mean to use one of the enum's variants?
15+
= help: did you mean to use one of the enum's variants?
1616

1717
error: the `Self` constructor can only be used with tuple or unit structs
1818
--> $DIR/issue-56199.rs:15:17
1919
|
2020
LL | let _ = Self;
21-
| ^^^^ did you mean `Self { /* fields */ }`?
21+
| ^^^^ help: use curly brackets: `Self { /* fields */ }`
2222

2323
error: the `Self` constructor can only be used with tuple or unit structs
2424
--> $DIR/issue-56199.rs:17:17
2525
|
2626
LL | let _ = Self();
27-
| ^^^^ did you mean `Self { /* fields */ }`?
27+
| ^^^^ help: use curly brackets: `Self { /* fields */ }`
2828

2929
error: aborting due to 4 previous errors
3030

src/test/ui/issues/issue-56835.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error: the `Self` constructor can only be used with tuple or unit structs
22
--> $DIR/issue-56835.rs:4:12
33
|
44
LL | fn bar(Self(foo): Self) {}
5-
| ^^^^^^^^^ did you mean `Self { /* fields */ }`?
5+
| ^^^^^^^^^ help: use curly brackets: `Self { /* fields */ }`
66

77
error[E0164]: expected tuple struct/variant, found self constructor `Self`
88
--> $DIR/issue-56835.rs:4:12

src/test/ui/rmeta_meta_main.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error[E0560]: struct `rmeta_meta::Foo` has no field named `field2`
22
--> $DIR/rmeta_meta_main.rs:13:19
33
|
44
LL | let _ = Foo { field2: 42 }; //~ ERROR struct `rmeta_meta::Foo` has no field named `field2`
5-
| ^^^^^^ field does not exist - did you mean `field`?
5+
| ^^^^^^ help: a field with a similar name exists: `field`
66

77
error: aborting due to previous error
88

src/test/ui/structs/struct-fields-hints-no-dupe.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error[E0560]: struct `A` has no field named `bar`
22
--> $DIR/struct-fields-hints-no-dupe.rs:10:9
33
|
44
LL | bar : 42,
5-
| ^^^ field does not exist - did you mean `barr`?
5+
| ^^^ help: a field with a similar name exists: `barr`
66

77
error: aborting due to previous error
88

src/test/ui/structs/struct-fields-hints.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error[E0560]: struct `A` has no field named `bar`
22
--> $DIR/struct-fields-hints.rs:10:9
33
|
44
LL | bar : 42,
5-
| ^^^ field does not exist - did you mean `car`?
5+
| ^^^ help: a field with a similar name exists: `car`
66

77
error: aborting due to previous error
88

src/test/ui/structs/struct-fields-typo.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ fn main() {
88
foo: 0,
99
bar: 0.5,
1010
};
11-
let x = foo.baa;//~ no field `baa` on type `BuildData`
12-
//~^ did you mean `bar`?
11+
let x = foo.baa; //~ ERROR no field `baa` on type `BuildData`
12+
//~| HELP a field with a similar name exists
13+
//~| SUGGESTION bar
1314
println!("{}", x);
1415
}

src/test/ui/structs/struct-fields-typo.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error[E0609]: no field `baa` on type `BuildData`
22
--> $DIR/struct-fields-typo.rs:11:17
33
|
4-
LL | let x = foo.baa;//~ no field `baa` on type `BuildData`
5-
| ^^^ did you mean `bar`?
4+
LL | let x = foo.baa; //~ ERROR no field `baa` on type `BuildData`
5+
| ^^^ help: a field with a similar name exists: `bar`
66

77
error: aborting due to previous error
88

src/test/ui/suggestions/suggest-private-fields.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error[E0560]: struct `xc::B` has no field named `aa`
22
--> $DIR/suggest-private-fields.rs:15:9
33
|
44
LL | aa: 20,
5-
| ^^ field does not exist - did you mean `a`?
5+
| ^^ help: a field with a similar name exists: `a`
66

77
error[E0560]: struct `xc::B` has no field named `bb`
88
--> $DIR/suggest-private-fields.rs:17:9
@@ -16,13 +16,13 @@ error[E0560]: struct `A` has no field named `aa`
1616
--> $DIR/suggest-private-fields.rs:22:9
1717
|
1818
LL | aa: 20,
19-
| ^^ field does not exist - did you mean `a`?
19+
| ^^ help: a field with a similar name exists: `a`
2020

2121
error[E0560]: struct `A` has no field named `bb`
2222
--> $DIR/suggest-private-fields.rs:24:9
2323
|
2424
LL | bb: 20,
25-
| ^^ field does not exist - did you mean `b`?
25+
| ^^ help: a field with a similar name exists: `b`
2626

2727
error: aborting due to 4 previous errors
2828

src/test/ui/tuple/tuple-index-not-tuple.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error[E0609]: no field `0` on type `Point`
22
--> $DIR/tuple-index-not-tuple.rs:6:12
33
|
44
LL | origin.0;
5-
| ^ did you mean `x`?
5+
| ^ help: a field with a similar name exists: `x`
66

77
error[E0609]: no field `0` on type `Empty`
88
--> $DIR/tuple-index-not-tuple.rs:8:11

src/test/ui/tuple/tuple-index-out-of-bounds.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error[E0609]: no field `2` on type `Point`
22
--> $DIR/tuple-index-out-of-bounds.rs:7:12
33
|
44
LL | origin.2;
5-
| ^ did you mean `0`?
5+
| ^ help: a field with a similar name exists: `0`
66

77
error[E0609]: no field `2` on type `({integer}, {integer})`
88
--> $DIR/tuple-index-out-of-bounds.rs:12:11

src/test/ui/union/union-suggest-field.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,12 @@ impl U {
99
fn main() {
1010
let u = U { principle: 0 };
1111
//~^ ERROR union `U` has no field named `principle`
12+
//~| HELP a field with a similar name exists
13+
//~| SUGGESTION principal
1214
let w = u.principial; //~ ERROR no field `principial` on type `U`
13-
//~^ did you mean `principal`?
15+
//~| HELP a field with a similar name exists
16+
//~| SUGGESTION principal
1417

1518
let y = u.calculate; //~ ERROR attempted to take value of method `calculate` on type `U`
19+
//~| HELP maybe a `()` to call it is missing
1620
}

src/test/ui/union/union-suggest-field.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@ error[E0560]: union `U` has no field named `principle`
22
--> $DIR/union-suggest-field.rs:10:17
33
|
44
LL | let u = U { principle: 0 };
5-
| ^^^^^^^^^ field does not exist - did you mean `principal`?
5+
| ^^^^^^^^^ help: a field with a similar name exists: `principal`
66

77
error[E0609]: no field `principial` on type `U`
8-
--> $DIR/union-suggest-field.rs:12:15
8+
--> $DIR/union-suggest-field.rs:14:15
99
|
1010
LL | let w = u.principial; //~ ERROR no field `principial` on type `U`
11-
| ^^^^^^^^^^ did you mean `principal`?
11+
| ^^^^^^^^^^ help: a field with a similar name exists: `principal`
1212

1313
error[E0615]: attempted to take value of method `calculate` on type `U`
14-
--> $DIR/union-suggest-field.rs:15:15
14+
--> $DIR/union-suggest-field.rs:18:15
1515
|
1616
LL | let y = u.calculate; //~ ERROR attempted to take value of method `calculate` on type `U`
1717
| ^^^^^^^^^

0 commit comments

Comments
 (0)