From 2ce2d145c2cbcb93389dbea429d7e034ff367f48 Mon Sep 17 00:00:00 2001 From: Hirochika Matsumoto Date: Sat, 30 Jan 2021 14:18:50 +0900 Subject: [PATCH] Account for union --- .../src/infer/error_reporting/mod.rs | 9 +++++++- src/test/ui/suggestions/field-access.fixed | 12 ++++++++++ src/test/ui/suggestions/field-access.rs | 12 ++++++++++ src/test/ui/suggestions/field-access.stderr | 22 +++++++++++++++---- 4 files changed, 50 insertions(+), 5 deletions(-) diff --git a/compiler/rustc_infer/src/infer/error_reporting/mod.rs b/compiler/rustc_infer/src/infer/error_reporting/mod.rs index 268819d3094d..447b4f6d1e5f 100644 --- a/compiler/rustc_infer/src/infer/error_reporting/mod.rs +++ b/compiler/rustc_infer/src/infer/error_reporting/mod.rs @@ -1842,13 +1842,20 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> { { 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, ); } diff --git a/src/test/ui/suggestions/field-access.fixed b/src/test/ui/suggestions/field-access.fixed index 72b29d7656ae..05a4a0eb1266 100644 --- a/src/test/ui/suggestions/field-access.fixed +++ b/src/test/ui/suggestions/field-access.fixed @@ -10,6 +10,11 @@ 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] @@ -20,4 +25,11 @@ fn main() { 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] + _ => (), + } } diff --git a/src/test/ui/suggestions/field-access.rs b/src/test/ui/suggestions/field-access.rs index 2710ead4dfbb..ad23c0ffa2e7 100644 --- a/src/test/ui/suggestions/field-access.rs +++ b/src/test/ui/suggestions/field-access.rs @@ -10,6 +10,11 @@ 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] @@ -20,4 +25,11 @@ fn main() { 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] + _ => (), + } } diff --git a/src/test/ui/suggestions/field-access.stderr b/src/test/ui/suggestions/field-access.stderr index 228510902f54..aad9872032a2 100644 --- a/src/test/ui/suggestions/field-access.stderr +++ b/src/test/ui/suggestions/field-access.stderr @@ -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 @@ -15,7 +15,7 @@ LL | if let B::Fst = a.b {}; | ^^^ error[E0308]: mismatched types - --> $DIR/field-access.rs:20:9 + --> $DIR/field-access.rs:25:9 | LL | Fst, | --- unit variant defined here @@ -32,7 +32,7 @@ LL | match a.b { | ^^^ error[E0308]: mismatched types - --> $DIR/field-access.rs:21:9 + --> $DIR/field-access.rs:26:9 | LL | Snd, | --- unit variant defined here @@ -48,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` + | +LL | match unsafe { foo.bar } { + | ^^^^^^^^^^^^^^^^^^ + +error: aborting due to 4 previous errors For more information about this error, try `rustc --explain E0308`.