Skip to content
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

Suggest accessing field when appropriate #81504

Merged
merged 9 commits into from
Feb 1, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
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
12 changes: 8 additions & 4 deletions compiler/rustc_infer/src/infer/error_reporting/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1838,20 +1838,24 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
.iter()
.filter(|field| field.vis.is_accessible_from(field.did, self.tcx))
.map(|field| (field.ident.name, field.ty(self.tcx, expected_substs)))
.inspect(|(name, ty)| {
debug!("suggest_field_where_appropriate: name={:?}, ty={:?}", name, ty)
})
.find(|(_, ty)| ty::TyS::same_type(ty, exp_found.found))
{
if let ObligationCauseCode::Pattern { span: Some(span), .. } = cause.code {
if let Ok(snippet) = self.tcx.sess.source_map().span_to_snippet(span) {
let suggestion = if expected_def.is_struct() {
format!("{}.{}", snippet, name)
} else if expected_def.is_union() {
format!("unsafe {{ {}.{} }}", snippet, name)
} else {
return;
};
diag.span_suggestion(
span,
&format!(
"you might have meant to use field `{}` of type `{}`",
name, ty
),
format!("{}.{}", snippet, name),
suggestion,
Applicability::MaybeIncorrect,
);
}
Expand Down
27 changes: 18 additions & 9 deletions src/test/ui/suggestions/field-access.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,26 @@ enum B {
Snd,
}

union Foo {
bar: u32,
qux: f32,
}

fn main() {
let a = A { b: B::Fst };
if let B::Fst = a.b {};
//~^ ERROR mismatched types [E0308]
// note: you might have meant to use field `b` of type `B`
if let B::Fst = a.b {}; //~ ERROR mismatched types [E0308]
//~^ HELP you might have meant to use field `b` of type `B`
match a.b {
B::Fst => (),
B::Snd => (),
//~^ HELP you might have meant to use field `b` of type `B`
//~| HELP you might have meant to use field `b` of type `B`
B::Fst => (), //~ ERROR mismatched types [E0308]
B::Snd => (), //~ ERROR mismatched types [E0308]
}

let foo = Foo { bar: 42 };
match unsafe { foo.bar } {
//~^ HELP you might have meant to use field `bar` of type `u32`
1u32 => (), //~ ERROR mismatched types [E0308]
_ => (),
}
//~^^^ ERROR mismatched types [E0308]
// note: you might have meant to use field `b` of type `B`
//~^^^^ ERROR mismatched types [E0308]
// note: you might have meant to use field `b` of type `B`
}
27 changes: 18 additions & 9 deletions src/test/ui/suggestions/field-access.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,26 @@ enum B {
Snd,
}

union Foo {
bar: u32,
qux: f32,
}

fn main() {
let a = A { b: B::Fst };
if let B::Fst = a {};
//~^ ERROR mismatched types [E0308]
// note: you might have meant to use field `b` of type `B`
if let B::Fst = a {}; //~ ERROR mismatched types [E0308]
//~^ HELP you might have meant to use field `b` of type `B`
match a {
B::Fst => (),
B::Snd => (),
//~^ HELP you might have meant to use field `b` of type `B`
//~| HELP you might have meant to use field `b` of type `B`
B::Fst => (), //~ ERROR mismatched types [E0308]
B::Snd => (), //~ ERROR mismatched types [E0308]
}

let foo = Foo { bar: 42 };
match foo {
//~^ HELP you might have meant to use field `bar` of type `u32`
1u32 => (), //~ ERROR mismatched types [E0308]
_ => (),
}
//~^^^ ERROR mismatched types [E0308]
// note: you might have meant to use field `b` of type `B`
//~^^^^ ERROR mismatched types [E0308]
// note: you might have meant to use field `b` of type `B`
}
25 changes: 20 additions & 5 deletions src/test/ui/suggestions/field-access.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error[E0308]: mismatched types
--> $DIR/field-access.rs:15:12
--> $DIR/field-access.rs:20:12
|
LL | Fst,
| --- unit variant defined here
Expand All @@ -15,13 +15,14 @@ LL | if let B::Fst = a.b {};
| ^^^

error[E0308]: mismatched types
--> $DIR/field-access.rs:19:9
--> $DIR/field-access.rs:25:9
|
LL | Fst,
| --- unit variant defined here
...
LL | match a {
| - this expression has type `A`
...
LL | B::Fst => (),
| ^^^^^^ expected struct `A`, found enum `B`
|
Expand All @@ -31,14 +32,14 @@ LL | match a.b {
| ^^^

error[E0308]: mismatched types
--> $DIR/field-access.rs:20:9
--> $DIR/field-access.rs:26:9
|
LL | Snd,
| --- unit variant defined here
...
LL | match a {
| - this expression has type `A`
LL | B::Fst => (),
...
LL | B::Snd => (),
| ^^^^^^ expected struct `A`, found enum `B`
|
Expand All @@ -47,6 +48,20 @@ help: you might have meant to use field `b` of type `B`
LL | match a.b {
| ^^^

error: aborting due to 3 previous errors
error[E0308]: mismatched types
--> $DIR/field-access.rs:32:9
|
LL | match foo {
| --- this expression has type `Foo`
LL |
LL | 1u32 => (),
| ^^^^ expected union `Foo`, found `u32`
|
help: you might have meant to use field `bar` of type `u32`
hkmatsumoto marked this conversation as resolved.
Show resolved Hide resolved
|
LL | match unsafe { foo.bar } {
| ^^^^^^^^^^^^^^^^^^

error: aborting due to 4 previous errors

For more information about this error, try `rustc --explain E0308`.