diff --git a/compiler/rustc_infer/messages.ftl b/compiler/rustc_infer/messages.ftl index 17de77b74a8..2534c1cbdab 100644 --- a/compiler/rustc_infer/messages.ftl +++ b/compiler/rustc_infer/messages.ftl @@ -362,3 +362,6 @@ infer_sarwa_option = you can convert from `&Option` to `Option<&T>` using `.a 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}` + +infer_sbfrit_change_return_type = you could change the return type to be a boxed trait object +infer_sbfrit_box_return_expr = if you change the return type to expect trait objects, box the returned expressions \ No newline at end of file diff --git a/compiler/rustc_infer/src/errors/mod.rs b/compiler/rustc_infer/src/errors/mod.rs index 25c4e9a55f8..5d72ad6fa05 100644 --- a/compiler/rustc_infer/src/errors/mod.rs +++ b/compiler/rustc_infer/src/errors/mod.rs @@ -1261,7 +1261,7 @@ pub struct FnItemsAreDistinct; pub struct FnUniqTypes; #[derive(Subdiagnostic)] -#[help(infer_fn_uniq_types)] +#[help(infer_fn_consider_casting)] pub struct FnConsiderCasting { pub casting: String, } @@ -1317,3 +1317,21 @@ pub enum SuggestAccessingField<'a> { ty: Ty<'a>, }, } + +#[derive(Subdiagnostic)] +pub enum SuggestBoxingForReturnImplTrait { + #[multipart_suggestion(infer_sbfrit_change_return_type, applicability = "maybe-incorrect")] + ChangeReturnType { + #[suggestion_part(code = "Box, + #[suggestion_part(code = ")")] + ends: Vec, + }, +} diff --git a/compiler/rustc_infer/src/infer/error_reporting/suggest.rs b/compiler/rustc_infer/src/infer/error_reporting/suggest.rs index 88d7cab0e4c..82ee365f3a0 100644 --- a/compiler/rustc_infer/src/infer/error_reporting/suggest.rs +++ b/compiler/rustc_infer/src/infer/error_reporting/suggest.rs @@ -15,7 +15,8 @@ use rustc_target::abi::FieldIdx; use crate::errors::{ ConsiderAddingAwait, DiagArg, FnConsiderCasting, FnItemsAreDistinct, FnUniqTypes, FunctionPointerSuggestion, SuggAddLetForLetChains, SuggestAccessingField, - SuggestAsRefWhereAppropriate, SuggestRemoveSemiOrReturnBinding, + SuggestAsRefWhereAppropriate, SuggestBoxingForReturnImplTrait, + SuggestRemoveSemiOrReturnBinding, }; use super::TypeErrCtxt; @@ -80,25 +81,20 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> { return_sp: Span, arm_spans: impl Iterator, ) { - err.multipart_suggestion( - "you could change the return type to be a boxed trait object", - vec![ - (return_sp.with_hi(return_sp.lo() + BytePos(4)), "Box".to_string()), - ], - Applicability::MaybeIncorrect, - ); - let sugg = arm_spans - .flat_map(|sp| { - [(sp.shrink_to_lo(), "Box::new(".to_string()), (sp.shrink_to_hi(), ")".to_string())] - .into_iter() - }) - .collect::>(); - err.multipart_suggestion( - "if you change the return type to expect trait objects, box the returned expressions", - sugg, - Applicability::MaybeIncorrect, - ); + let sugg = SuggestBoxingForReturnImplTrait::ChangeReturnType { + start_sp: return_sp.with_hi(return_sp.lo() + BytePos(4)), + end_sp: return_sp.shrink_to_hi(), + }; + err.subdiagnostic(sugg); + + let mut starts = Vec::new(); + let mut ends = Vec::new(); + for span in arm_spans { + starts.push(span.shrink_to_lo()); + ends.push(span.shrink_to_hi()); + } + let sugg = SuggestBoxingForReturnImplTrait::BoxReturnExpr { starts, ends }; + err.subdiagnostic(sugg); } pub(super) fn suggest_tuple_pattern(