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

Lines changed: 17 additions & 7 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 4 additions & 4 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

0 commit comments

Comments
 (0)