Skip to content

Commit 23b8567

Browse files
committed
Migrate SuggestBoxingForReturnImplTrait, Fix typo in infer_fn_consider_casting
1 parent d56b304 commit 23b8567

File tree

3 files changed

+38
-21
lines changed

3 files changed

+38
-21
lines changed

compiler/rustc_infer/messages.ftl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,3 +362,6 @@ infer_sarwa_option = you can convert from `&Option<T>` to `Option<&T>` using `.a
362362
infer_sarwa_result = you can convert from `&Result<T, E>` to `Result<&T, &E>` using `.as_ref()`
363363
364364
infer_suggest_accessing_field = you might have meant to use field `{$name}` whose type is `{$ty}`
365+
366+
infer_sbfrit_change_return_type = you could change the return type to be a boxed trait object
367+
infer_sbfrit_box_return_expr = if you change the return type to expect trait objects, box the returned expressions

compiler/rustc_infer/src/errors/mod.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1261,7 +1261,7 @@ pub struct FnItemsAreDistinct;
12611261
pub struct FnUniqTypes;
12621262

12631263
#[derive(Subdiagnostic)]
1264-
#[help(infer_fn_uniq_types)]
1264+
#[help(infer_fn_consider_casting)]
12651265
pub struct FnConsiderCasting {
12661266
pub casting: String,
12671267
}
@@ -1317,3 +1317,21 @@ pub enum SuggestAccessingField<'a> {
13171317
ty: Ty<'a>,
13181318
},
13191319
}
1320+
1321+
#[derive(Subdiagnostic)]
1322+
pub enum SuggestBoxingForReturnImplTrait {
1323+
#[multipart_suggestion(infer_sbfrit_change_return_type, applicability = "maybe-incorrect")]
1324+
ChangeReturnType {
1325+
#[suggestion_part(code = "Box<dyn")]
1326+
start_sp: Span,
1327+
#[suggestion_part(code = ">")]
1328+
end_sp: Span,
1329+
},
1330+
#[multipart_suggestion(infer_sbfrit_box_return_expr, applicability = "maybe-incorrect")]
1331+
BoxReturnExpr {
1332+
#[suggestion_part(code = "Box::new(")]
1333+
starts: Vec<Span>,
1334+
#[suggestion_part(code = ")")]
1335+
ends: Vec<Span>,
1336+
},
1337+
}

compiler/rustc_infer/src/infer/error_reporting/suggest.rs

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ use rustc_target::abi::FieldIdx;
1515
use crate::errors::{
1616
ConsiderAddingAwait, DiagArg, FnConsiderCasting, FnItemsAreDistinct, FnUniqTypes,
1717
FunctionPointerSuggestion, SuggAddLetForLetChains, SuggestAccessingField,
18-
SuggestAsRefWhereAppropriate, SuggestRemoveSemiOrReturnBinding,
18+
SuggestAsRefWhereAppropriate, SuggestBoxingForReturnImplTrait,
19+
SuggestRemoveSemiOrReturnBinding,
1920
};
2021

2122
use super::TypeErrCtxt;
@@ -80,25 +81,20 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
8081
return_sp: Span,
8182
arm_spans: impl Iterator<Item = Span>,
8283
) {
83-
err.multipart_suggestion(
84-
"you could change the return type to be a boxed trait object",
85-
vec![
86-
(return_sp.with_hi(return_sp.lo() + BytePos(4)), "Box<dyn".to_string()),
87-
(return_sp.shrink_to_hi(), ">".to_string()),
88-
],
89-
Applicability::MaybeIncorrect,
90-
);
91-
let sugg = arm_spans
92-
.flat_map(|sp| {
93-
[(sp.shrink_to_lo(), "Box::new(".to_string()), (sp.shrink_to_hi(), ")".to_string())]
94-
.into_iter()
95-
})
96-
.collect::<Vec<_>>();
97-
err.multipart_suggestion(
98-
"if you change the return type to expect trait objects, box the returned expressions",
99-
sugg,
100-
Applicability::MaybeIncorrect,
101-
);
84+
let sugg = SuggestBoxingForReturnImplTrait::ChangeReturnType {
85+
start_sp: return_sp.with_hi(return_sp.lo() + BytePos(4)),
86+
end_sp: return_sp.shrink_to_hi(),
87+
};
88+
err.subdiagnostic(sugg);
89+
90+
let mut starts = Vec::new();
91+
let mut ends = Vec::new();
92+
for span in arm_spans {
93+
starts.push(span.shrink_to_lo());
94+
ends.push(span.shrink_to_hi());
95+
}
96+
let sugg = SuggestBoxingForReturnImplTrait::BoxReturnExpr { starts, ends };
97+
err.subdiagnostic(sugg);
10298
}
10399

104100
pub(super) fn suggest_tuple_pattern(

0 commit comments

Comments
 (0)