Skip to content

Commit

Permalink
tweak logic of "unknown field" label
Browse files Browse the repository at this point in the history
  • Loading branch information
estebank committed Nov 18, 2023
1 parent 1ee37bf commit 289ce57
Show file tree
Hide file tree
Showing 12 changed files with 101 additions and 49 deletions.
56 changes: 31 additions & 25 deletions compiler/rustc_hir_typeck/src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2191,7 +2191,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
if let Some(field_name) =
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,
"a field with a similar name exists",
field_name,
Expand Down Expand Up @@ -2420,30 +2421,32 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
ty: Ty<'tcx>,
) {
let Some(output_ty) = self.get_impl_future_output_ty(ty) else {
err.span_label(field_ident.span, "unknown field");
return;
};
if let ty::Adt(def, _) = output_ty.kind() {
// no field access on enum type
if !def.is_enum() {
if def
.non_enum_variant()
.fields
.iter()
.any(|field| field.ident(self.tcx) == field_ident)
{
err.span_label(
field_ident.span,
"field not available in `impl Future`, but it is available in its `Output`",
);
err.span_suggestion_verbose(
base.span.shrink_to_hi(),
"consider `await`ing on the `Future` and access the field of its `Output`",
".await",
Applicability::MaybeIncorrect,
);
}
}
let ty::Adt(def, _) = output_ty.kind() else {
err.span_label(field_ident.span, "unknown field");
return;
};
// no field access on enum type
if def.is_enum() {
err.span_label(field_ident.span, "unknown field");
return;
}
if !def.non_enum_variant().fields.iter().any(|field| field.ident(self.tcx) == field_ident) {
err.span_label(field_ident.span, "unknown field");
return;
}
err.span_label(
field_ident.span,
"field not available in `impl Future`, but it is available in its `Output`",
);
err.span_suggestion_verbose(
base.span.shrink_to_hi(),
"consider `await`ing on the `Future` and access the field of its `Output`",
".await",
Applicability::MaybeIncorrect,
);
}

fn ban_nonexisting_field(
Expand All @@ -2467,16 +2470,17 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
self.suggest_first_deref_field(&mut err, expr, base, ident);
}
ty::Param(param_ty) => {
err.span_label(ident.span, "unknown field");
self.point_at_param_definition(&mut err, param_ty);
}
ty::Alias(ty::Opaque, _) => {
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| {
if let ty::Adt(def, _) = output_ty.kind()
&& !def.is_enum()
Expand Down Expand Up @@ -2635,6 +2639,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
field: Ident,
len: ty::Const<'tcx>,
) {
err.span_label(field.span, "unknown field");
if let (Some(len), Ok(user_index)) =
(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)
Expand All @@ -2657,6 +2662,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
base: &hir::Expr<'_>,
field: Ident,
) {
err.span_label(field.span, "unknown field");
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 suggestion = format!("(*{base}).{field}");
Expand Down
2 changes: 0 additions & 2 deletions tests/ui/async-await/issue-61076.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,10 @@ async fn baz() -> Result<(), ()> {
let _: i32 = tuple().0; //~ ERROR no field `0`
//~^ HELP consider `await`ing on the `Future`
//~| NOTE field not available in `impl Future`
//~| NOTE unknown field

let _: i32 = struct_().a; //~ ERROR no field `a`
//~^ HELP consider `await`ing on the `Future`
//~| NOTE field not available in `impl Future`
//~| NOTE unknown field

struct_().method(); //~ ERROR no method named
//~^ NOTE method not found in `impl Future<Output = Struct>`
Expand Down
16 changes: 5 additions & 11 deletions tests/ui/async-await/issue-61076.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -26,32 +26,26 @@ error[E0609]: no field `0` on type `impl Future<Output = Tuple>`
--> $DIR/issue-61076.rs:71:26
|
LL | let _: i32 = tuple().0;
| ^
| |
| field not available in `impl Future`, but it is available in its `Output`
| unknown field
| ^ field not available in `impl Future`, but it is available in its `Output`
|
help: consider `await`ing on the `Future` and access the field of its `Output`
|
LL | let _: i32 = tuple().await.0;
| ++++++

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;
| ^
| |
| field not available in `impl Future`, but it is available in its `Output`
| unknown field
| ^ field not available in `impl Future`, but it is available in its `Output`
|
help: consider `await`ing on the `Future` and access the field of its `Output`
|
LL | let _: i32 = struct_().await.a;
| ++++++

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();
| ^^^^^^ method not found in `impl Future<Output = Struct>`
Expand All @@ -62,7 +56,7 @@ LL | struct_().await.method();
| ++++++

error[E0308]: mismatched types
--> $DIR/issue-61076.rs:90:9
--> $DIR/issue-61076.rs:88:9
|
LL | match tuple() {
| ------- this expression has type `impl Future<Output = Tuple>`
Expand Down
6 changes: 5 additions & 1 deletion tests/ui/did_you_mean/dont-suggest-hygienic-fields.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,13 @@ LL | environment!();
| -------------- in this macro invocation
...
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)
help: a field with a similar name exists
|
LL | const CRATE: Crate = Crate { field: () };
| ~~~~~

error[E0609]: no field `field` on type `Compound`
--> $DIR/dont-suggest-hygienic-fields.rs:24:16
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@ error[E0560]: struct `Demo` has no field named `inocently_mispellable`
--> $DIR/issue-42599_available_fields_note.rs:16:39
|
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`
--> $DIR/issue-42599_available_fields_note.rs:21:39
Expand Down
7 changes: 6 additions & 1 deletion tests/ui/rmeta/rmeta_meta_main.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@ error[E0560]: struct `Foo` has no field named `field2`
--> $DIR/rmeta_meta_main.rs:13:19
|
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

Expand Down
7 changes: 6 additions & 1 deletion tests/ui/structs/struct-fields-hints-no-dupe.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@ error[E0560]: struct `A` has no field named `bar`
--> $DIR/struct-fields-hints-no-dupe.rs:10:9
|
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

Expand Down
7 changes: 6 additions & 1 deletion tests/ui/structs/struct-fields-hints.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@ error[E0560]: struct `A` has no field named `bar`
--> $DIR/struct-fields-hints.rs:10:9
|
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

Expand Down
21 changes: 18 additions & 3 deletions tests/ui/structs/suggest-private-fields.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@ error[E0560]: struct `B` has no field named `aa`
--> $DIR/suggest-private-fields.rs:15:9
|
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`
--> $DIR/suggest-private-fields.rs:17:9
Expand All @@ -16,13 +21,23 @@ error[E0560]: struct `A` has no field named `aa`
--> $DIR/suggest-private-fields.rs:22:9
|
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`
--> $DIR/suggest-private-fields.rs:24:9
|
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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,12 @@ error[E0560]: struct `RGB` has no field named `c`
--> $DIR/type-mismatch-struct-field-shorthand-2.rs:5:25
|
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

Expand Down
7 changes: 6 additions & 1 deletion tests/ui/union/union-suggest-field.mirunsafeck.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@ error[E0560]: union `U` has no field named `principle`
--> $DIR/union-suggest-field.rs:13:17
|
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`
--> $DIR/union-suggest-field.rs:17:15
Expand Down
7 changes: 6 additions & 1 deletion tests/ui/union/union-suggest-field.thirunsafeck.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@ error[E0560]: union `U` has no field named `principle`
--> $DIR/union-suggest-field.rs:13:17
|
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`
--> $DIR/union-suggest-field.rs:17:15
Expand Down

0 comments on commit 289ce57

Please sign in to comment.