review comments

This commit is contained in:
Esteban Küber 2024-01-03 19:25:31 +00:00
parent 79bef72fd5
commit 771966ba29

View File

@ -41,8 +41,8 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
else { else {
return; return;
}; };
let Some(sugg) = self.generics_suggestion(generics, self_ty.span, &impl_trait_name) let sugg = self.add_generic_param_suggestion(generics, self_ty.span, &impl_trait_name);
else { if sugg.is_empty() {
return; return;
}; };
diag.multipart_suggestion( diag.multipart_suggestion(
@ -56,12 +56,12 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
} }
} }
fn generics_suggestion( fn add_generic_param_suggestion(
&self, &self,
generics: &hir::Generics<'_>, generics: &hir::Generics<'_>,
self_ty_span: Span, self_ty_span: Span,
impl_trait_name: &str, impl_trait_name: &str,
) -> Option<Vec<(Span, String)>> { ) -> Vec<(Span, String)> {
// check if the trait has generics, to make a correct suggestion // check if the trait has generics, to make a correct suggestion
let param_name = generics.params.next_type_param_name(None); let param_name = generics.params.next_type_param_name(None);
@ -70,7 +70,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
} else { } else {
(generics.span, format!("<{param_name}: {impl_trait_name}>")) (generics.span, format!("<{param_name}: {impl_trait_name}>"))
}; };
Some(vec![(self_ty_span, param_name), add_generic_sugg]) vec![(self_ty_span, param_name), add_generic_sugg]
} }
/// Make sure that we are in the condition to suggest `impl Trait`. /// Make sure that we are in the condition to suggest `impl Trait`.
@ -86,7 +86,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
else { else {
return false; return false;
}; };
let Ok(trait_name) = self.tcx().sess.source_map().span_to_snippet(self_ty.span) else { let Ok(trait_name) = tcx.sess.source_map().span_to_snippet(self_ty.span) else {
return false; return false;
}; };
let impl_sugg = vec![(self_ty.span.shrink_to_lo(), "impl ".to_string())]; let impl_sugg = vec![(self_ty.span.shrink_to_lo(), "impl ".to_string())];
@ -98,6 +98,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
impl_sugg, impl_sugg,
Applicability::MachineApplicable, Applicability::MachineApplicable,
); );
if tcx.check_is_object_safe(def_id) {
diag.multipart_suggestion_verbose( diag.multipart_suggestion_verbose(
"alternatively, you can return an owned trait object", "alternatively, you can return an owned trait object",
vec![ vec![
@ -106,11 +107,13 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
], ],
Applicability::MachineApplicable, Applicability::MachineApplicable,
); );
}
return true; return true;
} }
for ty in sig.decl.inputs { for ty in sig.decl.inputs {
if ty.hir_id == self_ty.hir_id { if ty.hir_id == self_ty.hir_id {
if let Some(sugg) = self.generics_suggestion(generics, self_ty.span, &trait_name) { let sugg = self.add_generic_param_suggestion(generics, self_ty.span, &trait_name);
if !sugg.is_empty() {
diag.multipart_suggestion_verbose( diag.multipart_suggestion_verbose(
format!("use a new generic type parameter, constrained by `{trait_name}`"), format!("use a new generic type parameter, constrained by `{trait_name}`"),
sugg, sugg,
@ -124,6 +127,9 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
Applicability::MachineApplicable, Applicability::MachineApplicable,
); );
} }
if !tcx.check_is_object_safe(def_id) {
diag.note(format!("it is not object safe, so it can't be `dyn`"));
}
let sugg = if let hir::TyKind::TraitObject([_, _, ..], _, _) = self_ty.kind { let sugg = if let hir::TyKind::TraitObject([_, _, ..], _, _) = self_ty.kind {
// There are more than one trait bound, we need surrounding parentheses. // There are more than one trait bound, we need surrounding parentheses.
vec![ vec![