tweak logic of "unknown field" label

This commit is contained in:
Esteban Küber 2023-11-18 00:40:11 +00:00
parent 1ee37bf03f
commit 289ce572b3
12 changed files with 101 additions and 49 deletions

View File

@ -2191,7 +2191,8 @@ fn report_unknown_field(
if let Some(field_name) = if let Some(field_name) =
find_best_match_for_name(&available_field_names, field.ident.name, None) find_best_match_for_name(&available_field_names, field.ident.name, None)
{ {
err.span_suggestion( err.span_label(field.ident.span, "unknown field");
err.span_suggestion_verbose(
field.ident.span, field.ident.span,
"a field with a similar name exists", "a field with a similar name exists",
field_name, field_name,
@ -2420,17 +2421,22 @@ fn suggest_await_on_field_access(
ty: Ty<'tcx>, ty: Ty<'tcx>,
) { ) {
let Some(output_ty) = self.get_impl_future_output_ty(ty) else { let Some(output_ty) = self.get_impl_future_output_ty(ty) else {
err.span_label(field_ident.span, "unknown field");
return;
};
let ty::Adt(def, _) = output_ty.kind() else {
err.span_label(field_ident.span, "unknown field");
return; return;
}; };
if let ty::Adt(def, _) = output_ty.kind() {
// no field access on enum type // no field access on enum type
if !def.is_enum() { if def.is_enum() {
if def err.span_label(field_ident.span, "unknown field");
.non_enum_variant() return;
.fields }
.iter() if !def.non_enum_variant().fields.iter().any(|field| field.ident(self.tcx) == field_ident) {
.any(|field| field.ident(self.tcx) == field_ident) err.span_label(field_ident.span, "unknown field");
{ return;
}
err.span_label( err.span_label(
field_ident.span, field_ident.span,
"field not available in `impl Future`, but it is available in its `Output`", "field not available in `impl Future`, but it is available in its `Output`",
@ -2442,9 +2448,6 @@ fn suggest_await_on_field_access(
Applicability::MaybeIncorrect, Applicability::MaybeIncorrect,
); );
} }
}
}
}
fn ban_nonexisting_field( fn ban_nonexisting_field(
&self, &self,
@ -2467,15 +2470,16 @@ fn ban_nonexisting_field(
self.suggest_first_deref_field(&mut err, expr, base, ident); self.suggest_first_deref_field(&mut err, expr, base, ident);
} }
ty::Param(param_ty) => { ty::Param(param_ty) => {
err.span_label(ident.span, "unknown field");
self.point_at_param_definition(&mut err, param_ty); self.point_at_param_definition(&mut err, param_ty);
} }
ty::Alias(ty::Opaque, _) => { ty::Alias(ty::Opaque, _) => {
self.suggest_await_on_field_access(&mut err, ident, base, base_ty.peel_refs()); self.suggest_await_on_field_access(&mut err, ident, base, base_ty.peel_refs());
} }
_ => {} _ => {
}
err.span_label(ident.span, "unknown field"); err.span_label(ident.span, "unknown field");
}
}
self.suggest_fn_call(&mut err, base, base_ty, |output_ty| { self.suggest_fn_call(&mut err, base, base_ty, |output_ty| {
if let ty::Adt(def, _) = output_ty.kind() if let ty::Adt(def, _) = output_ty.kind()
@ -2635,6 +2639,7 @@ fn maybe_suggest_array_indexing(
field: Ident, field: Ident,
len: ty::Const<'tcx>, len: ty::Const<'tcx>,
) { ) {
err.span_label(field.span, "unknown field");
if let (Some(len), Ok(user_index)) = if let (Some(len), Ok(user_index)) =
(len.try_eval_target_usize(self.tcx, self.param_env), field.as_str().parse::<u64>()) (len.try_eval_target_usize(self.tcx, self.param_env), field.as_str().parse::<u64>())
&& let Ok(base) = self.tcx.sess.source_map().span_to_snippet(base.span) && let Ok(base) = self.tcx.sess.source_map().span_to_snippet(base.span)
@ -2657,6 +2662,7 @@ fn suggest_first_deref_field(
base: &hir::Expr<'_>, base: &hir::Expr<'_>,
field: Ident, field: Ident,
) { ) {
err.span_label(field.span, "unknown field");
if let Ok(base) = self.tcx.sess.source_map().span_to_snippet(base.span) { if let Ok(base) = self.tcx.sess.source_map().span_to_snippet(base.span) {
let msg = format!("`{base}` is a raw pointer; try dereferencing it"); let msg = format!("`{base}` is a raw pointer; try dereferencing it");
let suggestion = format!("(*{base}).{field}"); let suggestion = format!("(*{base}).{field}");

View File

@ -71,12 +71,10 @@ async fn baz() -> Result<(), ()> {
let _: i32 = tuple().0; //~ ERROR no field `0` let _: i32 = tuple().0; //~ ERROR no field `0`
//~^ HELP consider `await`ing on the `Future` //~^ HELP consider `await`ing on the `Future`
//~| NOTE field not available in `impl Future` //~| NOTE field not available in `impl Future`
//~| NOTE unknown field
let _: i32 = struct_().a; //~ ERROR no field `a` let _: i32 = struct_().a; //~ ERROR no field `a`
//~^ HELP consider `await`ing on the `Future` //~^ HELP consider `await`ing on the `Future`
//~| NOTE field not available in `impl Future` //~| NOTE field not available in `impl Future`
//~| NOTE unknown field
struct_().method(); //~ ERROR no method named struct_().method(); //~ ERROR no method named
//~^ NOTE method not found in `impl Future<Output = Struct>` //~^ NOTE method not found in `impl Future<Output = Struct>`

View File

@ -26,10 +26,7 @@ error[E0609]: no field `0` on type `impl Future<Output = Tuple>`
--> $DIR/issue-61076.rs:71:26 --> $DIR/issue-61076.rs:71:26
| |
LL | let _: i32 = tuple().0; LL | let _: i32 = tuple().0;
| ^ | ^ field not available in `impl Future`, but it is available in its `Output`
| |
| field not available in `impl Future`, but it is available in its `Output`
| unknown field
| |
help: consider `await`ing on the `Future` and access the field of its `Output` help: consider `await`ing on the `Future` and access the field of its `Output`
| |
@ -37,13 +34,10 @@ LL | let _: i32 = tuple().await.0;
| ++++++ | ++++++
error[E0609]: no field `a` on type `impl Future<Output = Struct>` error[E0609]: no field `a` on type `impl Future<Output = Struct>`
--> $DIR/issue-61076.rs:76:28 --> $DIR/issue-61076.rs:75:28
| |
LL | let _: i32 = struct_().a; LL | let _: i32 = struct_().a;
| ^ | ^ field not available in `impl Future`, but it is available in its `Output`
| |
| field not available in `impl Future`, but it is available in its `Output`
| unknown field
| |
help: consider `await`ing on the `Future` and access the field of its `Output` help: consider `await`ing on the `Future` and access the field of its `Output`
| |
@ -51,7 +45,7 @@ LL | let _: i32 = struct_().await.a;
| ++++++ | ++++++
error[E0599]: no method named `method` found for opaque type `impl Future<Output = Struct>` in the current scope error[E0599]: no method named `method` found for opaque type `impl Future<Output = Struct>` in the current scope
--> $DIR/issue-61076.rs:81:15 --> $DIR/issue-61076.rs:79:15
| |
LL | struct_().method(); LL | struct_().method();
| ^^^^^^ method not found in `impl Future<Output = Struct>` | ^^^^^^ method not found in `impl Future<Output = Struct>`
@ -62,7 +56,7 @@ LL | struct_().await.method();
| ++++++ | ++++++
error[E0308]: mismatched types error[E0308]: mismatched types
--> $DIR/issue-61076.rs:90:9 --> $DIR/issue-61076.rs:88:9
| |
LL | match tuple() { LL | match tuple() {
| ------- this expression has type `impl Future<Output = Tuple>` | ------- this expression has type `impl Future<Output = Tuple>`

View File

@ -5,9 +5,13 @@ LL | environment!();
| -------------- in this macro invocation | -------------- in this macro invocation
... ...
LL | const CRATE: Crate = Crate { fiel: () }; LL | const CRATE: Crate = Crate { fiel: () };
| ^^^^ help: a field with a similar name exists: `field` | ^^^^ unknown field
| |
= note: this error originates in the macro `environment` (in Nightly builds, run with -Z macro-backtrace for more info) = note: this error originates in the macro `environment` (in Nightly builds, run with -Z macro-backtrace for more info)
help: a field with a similar name exists
|
LL | const CRATE: Crate = Crate { field: () };
| ~~~~~
error[E0609]: no field `field` on type `Compound` error[E0609]: no field `field` on type `Compound`
--> $DIR/dont-suggest-hygienic-fields.rs:24:16 --> $DIR/dont-suggest-hygienic-fields.rs:24:16

View File

@ -2,7 +2,12 @@ error[E0560]: struct `Demo` has no field named `inocently_mispellable`
--> $DIR/issue-42599_available_fields_note.rs:16:39 --> $DIR/issue-42599_available_fields_note.rs:16:39
| |
LL | Self { secret_integer: 2, inocently_mispellable: () } LL | Self { secret_integer: 2, inocently_mispellable: () }
| ^^^^^^^^^^^^^^^^^^^^^ help: a field with a similar name exists: `innocently_misspellable` | ^^^^^^^^^^^^^^^^^^^^^ unknown field
|
help: a field with a similar name exists
|
LL | Self { secret_integer: 2, innocently_misspellable: () }
| ~~~~~~~~~~~~~~~~~~~~~~~
error[E0560]: struct `Demo` has no field named `egregiously_nonexistent_field` error[E0560]: struct `Demo` has no field named `egregiously_nonexistent_field`
--> $DIR/issue-42599_available_fields_note.rs:21:39 --> $DIR/issue-42599_available_fields_note.rs:21:39

View File

@ -2,7 +2,12 @@ error[E0560]: struct `Foo` has no field named `field2`
--> $DIR/rmeta_meta_main.rs:13:19 --> $DIR/rmeta_meta_main.rs:13:19
| |
LL | let _ = Foo { field2: 42 }; LL | let _ = Foo { field2: 42 };
| ^^^^^^ help: a field with a similar name exists: `field` | ^^^^^^ unknown field
|
help: a field with a similar name exists
|
LL | let _ = Foo { field: 42 };
| ~~~~~
error: aborting due to previous error error: aborting due to previous error

View File

@ -2,7 +2,12 @@ error[E0560]: struct `A` has no field named `bar`
--> $DIR/struct-fields-hints-no-dupe.rs:10:9 --> $DIR/struct-fields-hints-no-dupe.rs:10:9
| |
LL | bar : 42, LL | bar : 42,
| ^^^ help: a field with a similar name exists: `barr` | ^^^ unknown field
|
help: a field with a similar name exists
|
LL | barr : 42,
| ~~~~
error: aborting due to previous error error: aborting due to previous error

View File

@ -2,7 +2,12 @@ error[E0560]: struct `A` has no field named `bar`
--> $DIR/struct-fields-hints.rs:10:9 --> $DIR/struct-fields-hints.rs:10:9
| |
LL | bar : 42, LL | bar : 42,
| ^^^ help: a field with a similar name exists: `car` | ^^^ unknown field
|
help: a field with a similar name exists
|
LL | car : 42,
| ~~~
error: aborting due to previous error error: aborting due to previous error

View File

@ -2,7 +2,12 @@ error[E0560]: struct `B` has no field named `aa`
--> $DIR/suggest-private-fields.rs:15:9 --> $DIR/suggest-private-fields.rs:15:9
| |
LL | aa: 20, LL | aa: 20,
| ^^ help: a field with a similar name exists: `a` | ^^ unknown field
|
help: a field with a similar name exists
|
LL | a: 20,
| ~
error[E0560]: struct `B` has no field named `bb` error[E0560]: struct `B` has no field named `bb`
--> $DIR/suggest-private-fields.rs:17:9 --> $DIR/suggest-private-fields.rs:17:9
@ -16,13 +21,23 @@ error[E0560]: struct `A` has no field named `aa`
--> $DIR/suggest-private-fields.rs:22:9 --> $DIR/suggest-private-fields.rs:22:9
| |
LL | aa: 20, LL | aa: 20,
| ^^ help: a field with a similar name exists: `a` | ^^ unknown field
|
help: a field with a similar name exists
|
LL | a: 20,
| ~
error[E0560]: struct `A` has no field named `bb` error[E0560]: struct `A` has no field named `bb`
--> $DIR/suggest-private-fields.rs:24:9 --> $DIR/suggest-private-fields.rs:24:9
| |
LL | bb: 20, LL | bb: 20,
| ^^ help: a field with a similar name exists: `b` | ^^ unknown field
|
help: a field with a similar name exists
|
LL | b: 20,
| ~
error: aborting due to 4 previous errors error: aborting due to 4 previous errors

View File

@ -24,7 +24,12 @@ error[E0560]: struct `RGB` has no field named `c`
--> $DIR/type-mismatch-struct-field-shorthand-2.rs:5:25 --> $DIR/type-mismatch-struct-field-shorthand-2.rs:5:25
| |
LL | let _ = RGB { r, g, c }; LL | let _ = RGB { r, g, c };
| ^ help: a field with a similar name exists: `b` | ^ unknown field
|
help: a field with a similar name exists
|
LL | let _ = RGB { r, g, b };
| ~
error: aborting due to 3 previous errors error: aborting due to 3 previous errors

View File

@ -2,7 +2,12 @@ error[E0560]: union `U` has no field named `principle`
--> $DIR/union-suggest-field.rs:13:17 --> $DIR/union-suggest-field.rs:13:17
| |
LL | let u = U { principle: 0 }; LL | let u = U { principle: 0 };
| ^^^^^^^^^ help: a field with a similar name exists: `principal` | ^^^^^^^^^ unknown field
|
help: a field with a similar name exists
|
LL | let u = U { principal: 0 };
| ~~~~~~~~~
error[E0609]: no field `principial` on type `U` error[E0609]: no field `principial` on type `U`
--> $DIR/union-suggest-field.rs:17:15 --> $DIR/union-suggest-field.rs:17:15

View File

@ -2,7 +2,12 @@ error[E0560]: union `U` has no field named `principle`
--> $DIR/union-suggest-field.rs:13:17 --> $DIR/union-suggest-field.rs:13:17
| |
LL | let u = U { principle: 0 }; LL | let u = U { principle: 0 };
| ^^^^^^^^^ help: a field with a similar name exists: `principal` | ^^^^^^^^^ unknown field
|
help: a field with a similar name exists
|
LL | let u = U { principal: 0 };
| ~~~~~~~~~
error[E0609]: no field `principial` on type `U` error[E0609]: no field `principial` on type `U`
--> $DIR/union-suggest-field.rs:17:15 --> $DIR/union-suggest-field.rs:17:15