diff options
| author | Hirochika Matsumoto <matsujika@gmail.com> | 2021-01-30 14:18:50 +0900 |
|---|---|---|
| committer | Hirochika Matsumoto <matsujika@gmail.com> | 2021-01-30 14:18:50 +0900 |
| commit | 2ce2d145c2cbcb93389dbea429d7e034ff367f48 (patch) | |
| tree | 346a1ae112aef60652cfcafa4adcf8ea54367f11 /compiler | |
| parent | 08d31e0f09f79a41e0cfc26247d41fea1489adf9 (diff) | |
| download | rust-2ce2d145c2cbcb93389dbea429d7e034ff367f48.tar.gz rust-2ce2d145c2cbcb93389dbea429d7e034ff367f48.zip | |
Account for union
Diffstat (limited to 'compiler')
| -rw-r--r-- | compiler/rustc_infer/src/infer/error_reporting/mod.rs | 9 |
1 files changed, 8 insertions, 1 deletions
diff --git a/compiler/rustc_infer/src/infer/error_reporting/mod.rs b/compiler/rustc_infer/src/infer/error_reporting/mod.rs index 268819d3094..447b4f6d1e5 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, ); } |
