1
Fork 0

Stop passing both trait pred and trait ref

This commit is contained in:
Michael Goulet 2024-06-07 17:01:55 -04:00
parent 93d83c8f69
commit f8d12d9189
2 changed files with 18 additions and 16 deletions

View file

@ -4863,14 +4863,13 @@ impl<'a, 'hir> hir::intravisit::Visitor<'hir> for ReplaceImplTraitVisitor<'a> {
pub(super) fn get_explanation_based_on_obligation<'tcx>( pub(super) fn get_explanation_based_on_obligation<'tcx>(
tcx: TyCtxt<'tcx>, tcx: TyCtxt<'tcx>,
obligation: &PredicateObligation<'tcx>, obligation: &PredicateObligation<'tcx>,
trait_ref: ty::PolyTraitRef<'tcx>,
trait_predicate: &ty::PolyTraitPredicate<'tcx>, trait_predicate: &ty::PolyTraitPredicate<'tcx>,
pre_message: String, pre_message: String,
) -> String { ) -> String {
if let ObligationCauseCode::MainFunctionType = obligation.cause.code() { if let ObligationCauseCode::MainFunctionType = obligation.cause.code() {
"consider using `()`, or a `Result`".to_owned() "consider using `()`, or a `Result`".to_owned()
} else { } else {
let ty_desc = match trait_ref.skip_binder().self_ty().kind() { let ty_desc = match trait_predicate.self_ty().skip_binder().kind() {
ty::FnDef(_, _) => Some("fn item"), ty::FnDef(_, _) => Some("fn item"),
ty::Closure(_, _) => Some("closure"), ty::Closure(_, _) => Some("closure"),
_ => None, _ => None,
@ -4895,7 +4894,7 @@ pub(super) fn get_explanation_based_on_obligation<'tcx>(
format!( format!(
"{pre_message}the trait `{}` is not implemented for{desc} `{}`{post}", "{pre_message}the trait `{}` is not implemented for{desc} `{}`{post}",
trait_predicate.print_modifiers_and_trait_path(), trait_predicate.print_modifiers_and_trait_path(),
tcx.short_ty_string(trait_ref.skip_binder().self_ty(), &mut None), tcx.short_ty_string(trait_predicate.self_ty().skip_binder(), &mut None),
) )
} else { } else {
// "the trait bound `T: !Send` is not satisfied" reads better than "`!Send` is // "the trait bound `T: !Send` is not satisfied" reads better than "`!Send` is

View file

@ -603,7 +603,6 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
let explanation = get_explanation_based_on_obligation( let explanation = get_explanation_based_on_obligation(
self.tcx, self.tcx,
&obligation, &obligation,
leaf_trait_ref,
&leaf_trait_predicate, &leaf_trait_predicate,
pre_message, pre_message,
); );
@ -759,7 +758,6 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
self.try_to_add_help_message( self.try_to_add_help_message(
&obligation, &obligation,
leaf_trait_ref,
&leaf_trait_predicate, &leaf_trait_predicate,
&mut err, &mut err,
span, span,
@ -3215,7 +3213,6 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
fn try_to_add_help_message( fn try_to_add_help_message(
&self, &self,
obligation: &PredicateObligation<'tcx>, obligation: &PredicateObligation<'tcx>,
trait_ref: ty::PolyTraitRef<'tcx>,
trait_predicate: &ty::PolyTraitPredicate<'tcx>, trait_predicate: &ty::PolyTraitPredicate<'tcx>,
err: &mut Diag<'_>, err: &mut Diag<'_>,
span: Span, span: Span,
@ -3233,15 +3230,21 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
}; };
// Try to report a help message // Try to report a help message
let trait_def_id = trait_predicate.def_id();
if is_fn_trait if is_fn_trait
&& let Ok((implemented_kind, params)) = self.type_implements_fn_trait( && let Ok((implemented_kind, params)) = self.type_implements_fn_trait(
obligation.param_env, obligation.param_env,
trait_ref.self_ty(), trait_predicate.self_ty(),
trait_predicate.skip_binder().polarity, trait_predicate.skip_binder().polarity,
) )
{ {
self.add_help_message_for_fn_trait(trait_ref, err, implemented_kind, params); self.add_help_message_for_fn_trait(
} else if !trait_ref.has_non_region_infer() trait_predicate.to_poly_trait_ref(),
err,
implemented_kind,
params,
);
} else if !trait_predicate.has_non_region_infer()
&& self.predicate_can_apply(obligation.param_env, *trait_predicate) && self.predicate_can_apply(obligation.param_env, *trait_predicate)
{ {
// If a where-clause may be useful, remind the // If a where-clause may be useful, remind the
@ -3257,13 +3260,13 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
None, None,
obligation.cause.body_id, obligation.cause.body_id,
); );
} else if trait_ref.def_id().is_local() } else if trait_def_id.is_local()
&& self.tcx.trait_impls_of(trait_ref.def_id()).is_empty() && self.tcx.trait_impls_of(trait_def_id).is_empty()
&& !self.tcx.trait_is_auto(trait_ref.def_id()) && !self.tcx.trait_is_auto(trait_def_id)
&& !self.tcx.trait_is_alias(trait_ref.def_id()) && !self.tcx.trait_is_alias(trait_def_id)
{ {
err.span_help( err.span_help(
self.tcx.def_span(trait_ref.def_id()), self.tcx.def_span(trait_def_id),
crate::fluent_generated::trait_selection_trait_has_no_impls, crate::fluent_generated::trait_selection_trait_has_no_impls,
); );
} else if !suggested && !unsatisfied_const { } else if !suggested && !unsatisfied_const {
@ -3271,7 +3274,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
let impl_candidates = self.find_similar_impl_candidates(*trait_predicate); let impl_candidates = self.find_similar_impl_candidates(*trait_predicate);
if !self.report_similar_impl_candidates( if !self.report_similar_impl_candidates(
&impl_candidates, &impl_candidates,
trait_ref, trait_predicate.to_poly_trait_ref(),
body_def_id, body_def_id,
err, err,
true, true,
@ -3288,7 +3291,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
self.suggest_convert_to_slice( self.suggest_convert_to_slice(
err, err,
obligation, obligation,
trait_ref, trait_predicate.to_poly_trait_ref(),
impl_candidates.as_slice(), impl_candidates.as_slice(),
span, span,
); );