mirror of
https://github.com/rust-lang/rust.git
synced 2025-02-03 18:43:38 +00:00
Migrate SuggestBoxingForReturnImplTrait, Fix typo in infer_fn_consider_casting
This commit is contained in:
parent
d56b304bc8
commit
23b8567a78
@ -362,3 +362,6 @@ infer_sarwa_option = you can convert from `&Option<T>` to `Option<&T>` using `.a
|
|||||||
infer_sarwa_result = you can convert from `&Result<T, E>` to `Result<&T, &E>` using `.as_ref()`
|
infer_sarwa_result = you can convert from `&Result<T, E>` to `Result<&T, &E>` using `.as_ref()`
|
||||||
|
|
||||||
infer_suggest_accessing_field = you might have meant to use field `{$name}` whose type is `{$ty}`
|
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
|
@ -1261,7 +1261,7 @@ pub struct FnItemsAreDistinct;
|
|||||||
pub struct FnUniqTypes;
|
pub struct FnUniqTypes;
|
||||||
|
|
||||||
#[derive(Subdiagnostic)]
|
#[derive(Subdiagnostic)]
|
||||||
#[help(infer_fn_uniq_types)]
|
#[help(infer_fn_consider_casting)]
|
||||||
pub struct FnConsiderCasting {
|
pub struct FnConsiderCasting {
|
||||||
pub casting: String,
|
pub casting: String,
|
||||||
}
|
}
|
||||||
@ -1317,3 +1317,21 @@ pub enum SuggestAccessingField<'a> {
|
|||||||
ty: Ty<'a>,
|
ty: Ty<'a>,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Subdiagnostic)]
|
||||||
|
pub enum SuggestBoxingForReturnImplTrait {
|
||||||
|
#[multipart_suggestion(infer_sbfrit_change_return_type, applicability = "maybe-incorrect")]
|
||||||
|
ChangeReturnType {
|
||||||
|
#[suggestion_part(code = "Box<dyn")]
|
||||||
|
start_sp: Span,
|
||||||
|
#[suggestion_part(code = ">")]
|
||||||
|
end_sp: Span,
|
||||||
|
},
|
||||||
|
#[multipart_suggestion(infer_sbfrit_box_return_expr, applicability = "maybe-incorrect")]
|
||||||
|
BoxReturnExpr {
|
||||||
|
#[suggestion_part(code = "Box::new(")]
|
||||||
|
starts: Vec<Span>,
|
||||||
|
#[suggestion_part(code = ")")]
|
||||||
|
ends: Vec<Span>,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
@ -15,7 +15,8 @@ use rustc_target::abi::FieldIdx;
|
|||||||
use crate::errors::{
|
use crate::errors::{
|
||||||
ConsiderAddingAwait, DiagArg, FnConsiderCasting, FnItemsAreDistinct, FnUniqTypes,
|
ConsiderAddingAwait, DiagArg, FnConsiderCasting, FnItemsAreDistinct, FnUniqTypes,
|
||||||
FunctionPointerSuggestion, SuggAddLetForLetChains, SuggestAccessingField,
|
FunctionPointerSuggestion, SuggAddLetForLetChains, SuggestAccessingField,
|
||||||
SuggestAsRefWhereAppropriate, SuggestRemoveSemiOrReturnBinding,
|
SuggestAsRefWhereAppropriate, SuggestBoxingForReturnImplTrait,
|
||||||
|
SuggestRemoveSemiOrReturnBinding,
|
||||||
};
|
};
|
||||||
|
|
||||||
use super::TypeErrCtxt;
|
use super::TypeErrCtxt;
|
||||||
@ -80,25 +81,20 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
|
|||||||
return_sp: Span,
|
return_sp: Span,
|
||||||
arm_spans: impl Iterator<Item = Span>,
|
arm_spans: impl Iterator<Item = Span>,
|
||||||
) {
|
) {
|
||||||
err.multipart_suggestion(
|
let sugg = SuggestBoxingForReturnImplTrait::ChangeReturnType {
|
||||||
"you could change the return type to be a boxed trait object",
|
start_sp: return_sp.with_hi(return_sp.lo() + BytePos(4)),
|
||||||
vec![
|
end_sp: return_sp.shrink_to_hi(),
|
||||||
(return_sp.with_hi(return_sp.lo() + BytePos(4)), "Box<dyn".to_string()),
|
};
|
||||||
(return_sp.shrink_to_hi(), ">".to_string()),
|
err.subdiagnostic(sugg);
|
||||||
],
|
|
||||||
Applicability::MaybeIncorrect,
|
let mut starts = Vec::new();
|
||||||
);
|
let mut ends = Vec::new();
|
||||||
let sugg = arm_spans
|
for span in arm_spans {
|
||||||
.flat_map(|sp| {
|
starts.push(span.shrink_to_lo());
|
||||||
[(sp.shrink_to_lo(), "Box::new(".to_string()), (sp.shrink_to_hi(), ")".to_string())]
|
ends.push(span.shrink_to_hi());
|
||||||
.into_iter()
|
}
|
||||||
})
|
let sugg = SuggestBoxingForReturnImplTrait::BoxReturnExpr { starts, ends };
|
||||||
.collect::<Vec<_>>();
|
err.subdiagnostic(sugg);
|
||||||
err.multipart_suggestion(
|
|
||||||
"if you change the return type to expect trait objects, box the returned expressions",
|
|
||||||
sugg,
|
|
||||||
Applicability::MaybeIncorrect,
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(super) fn suggest_tuple_pattern(
|
pub(super) fn suggest_tuple_pattern(
|
||||||
|
Loading…
Reference in New Issue
Block a user