2022-12-04 03:19:10 +00:00
|
|
|
use crate::traits::{specialization_graph, translate_substs};
|
|
|
|
|
|
2023-01-17 11:47:47 +01:00
|
|
|
use super::assembly::{self, Candidate, CandidateSource};
|
2023-01-17 12:26:28 +01:00
|
|
|
use super::infcx_ext::InferCtxtExt;
|
2023-01-17 11:47:47 +01:00
|
|
|
use super::{Certainty, EvalCtxt, Goal, QueryResult};
|
2022-12-04 03:19:10 +00:00
|
|
|
use rustc_errors::ErrorGuaranteed;
|
|
|
|
|
use rustc_hir::def::DefKind;
|
|
|
|
|
use rustc_hir::def_id::DefId;
|
2023-01-17 12:26:28 +01:00
|
|
|
use rustc_infer::infer::InferCtxt;
|
2022-12-04 03:19:10 +00:00
|
|
|
use rustc_infer::traits::query::NoSolution;
|
|
|
|
|
use rustc_infer::traits::specialization_graph::LeafDef;
|
2023-01-17 12:26:28 +01:00
|
|
|
use rustc_infer::traits::Reveal;
|
2022-12-04 03:19:10 +00:00
|
|
|
use rustc_middle::ty::fast_reject::{DeepRejectCtxt, TreatParams};
|
|
|
|
|
use rustc_middle::ty::ProjectionPredicate;
|
|
|
|
|
use rustc_middle::ty::TypeVisitable;
|
2022-12-19 07:01:38 +00:00
|
|
|
use rustc_middle::ty::{self, Ty, TyCtxt};
|
2022-12-04 03:19:10 +00:00
|
|
|
use rustc_span::DUMMY_SP;
|
|
|
|
|
use std::iter;
|
|
|
|
|
|
2023-01-17 10:21:30 +01:00
|
|
|
impl<'tcx> EvalCtxt<'_, 'tcx> {
|
2022-12-04 03:19:10 +00:00
|
|
|
pub(super) fn compute_projection_goal(
|
|
|
|
|
&mut self,
|
2023-01-17 10:21:30 +01:00
|
|
|
goal: Goal<'tcx, ProjectionPredicate<'tcx>>,
|
2022-12-04 03:19:10 +00:00
|
|
|
) -> QueryResult<'tcx> {
|
2023-01-17 11:47:47 +01:00
|
|
|
let candidates = self.assemble_and_evaluate_candidates(goal);
|
2022-12-04 03:19:10 +00:00
|
|
|
self.merge_project_candidates(candidates)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn merge_project_candidates(
|
|
|
|
|
&mut self,
|
|
|
|
|
mut candidates: Vec<Candidate<'tcx>>,
|
|
|
|
|
) -> QueryResult<'tcx> {
|
|
|
|
|
match candidates.len() {
|
|
|
|
|
0 => return Err(NoSolution),
|
|
|
|
|
1 => return Ok(candidates.pop().unwrap().result),
|
|
|
|
|
_ => {}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if candidates.len() > 1 {
|
|
|
|
|
let mut i = 0;
|
|
|
|
|
'outer: while i < candidates.len() {
|
|
|
|
|
for j in (0..candidates.len()).filter(|&j| i != j) {
|
|
|
|
|
if self.project_candidate_should_be_dropped_in_favor_of(
|
|
|
|
|
&candidates[i],
|
|
|
|
|
&candidates[j],
|
|
|
|
|
) {
|
|
|
|
|
debug!(candidate = ?candidates[i], "Dropping candidate #{}/{}", i, candidates.len());
|
|
|
|
|
candidates.swap_remove(i);
|
|
|
|
|
continue 'outer;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
debug!(candidate = ?candidates[i], "Retaining candidate #{}/{}", i, candidates.len());
|
|
|
|
|
// If there are *STILL* multiple candidates, give up
|
|
|
|
|
// and report ambiguity.
|
|
|
|
|
i += 1;
|
|
|
|
|
if i > 1 {
|
|
|
|
|
debug!("multiple matches, ambig");
|
|
|
|
|
// FIXME: return overflow if all candidates overflow, otherwise return ambiguity.
|
|
|
|
|
unimplemented!();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Ok(candidates.pop().unwrap().result)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn project_candidate_should_be_dropped_in_favor_of(
|
|
|
|
|
&self,
|
|
|
|
|
candidate: &Candidate<'tcx>,
|
|
|
|
|
other: &Candidate<'tcx>,
|
|
|
|
|
) -> bool {
|
|
|
|
|
// FIXME: implement this
|
|
|
|
|
match (candidate.source, other.source) {
|
|
|
|
|
(CandidateSource::Impl(_), _)
|
|
|
|
|
| (CandidateSource::ParamEnv(_), _)
|
2023-01-17 11:47:47 +01:00
|
|
|
| (CandidateSource::BuiltinImpl, _)
|
|
|
|
|
| (CandidateSource::AliasBound(_), _) => unimplemented!(),
|
2022-12-04 03:19:10 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-19 07:01:38 +00:00
|
|
|
impl<'tcx> assembly::GoalKind<'tcx> for ProjectionPredicate<'tcx> {
|
|
|
|
|
fn self_ty(self) -> Ty<'tcx> {
|
|
|
|
|
self.self_ty()
|
2022-12-04 03:19:10 +00:00
|
|
|
}
|
|
|
|
|
|
2022-12-19 07:01:38 +00:00
|
|
|
fn with_self_ty(self, tcx: TyCtxt<'tcx>, self_ty: Ty<'tcx>) -> Self {
|
|
|
|
|
self.with_self_ty(tcx, self_ty)
|
2022-12-04 03:19:10 +00:00
|
|
|
}
|
|
|
|
|
|
2022-12-19 07:01:38 +00:00
|
|
|
fn trait_def_id(self, tcx: TyCtxt<'tcx>) -> DefId {
|
|
|
|
|
self.trait_def_id(tcx)
|
2022-12-04 03:19:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn consider_impl_candidate(
|
2023-01-17 11:47:47 +01:00
|
|
|
ecx: &mut EvalCtxt<'_, 'tcx>,
|
2022-12-04 03:19:10 +00:00
|
|
|
goal: Goal<'tcx, ProjectionPredicate<'tcx>>,
|
|
|
|
|
impl_def_id: DefId,
|
2023-01-17 11:47:47 +01:00
|
|
|
) -> Result<Certainty, NoSolution> {
|
|
|
|
|
let tcx = ecx.tcx();
|
2023-01-17 10:21:30 +01:00
|
|
|
|
2022-12-04 03:19:10 +00:00
|
|
|
let goal_trait_ref = goal.predicate.projection_ty.trait_ref(tcx);
|
2023-01-10 14:57:22 -07:00
|
|
|
let impl_trait_ref = tcx.impl_trait_ref(impl_def_id).unwrap();
|
2022-12-04 03:19:10 +00:00
|
|
|
let drcx = DeepRejectCtxt { treat_obligation_params: TreatParams::AsPlaceholder };
|
|
|
|
|
if iter::zip(goal_trait_ref.substs, impl_trait_ref.skip_binder().substs)
|
|
|
|
|
.any(|(goal, imp)| !drcx.generic_args_may_unify(goal, imp))
|
|
|
|
|
{
|
2023-01-17 11:47:47 +01:00
|
|
|
return Err(NoSolution);
|
2022-12-04 03:19:10 +00:00
|
|
|
}
|
|
|
|
|
|
2023-01-17 11:47:47 +01:00
|
|
|
ecx.infcx.probe(|_| {
|
|
|
|
|
let impl_substs = ecx.infcx.fresh_substs_for_item(DUMMY_SP, impl_def_id);
|
2022-12-04 03:19:10 +00:00
|
|
|
let impl_trait_ref = impl_trait_ref.subst(tcx, impl_substs);
|
|
|
|
|
|
2023-01-17 12:26:28 +01:00
|
|
|
let mut nested_goals = ecx.infcx.eq(goal.param_env, goal_trait_ref, impl_trait_ref)?;
|
2023-01-03 03:43:11 +00:00
|
|
|
let where_clause_bounds = tcx
|
|
|
|
|
.predicates_of(impl_def_id)
|
|
|
|
|
.instantiate(tcx, impl_substs)
|
|
|
|
|
.predicates
|
|
|
|
|
.into_iter()
|
|
|
|
|
.map(|pred| goal.with(tcx, pred));
|
|
|
|
|
|
2023-01-17 12:26:28 +01:00
|
|
|
nested_goals.extend(where_clause_bounds);
|
2023-01-17 11:47:47 +01:00
|
|
|
let trait_ref_certainty = ecx.evaluate_all(nested_goals)?;
|
2022-12-04 03:19:10 +00:00
|
|
|
|
2022-12-19 07:01:38 +00:00
|
|
|
let Some(assoc_def) = fetch_eligible_assoc_item_def(
|
2023-01-17 11:47:47 +01:00
|
|
|
ecx.infcx,
|
2022-12-04 03:19:10 +00:00
|
|
|
goal.param_env,
|
|
|
|
|
goal_trait_ref,
|
|
|
|
|
goal.predicate.def_id(),
|
|
|
|
|
impl_def_id
|
|
|
|
|
) else {
|
2023-01-17 11:47:47 +01:00
|
|
|
return Err(NoSolution);
|
2022-12-04 03:19:10 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if !assoc_def.item.defaultness(tcx).has_value() {
|
|
|
|
|
tcx.sess.delay_span_bug(
|
|
|
|
|
tcx.def_span(assoc_def.item.def_id),
|
|
|
|
|
"missing value for assoc item in impl",
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Getting the right substitutions here is complex, e.g. given:
|
|
|
|
|
// - a goal `<Vec<u32> as Trait<i32>>::Assoc<u64>`
|
|
|
|
|
// - the applicable impl `impl<T> Trait<i32> for Vec<T>`
|
|
|
|
|
// - and the impl which defines `Assoc` being `impl<T, U> Trait<U> for Vec<T>`
|
|
|
|
|
//
|
|
|
|
|
// We first rebase the goal substs onto the impl, going from `[Vec<u32>, i32, u64]`
|
|
|
|
|
// to `[u32, u64]`.
|
|
|
|
|
//
|
|
|
|
|
// And then map these substs to the substs of the defining impl of `Assoc`, going
|
|
|
|
|
// from `[u32, u64]` to `[u32, i32, u64]`.
|
|
|
|
|
let impl_substs_with_gat = goal.predicate.projection_ty.substs.rebase_onto(
|
|
|
|
|
tcx,
|
|
|
|
|
goal_trait_ref.def_id,
|
2023-01-14 04:47:06 +00:00
|
|
|
impl_substs,
|
2022-12-04 03:19:10 +00:00
|
|
|
);
|
|
|
|
|
let substs = translate_substs(
|
2023-01-17 11:47:47 +01:00
|
|
|
ecx.infcx,
|
2022-12-04 03:19:10 +00:00
|
|
|
goal.param_env,
|
|
|
|
|
impl_def_id,
|
|
|
|
|
impl_substs_with_gat,
|
|
|
|
|
assoc_def.defining_node,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// Finally we construct the actual value of the associated type.
|
|
|
|
|
let is_const = matches!(tcx.def_kind(assoc_def.item.def_id), DefKind::AssocConst);
|
|
|
|
|
let ty = tcx.bound_type_of(assoc_def.item.def_id);
|
|
|
|
|
let term: ty::EarlyBinder<ty::Term<'tcx>> = if is_const {
|
2023-01-17 10:21:30 +01:00
|
|
|
let identity_substs =
|
|
|
|
|
ty::InternalSubsts::identity_for_item(tcx, assoc_def.item.def_id);
|
2022-12-04 03:19:10 +00:00
|
|
|
let did = ty::WithOptConstParam::unknown(assoc_def.item.def_id);
|
|
|
|
|
let kind =
|
|
|
|
|
ty::ConstKind::Unevaluated(ty::UnevaluatedConst::new(did, identity_substs));
|
|
|
|
|
ty.map_bound(|ty| tcx.mk_const(kind, ty).into())
|
|
|
|
|
} else {
|
|
|
|
|
ty.map_bound(|ty| ty.into())
|
|
|
|
|
};
|
|
|
|
|
|
2023-01-17 12:26:28 +01:00
|
|
|
let nested_goals =
|
|
|
|
|
ecx.infcx.eq(goal.param_env, goal.predicate.term, term.subst(tcx, substs))?;
|
2023-01-17 11:47:47 +01:00
|
|
|
let rhs_certainty = ecx.evaluate_all(nested_goals)?;
|
2022-12-04 03:19:10 +00:00
|
|
|
|
2023-01-17 11:47:47 +01:00
|
|
|
Ok(trait_ref_certainty.unify_and(rhs_certainty))
|
2022-12-04 03:19:10 +00:00
|
|
|
})
|
|
|
|
|
}
|
2023-01-17 11:47:47 +01:00
|
|
|
|
|
|
|
|
fn consider_builtin_sized_candidate(
|
|
|
|
|
_ecx: &mut EvalCtxt<'_, 'tcx>,
|
|
|
|
|
goal: Goal<'tcx, Self>,
|
|
|
|
|
) -> Result<Certainty, NoSolution> {
|
|
|
|
|
bug!("`Sized` does not have an associated type: {:?}", goal);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn consider_assumption(
|
|
|
|
|
_ecx: &mut EvalCtxt<'_, 'tcx>,
|
|
|
|
|
_goal: Goal<'tcx, Self>,
|
|
|
|
|
assumption: ty::Predicate<'tcx>,
|
|
|
|
|
) -> Result<Certainty, NoSolution> {
|
|
|
|
|
if let Some(_poly_projection_pred) = assumption.to_opt_poly_projection_pred() {
|
|
|
|
|
unimplemented!()
|
|
|
|
|
} else {
|
|
|
|
|
Err(NoSolution)
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-12-19 07:01:38 +00:00
|
|
|
}
|
2022-12-04 03:19:10 +00:00
|
|
|
|
2022-12-19 07:01:38 +00:00
|
|
|
/// This behavior is also implemented in `rustc_ty_utils` and in the old `project` code.
|
|
|
|
|
///
|
|
|
|
|
/// FIXME: We should merge these 3 implementations as it's likely that they otherwise
|
|
|
|
|
/// diverge.
|
|
|
|
|
#[instrument(level = "debug", skip(infcx, param_env), ret)]
|
|
|
|
|
fn fetch_eligible_assoc_item_def<'tcx>(
|
|
|
|
|
infcx: &InferCtxt<'tcx>,
|
|
|
|
|
param_env: ty::ParamEnv<'tcx>,
|
|
|
|
|
goal_trait_ref: ty::TraitRef<'tcx>,
|
|
|
|
|
trait_assoc_def_id: DefId,
|
|
|
|
|
impl_def_id: DefId,
|
|
|
|
|
) -> Option<LeafDef> {
|
|
|
|
|
let node_item = specialization_graph::assoc_def(infcx.tcx, impl_def_id, trait_assoc_def_id)
|
|
|
|
|
.map_err(|ErrorGuaranteed { .. }| ())
|
|
|
|
|
.ok()?;
|
|
|
|
|
|
|
|
|
|
let eligible = if node_item.is_final() {
|
|
|
|
|
// Non-specializable items are always projectable.
|
|
|
|
|
true
|
|
|
|
|
} else {
|
|
|
|
|
// Only reveal a specializable default if we're past type-checking
|
|
|
|
|
// and the obligation is monomorphic, otherwise passes such as
|
|
|
|
|
// transmute checking and polymorphic MIR optimizations could
|
|
|
|
|
// get a result which isn't correct for all monomorphizations.
|
|
|
|
|
if param_env.reveal() == Reveal::All {
|
|
|
|
|
let poly_trait_ref = infcx.resolve_vars_if_possible(goal_trait_ref);
|
|
|
|
|
!poly_trait_ref.still_further_specializable()
|
2022-12-04 03:19:10 +00:00
|
|
|
} else {
|
2022-12-19 07:01:38 +00:00
|
|
|
debug!(?node_item.item.def_id, "not eligible due to default");
|
|
|
|
|
false
|
|
|
|
|
}
|
|
|
|
|
};
|
2022-12-04 03:19:10 +00:00
|
|
|
|
2022-12-19 07:01:38 +00:00
|
|
|
if eligible { Some(node_item) } else { None }
|
2022-12-04 03:19:10 +00:00
|
|
|
}
|