From d56b304bc8bd603d5f4298793f33f1cbb6c9d0d1 Mon Sep 17 00:00:00 2001 From: IQuant Date: Fri, 3 Mar 2023 16:17:53 +0300 Subject: [PATCH] Migrate SuggestAccessingField --- compiler/rustc_infer/messages.ftl | 2 ++ compiler/rustc_infer/src/errors/mod.rs | 28 +++++++++++++++++++ .../src/infer/error_reporting/suggest.rs | 27 ++++-------------- 3 files changed, 35 insertions(+), 22 deletions(-) diff --git a/compiler/rustc_infer/messages.ftl b/compiler/rustc_infer/messages.ftl index 2cdc513d73c..17de77b74a8 100644 --- a/compiler/rustc_infer/messages.ftl +++ b/compiler/rustc_infer/messages.ftl @@ -360,3 +360,5 @@ infer_fn_consider_casting = consider casting the fn item to a fn pointer: `{$cas infer_sarwa_option = you can convert from `&Option` to `Option<&T>` using `.as_ref()` infer_sarwa_result = you can convert from `&Result` to `Result<&T, &E>` using `.as_ref()` + +infer_suggest_accessing_field = you might have meant to use field `{$name}` whose type is `{$ty}` diff --git a/compiler/rustc_infer/src/errors/mod.rs b/compiler/rustc_infer/src/errors/mod.rs index 481199d99ea..25c4e9a55f8 100644 --- a/compiler/rustc_infer/src/errors/mod.rs +++ b/compiler/rustc_infer/src/errors/mod.rs @@ -1289,3 +1289,31 @@ pub enum SuggestAsRefWhereAppropriate<'a> { snippet: &'a str, }, } + +#[derive(Subdiagnostic)] +pub enum SuggestAccessingField<'a> { + #[suggestion( + infer_suggest_accessing_field, + code = "{snippet}.{name}", + applicability = "maybe-incorrect" + )] + Safe { + #[primary_span] + span: Span, + snippet: String, + name: Symbol, + ty: Ty<'a>, + }, + #[suggestion( + infer_suggest_accessing_field, + code = "unsafe {{ {snippet}.{name} }}", + applicability = "maybe-incorrect" + )] + Unsafe { + #[primary_span] + span: Span, + snippet: String, + name: Symbol, + ty: Ty<'a>, + }, +} diff --git a/compiler/rustc_infer/src/infer/error_reporting/suggest.rs b/compiler/rustc_infer/src/infer/error_reporting/suggest.rs index 2d173e9d577..88d7cab0e4c 100644 --- a/compiler/rustc_infer/src/infer/error_reporting/suggest.rs +++ b/compiler/rustc_infer/src/infer/error_reporting/suggest.rs @@ -14,8 +14,8 @@ use crate::errors::{ ConsiderAddingAwait, DiagArg, FnConsiderCasting, FnItemsAreDistinct, FnUniqTypes, - FunctionPointerSuggestion, SuggAddLetForLetChains, SuggestAsRefWhereAppropriate, - SuggestRemoveSemiOrReturnBinding, + FunctionPointerSuggestion, SuggAddLetForLetChains, SuggestAccessingField, + SuggestAsRefWhereAppropriate, SuggestRemoveSemiOrReturnBinding, }; use super::TypeErrCtxt; @@ -264,15 +264,6 @@ pub(super) fn suggest_await_on_expect_found( } } - pub fn suggest_await_on_future(&self, diag: &mut Diagnostic, sp: Span) { - diag.span_suggestion_verbose( - sp.shrink_to_hi(), - "consider `await`ing on the `Future`", - ".await", - Applicability::MaybeIncorrect, - ); - } - pub(super) fn suggest_accessing_field_where_appropriate( &self, cause: &ObligationCause<'tcx>, @@ -299,21 +290,13 @@ pub(super) fn suggest_accessing_field_where_appropriate( 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) + SuggestAccessingField::Safe { span, snippet, name, ty } } else if expected_def.is_union() { - format!("unsafe {{ {}.{} }}", snippet, name) + SuggestAccessingField::Unsafe { span, snippet, name, ty } } else { return; }; - diag.span_suggestion( - span, - &format!( - "you might have meant to use field `{}` whose type is `{}`", - name, ty - ), - suggestion, - Applicability::MaybeIncorrect, - ); + diag.subdiagnostic(suggestion); } } }