Extract select_inherent_assoc_type_candidates

This commit is contained in:
Santiago Pastorino 2023-11-21 11:19:33 -03:00
parent 8694b0973a
commit 4b26bb544e
No known key found for this signature in database
GPG Key ID: 8131A24E0C79EFAF

View File

@ -27,11 +27,12 @@ use rustc_hir::def_id::{DefId, LocalDefId};
use rustc_hir::intravisit::{walk_generics, Visitor as _}; use rustc_hir::intravisit::{walk_generics, Visitor as _};
use rustc_hir::{GenericArg, GenericArgs, OpaqueTyOrigin}; use rustc_hir::{GenericArg, GenericArgs, OpaqueTyOrigin};
use rustc_infer::infer::{InferCtxt, InferOk, TyCtxtInferExt}; use rustc_infer::infer::{InferCtxt, InferOk, TyCtxtInferExt};
use rustc_infer::traits::ObligationCause; use rustc_infer::traits::{Obligation, ObligationCause};
use rustc_middle::middle::stability::AllowUnstable; use rustc_middle::middle::stability::AllowUnstable;
use rustc_middle::ty::GenericParamDefKind; use rustc_middle::ty::GenericParamDefKind;
use rustc_middle::ty::{ use rustc_middle::ty::{
self, Const, GenericArgKind, GenericArgsRef, IsSuggestable, Ty, TyCtxt, TypeVisitableExt, self, Const, GenericArgKind, GenericArgsRef, IsSuggestable, ParamEnv, Predicate, Ty, TyCtxt,
TypeVisitableExt,
}; };
use rustc_session::lint::builtin::AMBIGUOUS_ASSOCIATED_ITEMS; use rustc_session::lint::builtin::AMBIGUOUS_ASSOCIATED_ITEMS;
use rustc_span::edit_distance::find_best_match_for_name; use rustc_span::edit_distance::find_best_match_for_name;
@ -1607,7 +1608,6 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
let param_env = tcx.param_env(block.owner); let param_env = tcx.param_env(block.owner);
let cause = ObligationCause::misc(span, block.owner.def_id); let cause = ObligationCause::misc(span, block.owner.def_id);
let mut fulfillment_errors = Vec::new();
let mut universes = if self_ty.has_escaping_bound_vars() { let mut universes = if self_ty.has_escaping_bound_vars() {
vec![None; self_ty.outer_exclusive_binder().as_usize()] vec![None; self_ty.outer_exclusive_binder().as_usize()]
} else { } else {
@ -1619,10 +1619,56 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
&mut universes, &mut universes,
self_ty, self_ty,
|self_ty| { |self_ty| {
let tcx = self.tcx();
let InferOk { value: self_ty, obligations } = let InferOk { value: self_ty, obligations } =
infcx.at(&cause, param_env).normalize(self_ty); infcx.at(&cause, param_env).normalize(self_ty);
let mut applicable_candidates: Vec<_> = candidates let (impl_, (assoc_item, def_scope)) = self.select_inherent_assoc_type_candidates(
infcx,
name,
span,
self_ty,
cause,
param_env,
obligations,
candidates,
)?;
self.check_assoc_ty(assoc_item, name, def_scope, block, span);
// FIXME(fmease): Currently creating throwaway `parent_args` to please
// `create_args_for_associated_item`. Modify the latter instead (or sth. similar) to
// not require the parent args logic.
let parent_args = ty::GenericArgs::identity_for_item(tcx, impl_);
let args =
self.create_args_for_associated_item(span, assoc_item, segment, parent_args);
let args = tcx.mk_args_from_iter(
std::iter::once(ty::GenericArg::from(self_ty))
.chain(args.into_iter().skip(parent_args.len())),
);
let ty = Ty::new_alias(tcx, ty::Inherent, ty::AliasTy::new(tcx, assoc_item, args));
Ok(Some((ty, assoc_item)))
},
)
}
fn select_inherent_assoc_type_candidates(
&self,
infcx: &InferCtxt<'tcx>,
name: Ident,
span: Span,
self_ty: Ty<'tcx>,
cause: ObligationCause<'tcx>,
param_env: ParamEnv<'tcx>,
obligations: Vec<Obligation<'tcx, Predicate<'tcx>>>,
candidates: Vec<(DefId, (DefId, DefId))>,
) -> Result<(DefId, (DefId, DefId)), ErrorGuaranteed> {
let tcx = self.tcx();
let mut fulfillment_errors = Vec::new();
let applicable_candidates: Vec<_> = candidates
.iter() .iter()
.copied() .copied()
.filter(|&(impl_, _)| { .filter(|&(impl_, _)| {
@ -1635,10 +1681,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
let impl_ty = ocx.normalize(&cause, param_env, impl_ty); let impl_ty = ocx.normalize(&cause, param_env, impl_ty);
// Check that the self types can be related. // Check that the self types can be related.
if ocx if ocx.eq(&ObligationCause::dummy(), param_env, impl_ty, self_ty).is_err() {
.eq(&ObligationCause::dummy(), param_env, impl_ty, self_ty)
.is_err()
{
return false; return false;
} }
@ -1663,50 +1706,23 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
}) })
.collect(); .collect();
if applicable_candidates.len() > 1 { match &applicable_candidates[..] {
return Err(self.complain_about_ambiguous_inherent_assoc_type( &[] => Err(self.complain_about_inherent_assoc_type_not_found(
name,
applicable_candidates
.into_iter()
.map(|(_, (candidate, _))| candidate)
.collect(),
span,
));
}
if let Some((impl_, (assoc_item, def_scope))) = applicable_candidates.pop() {
self.check_assoc_ty(assoc_item, name, def_scope, block, span);
// FIXME(fmease): Currently creating throwaway `parent_args` to please
// `create_args_for_associated_item`. Modify the latter instead (or sth. similar) to
// not require the parent args logic.
let parent_args = ty::GenericArgs::identity_for_item(tcx, impl_);
let args = self.create_args_for_associated_item(
span,
assoc_item,
segment,
parent_args,
);
let args = tcx.mk_args_from_iter(
std::iter::once(ty::GenericArg::from(self_ty))
.chain(args.into_iter().skip(parent_args.len())),
);
let ty =
Ty::new_alias(tcx, ty::Inherent, ty::AliasTy::new(tcx, assoc_item, args));
return Ok(Some((ty, assoc_item)));
}
Err(self.complain_about_inherent_assoc_type_not_found(
name, name,
self_ty, self_ty,
candidates, candidates,
fulfillment_errors, fulfillment_errors,
span, span,
)) )),
},
) &[applicable_candidate] => Ok(applicable_candidate),
&[_, ..] => Err(self.complain_about_ambiguous_inherent_assoc_type(
name,
applicable_candidates.into_iter().map(|(_, (candidate, _))| candidate).collect(),
span,
)),
}
} }
fn lookup_assoc_ty( fn lookup_assoc_ty(