1
Fork 0

Address review comments

This commit is contained in:
Michael Goulet 2022-12-28 18:35:16 +00:00
parent 992ba801c2
commit 6e794dcc8b

View file

@ -324,13 +324,8 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
&format!("`{}: {}`", sup, sub), &format!("`{}: {}`", sup, sub),
); );
// We should only suggest rewriting the `where` clause if the predicate is within that `where` clause // We should only suggest rewriting the `where` clause if the predicate is within that `where` clause
if self if let Some(generics) = self.tcx.hir().get_generics(impl_item_def_id)
.tcx && generics.where_clause_span.contains(span)
.hir()
.get_generics(impl_item_def_id)
.unwrap()
.where_clause_span
.contains(span)
{ {
self.suggest_copy_trait_method_bounds( self.suggest_copy_trait_method_bounds(
trait_item_def_id, trait_item_def_id,
@ -390,12 +385,13 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
// but right now it's not really very smart when it comes to implicit `Sized` // but right now it's not really very smart when it comes to implicit `Sized`
// predicates and bounds on the trait itself. // predicates and bounds on the trait itself.
let impl_def_id = let Some(impl_def_id) =
self.tcx.associated_item(impl_item_def_id).impl_container(self.tcx).unwrap(); self.tcx.associated_item(impl_item_def_id).impl_container(self.tcx) else { return; };
let trait_substs = self let Some(trait_ref) = self
.tcx .tcx
.impl_trait_ref(impl_def_id) .impl_trait_ref(impl_def_id)
.unwrap() else { return; };
let trait_substs = trait_ref
// Replace the explicit self type with `Self` for better suggestion rendering // Replace the explicit self type with `Self` for better suggestion rendering
.with_self_ty(self.tcx, self.tcx.mk_ty_param(0, kw::SelfUpper)) .with_self_ty(self.tcx, self.tcx.mk_ty_param(0, kw::SelfUpper))
.substs; .substs;
@ -403,39 +399,37 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
ty::InternalSubsts::identity_for_item(self.tcx, impl_item_def_id.to_def_id()) ty::InternalSubsts::identity_for_item(self.tcx, impl_item_def_id.to_def_id())
.rebase_onto(self.tcx, impl_def_id, trait_substs); .rebase_onto(self.tcx, impl_def_id, trait_substs);
let mut is_suggestable = true; let Ok(trait_predicates) = self
let trait_predicates = self
.tcx .tcx
.bound_explicit_predicates_of(trait_item_def_id) .bound_explicit_predicates_of(trait_item_def_id)
.map_bound(|p| p.predicates) .map_bound(|p| p.predicates)
.subst_iter_copied(self.tcx, trait_item_substs) .subst_iter_copied(self.tcx, trait_item_substs)
.map(|(pred, _)| { .map(|(pred, _)| {
if !pred.is_suggestable(self.tcx, false) { if pred.is_suggestable(self.tcx, false) {
is_suggestable = false; Ok(pred.to_string())
} else {
Err(())
} }
pred.to_string()
}) })
.collect::<Vec<_>>(); .collect::<Result<Vec<_>, ()>>() else { return; };
let generics = self.tcx.hir().get_generics(impl_item_def_id).unwrap(); let Some(generics) = self.tcx.hir().get_generics(impl_item_def_id) else { return; };
if is_suggestable { if trait_predicates.is_empty() {
if trait_predicates.is_empty() { err.span_suggestion_verbose(
err.span_suggestion_verbose( generics.where_clause_span,
generics.where_clause_span, "remove the `where` clause",
"remove the `where` clause", String::new(),
String::new(), Applicability::MachineApplicable,
Applicability::MachineApplicable, );
); } else {
} else { let space = if generics.where_clause_span.is_empty() { " " } else { "" };
let space = if generics.where_clause_span.is_empty() { " " } else { "" }; err.span_suggestion_verbose(
err.span_suggestion_verbose( generics.where_clause_span,
generics.where_clause_span, "copy the `where` clause predicates from the trait",
"copy the `where` clause predicates from the trait", format!("{space}where {}", trait_predicates.join(", ")),
format!("{space}where {}", trait_predicates.join(", ")), Applicability::MachineApplicable,
Applicability::MachineApplicable, );
);
}
} }
} }