From 2ce2d145c2cbcb93389dbea429d7e034ff367f48 Mon Sep 17 00:00:00 2001 From: Hirochika Matsumoto Date: Sat, 30 Jan 2021 14:18:50 +0900 Subject: Account for union --- compiler/rustc_infer/src/infer/error_reporting/mod.rs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) (limited to 'compiler') 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, ); } -- cgit 1.4.1-3-g733a5