2020-05-25 22:33:21 +02:00
|
|
|
//! Confirmation.
|
|
|
|
//!
|
|
|
|
//! Confirmation unifies the output type parameters of the trait
|
|
|
|
//! with the values found in the obligation, possibly yielding a
|
|
|
|
//! type error. See the [rustc dev guide] for more details.
|
|
|
|
//!
|
|
|
|
//! [rustc dev guide]:
|
|
|
|
//! https://rustc-dev-guide.rust-lang.org/traits/resolution.html#confirmation
|
|
|
|
use rustc_data_structures::stack::ensure_sufficient_stack;
|
2020-08-18 11:47:27 +01:00
|
|
|
use rustc_hir::lang_items::LangItem;
|
2020-05-25 22:33:21 +02:00
|
|
|
use rustc_index::bit_set::GrowableBitSet;
|
2020-07-24 21:59:43 +01:00
|
|
|
use rustc_infer::infer::InferOk;
|
2020-07-05 12:16:25 +01:00
|
|
|
use rustc_infer::infer::LateBoundRegionConversionTime::HigherRankedType;
|
2020-05-25 22:33:21 +02:00
|
|
|
use rustc_middle::ty::subst::{GenericArg, GenericArgKind, Subst, SubstsRef};
|
2020-07-24 21:59:43 +01:00
|
|
|
use rustc_middle::ty::{self, Ty};
|
2021-12-12 12:34:46 +08:00
|
|
|
use rustc_middle::ty::{ToPolyTraitRef, ToPredicate};
|
2020-05-25 22:33:21 +02:00
|
|
|
use rustc_span::def_id::DefId;
|
|
|
|
|
2020-07-05 12:16:25 +01:00
|
|
|
use crate::traits::project::{normalize_with_depth, normalize_with_depth_to};
|
2020-05-25 22:33:21 +02:00
|
|
|
use crate::traits::select::TraitObligationExt;
|
2021-10-13 13:58:41 +00:00
|
|
|
use crate::traits::util::{self, closure_trait_ref_and_return_type, predicate_for_trait_def};
|
2020-05-25 22:33:21 +02:00
|
|
|
use crate::traits::{
|
2021-10-13 13:58:41 +00:00
|
|
|
BuiltinDerivedObligation, DerivedObligationCause, ImplDerivedObligation,
|
|
|
|
ImplDerivedObligationCause, ImplSource, ImplSourceAutoImplData, ImplSourceBuiltinData,
|
|
|
|
ImplSourceClosureData, ImplSourceConstDestructData, ImplSourceDiscriminantKindData,
|
|
|
|
ImplSourceFnPointerData, ImplSourceGeneratorData, ImplSourceObjectData, ImplSourcePointeeData,
|
|
|
|
ImplSourceTraitAliasData, ImplSourceTraitUpcastingData, ImplSourceUserDefinedData, Normalized,
|
|
|
|
ObjectCastObligation, Obligation, ObligationCause, OutputTypeParameterMismatch,
|
|
|
|
PredicateObligation, Selection, SelectionError, TraitNotObjectSafe, TraitObligation,
|
|
|
|
Unimplemented, VtblSegment,
|
2020-05-25 22:33:21 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
use super::BuiltinImplConditions;
|
|
|
|
use super::SelectionCandidate::{self, *};
|
|
|
|
use super::SelectionContext;
|
|
|
|
|
|
|
|
use std::iter;
|
2021-08-18 12:45:18 +08:00
|
|
|
use std::ops::ControlFlow;
|
2020-05-25 22:33:21 +02:00
|
|
|
|
|
|
|
impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
|
2020-10-11 11:37:56 +01:00
|
|
|
#[instrument(level = "debug", skip(self))]
|
2020-05-25 22:33:21 +02:00
|
|
|
pub(super) fn confirm_candidate(
|
|
|
|
&mut self,
|
|
|
|
obligation: &TraitObligation<'tcx>,
|
|
|
|
candidate: SelectionCandidate<'tcx>,
|
|
|
|
) -> Result<Selection<'tcx>, SelectionError<'tcx>> {
|
2021-12-25 00:33:23 +08:00
|
|
|
let mut obligation = obligation;
|
|
|
|
let new_obligation;
|
|
|
|
|
|
|
|
// HACK(const_trait_impl): the surrounding environment is remapped to a non-const context
|
|
|
|
// because nested obligations might be actually `~const` then (incorrectly) requiring
|
|
|
|
// const impls. for example:
|
|
|
|
// ```
|
|
|
|
// pub trait Super {}
|
|
|
|
// pub trait Sub: Super {}
|
|
|
|
//
|
|
|
|
// impl<A> const Super for &A where A: ~const Super {}
|
|
|
|
// impl<A> const Sub for &A where A: ~const Sub {}
|
|
|
|
// ```
|
|
|
|
//
|
|
|
|
// The procedure to check the code above without the remapping code is as follows:
|
|
|
|
// ```
|
|
|
|
// CheckWf(impl const Sub for &A where A: ~const Sub) // <- const env
|
|
|
|
// CheckPredicate(&A: Super)
|
|
|
|
// CheckPredicate(A: ~const Super) // <- still const env, failure
|
|
|
|
// ```
|
2022-01-26 19:24:01 -08:00
|
|
|
if obligation.param_env.is_const() && !obligation.predicate.is_const_if_const() {
|
2021-12-25 00:33:23 +08:00
|
|
|
new_obligation = TraitObligation {
|
|
|
|
cause: obligation.cause.clone(),
|
|
|
|
param_env: obligation.param_env.without_const(),
|
|
|
|
..*obligation
|
|
|
|
};
|
|
|
|
obligation = &new_obligation;
|
|
|
|
}
|
|
|
|
|
2020-05-25 22:33:21 +02:00
|
|
|
match candidate {
|
|
|
|
BuiltinCandidate { has_nested } => {
|
|
|
|
let data = self.confirm_builtin_candidate(obligation, has_nested);
|
2020-09-24 19:22:36 +02:00
|
|
|
Ok(ImplSource::Builtin(data))
|
2020-05-25 22:33:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ParamCandidate(param) => {
|
2021-12-12 12:34:46 +08:00
|
|
|
let obligations =
|
|
|
|
self.confirm_param_candidate(obligation, param.map_bound(|t| t.trait_ref));
|
|
|
|
Ok(ImplSource::Param(obligations, param.skip_binder().constness))
|
2020-05-25 22:33:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ImplCandidate(impl_def_id) => {
|
2020-09-24 19:22:36 +02:00
|
|
|
Ok(ImplSource::UserDefined(self.confirm_impl_candidate(obligation, impl_def_id)))
|
2020-05-25 22:33:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
AutoImplCandidate(trait_def_id) => {
|
|
|
|
let data = self.confirm_auto_impl_candidate(obligation, trait_def_id);
|
2020-09-24 19:22:36 +02:00
|
|
|
Ok(ImplSource::AutoImpl(data))
|
2020-05-25 22:33:21 +02:00
|
|
|
}
|
|
|
|
|
2020-07-23 21:59:20 +01:00
|
|
|
ProjectionCandidate(idx) => {
|
2020-10-08 21:49:36 +01:00
|
|
|
let obligations = self.confirm_projection_candidate(obligation, idx)?;
|
2020-11-22 02:13:53 +01:00
|
|
|
// FIXME(jschievink): constness
|
2021-08-27 05:02:23 +00:00
|
|
|
Ok(ImplSource::Param(obligations, ty::BoundConstness::NotConst))
|
2020-05-25 22:33:21 +02:00
|
|
|
}
|
|
|
|
|
2020-10-08 21:52:40 +01:00
|
|
|
ObjectCandidate(idx) => {
|
|
|
|
let data = self.confirm_object_candidate(obligation, idx)?;
|
|
|
|
Ok(ImplSource::Object(data))
|
|
|
|
}
|
|
|
|
|
2020-05-25 22:33:21 +02:00
|
|
|
ClosureCandidate => {
|
|
|
|
let vtable_closure = self.confirm_closure_candidate(obligation)?;
|
2020-09-24 19:22:36 +02:00
|
|
|
Ok(ImplSource::Closure(vtable_closure))
|
2020-05-25 22:33:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
GeneratorCandidate => {
|
|
|
|
let vtable_generator = self.confirm_generator_candidate(obligation)?;
|
2020-09-24 19:22:36 +02:00
|
|
|
Ok(ImplSource::Generator(vtable_generator))
|
2020-05-25 22:33:21 +02:00
|
|
|
}
|
|
|
|
|
2021-09-15 11:28:10 +00:00
|
|
|
FnPointerCandidate { .. } => {
|
2020-05-25 22:33:21 +02:00
|
|
|
let data = self.confirm_fn_pointer_candidate(obligation)?;
|
2020-09-24 19:22:36 +02:00
|
|
|
Ok(ImplSource::FnPointer(data))
|
2020-05-25 22:33:21 +02:00
|
|
|
}
|
|
|
|
|
2020-05-11 15:25:33 +00:00
|
|
|
DiscriminantKindCandidate => {
|
2020-09-24 19:22:36 +02:00
|
|
|
Ok(ImplSource::DiscriminantKind(ImplSourceDiscriminantKindData))
|
2020-05-11 15:25:33 +00:00
|
|
|
}
|
2020-05-25 22:33:21 +02:00
|
|
|
|
2020-10-19 15:38:11 +02:00
|
|
|
PointeeCandidate => Ok(ImplSource::Pointee(ImplSourcePointeeData)),
|
|
|
|
|
2020-05-25 22:33:21 +02:00
|
|
|
TraitAliasCandidate(alias_def_id) => {
|
|
|
|
let data = self.confirm_trait_alias_candidate(obligation, alias_def_id);
|
2020-09-24 19:22:36 +02:00
|
|
|
Ok(ImplSource::TraitAlias(data))
|
2020-05-25 22:33:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
BuiltinObjectCandidate => {
|
|
|
|
// This indicates something like `Trait + Send: Send`. In this case, we know that
|
|
|
|
// this holds because that's what the object type is telling us, and there's really
|
|
|
|
// no additional obligations to prove and no types in particular to unify, etc.
|
2021-08-27 05:02:23 +00:00
|
|
|
Ok(ImplSource::Param(Vec::new(), ty::BoundConstness::NotConst))
|
2020-05-25 22:33:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
BuiltinUnsizeCandidate => {
|
|
|
|
let data = self.confirm_builtin_unsize_candidate(obligation)?;
|
2020-09-24 19:22:36 +02:00
|
|
|
Ok(ImplSource::Builtin(data))
|
2020-05-25 22:33:21 +02:00
|
|
|
}
|
2021-08-18 02:41:29 +08:00
|
|
|
|
|
|
|
TraitUpcastingUnsizeCandidate(idx) => {
|
|
|
|
let data = self.confirm_trait_upcasting_unsize_candidate(obligation, idx)?;
|
2021-08-18 12:45:18 +08:00
|
|
|
Ok(ImplSource::TraitUpcasting(data))
|
2021-08-18 02:41:29 +08:00
|
|
|
}
|
2021-09-01 16:34:28 +00:00
|
|
|
|
2022-03-21 16:52:41 +11:00
|
|
|
ConstDestructCandidate(def_id) => {
|
|
|
|
let data = self.confirm_const_destruct_candidate(obligation, def_id)?;
|
|
|
|
Ok(ImplSource::ConstDestruct(data))
|
2022-01-18 01:43:49 -08:00
|
|
|
}
|
2020-05-25 22:33:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-28 12:41:46 +01:00
|
|
|
fn confirm_projection_candidate(
|
|
|
|
&mut self,
|
|
|
|
obligation: &TraitObligation<'tcx>,
|
2020-07-23 21:59:20 +01:00
|
|
|
idx: usize,
|
2020-10-08 21:49:36 +01:00
|
|
|
) -> Result<Vec<PredicateObligation<'tcx>>, SelectionError<'tcx>> {
|
2020-05-22 17:48:07 +00:00
|
|
|
self.infcx.commit_unconditionally(|_| {
|
2020-07-23 21:59:20 +01:00
|
|
|
let tcx = self.tcx();
|
|
|
|
|
2020-09-07 10:01:45 +01:00
|
|
|
let trait_predicate = self.infcx.shallow_resolve(obligation.predicate);
|
|
|
|
let placeholder_trait_predicate =
|
2021-12-12 12:34:46 +08:00
|
|
|
self.infcx().replace_bound_vars_with_placeholders(trait_predicate).trait_ref;
|
2020-09-07 10:01:45 +01:00
|
|
|
let placeholder_self_ty = placeholder_trait_predicate.self_ty();
|
2021-09-15 22:55:10 -04:00
|
|
|
let placeholder_trait_predicate = ty::Binder::dummy(placeholder_trait_predicate);
|
2020-09-07 10:01:45 +01:00
|
|
|
let (def_id, substs) = match *placeholder_self_ty.kind() {
|
2020-07-23 21:59:20 +01:00
|
|
|
ty::Projection(proj) => (proj.item_def_id, proj.substs),
|
|
|
|
ty::Opaque(def_id, substs) => (def_id, substs),
|
2020-09-07 10:01:45 +01:00
|
|
|
_ => bug!("projection candidate for unexpected type: {:?}", placeholder_self_ty),
|
2020-07-23 21:59:20 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
let candidate_predicate = tcx.item_bounds(def_id)[idx].subst(tcx, substs);
|
|
|
|
let candidate = candidate_predicate
|
2021-12-12 12:34:46 +08:00
|
|
|
.to_opt_poly_trait_pred()
|
|
|
|
.expect("projection candidate is not a trait predicate")
|
|
|
|
.map_bound(|t| t.trait_ref);
|
2020-09-07 10:01:45 +01:00
|
|
|
let mut obligations = Vec::new();
|
|
|
|
let candidate = normalize_with_depth_to(
|
2020-07-24 19:10:22 +01:00
|
|
|
self,
|
|
|
|
obligation.param_env,
|
|
|
|
obligation.cause.clone(),
|
|
|
|
obligation.recursion_depth + 1,
|
2020-10-24 02:21:18 +02:00
|
|
|
candidate,
|
2020-09-07 10:01:45 +01:00
|
|
|
&mut obligations,
|
2020-07-24 19:10:22 +01:00
|
|
|
);
|
|
|
|
|
2020-10-08 21:49:36 +01:00
|
|
|
obligations.extend(self.infcx.commit_if_ok(|_| {
|
2020-07-24 19:10:22 +01:00
|
|
|
self.infcx
|
|
|
|
.at(&obligation.cause, obligation.param_env)
|
2021-12-12 12:34:46 +08:00
|
|
|
.sup(placeholder_trait_predicate, candidate)
|
2020-07-24 19:10:22 +01:00
|
|
|
.map(|InferOk { obligations, .. }| obligations)
|
2020-10-08 21:49:36 +01:00
|
|
|
.map_err(|_| Unimplemented)
|
|
|
|
})?);
|
2020-07-25 13:41:53 +01:00
|
|
|
|
2020-09-07 10:01:45 +01:00
|
|
|
if let ty::Projection(..) = placeholder_self_ty.kind() {
|
2022-01-09 23:53:57 -05:00
|
|
|
let predicates = tcx.predicates_of(def_id).instantiate_own(tcx, substs).predicates;
|
|
|
|
debug!(?predicates, "projection predicates");
|
|
|
|
for predicate in predicates {
|
2020-07-25 13:41:53 +01:00
|
|
|
let normalized = normalize_with_depth_to(
|
|
|
|
self,
|
|
|
|
obligation.param_env,
|
|
|
|
obligation.cause.clone(),
|
|
|
|
obligation.recursion_depth + 1,
|
2020-10-24 02:21:18 +02:00
|
|
|
predicate,
|
2020-07-25 13:41:53 +01:00
|
|
|
&mut obligations,
|
|
|
|
);
|
|
|
|
obligations.push(Obligation::with_depth(
|
|
|
|
obligation.cause.clone(),
|
|
|
|
obligation.recursion_depth + 1,
|
|
|
|
obligation.param_env,
|
|
|
|
normalized,
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-08 21:49:36 +01:00
|
|
|
Ok(obligations)
|
2020-05-25 22:33:21 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
fn confirm_param_candidate(
|
|
|
|
&mut self,
|
|
|
|
obligation: &TraitObligation<'tcx>,
|
|
|
|
param: ty::PolyTraitRef<'tcx>,
|
|
|
|
) -> Vec<PredicateObligation<'tcx>> {
|
2020-10-11 11:37:56 +01:00
|
|
|
debug!(?obligation, ?param, "confirm_param_candidate");
|
2020-05-25 22:33:21 +02:00
|
|
|
|
|
|
|
// During evaluation, we already checked that this
|
|
|
|
// where-clause trait-ref could be unified with the obligation
|
|
|
|
// trait-ref. Repeat that unification now without any
|
|
|
|
// transactional boundary; it should not fail.
|
|
|
|
match self.match_where_clause_trait_ref(obligation, param) {
|
|
|
|
Ok(obligations) => obligations,
|
|
|
|
Err(()) => {
|
|
|
|
bug!(
|
|
|
|
"Where clause `{:?}` was applicable to `{:?}` but now is not",
|
|
|
|
param,
|
|
|
|
obligation
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn confirm_builtin_candidate(
|
|
|
|
&mut self,
|
|
|
|
obligation: &TraitObligation<'tcx>,
|
|
|
|
has_nested: bool,
|
2020-05-11 15:25:33 +00:00
|
|
|
) -> ImplSourceBuiltinData<PredicateObligation<'tcx>> {
|
2020-10-11 11:37:56 +01:00
|
|
|
debug!(?obligation, ?has_nested, "confirm_builtin_candidate");
|
2020-05-25 22:33:21 +02:00
|
|
|
|
|
|
|
let lang_items = self.tcx().lang_items();
|
|
|
|
let obligations = if has_nested {
|
|
|
|
let trait_def = obligation.predicate.def_id();
|
|
|
|
let conditions = if Some(trait_def) == lang_items.sized_trait() {
|
|
|
|
self.sized_conditions(obligation)
|
|
|
|
} else if Some(trait_def) == lang_items.copy_trait() {
|
|
|
|
self.copy_clone_conditions(obligation)
|
|
|
|
} else if Some(trait_def) == lang_items.clone_trait() {
|
|
|
|
self.copy_clone_conditions(obligation)
|
|
|
|
} else {
|
|
|
|
bug!("unexpected builtin trait {:?}", trait_def)
|
|
|
|
};
|
2022-02-19 00:48:31 +01:00
|
|
|
let BuiltinImplConditions::Where(nested) = conditions else {
|
|
|
|
bug!("obligation {:?} had matched a builtin impl but now doesn't", obligation);
|
2020-05-25 22:33:21 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
let cause = obligation.derived_cause(BuiltinDerivedObligation);
|
|
|
|
ensure_sufficient_stack(|| {
|
|
|
|
self.collect_predicates_for_types(
|
|
|
|
obligation.param_env,
|
|
|
|
cause,
|
|
|
|
obligation.recursion_depth + 1,
|
|
|
|
trait_def,
|
|
|
|
nested,
|
|
|
|
)
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
vec![]
|
|
|
|
};
|
|
|
|
|
2020-10-11 11:37:56 +01:00
|
|
|
debug!(?obligations);
|
2020-05-25 22:33:21 +02:00
|
|
|
|
2020-05-11 15:25:33 +00:00
|
|
|
ImplSourceBuiltinData { nested: obligations }
|
2020-05-25 22:33:21 +02:00
|
|
|
}
|
|
|
|
|
2021-08-22 14:46:15 +02:00
|
|
|
/// This handles the case where an `auto trait Foo` impl is being used.
|
2020-05-25 22:33:21 +02:00
|
|
|
/// The idea is that the impl applies to `X : Foo` if the following conditions are met:
|
|
|
|
///
|
|
|
|
/// 1. For each constituent type `Y` in `X`, `Y : Foo` holds
|
|
|
|
/// 2. For each where-clause `C` declared on `Foo`, `[Self => X] C` holds.
|
|
|
|
fn confirm_auto_impl_candidate(
|
|
|
|
&mut self,
|
|
|
|
obligation: &TraitObligation<'tcx>,
|
|
|
|
trait_def_id: DefId,
|
2020-05-11 15:25:33 +00:00
|
|
|
) -> ImplSourceAutoImplData<PredicateObligation<'tcx>> {
|
2020-10-11 11:37:56 +01:00
|
|
|
debug!(?obligation, ?trait_def_id, "confirm_auto_impl_candidate");
|
2020-05-25 22:33:21 +02:00
|
|
|
|
2020-12-16 22:36:14 -05:00
|
|
|
let self_ty = self.infcx.shallow_resolve(obligation.predicate.self_ty());
|
|
|
|
let types = self.constituent_types_for_ty(self_ty);
|
2020-05-25 22:33:21 +02:00
|
|
|
self.vtable_auto_impl(obligation, trait_def_id, types)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// See `confirm_auto_impl_candidate`.
|
|
|
|
fn vtable_auto_impl(
|
|
|
|
&mut self,
|
|
|
|
obligation: &TraitObligation<'tcx>,
|
|
|
|
trait_def_id: DefId,
|
2020-10-05 16:51:33 -04:00
|
|
|
nested: ty::Binder<'tcx, Vec<Ty<'tcx>>>,
|
2020-05-11 15:25:33 +00:00
|
|
|
) -> ImplSourceAutoImplData<PredicateObligation<'tcx>> {
|
2020-10-11 11:37:56 +01:00
|
|
|
debug!(?nested, "vtable_auto_impl");
|
2020-05-25 22:33:21 +02:00
|
|
|
ensure_sufficient_stack(|| {
|
|
|
|
let cause = obligation.derived_cause(BuiltinDerivedObligation);
|
|
|
|
|
|
|
|
let trait_obligations: Vec<PredicateObligation<'_>> =
|
|
|
|
self.infcx.commit_unconditionally(|_| {
|
|
|
|
let poly_trait_ref = obligation.predicate.to_poly_trait_ref();
|
2020-10-24 02:21:18 +02:00
|
|
|
let trait_ref = self.infcx.replace_bound_vars_with_placeholders(poly_trait_ref);
|
2020-05-25 22:33:21 +02:00
|
|
|
self.impl_or_trait_obligations(
|
2021-10-13 13:58:41 +00:00
|
|
|
&cause,
|
2020-05-25 22:33:21 +02:00
|
|
|
obligation.recursion_depth + 1,
|
|
|
|
obligation.param_env,
|
|
|
|
trait_def_id,
|
|
|
|
&trait_ref.substs,
|
2021-10-13 13:58:41 +00:00
|
|
|
obligation.predicate,
|
2020-05-25 22:33:21 +02:00
|
|
|
)
|
|
|
|
});
|
|
|
|
|
2021-10-13 13:58:41 +00:00
|
|
|
let mut obligations = self.collect_predicates_for_types(
|
|
|
|
obligation.param_env,
|
|
|
|
cause,
|
|
|
|
obligation.recursion_depth + 1,
|
|
|
|
trait_def_id,
|
|
|
|
nested,
|
|
|
|
);
|
|
|
|
|
2020-05-25 22:33:21 +02:00
|
|
|
// Adds the predicates from the trait. Note that this contains a `Self: Trait`
|
|
|
|
// predicate as usual. It won't have any effect since auto traits are coinductive.
|
|
|
|
obligations.extend(trait_obligations);
|
|
|
|
|
2020-10-11 11:37:56 +01:00
|
|
|
debug!(?obligations, "vtable_auto_impl");
|
2020-05-25 22:33:21 +02:00
|
|
|
|
2020-05-11 15:25:33 +00:00
|
|
|
ImplSourceAutoImplData { trait_def_id, nested: obligations }
|
2020-05-25 22:33:21 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
fn confirm_impl_candidate(
|
|
|
|
&mut self,
|
|
|
|
obligation: &TraitObligation<'tcx>,
|
|
|
|
impl_def_id: DefId,
|
2020-06-02 15:54:24 +00:00
|
|
|
) -> ImplSourceUserDefinedData<'tcx, PredicateObligation<'tcx>> {
|
2020-10-11 11:37:56 +01:00
|
|
|
debug!(?obligation, ?impl_def_id, "confirm_impl_candidate");
|
2020-05-25 22:33:21 +02:00
|
|
|
|
|
|
|
// First, create the substitutions by matching the impl again,
|
|
|
|
// this time not in a probe.
|
2020-05-22 17:48:07 +00:00
|
|
|
self.infcx.commit_unconditionally(|_| {
|
|
|
|
let substs = self.rematch_impl(impl_def_id, obligation);
|
2020-10-11 11:37:56 +01:00
|
|
|
debug!(?substs, "impl substs");
|
2020-05-25 22:33:21 +02:00
|
|
|
ensure_sufficient_stack(|| {
|
|
|
|
self.vtable_impl(
|
|
|
|
impl_def_id,
|
|
|
|
substs,
|
2021-10-13 13:58:41 +00:00
|
|
|
&obligation.cause,
|
2020-05-25 22:33:21 +02:00
|
|
|
obligation.recursion_depth + 1,
|
|
|
|
obligation.param_env,
|
2021-10-13 13:58:41 +00:00
|
|
|
obligation.predicate,
|
2020-05-25 22:33:21 +02:00
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
fn vtable_impl(
|
|
|
|
&mut self,
|
|
|
|
impl_def_id: DefId,
|
2020-12-17 20:16:10 +00:00
|
|
|
substs: Normalized<'tcx, SubstsRef<'tcx>>,
|
2021-10-13 13:58:41 +00:00
|
|
|
cause: &ObligationCause<'tcx>,
|
2020-05-25 22:33:21 +02:00
|
|
|
recursion_depth: usize,
|
|
|
|
param_env: ty::ParamEnv<'tcx>,
|
2021-10-13 13:58:41 +00:00
|
|
|
parent_trait_pred: ty::Binder<'tcx, ty::TraitPredicate<'tcx>>,
|
2020-06-02 15:54:24 +00:00
|
|
|
) -> ImplSourceUserDefinedData<'tcx, PredicateObligation<'tcx>> {
|
2020-10-11 11:37:56 +01:00
|
|
|
debug!(?impl_def_id, ?substs, ?recursion_depth, "vtable_impl");
|
2020-05-25 22:33:21 +02:00
|
|
|
|
|
|
|
let mut impl_obligations = self.impl_or_trait_obligations(
|
|
|
|
cause,
|
|
|
|
recursion_depth,
|
|
|
|
param_env,
|
|
|
|
impl_def_id,
|
|
|
|
&substs.value,
|
2021-10-13 13:58:41 +00:00
|
|
|
parent_trait_pred,
|
2020-05-25 22:33:21 +02:00
|
|
|
);
|
|
|
|
|
2020-10-11 11:37:56 +01:00
|
|
|
debug!(?impl_obligations, "vtable_impl");
|
2020-05-25 22:33:21 +02:00
|
|
|
|
|
|
|
// Because of RFC447, the impl-trait-ref and obligations
|
|
|
|
// are sufficient to determine the impl substs, without
|
|
|
|
// relying on projections in the impl-trait-ref.
|
|
|
|
//
|
|
|
|
// e.g., `impl<U: Tr, V: Iterator<Item=U>> Foo<<U as Tr>::T> for V`
|
2020-12-17 20:16:10 +00:00
|
|
|
impl_obligations.extend(substs.obligations);
|
2020-05-25 22:33:21 +02:00
|
|
|
|
2020-12-17 20:16:10 +00:00
|
|
|
ImplSourceUserDefinedData { impl_def_id, substs: substs.value, nested: impl_obligations }
|
2020-05-25 22:33:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn confirm_object_candidate(
|
|
|
|
&mut self,
|
|
|
|
obligation: &TraitObligation<'tcx>,
|
2020-10-08 21:52:40 +01:00
|
|
|
index: usize,
|
|
|
|
) -> Result<ImplSourceObjectData<'tcx, PredicateObligation<'tcx>>, SelectionError<'tcx>> {
|
2020-07-05 12:16:25 +01:00
|
|
|
let tcx = self.tcx();
|
2020-10-08 21:52:40 +01:00
|
|
|
debug!(?obligation, ?index, "confirm_object_candidate");
|
2020-05-25 22:33:21 +02:00
|
|
|
|
2020-10-24 02:21:18 +02:00
|
|
|
let trait_predicate = self.infcx.replace_bound_vars_with_placeholders(obligation.predicate);
|
2020-07-05 12:16:25 +01:00
|
|
|
let self_ty = self.infcx.shallow_resolve(trait_predicate.self_ty());
|
|
|
|
let obligation_trait_ref = ty::Binder::dummy(trait_predicate.trait_ref);
|
2022-02-19 00:48:31 +01:00
|
|
|
let ty::Dynamic(data, ..) = *self_ty.kind() else {
|
|
|
|
span_bug!(obligation.cause.span, "object candidate with non-object");
|
2020-05-25 22:33:21 +02:00
|
|
|
};
|
|
|
|
|
2020-12-11 15:02:46 -05:00
|
|
|
let object_trait_ref = data.principal().unwrap_or_else(|| {
|
|
|
|
span_bug!(obligation.cause.span, "object candidate with no principal")
|
|
|
|
});
|
|
|
|
let object_trait_ref = self
|
|
|
|
.infcx
|
|
|
|
.replace_bound_vars_with_fresh_vars(
|
|
|
|
obligation.cause.span,
|
|
|
|
HigherRankedType,
|
|
|
|
object_trait_ref,
|
|
|
|
)
|
|
|
|
.0;
|
|
|
|
let object_trait_ref = object_trait_ref.with_self_ty(self.tcx(), self_ty);
|
2020-06-28 20:34:56 +01:00
|
|
|
|
2020-05-25 22:33:21 +02:00
|
|
|
let mut nested = vec![];
|
|
|
|
|
2020-10-08 21:52:40 +01:00
|
|
|
let mut supertraits = util::supertraits(tcx, ty::Binder::dummy(object_trait_ref));
|
|
|
|
let unnormalized_upcast_trait_ref =
|
2021-06-14 18:02:53 +08:00
|
|
|
supertraits.nth(index).expect("supertraits iterator no longer has as many elements");
|
2020-10-08 21:52:40 +01:00
|
|
|
|
|
|
|
let upcast_trait_ref = normalize_with_depth_to(
|
|
|
|
self,
|
|
|
|
obligation.param_env,
|
|
|
|
obligation.cause.clone(),
|
|
|
|
obligation.recursion_depth + 1,
|
2020-10-24 02:21:18 +02:00
|
|
|
unnormalized_upcast_trait_ref,
|
2020-10-08 21:52:40 +01:00
|
|
|
&mut nested,
|
|
|
|
);
|
2020-05-25 22:33:21 +02:00
|
|
|
|
2020-10-08 21:52:40 +01:00
|
|
|
nested.extend(self.infcx.commit_if_ok(|_| {
|
|
|
|
self.infcx
|
|
|
|
.at(&obligation.cause, obligation.param_env)
|
|
|
|
.sup(obligation_trait_ref, upcast_trait_ref)
|
|
|
|
.map(|InferOk { obligations, .. }| obligations)
|
|
|
|
.map_err(|_| Unimplemented)
|
|
|
|
})?);
|
2020-07-05 12:16:25 +01:00
|
|
|
|
2020-08-15 12:29:23 +01:00
|
|
|
// Check supertraits hold. This is so that their associated type bounds
|
|
|
|
// will be checked in the code below.
|
2020-08-14 21:51:28 +01:00
|
|
|
for super_trait in tcx
|
|
|
|
.super_predicates_of(trait_predicate.def_id())
|
|
|
|
.instantiate(tcx, trait_predicate.trait_ref.substs)
|
|
|
|
.predicates
|
|
|
|
.into_iter()
|
|
|
|
{
|
2022-01-31 19:11:23 -08:00
|
|
|
let normalized_super_trait = normalize_with_depth_to(
|
|
|
|
self,
|
|
|
|
obligation.param_env,
|
|
|
|
obligation.cause.clone(),
|
|
|
|
obligation.recursion_depth + 1,
|
|
|
|
super_trait,
|
|
|
|
&mut nested,
|
|
|
|
);
|
|
|
|
nested.push(Obligation::new(
|
|
|
|
obligation.cause.clone(),
|
|
|
|
obligation.param_env,
|
|
|
|
normalized_super_trait,
|
|
|
|
));
|
2020-08-14 21:51:28 +01:00
|
|
|
}
|
2020-07-24 21:59:43 +01:00
|
|
|
|
2020-07-05 12:16:25 +01:00
|
|
|
let assoc_types: Vec<_> = tcx
|
2020-07-24 21:59:43 +01:00
|
|
|
.associated_items(trait_predicate.def_id())
|
2020-07-05 12:16:25 +01:00
|
|
|
.in_definition_order()
|
|
|
|
.filter_map(
|
|
|
|
|item| if item.kind == ty::AssocKind::Type { Some(item.def_id) } else { None },
|
|
|
|
)
|
|
|
|
.collect();
|
|
|
|
|
2020-07-24 21:59:43 +01:00
|
|
|
for assoc_type in assoc_types {
|
|
|
|
if !tcx.generics_of(assoc_type).params.is_empty() {
|
2021-04-27 14:34:23 -04:00
|
|
|
tcx.sess.delay_span_bug(
|
2020-07-24 21:59:43 +01:00
|
|
|
obligation.cause.span,
|
2021-04-27 14:34:23 -04:00
|
|
|
"GATs in trait object shouldn't have been considered",
|
2020-07-24 21:59:43 +01:00
|
|
|
);
|
2021-04-27 14:34:23 -04:00
|
|
|
return Err(SelectionError::Unimplemented);
|
2020-07-24 21:59:43 +01:00
|
|
|
}
|
|
|
|
// This maybe belongs in wf, but that can't (doesn't) handle
|
|
|
|
// higher-ranked things.
|
|
|
|
// Prevent, e.g., `dyn Iterator<Item = str>`.
|
|
|
|
for bound in self.tcx().item_bounds(assoc_type) {
|
|
|
|
let subst_bound = bound.subst(tcx, trait_predicate.trait_ref.substs);
|
|
|
|
let normalized_bound = normalize_with_depth_to(
|
|
|
|
self,
|
|
|
|
obligation.param_env,
|
|
|
|
obligation.cause.clone(),
|
|
|
|
obligation.recursion_depth + 1,
|
2020-10-24 02:21:18 +02:00
|
|
|
subst_bound,
|
2020-07-24 21:59:43 +01:00
|
|
|
&mut nested,
|
|
|
|
);
|
|
|
|
nested.push(Obligation::new(
|
|
|
|
obligation.cause.clone(),
|
2020-12-11 17:49:00 +01:00
|
|
|
obligation.param_env,
|
2020-07-24 21:59:43 +01:00
|
|
|
normalized_bound,
|
|
|
|
));
|
2020-06-28 20:34:56 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-11 11:37:56 +01:00
|
|
|
debug!(?nested, "object nested obligations");
|
2021-06-14 18:02:53 +08:00
|
|
|
|
|
|
|
let vtable_base = super::super::vtable_trait_first_method_offset(
|
|
|
|
tcx,
|
|
|
|
(unnormalized_upcast_trait_ref, ty::Binder::dummy(object_trait_ref)),
|
|
|
|
);
|
|
|
|
|
2020-10-08 21:52:40 +01:00
|
|
|
Ok(ImplSourceObjectData { upcast_trait_ref, vtable_base, nested })
|
2020-05-25 22:33:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn confirm_fn_pointer_candidate(
|
|
|
|
&mut self,
|
|
|
|
obligation: &TraitObligation<'tcx>,
|
2020-05-11 15:25:33 +00:00
|
|
|
) -> Result<ImplSourceFnPointerData<'tcx, PredicateObligation<'tcx>>, SelectionError<'tcx>>
|
|
|
|
{
|
2020-10-11 11:37:56 +01:00
|
|
|
debug!(?obligation, "confirm_fn_pointer_candidate");
|
2020-05-25 22:33:21 +02:00
|
|
|
|
|
|
|
// Okay to skip binder; it is reintroduced below.
|
2020-06-24 23:40:33 +02:00
|
|
|
let self_ty = self.infcx.shallow_resolve(obligation.self_ty().skip_binder());
|
2020-05-25 22:33:21 +02:00
|
|
|
let sig = self_ty.fn_sig(self.tcx());
|
|
|
|
let trait_ref = closure_trait_ref_and_return_type(
|
|
|
|
self.tcx(),
|
|
|
|
obligation.predicate.def_id(),
|
|
|
|
self_ty,
|
|
|
|
sig,
|
|
|
|
util::TupleArgumentsFlag::Yes,
|
|
|
|
)
|
|
|
|
.map_bound(|(trait_ref, _)| trait_ref);
|
|
|
|
|
2022-01-26 21:47:03 -08:00
|
|
|
let nested = self.confirm_poly_trait_refs(obligation, trait_ref)?;
|
|
|
|
Ok(ImplSourceFnPointerData { fn_ty: self_ty, nested })
|
2020-05-25 22:33:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn confirm_trait_alias_candidate(
|
|
|
|
&mut self,
|
|
|
|
obligation: &TraitObligation<'tcx>,
|
|
|
|
alias_def_id: DefId,
|
2020-05-11 15:25:33 +00:00
|
|
|
) -> ImplSourceTraitAliasData<'tcx, PredicateObligation<'tcx>> {
|
2020-10-11 11:37:56 +01:00
|
|
|
debug!(?obligation, ?alias_def_id, "confirm_trait_alias_candidate");
|
2020-05-25 22:33:21 +02:00
|
|
|
|
|
|
|
self.infcx.commit_unconditionally(|_| {
|
2020-10-24 02:21:18 +02:00
|
|
|
let predicate = self.infcx().replace_bound_vars_with_placeholders(obligation.predicate);
|
2020-05-25 22:33:21 +02:00
|
|
|
let trait_ref = predicate.trait_ref;
|
|
|
|
let trait_def_id = trait_ref.def_id;
|
|
|
|
let substs = trait_ref.substs;
|
|
|
|
|
|
|
|
let trait_obligations = self.impl_or_trait_obligations(
|
2021-10-13 13:58:41 +00:00
|
|
|
&obligation.cause,
|
2020-05-25 22:33:21 +02:00
|
|
|
obligation.recursion_depth,
|
|
|
|
obligation.param_env,
|
|
|
|
trait_def_id,
|
|
|
|
&substs,
|
2021-10-13 13:58:41 +00:00
|
|
|
obligation.predicate,
|
2020-05-25 22:33:21 +02:00
|
|
|
);
|
|
|
|
|
2020-10-11 11:37:56 +01:00
|
|
|
debug!(?trait_def_id, ?trait_obligations, "trait alias obligations");
|
2020-05-25 22:33:21 +02:00
|
|
|
|
2020-05-11 15:25:33 +00:00
|
|
|
ImplSourceTraitAliasData { alias_def_id, substs, nested: trait_obligations }
|
2020-05-25 22:33:21 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
fn confirm_generator_candidate(
|
|
|
|
&mut self,
|
|
|
|
obligation: &TraitObligation<'tcx>,
|
2020-05-11 15:25:33 +00:00
|
|
|
) -> Result<ImplSourceGeneratorData<'tcx, PredicateObligation<'tcx>>, SelectionError<'tcx>>
|
|
|
|
{
|
2020-05-25 22:33:21 +02:00
|
|
|
// Okay to skip binder because the substs on generator types never
|
|
|
|
// touch bound regions, they just capture the in-scope
|
|
|
|
// type/region parameters.
|
2020-06-24 23:40:33 +02:00
|
|
|
let self_ty = self.infcx.shallow_resolve(obligation.self_ty().skip_binder());
|
2022-02-19 00:48:31 +01:00
|
|
|
let ty::Generator(generator_def_id, substs, _) = *self_ty.kind() else {
|
|
|
|
bug!("closure candidate for non-closure {:?}", obligation);
|
2020-05-25 22:33:21 +02:00
|
|
|
};
|
|
|
|
|
2020-10-11 11:37:56 +01:00
|
|
|
debug!(?obligation, ?generator_def_id, ?substs, "confirm_generator_candidate");
|
2020-05-25 22:33:21 +02:00
|
|
|
|
|
|
|
let trait_ref = self.generator_trait_ref_unnormalized(obligation, substs);
|
|
|
|
|
2022-01-26 21:47:03 -08:00
|
|
|
let nested = self.confirm_poly_trait_refs(obligation, trait_ref)?;
|
|
|
|
debug!(?trait_ref, ?nested, "generator candidate obligations");
|
2020-05-25 22:33:21 +02:00
|
|
|
|
2022-01-26 21:47:03 -08:00
|
|
|
Ok(ImplSourceGeneratorData { generator_def_id, substs, nested })
|
2020-05-25 22:33:21 +02:00
|
|
|
}
|
|
|
|
|
2021-08-20 13:36:04 +00:00
|
|
|
#[instrument(skip(self), level = "debug")]
|
2020-05-25 22:33:21 +02:00
|
|
|
fn confirm_closure_candidate(
|
|
|
|
&mut self,
|
|
|
|
obligation: &TraitObligation<'tcx>,
|
2020-05-11 15:25:33 +00:00
|
|
|
) -> Result<ImplSourceClosureData<'tcx, PredicateObligation<'tcx>>, SelectionError<'tcx>> {
|
2020-05-25 22:33:21 +02:00
|
|
|
let kind = self
|
|
|
|
.tcx()
|
|
|
|
.fn_trait_kind_from_lang_item(obligation.predicate.def_id())
|
|
|
|
.unwrap_or_else(|| bug!("closure candidate for non-fn trait {:?}", obligation));
|
|
|
|
|
|
|
|
// Okay to skip binder because the substs on closure types never
|
|
|
|
// touch bound regions, they just capture the in-scope
|
|
|
|
// type/region parameters.
|
2020-06-24 23:40:33 +02:00
|
|
|
let self_ty = self.infcx.shallow_resolve(obligation.self_ty().skip_binder());
|
2022-02-19 00:48:31 +01:00
|
|
|
let ty::Closure(closure_def_id, substs) = *self_ty.kind() else {
|
|
|
|
bug!("closure candidate for non-closure {:?}", obligation);
|
2020-05-25 22:33:21 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
let trait_ref = self.closure_trait_ref_unnormalized(obligation, substs);
|
2022-01-26 21:47:03 -08:00
|
|
|
let mut nested = self.confirm_poly_trait_refs(obligation, trait_ref)?;
|
2020-05-25 22:33:21 +02:00
|
|
|
|
2022-01-26 21:47:03 -08:00
|
|
|
debug!(?closure_def_id, ?trait_ref, ?nested, "confirm closure candidate obligations");
|
2020-05-25 22:33:21 +02:00
|
|
|
|
|
|
|
// FIXME: Chalk
|
|
|
|
|
|
|
|
if !self.tcx().sess.opts.debugging_opts.chalk {
|
2022-01-26 21:47:03 -08:00
|
|
|
nested.push(Obligation::new(
|
2020-05-25 22:33:21 +02:00
|
|
|
obligation.cause.clone(),
|
|
|
|
obligation.param_env,
|
2021-09-15 20:54:50 -04:00
|
|
|
ty::Binder::dummy(ty::PredicateKind::ClosureKind(closure_def_id, substs, kind))
|
2020-05-25 22:33:21 +02:00
|
|
|
.to_predicate(self.tcx()),
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2022-01-26 21:47:03 -08:00
|
|
|
Ok(ImplSourceClosureData { closure_def_id, substs, nested })
|
2020-05-25 22:33:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// In the case of closure types and fn pointers,
|
|
|
|
/// we currently treat the input type parameters on the trait as
|
|
|
|
/// outputs. This means that when we have a match we have only
|
|
|
|
/// considered the self type, so we have to go back and make sure
|
|
|
|
/// to relate the argument types too. This is kind of wrong, but
|
|
|
|
/// since we control the full set of impls, also not that wrong,
|
|
|
|
/// and it DOES yield better error messages (since we don't report
|
|
|
|
/// errors as if there is no applicable impl, but rather report
|
|
|
|
/// errors are about mismatched argument types.
|
|
|
|
///
|
|
|
|
/// Here is an example. Imagine we have a closure expression
|
|
|
|
/// and we desugared it so that the type of the expression is
|
2020-06-06 12:05:37 +02:00
|
|
|
/// `Closure`, and `Closure` expects `i32` as argument. Then it
|
2020-05-25 22:33:21 +02:00
|
|
|
/// is "as if" the compiler generated this impl:
|
|
|
|
///
|
2020-06-06 12:05:37 +02:00
|
|
|
/// impl Fn(i32) for Closure { ... }
|
2020-05-25 22:33:21 +02:00
|
|
|
///
|
2020-06-06 12:05:37 +02:00
|
|
|
/// Now imagine our obligation is `Closure: Fn(usize)`. So far
|
2020-05-25 22:33:21 +02:00
|
|
|
/// we have matched the self type `Closure`. At this point we'll
|
2020-06-06 12:05:37 +02:00
|
|
|
/// compare the `i32` to `usize` and generate an error.
|
2020-05-25 22:33:21 +02:00
|
|
|
///
|
|
|
|
/// Note that this checking occurs *after* the impl has selected,
|
|
|
|
/// because these output type parameters should not affect the
|
|
|
|
/// selection of the impl. Therefore, if there is a mismatch, we
|
|
|
|
/// report an error to the user.
|
2021-08-20 13:36:04 +00:00
|
|
|
#[instrument(skip(self), level = "trace")]
|
2020-05-25 22:33:21 +02:00
|
|
|
fn confirm_poly_trait_refs(
|
|
|
|
&mut self,
|
2022-01-26 21:47:03 -08:00
|
|
|
obligation: &TraitObligation<'tcx>,
|
2020-05-25 22:33:21 +02:00
|
|
|
expected_trait_ref: ty::PolyTraitRef<'tcx>,
|
|
|
|
) -> Result<Vec<PredicateObligation<'tcx>>, SelectionError<'tcx>> {
|
2022-01-26 21:47:03 -08:00
|
|
|
let obligation_trait_ref = obligation.predicate.to_poly_trait_ref();
|
|
|
|
// Normalize the obligation and expected trait refs together, because why not
|
|
|
|
let Normalized { obligations: nested, value: (obligation_trait_ref, expected_trait_ref) } =
|
|
|
|
ensure_sufficient_stack(|| {
|
|
|
|
self.infcx.commit_unconditionally(|_| {
|
|
|
|
normalize_with_depth(
|
|
|
|
self,
|
|
|
|
obligation.param_env,
|
|
|
|
obligation.cause.clone(),
|
|
|
|
obligation.recursion_depth + 1,
|
|
|
|
(obligation_trait_ref, expected_trait_ref),
|
|
|
|
)
|
|
|
|
})
|
|
|
|
});
|
|
|
|
|
2020-05-25 22:33:21 +02:00
|
|
|
self.infcx
|
2022-01-26 21:47:03 -08:00
|
|
|
.at(&obligation.cause, obligation.param_env)
|
2020-05-25 22:33:21 +02:00
|
|
|
.sup(obligation_trait_ref, expected_trait_ref)
|
2022-01-26 21:47:03 -08:00
|
|
|
.map(|InferOk { mut obligations, .. }| {
|
|
|
|
obligations.extend(nested);
|
|
|
|
obligations
|
|
|
|
})
|
2020-05-25 22:33:21 +02:00
|
|
|
.map_err(|e| OutputTypeParameterMismatch(expected_trait_ref, obligation_trait_ref, e))
|
|
|
|
}
|
|
|
|
|
2021-08-18 02:41:29 +08:00
|
|
|
fn confirm_trait_upcasting_unsize_candidate(
|
|
|
|
&mut self,
|
|
|
|
obligation: &TraitObligation<'tcx>,
|
|
|
|
idx: usize,
|
2021-08-18 12:45:18 +08:00
|
|
|
) -> Result<ImplSourceTraitUpcastingData<'tcx, PredicateObligation<'tcx>>, SelectionError<'tcx>>
|
|
|
|
{
|
2021-08-18 02:41:29 +08:00
|
|
|
let tcx = self.tcx();
|
|
|
|
|
|
|
|
// `assemble_candidates_for_unsizing` should ensure there are no late-bound
|
|
|
|
// regions here. See the comment there for more details.
|
|
|
|
let source = self.infcx.shallow_resolve(obligation.self_ty().no_bound_vars().unwrap());
|
|
|
|
let target = obligation.predicate.skip_binder().trait_ref.substs.type_at(1);
|
|
|
|
let target = self.infcx.shallow_resolve(target);
|
|
|
|
|
|
|
|
debug!(?source, ?target, "confirm_trait_upcasting_unsize_candidate");
|
|
|
|
|
|
|
|
let mut nested = vec![];
|
2021-08-18 12:45:18 +08:00
|
|
|
let source_trait_ref;
|
|
|
|
let upcast_trait_ref;
|
2021-08-18 02:41:29 +08:00
|
|
|
match (source.kind(), target.kind()) {
|
|
|
|
// TraitA+Kx+'a -> TraitB+Ky+'b (trait upcasting coercion).
|
|
|
|
(&ty::Dynamic(ref data_a, r_a), &ty::Dynamic(ref data_b, r_b)) => {
|
|
|
|
// See `assemble_candidates_for_unsizing` for more info.
|
2022-03-16 20:12:30 +08:00
|
|
|
// We already checked the compatibility of auto traits within `assemble_candidates_for_unsizing`.
|
2021-08-18 02:41:29 +08:00
|
|
|
let principal_a = data_a.principal().unwrap();
|
2021-08-18 12:45:18 +08:00
|
|
|
source_trait_ref = principal_a.with_self_ty(tcx, source);
|
|
|
|
upcast_trait_ref = util::supertraits(tcx, source_trait_ref).nth(idx).unwrap();
|
|
|
|
assert_eq!(data_b.principal_def_id(), Some(upcast_trait_ref.def_id()));
|
|
|
|
let existential_predicate = upcast_trait_ref.map_bound(|trait_ref| {
|
2021-08-18 02:41:29 +08:00
|
|
|
ty::ExistentialPredicate::Trait(ty::ExistentialTraitRef::erase_self_ty(
|
|
|
|
tcx, trait_ref,
|
|
|
|
))
|
|
|
|
});
|
|
|
|
let iter = Some(existential_predicate)
|
|
|
|
.into_iter()
|
|
|
|
.chain(
|
|
|
|
data_a
|
|
|
|
.projection_bounds()
|
|
|
|
.map(|b| b.map_bound(ty::ExistentialPredicate::Projection)),
|
|
|
|
)
|
|
|
|
.chain(
|
|
|
|
data_b
|
|
|
|
.auto_traits()
|
|
|
|
.map(ty::ExistentialPredicate::AutoTrait)
|
|
|
|
.map(ty::Binder::dummy),
|
|
|
|
);
|
|
|
|
let existential_predicates = tcx.mk_poly_existential_predicates(iter);
|
|
|
|
let source_trait = tcx.mk_dynamic(existential_predicates, r_b);
|
|
|
|
|
|
|
|
// Require that the traits involved in this upcast are **equal**;
|
|
|
|
// only the **lifetime bound** is changed.
|
|
|
|
let InferOk { obligations, .. } = self
|
|
|
|
.infcx
|
|
|
|
.at(&obligation.cause, obligation.param_env)
|
|
|
|
.sup(target, source_trait)
|
|
|
|
.map_err(|_| Unimplemented)?;
|
|
|
|
nested.extend(obligations);
|
|
|
|
|
|
|
|
// Register one obligation for 'a: 'b.
|
|
|
|
let cause = ObligationCause::new(
|
|
|
|
obligation.cause.span,
|
|
|
|
obligation.cause.body_id,
|
|
|
|
ObjectCastObligation(target),
|
|
|
|
);
|
|
|
|
let outlives = ty::OutlivesPredicate(r_a, r_b);
|
|
|
|
nested.push(Obligation::with_depth(
|
|
|
|
cause,
|
|
|
|
obligation.recursion_depth + 1,
|
|
|
|
obligation.param_env,
|
|
|
|
obligation.predicate.rebind(outlives).to_predicate(tcx),
|
|
|
|
));
|
|
|
|
}
|
|
|
|
_ => bug!(),
|
|
|
|
};
|
|
|
|
|
2021-08-18 12:45:18 +08:00
|
|
|
let vtable_segment_callback = {
|
|
|
|
let mut vptr_offset = 0;
|
|
|
|
move |segment| {
|
|
|
|
match segment {
|
|
|
|
VtblSegment::MetadataDSA => {
|
|
|
|
vptr_offset += ty::COMMON_VTABLE_ENTRIES.len();
|
|
|
|
}
|
|
|
|
VtblSegment::TraitOwnEntries { trait_ref, emit_vptr } => {
|
|
|
|
vptr_offset += util::count_own_vtable_entries(tcx, trait_ref);
|
|
|
|
if trait_ref == upcast_trait_ref {
|
|
|
|
if emit_vptr {
|
|
|
|
return ControlFlow::Break(Some(vptr_offset));
|
|
|
|
} else {
|
|
|
|
return ControlFlow::Break(None);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if emit_vptr {
|
|
|
|
vptr_offset += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ControlFlow::Continue(())
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let vtable_vptr_slot =
|
|
|
|
super::super::prepare_vtable_segments(tcx, source_trait_ref, vtable_segment_callback)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
Ok(ImplSourceTraitUpcastingData { upcast_trait_ref, vtable_vptr_slot, nested })
|
2021-08-18 02:41:29 +08:00
|
|
|
}
|
|
|
|
|
2020-05-25 22:33:21 +02:00
|
|
|
fn confirm_builtin_unsize_candidate(
|
|
|
|
&mut self,
|
|
|
|
obligation: &TraitObligation<'tcx>,
|
2020-05-11 15:25:33 +00:00
|
|
|
) -> Result<ImplSourceBuiltinData<PredicateObligation<'tcx>>, SelectionError<'tcx>> {
|
2020-05-25 22:33:21 +02:00
|
|
|
let tcx = self.tcx();
|
|
|
|
|
|
|
|
// `assemble_candidates_for_unsizing` should ensure there are no late-bound
|
|
|
|
// regions here. See the comment there for more details.
|
|
|
|
let source = self.infcx.shallow_resolve(obligation.self_ty().no_bound_vars().unwrap());
|
|
|
|
let target = obligation.predicate.skip_binder().trait_ref.substs.type_at(1);
|
|
|
|
let target = self.infcx.shallow_resolve(target);
|
|
|
|
|
2020-10-11 11:37:56 +01:00
|
|
|
debug!(?source, ?target, "confirm_builtin_unsize_candidate");
|
2020-05-25 22:33:21 +02:00
|
|
|
|
|
|
|
let mut nested = vec![];
|
2020-08-03 00:49:11 +02:00
|
|
|
match (source.kind(), target.kind()) {
|
2021-08-18 02:41:29 +08:00
|
|
|
// Trait+Kx+'a -> Trait+Ky+'b (auto traits and lifetime subtyping).
|
2020-05-25 22:33:21 +02:00
|
|
|
(&ty::Dynamic(ref data_a, r_a), &ty::Dynamic(ref data_b, r_b)) => {
|
2021-08-18 02:41:29 +08:00
|
|
|
// See `assemble_candidates_for_unsizing` for more info.
|
2022-03-16 20:12:30 +08:00
|
|
|
// We already checked the compatibility of auto traits within `assemble_candidates_for_unsizing`.
|
2021-08-18 02:41:29 +08:00
|
|
|
let iter = data_a
|
|
|
|
.principal()
|
|
|
|
.map(|b| b.map_bound(ty::ExistentialPredicate::Trait))
|
2020-12-11 15:02:46 -05:00
|
|
|
.into_iter()
|
|
|
|
.chain(
|
|
|
|
data_a
|
|
|
|
.projection_bounds()
|
|
|
|
.map(|b| b.map_bound(ty::ExistentialPredicate::Projection)),
|
|
|
|
)
|
|
|
|
.chain(
|
|
|
|
data_b
|
|
|
|
.auto_traits()
|
|
|
|
.map(ty::ExistentialPredicate::AutoTrait)
|
|
|
|
.map(ty::Binder::dummy),
|
|
|
|
);
|
|
|
|
let existential_predicates = tcx.mk_poly_existential_predicates(iter);
|
2020-05-25 22:33:21 +02:00
|
|
|
let source_trait = tcx.mk_dynamic(existential_predicates, r_b);
|
|
|
|
|
|
|
|
// Require that the traits involved in this upcast are **equal**;
|
|
|
|
// only the **lifetime bound** is changed.
|
|
|
|
let InferOk { obligations, .. } = self
|
|
|
|
.infcx
|
|
|
|
.at(&obligation.cause, obligation.param_env)
|
2020-05-20 14:31:51 +00:00
|
|
|
.sup(target, source_trait)
|
2020-05-25 22:33:21 +02:00
|
|
|
.map_err(|_| Unimplemented)?;
|
|
|
|
nested.extend(obligations);
|
|
|
|
|
|
|
|
// Register one obligation for 'a: 'b.
|
|
|
|
let cause = ObligationCause::new(
|
|
|
|
obligation.cause.span,
|
|
|
|
obligation.cause.body_id,
|
|
|
|
ObjectCastObligation(target),
|
|
|
|
);
|
|
|
|
let outlives = ty::OutlivesPredicate(r_a, r_b);
|
|
|
|
nested.push(Obligation::with_depth(
|
|
|
|
cause,
|
|
|
|
obligation.recursion_depth + 1,
|
|
|
|
obligation.param_env,
|
2021-01-07 00:41:55 -05:00
|
|
|
obligation.predicate.rebind(outlives).to_predicate(tcx),
|
2020-05-25 22:33:21 +02:00
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
// `T` -> `Trait`
|
|
|
|
(_, &ty::Dynamic(ref data, r)) => {
|
|
|
|
let mut object_dids = data.auto_traits().chain(data.principal_def_id());
|
|
|
|
if let Some(did) = object_dids.find(|did| !tcx.is_object_safe(*did)) {
|
|
|
|
return Err(TraitNotObjectSafe(did));
|
|
|
|
}
|
|
|
|
|
|
|
|
let cause = ObligationCause::new(
|
|
|
|
obligation.cause.span,
|
|
|
|
obligation.cause.body_id,
|
|
|
|
ObjectCastObligation(target),
|
|
|
|
);
|
|
|
|
|
|
|
|
let predicate_to_obligation = |predicate| {
|
|
|
|
Obligation::with_depth(
|
|
|
|
cause.clone(),
|
|
|
|
obligation.recursion_depth + 1,
|
|
|
|
obligation.param_env,
|
|
|
|
predicate,
|
|
|
|
)
|
|
|
|
};
|
|
|
|
|
|
|
|
// Create obligations:
|
|
|
|
// - Casting `T` to `Trait`
|
|
|
|
// - For all the various builtin bounds attached to the object cast. (In other
|
|
|
|
// words, if the object type is `Foo + Send`, this would create an obligation for
|
|
|
|
// the `Send` check.)
|
|
|
|
// - Projection predicates
|
|
|
|
nested.extend(
|
|
|
|
data.iter().map(|predicate| {
|
|
|
|
predicate_to_obligation(predicate.with_self_ty(tcx, source))
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
|
|
|
|
// We can only make objects from sized types.
|
2021-09-15 20:54:50 -04:00
|
|
|
let tr = ty::Binder::dummy(ty::TraitRef::new(
|
2020-08-18 11:47:27 +01:00
|
|
|
tcx.require_lang_item(LangItem::Sized, None),
|
2020-05-25 22:33:21 +02:00
|
|
|
tcx.mk_substs_trait(source, &[]),
|
2021-09-15 20:54:50 -04:00
|
|
|
));
|
2020-05-25 22:33:21 +02:00
|
|
|
nested.push(predicate_to_obligation(tr.without_const().to_predicate(tcx)));
|
|
|
|
|
|
|
|
// If the type is `Foo + 'a`, ensure that the type
|
|
|
|
// being cast to `Foo + 'a` outlives `'a`:
|
|
|
|
let outlives = ty::OutlivesPredicate(source, r);
|
|
|
|
nested.push(predicate_to_obligation(ty::Binder::dummy(outlives).to_predicate(tcx)));
|
|
|
|
}
|
|
|
|
|
|
|
|
// `[T; n]` -> `[T]`
|
|
|
|
(&ty::Array(a, _), &ty::Slice(b)) => {
|
|
|
|
let InferOk { obligations, .. } = self
|
|
|
|
.infcx
|
|
|
|
.at(&obligation.cause, obligation.param_env)
|
|
|
|
.eq(b, a)
|
|
|
|
.map_err(|_| Unimplemented)?;
|
|
|
|
nested.extend(obligations);
|
|
|
|
}
|
|
|
|
|
|
|
|
// `Struct<T>` -> `Struct<U>`
|
|
|
|
(&ty::Adt(def, substs_a), &ty::Adt(_, substs_b)) => {
|
|
|
|
let maybe_unsizing_param_idx = |arg: GenericArg<'tcx>| match arg.unpack() {
|
2020-08-03 00:49:11 +02:00
|
|
|
GenericArgKind::Type(ty) => match ty.kind() {
|
2020-05-25 22:33:21 +02:00
|
|
|
ty::Param(p) => Some(p.index),
|
|
|
|
_ => None,
|
|
|
|
},
|
|
|
|
|
|
|
|
// Lifetimes aren't allowed to change during unsizing.
|
|
|
|
GenericArgKind::Lifetime(_) => None,
|
|
|
|
|
2022-02-02 14:24:45 +11:00
|
|
|
GenericArgKind::Const(ct) => match ct.val() {
|
2020-05-25 22:33:21 +02:00
|
|
|
ty::ConstKind::Param(p) => Some(p.index),
|
|
|
|
_ => None,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2021-02-03 23:56:47 +01:00
|
|
|
// FIXME(eddyb) cache this (including computing `unsizing_params`)
|
|
|
|
// by putting it in a query; it would only need the `DefId` as it
|
|
|
|
// looks at declared field types, not anything substituted.
|
|
|
|
|
2020-05-25 22:33:21 +02:00
|
|
|
// The last field of the structure has to exist and contain type/const parameters.
|
|
|
|
let (tail_field, prefix_fields) =
|
|
|
|
def.non_enum_variant().fields.split_last().ok_or(Unimplemented)?;
|
|
|
|
let tail_field_ty = tcx.type_of(tail_field.did);
|
|
|
|
|
|
|
|
let mut unsizing_params = GrowableBitSet::new_empty();
|
2022-01-12 03:19:52 +00:00
|
|
|
for arg in tail_field_ty.walk() {
|
2021-10-30 15:56:02 +02:00
|
|
|
if let Some(i) = maybe_unsizing_param_idx(arg) {
|
|
|
|
unsizing_params.insert(i);
|
2021-02-03 23:56:47 +01:00
|
|
|
}
|
2021-10-30 15:56:02 +02:00
|
|
|
}
|
2021-02-03 23:56:47 +01:00
|
|
|
|
2021-10-30 15:56:02 +02:00
|
|
|
// Ensure none of the other fields mention the parameters used
|
|
|
|
// in unsizing.
|
|
|
|
for field in prefix_fields {
|
2022-01-12 03:19:52 +00:00
|
|
|
for arg in tcx.type_of(field.did).walk() {
|
2020-05-25 22:33:21 +02:00
|
|
|
if let Some(i) = maybe_unsizing_param_idx(arg) {
|
2021-10-30 15:56:02 +02:00
|
|
|
unsizing_params.remove(i);
|
2020-05-25 22:33:21 +02:00
|
|
|
}
|
|
|
|
}
|
2021-10-30 15:56:02 +02:00
|
|
|
}
|
2020-05-25 22:33:21 +02:00
|
|
|
|
2021-10-30 15:56:02 +02:00
|
|
|
if unsizing_params.is_empty() {
|
|
|
|
return Err(Unimplemented);
|
2021-01-05 16:33:10 +01:00
|
|
|
}
|
|
|
|
|
2020-05-25 22:33:21 +02:00
|
|
|
// Extract `TailField<T>` and `TailField<U>` from `Struct<T>` and `Struct<U>`.
|
|
|
|
let source_tail = tail_field_ty.subst(tcx, substs_a);
|
|
|
|
let target_tail = tail_field_ty.subst(tcx, substs_b);
|
|
|
|
|
|
|
|
// Check that the source struct with the target's
|
|
|
|
// unsizing parameters is equal to the target.
|
|
|
|
let substs = tcx.mk_substs(substs_a.iter().enumerate().map(|(i, k)| {
|
|
|
|
if unsizing_params.contains(i as u32) { substs_b[i] } else { k }
|
|
|
|
}));
|
|
|
|
let new_struct = tcx.mk_adt(def, substs);
|
|
|
|
let InferOk { obligations, .. } = self
|
|
|
|
.infcx
|
|
|
|
.at(&obligation.cause, obligation.param_env)
|
|
|
|
.eq(target, new_struct)
|
|
|
|
.map_err(|_| Unimplemented)?;
|
|
|
|
nested.extend(obligations);
|
|
|
|
|
|
|
|
// Construct the nested `TailField<T>: Unsize<TailField<U>>` predicate.
|
|
|
|
nested.push(predicate_for_trait_def(
|
|
|
|
tcx,
|
|
|
|
obligation.param_env,
|
|
|
|
obligation.cause.clone(),
|
|
|
|
obligation.predicate.def_id(),
|
|
|
|
obligation.recursion_depth + 1,
|
|
|
|
source_tail,
|
|
|
|
&[target_tail.into()],
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
// `(.., T)` -> `(.., U)`
|
|
|
|
(&ty::Tuple(tys_a), &ty::Tuple(tys_b)) => {
|
|
|
|
assert_eq!(tys_a.len(), tys_b.len());
|
|
|
|
|
|
|
|
// The last field of the tuple has to exist.
|
|
|
|
let (&a_last, a_mid) = tys_a.split_last().ok_or(Unimplemented)?;
|
|
|
|
let &b_last = tys_b.last().unwrap();
|
|
|
|
|
|
|
|
// Check that the source tuple with the target's
|
|
|
|
// last element is equal to the target.
|
2022-02-07 16:06:31 +01:00
|
|
|
let new_tuple = tcx.mk_tup(a_mid.iter().copied().chain(iter::once(b_last)));
|
2020-05-25 22:33:21 +02:00
|
|
|
let InferOk { obligations, .. } = self
|
|
|
|
.infcx
|
|
|
|
.at(&obligation.cause, obligation.param_env)
|
|
|
|
.eq(target, new_tuple)
|
|
|
|
.map_err(|_| Unimplemented)?;
|
|
|
|
nested.extend(obligations);
|
|
|
|
|
|
|
|
// Construct the nested `T: Unsize<U>` predicate.
|
|
|
|
nested.push(ensure_sufficient_stack(|| {
|
|
|
|
predicate_for_trait_def(
|
|
|
|
tcx,
|
|
|
|
obligation.param_env,
|
|
|
|
obligation.cause.clone(),
|
|
|
|
obligation.predicate.def_id(),
|
|
|
|
obligation.recursion_depth + 1,
|
2022-02-07 16:06:31 +01:00
|
|
|
a_last,
|
|
|
|
&[b_last.into()],
|
2020-05-25 22:33:21 +02:00
|
|
|
)
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
_ => bug!(),
|
|
|
|
};
|
|
|
|
|
2020-05-11 15:25:33 +00:00
|
|
|
Ok(ImplSourceBuiltinData { nested })
|
2020-05-25 22:33:21 +02:00
|
|
|
}
|
2022-01-18 01:43:49 -08:00
|
|
|
|
2022-03-21 16:52:41 +11:00
|
|
|
fn confirm_const_destruct_candidate(
|
2022-01-18 01:43:49 -08:00
|
|
|
&mut self,
|
|
|
|
obligation: &TraitObligation<'tcx>,
|
|
|
|
impl_def_id: Option<DefId>,
|
2022-03-21 16:52:41 +11:00
|
|
|
) -> Result<ImplSourceConstDestructData<PredicateObligation<'tcx>>, SelectionError<'tcx>> {
|
|
|
|
// `~const Destruct` in a non-const environment is always trivially true, since our type is `Drop`
|
|
|
|
if !obligation.is_const() {
|
|
|
|
return Ok(ImplSourceConstDestructData { nested: vec![] });
|
|
|
|
}
|
|
|
|
|
|
|
|
let drop_trait = self.tcx().require_lang_item(LangItem::Drop, None);
|
|
|
|
// FIXME: remove if statement below when beta is bumped
|
|
|
|
#[cfg(bootstrap)]
|
|
|
|
{}
|
|
|
|
|
2022-03-21 17:07:09 +11:00
|
|
|
if obligation.predicate.skip_binder().def_id() == drop_trait {
|
2022-03-21 16:52:41 +11:00
|
|
|
return Ok(ImplSourceConstDestructData { nested: vec![] });
|
2022-01-18 01:43:49 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
let tcx = self.tcx();
|
|
|
|
let self_ty = self.infcx.shallow_resolve(obligation.self_ty());
|
|
|
|
|
|
|
|
let mut nested = vec![];
|
2022-01-19 01:23:23 -08:00
|
|
|
let cause = obligation.derived_cause(BuiltinDerivedObligation);
|
2022-01-18 01:43:49 -08:00
|
|
|
|
2022-01-19 01:23:23 -08:00
|
|
|
// If we have a custom `impl const Drop`, then
|
2022-03-21 16:52:41 +11:00
|
|
|
// first check it like a regular impl candidate.
|
|
|
|
// This is copied from confirm_impl_candidate but remaps the predicate to `~const Drop` beforehand.
|
2022-01-18 01:43:49 -08:00
|
|
|
if let Some(impl_def_id) = impl_def_id {
|
2022-03-21 16:52:41 +11:00
|
|
|
let obligations = self.infcx.commit_unconditionally(|_| {
|
|
|
|
let mut new_obligation = obligation.clone();
|
2022-03-21 17:07:09 +11:00
|
|
|
new_obligation.predicate = new_obligation.predicate.map_bound(|mut trait_pred| {
|
|
|
|
trait_pred.trait_ref.def_id = drop_trait;
|
|
|
|
trait_pred
|
|
|
|
});
|
2022-03-21 16:52:41 +11:00
|
|
|
let substs = self.rematch_impl(impl_def_id, &new_obligation);
|
|
|
|
debug!(?substs, "impl substs");
|
2021-10-13 13:58:41 +00:00
|
|
|
|
|
|
|
let derived = DerivedObligationCause {
|
|
|
|
parent_trait_pred: obligation.predicate,
|
|
|
|
parent_code: obligation.cause.clone_code(),
|
|
|
|
};
|
|
|
|
let derived_code = ImplDerivedObligation(Box::new(ImplDerivedObligationCause {
|
|
|
|
derived,
|
|
|
|
impl_def_id,
|
|
|
|
span: obligation.cause.span,
|
|
|
|
}));
|
|
|
|
|
|
|
|
let cause = ObligationCause::new(
|
|
|
|
obligation.cause.span,
|
|
|
|
obligation.cause.body_id,
|
|
|
|
derived_code,
|
|
|
|
);
|
2022-03-21 16:52:41 +11:00
|
|
|
ensure_sufficient_stack(|| {
|
|
|
|
self.vtable_impl(
|
|
|
|
impl_def_id,
|
|
|
|
substs,
|
2021-10-13 13:58:41 +00:00
|
|
|
&cause,
|
2022-03-21 16:52:41 +11:00
|
|
|
new_obligation.recursion_depth + 1,
|
|
|
|
new_obligation.param_env,
|
2021-10-13 13:58:41 +00:00
|
|
|
obligation.predicate,
|
2022-03-21 16:52:41 +11:00
|
|
|
)
|
|
|
|
})
|
|
|
|
});
|
|
|
|
nested.extend(obligations.nested);
|
2022-01-18 01:43:49 -08:00
|
|
|
}
|
|
|
|
|
2022-01-19 01:23:23 -08:00
|
|
|
// We want to confirm the ADT's fields if we have an ADT
|
|
|
|
let mut stack = match *self_ty.skip_binder().kind() {
|
|
|
|
ty::Adt(def, substs) => def.all_fields().map(|f| f.ty(tcx, substs)).collect(),
|
|
|
|
_ => vec![self_ty.skip_binder()],
|
|
|
|
};
|
|
|
|
|
|
|
|
while let Some(nested_ty) = stack.pop() {
|
|
|
|
match *nested_ty.kind() {
|
|
|
|
// We know these types are trivially drop
|
|
|
|
ty::Bool
|
|
|
|
| ty::Char
|
|
|
|
| ty::Int(_)
|
|
|
|
| ty::Uint(_)
|
|
|
|
| ty::Float(_)
|
|
|
|
| ty::Infer(ty::IntVar(_))
|
|
|
|
| ty::Infer(ty::FloatVar(_))
|
|
|
|
| ty::Str
|
|
|
|
| ty::RawPtr(_)
|
|
|
|
| ty::Ref(..)
|
|
|
|
| ty::FnDef(..)
|
|
|
|
| ty::FnPtr(_)
|
2022-01-19 20:07:04 -08:00
|
|
|
| ty::Never
|
|
|
|
| ty::Foreign(_) => {}
|
2022-01-19 01:23:23 -08:00
|
|
|
|
|
|
|
// These types are built-in, so we can fast-track by registering
|
2022-03-16 20:12:30 +08:00
|
|
|
// nested predicates for their constituent type(s)
|
2022-01-19 01:23:23 -08:00
|
|
|
ty::Array(ty, _) | ty::Slice(ty) => {
|
|
|
|
stack.push(ty);
|
|
|
|
}
|
|
|
|
ty::Tuple(tys) => {
|
2022-02-07 16:06:31 +01:00
|
|
|
stack.extend(tys.iter());
|
2022-01-19 01:23:23 -08:00
|
|
|
}
|
|
|
|
ty::Closure(_, substs) => {
|
|
|
|
stack.push(substs.as_closure().tupled_upvars_ty());
|
|
|
|
}
|
|
|
|
ty::Generator(_, substs, _) => {
|
|
|
|
let generator = substs.as_generator();
|
|
|
|
stack.extend([generator.tupled_upvars_ty(), generator.witness()]);
|
|
|
|
}
|
|
|
|
ty::GeneratorWitness(tys) => {
|
|
|
|
stack.extend(tcx.erase_late_bound_regions(tys).to_vec());
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we have a projection type, make sure to normalize it so we replace it
|
|
|
|
// with a fresh infer variable
|
|
|
|
ty::Projection(..) => {
|
|
|
|
self.infcx.commit_unconditionally(|_| {
|
|
|
|
let predicate = normalize_with_depth_to(
|
|
|
|
self,
|
|
|
|
obligation.param_env,
|
|
|
|
cause.clone(),
|
|
|
|
obligation.recursion_depth + 1,
|
|
|
|
self_ty
|
|
|
|
.rebind(ty::TraitPredicate {
|
|
|
|
trait_ref: ty::TraitRef {
|
2022-03-21 17:07:09 +11:00
|
|
|
def_id: self
|
|
|
|
.tcx()
|
|
|
|
.require_lang_item(LangItem::Destruct, None),
|
2022-01-19 01:23:23 -08:00
|
|
|
substs: self.tcx().mk_substs_trait(nested_ty, &[]),
|
|
|
|
},
|
|
|
|
constness: ty::BoundConstness::ConstIfConst,
|
|
|
|
polarity: ty::ImplPolarity::Positive,
|
|
|
|
})
|
|
|
|
.to_predicate(tcx),
|
|
|
|
&mut nested,
|
|
|
|
);
|
|
|
|
|
|
|
|
nested.push(Obligation::with_depth(
|
|
|
|
cause.clone(),
|
|
|
|
obligation.recursion_depth + 1,
|
|
|
|
obligation.param_env,
|
|
|
|
predicate,
|
|
|
|
));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we have any other type (e.g. an ADT), just register a nested obligation
|
|
|
|
// since it's either not `const Drop` (and we raise an error during selection),
|
|
|
|
// or it's an ADT (and we need to check for a custom impl during selection)
|
|
|
|
_ => {
|
|
|
|
let predicate = self_ty
|
2022-01-18 01:43:49 -08:00
|
|
|
.rebind(ty::TraitPredicate {
|
|
|
|
trait_ref: ty::TraitRef {
|
2022-03-21 16:52:41 +11:00
|
|
|
def_id: self.tcx().require_lang_item(LangItem::Destruct, None),
|
2022-01-19 01:23:23 -08:00
|
|
|
substs: self.tcx().mk_substs_trait(nested_ty, &[]),
|
2022-01-18 01:43:49 -08:00
|
|
|
},
|
|
|
|
constness: ty::BoundConstness::ConstIfConst,
|
|
|
|
polarity: ty::ImplPolarity::Positive,
|
|
|
|
})
|
2022-01-19 01:23:23 -08:00
|
|
|
.to_predicate(tcx);
|
2022-01-18 01:43:49 -08:00
|
|
|
|
2022-01-19 01:23:23 -08:00
|
|
|
nested.push(Obligation::with_depth(
|
|
|
|
cause.clone(),
|
|
|
|
obligation.recursion_depth + 1,
|
|
|
|
obligation.param_env,
|
|
|
|
predicate,
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
2022-01-18 01:43:49 -08:00
|
|
|
}
|
|
|
|
|
2022-03-21 16:52:41 +11:00
|
|
|
Ok(ImplSourceConstDestructData { nested })
|
2022-01-18 01:43:49 -08:00
|
|
|
}
|
2020-05-25 22:33:21 +02:00
|
|
|
}
|