rust/compiler/rustc_next_trait_solver/src/solve/assembly/mod.rs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

788 lines
32 KiB
Rust
Raw Normal View History

2022-12-19 07:01:38 +00:00
//! Code shared by trait and projection goals for candidate assembly.
pub(super) mod structural_traits;
use derive_where::derive_where;
2024-06-17 17:59:08 -04:00
use rustc_type_ir::fold::TypeFoldable;
use rustc_type_ir::inherent::*;
use rustc_type_ir::lang_items::TraitSolverLangItem;
use rustc_type_ir::visit::TypeVisitableExt as _;
use rustc_type_ir::{self as ty, elaborate, Interner, Upcast as _};
2024-06-17 19:12:23 -04:00
use tracing::{debug, instrument};
2024-06-17 17:59:08 -04:00
2024-06-18 19:13:54 -04:00
use crate::delegate::SolverDelegate;
2024-06-17 17:59:08 -04:00
use crate::solve::inspect::ProbeKind;
use crate::solve::{
BuiltinImplSource, CandidateSource, CanonicalResponse, Certainty, EvalCtxt, Goal, GoalSource,
MaybeCause, NoSolution, QueryResult, SolverMode,
};
2022-12-19 07:01:38 +00:00
/// A candidate is a possible way to prove a goal.
///
/// It consists of both the `source`, which describes how that goal would be proven,
/// and the `result` when using the given `source`.
#[derive_where(Clone, Debug; I: Interner)]
2024-06-14 15:59:47 -04:00
pub(super) struct Candidate<I: Interner> {
pub(super) source: CandidateSource<I>,
pub(super) result: CanonicalResponse<I>,
2022-12-19 07:01:38 +00:00
}
/// Methods used to assemble candidates for either trait or projection goals.
2024-06-18 19:13:54 -04:00
pub(super) trait GoalKind<D, I = <D as SolverDelegate>::Interner>:
2024-06-17 17:59:08 -04:00
TypeFoldable<I> + Copy + Eq + std::fmt::Display
where
2024-06-18 19:13:54 -04:00
D: SolverDelegate<Interner = I>,
2024-06-17 17:59:08 -04:00
I: Interner,
{
2024-06-17 17:59:08 -04:00
fn self_ty(self) -> I::Ty;
2022-12-19 07:01:38 +00:00
2024-06-21 12:39:55 -04:00
fn trait_ref(self, cx: I) -> ty::TraitRef<I>;
2023-03-21 16:26:23 +01:00
2024-06-21 12:39:55 -04:00
fn with_self_ty(self, cx: I, self_ty: I::Ty) -> Self;
2022-12-19 07:01:38 +00:00
2024-06-21 12:39:55 -04:00
fn trait_def_id(self, cx: I) -> I::DefId;
2022-12-19 07:01:38 +00:00
/// Try equating an assumption predicate against a goal's predicate. If it
/// holds, then execute the `then` callback, which should do any additional
/// work, then produce a response (typically by executing
/// [`EvalCtxt::evaluate_added_goals_and_make_canonical_response`]).
fn probe_and_match_goal_against_assumption(
2024-06-18 19:13:54 -04:00
ecx: &mut EvalCtxt<'_, D>,
2024-06-17 17:59:08 -04:00
source: CandidateSource<I>,
goal: Goal<I, Self>,
assumption: I::Clause,
2024-06-18 19:13:54 -04:00
then: impl FnOnce(&mut EvalCtxt<'_, D>) -> QueryResult<I>,
2024-06-17 17:59:08 -04:00
) -> Result<Candidate<I>, NoSolution>;
/// Consider a clause, which consists of a "assumption" and some "requirements",
/// to satisfy a goal. If the requirements hold, then attempt to satisfy our
/// goal by equating it with the assumption.
2024-04-26 12:36:33 -04:00
fn probe_and_consider_implied_clause(
2024-06-18 19:13:54 -04:00
ecx: &mut EvalCtxt<'_, D>,
2024-06-17 17:59:08 -04:00
parent_source: CandidateSource<I>,
goal: Goal<I, Self>,
assumption: I::Clause,
requirements: impl IntoIterator<Item = (GoalSource, Goal<I, I::Predicate>)>,
) -> Result<Candidate<I>, NoSolution> {
Self::probe_and_match_goal_against_assumption(ecx, parent_source, goal, assumption, |ecx| {
for (nested_source, goal) in requirements {
ecx.add_goal(nested_source, goal);
}
ecx.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
})
}
/// Consider a clause specifically for a `dyn Trait` self type. This requires
/// additionally checking all of the supertraits and object bounds to hold,
/// since they're not implied by the well-formedness of the object type.
2024-04-26 12:36:33 -04:00
fn probe_and_consider_object_bound_candidate(
2024-06-18 19:13:54 -04:00
ecx: &mut EvalCtxt<'_, D>,
2024-06-17 17:59:08 -04:00
source: CandidateSource<I>,
goal: Goal<I, Self>,
assumption: I::Clause,
) -> Result<Candidate<I>, NoSolution> {
Self::probe_and_match_goal_against_assumption(ecx, source, goal, assumption, |ecx| {
2024-06-21 12:39:55 -04:00
let cx = ecx.cx();
2024-06-17 17:59:08 -04:00
let ty::Dynamic(bounds, _, _) = goal.predicate.self_ty().kind() else {
panic!("expected object type in `probe_and_consider_object_bound_candidate`");
};
2023-12-18 07:49:46 +01:00
ecx.add_goals(
GoalSource::ImplWhereBound,
2023-12-18 07:49:46 +01:00
structural_traits::predicates_for_object_candidate(
ecx,
goal.param_env,
2024-06-21 12:39:55 -04:00
goal.predicate.trait_ref(cx),
2023-12-18 07:49:46 +01:00
bounds,
),
);
ecx.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
})
}
2023-02-16 03:04:08 +00:00
fn consider_impl_candidate(
2024-06-18 19:13:54 -04:00
ecx: &mut EvalCtxt<'_, D>,
2024-06-17 17:59:08 -04:00
goal: Goal<I, Self>,
impl_def_id: I::DefId,
) -> Result<Candidate<I>, NoSolution>;
2022-12-19 07:01:38 +00:00
2023-07-20 11:26:22 +02:00
/// If the predicate contained an error, we want to avoid emitting unnecessary trait
/// errors but still want to emit errors for other trait goals. We have some special
/// handling for this case.
///
2023-07-20 11:26:22 +02:00
/// Trait goals always hold while projection goals never do. This is a bit arbitrary
/// but prevents incorrect normalization while hiding any trait errors.
fn consider_error_guaranteed_candidate(
2024-06-18 19:13:54 -04:00
ecx: &mut EvalCtxt<'_, D>,
2024-06-17 17:59:08 -04:00
guar: I::ErrorGuaranteed,
) -> Result<Candidate<I>, NoSolution>;
2023-07-20 11:26:22 +02:00
/// A type implements an `auto trait` if its components do as well.
///
/// These components are given by built-in rules from
/// [`structural_traits::instantiate_constituent_tys_for_auto_trait`].
2023-01-17 20:16:30 +00:00
fn consider_auto_trait_candidate(
2024-06-18 19:13:54 -04:00
ecx: &mut EvalCtxt<'_, D>,
2024-06-17 17:59:08 -04:00
goal: Goal<I, Self>,
) -> Result<Candidate<I>, NoSolution>;
2023-01-17 20:16:30 +00:00
/// A trait alias holds if the RHS traits and `where` clauses hold.
2023-01-17 20:16:30 +00:00
fn consider_trait_alias_candidate(
2024-06-18 19:13:54 -04:00
ecx: &mut EvalCtxt<'_, D>,
2024-06-17 17:59:08 -04:00
goal: Goal<I, Self>,
) -> Result<Candidate<I>, NoSolution>;
2023-01-17 20:16:30 +00:00
2024-03-13 23:35:24 +01:00
/// A type is `Sized` if its tail component is `Sized`.
2023-07-20 11:26:22 +02:00
///
/// These components are given by built-in rules from
/// [`structural_traits::instantiate_constituent_tys_for_sized_trait`].
2023-01-17 20:16:30 +00:00
fn consider_builtin_sized_candidate(
2024-06-18 19:13:54 -04:00
ecx: &mut EvalCtxt<'_, D>,
2024-06-17 17:59:08 -04:00
goal: Goal<I, Self>,
) -> Result<Candidate<I>, NoSolution>;
2023-01-17 20:24:58 +00:00
2023-07-20 11:26:22 +02:00
/// A type is `Copy` or `Clone` if its components are `Copy` or `Clone`.
///
/// These components are given by built-in rules from
/// [`structural_traits::instantiate_constituent_tys_for_copy_clone_trait`].
2023-01-17 20:24:58 +00:00
fn consider_builtin_copy_clone_candidate(
2024-06-18 19:13:54 -04:00
ecx: &mut EvalCtxt<'_, D>,
2024-06-17 17:59:08 -04:00
goal: Goal<I, Self>,
) -> Result<Candidate<I>, NoSolution>;
2023-01-18 23:21:12 +00:00
/// A type is `PointerLike` if we can compute its layout, and that layout
/// matches the layout of `usize`.
2023-02-07 18:02:20 +00:00
fn consider_builtin_pointer_like_candidate(
2024-06-18 19:13:54 -04:00
ecx: &mut EvalCtxt<'_, D>,
2024-06-17 17:59:08 -04:00
goal: Goal<I, Self>,
) -> Result<Candidate<I>, NoSolution>;
/// A type is a `FnPtr` if it is of `FnPtr` type.
2022-07-20 14:32:58 +02:00
fn consider_builtin_fn_ptr_trait_candidate(
2024-06-18 19:13:54 -04:00
ecx: &mut EvalCtxt<'_, D>,
2024-06-17 17:59:08 -04:00
goal: Goal<I, Self>,
) -> Result<Candidate<I>, NoSolution>;
2022-07-20 14:32:58 +02:00
/// A callable type (a closure, fn def, or fn ptr) is known to implement the `Fn<A>`
/// family of traits where `A` is given by the signature of the type.
fn consider_builtin_fn_trait_candidates(
2024-06-18 19:13:54 -04:00
ecx: &mut EvalCtxt<'_, D>,
2024-06-17 17:59:08 -04:00
goal: Goal<I, Self>,
kind: ty::ClosureKind,
2024-06-17 17:59:08 -04:00
) -> Result<Candidate<I>, NoSolution>;
/// An async closure is known to implement the `AsyncFn<A>` family of traits
/// where `A` is given by the signature of the type.
fn consider_builtin_async_fn_trait_candidates(
2024-06-18 19:13:54 -04:00
ecx: &mut EvalCtxt<'_, D>,
2024-06-17 17:59:08 -04:00
goal: Goal<I, Self>,
kind: ty::ClosureKind,
2024-06-17 17:59:08 -04:00
) -> Result<Candidate<I>, NoSolution>;
2024-01-25 03:50:23 +00:00
/// Compute the built-in logic of the `AsyncFnKindHelper` helper trait, which
/// is used internally to delay computation for async closures until after
/// upvar analysis is performed in HIR typeck.
fn consider_builtin_async_fn_kind_helper_candidate(
2024-06-18 19:13:54 -04:00
ecx: &mut EvalCtxt<'_, D>,
2024-06-17 17:59:08 -04:00
goal: Goal<I, Self>,
) -> Result<Candidate<I>, NoSolution>;
/// `Tuple` is implemented if the `Self` type is a tuple.
fn consider_builtin_tuple_candidate(
2024-06-18 19:13:54 -04:00
ecx: &mut EvalCtxt<'_, D>,
2024-06-17 17:59:08 -04:00
goal: Goal<I, Self>,
) -> Result<Candidate<I>, NoSolution>;
2023-01-24 23:24:25 +00:00
/// `Pointee` is always implemented.
///
/// See the projection implementation for the `Metadata` types for all of
/// the built-in types. For structs, the metadata type is given by the struct
/// tail.
2023-01-24 23:24:25 +00:00
fn consider_builtin_pointee_candidate(
2024-06-18 19:13:54 -04:00
ecx: &mut EvalCtxt<'_, D>,
2024-06-17 17:59:08 -04:00
goal: Goal<I, Self>,
) -> Result<Candidate<I>, NoSolution>;
2023-01-24 23:38:20 +00:00
2023-10-19 21:46:28 +00:00
/// A coroutine (that comes from an `async` desugaring) is known to implement
/// `Future<Output = O>`, where `O` is given by the coroutine's return type
/// that was computed during type-checking.
2023-01-24 23:38:20 +00:00
fn consider_builtin_future_candidate(
2024-06-18 19:13:54 -04:00
ecx: &mut EvalCtxt<'_, D>,
2024-06-17 17:59:08 -04:00
goal: Goal<I, Self>,
) -> Result<Candidate<I>, NoSolution>;
2023-01-24 23:38:20 +00:00
/// A coroutine (that comes from a `gen` desugaring) is known to implement
/// `Iterator<Item = O>`, where `O` is given by the generator's yield type
/// that was computed during type-checking.
fn consider_builtin_iterator_candidate(
2024-06-18 19:13:54 -04:00
ecx: &mut EvalCtxt<'_, D>,
2024-06-17 17:59:08 -04:00
goal: Goal<I, Self>,
) -> Result<Candidate<I>, NoSolution>;
/// A coroutine (that comes from a `gen` desugaring) is known to implement
/// `FusedIterator`
fn consider_builtin_fused_iterator_candidate(
2024-06-18 19:13:54 -04:00
ecx: &mut EvalCtxt<'_, D>,
2024-06-17 17:59:08 -04:00
goal: Goal<I, Self>,
) -> Result<Candidate<I>, NoSolution>;
2023-11-28 18:18:19 +00:00
fn consider_builtin_async_iterator_candidate(
2024-06-18 19:13:54 -04:00
ecx: &mut EvalCtxt<'_, D>,
2024-06-17 17:59:08 -04:00
goal: Goal<I, Self>,
) -> Result<Candidate<I>, NoSolution>;
2023-11-28 18:18:19 +00:00
/// A coroutine (that doesn't come from an `async` or `gen` desugaring) is known to
2023-10-19 16:06:43 +00:00
/// implement `Coroutine<R, Yield = Y, Return = O>`, given the resume, yield,
2023-10-19 21:46:28 +00:00
/// and return types of the coroutine computed during type-checking.
fn consider_builtin_coroutine_candidate(
2024-06-18 19:13:54 -04:00
ecx: &mut EvalCtxt<'_, D>,
2024-06-17 17:59:08 -04:00
goal: Goal<I, Self>,
) -> Result<Candidate<I>, NoSolution>;
fn consider_builtin_discriminant_kind_candidate(
2024-06-18 19:13:54 -04:00
ecx: &mut EvalCtxt<'_, D>,
2024-06-17 17:59:08 -04:00
goal: Goal<I, Self>,
) -> Result<Candidate<I>, NoSolution>;
fn consider_builtin_async_destruct_candidate(
2024-06-18 19:13:54 -04:00
ecx: &mut EvalCtxt<'_, D>,
2024-06-17 17:59:08 -04:00
goal: Goal<I, Self>,
) -> Result<Candidate<I>, NoSolution>;
fn consider_builtin_destruct_candidate(
2024-06-18 19:13:54 -04:00
ecx: &mut EvalCtxt<'_, D>,
2024-06-17 17:59:08 -04:00
goal: Goal<I, Self>,
) -> Result<Candidate<I>, NoSolution>;
2023-04-09 00:09:53 +00:00
fn consider_builtin_transmute_candidate(
2024-06-18 19:13:54 -04:00
ecx: &mut EvalCtxt<'_, D>,
2024-06-17 17:59:08 -04:00
goal: Goal<I, Self>,
) -> Result<Candidate<I>, NoSolution>;
2023-07-24 22:02:52 +00:00
/// Consider (possibly several) candidates to upcast or unsize a type to another
/// type, excluding the coercion of a sized type into a `dyn Trait`.
2023-07-24 22:02:52 +00:00
///
/// We return the `BuiltinImplSource` for each candidate as it is needed
/// for unsize coercion in hir typeck and because it is difficult to
/// otherwise recompute this for codegen. This is a bit of a mess but the
/// easiest way to maintain the existing behavior for now.
fn consider_structural_builtin_unsize_candidates(
2024-06-18 19:13:54 -04:00
ecx: &mut EvalCtxt<'_, D>,
2024-06-17 17:59:08 -04:00
goal: Goal<I, Self>,
) -> Vec<Candidate<I>>;
2024-06-24 16:25:17 +00:00
2024-06-26 16:36:42 +00:00
fn consider_builtin_effects_intersection_candidate(
2024-06-24 16:25:17 +00:00
ecx: &mut EvalCtxt<'_, D>,
goal: Goal<I, Self>,
) -> Result<Candidate<I>, NoSolution>;
}
2023-01-17 20:24:58 +00:00
2024-06-18 19:13:54 -04:00
impl<D, I> EvalCtxt<'_, D>
2024-06-17 17:59:08 -04:00
where
2024-06-18 19:13:54 -04:00
D: SolverDelegate<Interner = I>,
2024-06-17 17:59:08 -04:00
I: Interner,
{
2024-06-18 19:13:54 -04:00
pub(super) fn assemble_and_evaluate_candidates<G: GoalKind<D>>(
&mut self,
2024-06-17 17:59:08 -04:00
goal: Goal<I, G>,
) -> Vec<Candidate<I>> {
let Ok(normalized_self_ty) =
self.structurally_normalize_ty(goal.param_env, goal.predicate.self_ty())
else {
return vec![];
};
if normalized_self_ty.is_ty_var() {
debug!("self type has been normalized to infer");
return self.forced_ambiguity(MaybeCause::Ambiguity).into_iter().collect();
}
2024-06-18 19:13:54 -04:00
let goal: Goal<I, G> =
goal.with(self.cx(), goal.predicate.with_self_ty(self.cx(), normalized_self_ty));
// Vars that show up in the rest of the goal substs may have been constrained by
// normalizing the self type as well, since type variables are not uniquified.
let goal = self.resolve_vars_if_possible(goal);
2023-01-19 03:26:54 +00:00
let mut candidates = vec![];
2022-12-19 07:01:38 +00:00
2024-06-17 17:59:08 -04:00
self.assemble_impl_candidates(goal, &mut candidates);
2022-12-19 07:01:38 +00:00
self.assemble_builtin_impl_candidates(goal, &mut candidates);
2022-12-19 07:01:38 +00:00
self.assemble_alias_bound_candidates(goal, &mut candidates);
2023-01-17 18:19:11 +00:00
self.assemble_object_bound_candidates(goal, &mut candidates);
self.assemble_param_env_candidates(goal, &mut candidates);
match self.solver_mode() {
SolverMode::Normal => self.discard_impls_shadowed_by_env(goal, &mut candidates),
SolverMode::Coherence => {
self.assemble_coherence_unknowable_candidates(goal, &mut candidates)
}
}
candidates
2022-12-19 07:01:38 +00:00
}
pub(super) fn forced_ambiguity(
&mut self,
cause: MaybeCause,
2024-06-17 17:59:08 -04:00
) -> Result<Candidate<I>, NoSolution> {
// This may fail if `try_evaluate_added_goals` overflows because it
// fails to reach a fixpoint but ends up getting an error after
// running for some additional step.
//
// cc trait-system-refactor-initiative#105
let source = CandidateSource::BuiltinImpl(BuiltinImplSource::Misc);
let certainty = Certainty::Maybe(cause);
self.probe_trait_candidate(source)
.enter(|this| this.evaluate_added_goals_and_make_canonical_response(certainty))
}
#[instrument(level = "trace", skip_all)]
2024-06-18 19:13:54 -04:00
fn assemble_impl_candidates<G: GoalKind<D>>(
&mut self,
2024-06-17 17:59:08 -04:00
goal: Goal<I, G>,
candidates: &mut Vec<Candidate<I>>,
) {
2024-06-21 12:39:55 -04:00
let cx = self.cx();
cx.for_each_relevant_impl(
goal.predicate.trait_def_id(cx),
2024-06-17 17:59:08 -04:00
goal.predicate.self_ty(),
|impl_def_id| {
// For every `default impl`, there's always a non-default `impl`
// that will *also* apply. There's no reason to register a candidate
// for this impl, since it is *not* proof that the trait goal holds.
2024-06-21 12:39:55 -04:00
if cx.impl_is_default(impl_def_id) {
2024-06-17 17:59:08 -04:00
return;
}
2024-06-17 17:59:08 -04:00
match G::consider_impl_candidate(self, goal, impl_def_id) {
Ok(candidate) => candidates.push(candidate),
Err(NoSolution) => (),
}
2024-06-17 17:59:08 -04:00
},
);
2022-12-19 07:01:38 +00:00
}
#[instrument(level = "trace", skip_all)]
2024-06-18 19:13:54 -04:00
fn assemble_builtin_impl_candidates<G: GoalKind<D>>(
&mut self,
2024-06-17 17:59:08 -04:00
goal: Goal<I, G>,
candidates: &mut Vec<Candidate<I>>,
) {
2024-06-21 12:39:55 -04:00
let cx = self.cx();
let trait_def_id = goal.predicate.trait_def_id(cx);
// N.B. When assembling built-in candidates for lang items that are also
// `auto` traits, then the auto trait candidate that is assembled in
// `consider_auto_trait_candidate` MUST be disqualified to remain sound.
//
// Instead of adding the logic here, it's a better idea to add it in
// `EvalCtxt::disqualify_auto_trait_candidate_due_to_possible_impl` in
// `solve::trait_goals` instead.
let result = if let Err(guar) = goal.predicate.error_reported() {
G::consider_error_guaranteed_candidate(self, guar)
2024-06-21 12:39:55 -04:00
} else if cx.trait_is_auto(trait_def_id) {
2023-01-17 20:16:30 +00:00
G::consider_auto_trait_candidate(self, goal)
2024-06-21 12:39:55 -04:00
} else if cx.trait_is_alias(trait_def_id) {
2023-01-17 20:16:30 +00:00
G::consider_trait_alias_candidate(self, goal)
} else {
match cx.as_lang_item(trait_def_id) {
Some(TraitSolverLangItem::Sized) => G::consider_builtin_sized_candidate(self, goal),
Some(TraitSolverLangItem::Copy | TraitSolverLangItem::Clone) => {
G::consider_builtin_copy_clone_candidate(self, goal)
}
Some(TraitSolverLangItem::Fn) => {
G::consider_builtin_fn_trait_candidates(self, goal, ty::ClosureKind::Fn)
}
Some(TraitSolverLangItem::FnMut) => {
G::consider_builtin_fn_trait_candidates(self, goal, ty::ClosureKind::FnMut)
}
Some(TraitSolverLangItem::FnOnce) => {
G::consider_builtin_fn_trait_candidates(self, goal, ty::ClosureKind::FnOnce)
}
Some(TraitSolverLangItem::AsyncFn) => {
G::consider_builtin_async_fn_trait_candidates(self, goal, ty::ClosureKind::Fn)
}
Some(TraitSolverLangItem::AsyncFnMut) => {
G::consider_builtin_async_fn_trait_candidates(
self,
goal,
ty::ClosureKind::FnMut,
)
}
Some(TraitSolverLangItem::AsyncFnOnce) => {
G::consider_builtin_async_fn_trait_candidates(
self,
goal,
ty::ClosureKind::FnOnce,
)
}
Some(TraitSolverLangItem::PointerLike) => {
G::consider_builtin_pointer_like_candidate(self, goal)
}
Some(TraitSolverLangItem::FnPtrTrait) => {
G::consider_builtin_fn_ptr_trait_candidate(self, goal)
}
Some(TraitSolverLangItem::AsyncFnKindHelper) => {
G::consider_builtin_async_fn_kind_helper_candidate(self, goal)
}
Some(TraitSolverLangItem::Tuple) => G::consider_builtin_tuple_candidate(self, goal),
Some(TraitSolverLangItem::PointeeTrait) => {
G::consider_builtin_pointee_candidate(self, goal)
}
Some(TraitSolverLangItem::Future) => {
G::consider_builtin_future_candidate(self, goal)
}
Some(TraitSolverLangItem::Iterator) => {
G::consider_builtin_iterator_candidate(self, goal)
}
Some(TraitSolverLangItem::FusedIterator) => {
G::consider_builtin_fused_iterator_candidate(self, goal)
}
Some(TraitSolverLangItem::AsyncIterator) => {
G::consider_builtin_async_iterator_candidate(self, goal)
}
Some(TraitSolverLangItem::Coroutine) => {
G::consider_builtin_coroutine_candidate(self, goal)
}
Some(TraitSolverLangItem::DiscriminantKind) => {
G::consider_builtin_discriminant_kind_candidate(self, goal)
}
Some(TraitSolverLangItem::AsyncDestruct) => {
G::consider_builtin_async_destruct_candidate(self, goal)
}
Some(TraitSolverLangItem::Destruct) => {
G::consider_builtin_destruct_candidate(self, goal)
}
Some(TraitSolverLangItem::TransmuteTrait) => {
G::consider_builtin_transmute_candidate(self, goal)
}
Some(TraitSolverLangItem::EffectsIntersection) => {
G::consider_builtin_effects_intersection_candidate(self, goal)
}
_ => Err(NoSolution),
}
};
candidates.extend(result);
2023-01-23 23:56:54 +00:00
// There may be multiple unsize candidates for a trait with several supertraits:
// `trait Foo: Bar<A> + Bar<B>` and `dyn Foo: Unsize<dyn Bar<_>>`
2024-06-21 12:39:55 -04:00
if cx.is_lang_item(trait_def_id, TraitSolverLangItem::Unsize) {
candidates.extend(G::consider_structural_builtin_unsize_candidates(self, goal));
2023-01-23 23:56:54 +00:00
}
}
#[instrument(level = "trace", skip_all)]
2024-06-18 19:13:54 -04:00
fn assemble_param_env_candidates<G: GoalKind<D>>(
&mut self,
2024-06-17 17:59:08 -04:00
goal: Goal<I, G>,
candidates: &mut Vec<Candidate<I>>,
) {
2024-06-17 17:59:08 -04:00
for (i, assumption) in goal.param_env.caller_bounds().into_iter().enumerate() {
candidates.extend(G::probe_and_consider_implied_clause(
self,
CandidateSource::ParamEnv(i),
goal,
assumption,
[],
));
}
}
#[instrument(level = "trace", skip_all)]
2024-06-18 19:13:54 -04:00
fn assemble_alias_bound_candidates<G: GoalKind<D>>(
&mut self,
2024-06-17 17:59:08 -04:00
goal: Goal<I, G>,
candidates: &mut Vec<Candidate<I>>,
) {
2024-02-07 00:19:12 +00:00
let () = self.probe(|_| ProbeKind::NormalizedSelfTyAssembly).enter(|ecx| {
ecx.assemble_alias_bound_candidates_recur(goal.predicate.self_ty(), goal, candidates);
});
}
2024-02-07 00:19:12 +00:00
/// For some deeply nested `<T>::A::B::C::D` rigid associated type,
/// we should explore the item bounds for all levels, since the
/// `associated_type_bounds` feature means that a parent associated
/// type may carry bounds for a nested associated type.
///
/// If we have a projection, check that its self type is a rigid projection.
/// If so, continue searching by recursively calling after normalization.
// FIXME: This may recurse infinitely, but I can't seem to trigger it without
// hitting another overflow error something. Add a depth parameter needed later.
2024-06-18 19:13:54 -04:00
fn assemble_alias_bound_candidates_recur<G: GoalKind<D>>(
2024-02-07 00:19:12 +00:00
&mut self,
2024-06-17 17:59:08 -04:00
self_ty: I::Ty,
goal: Goal<I, G>,
candidates: &mut Vec<Candidate<I>>,
2024-02-07 00:19:12 +00:00
) {
2024-06-17 17:59:08 -04:00
let (kind, alias_ty) = match self_ty.kind() {
ty::Bool
| ty::Char
| ty::Int(_)
| ty::Uint(_)
| ty::Float(_)
| ty::Adt(_, _)
| ty::Foreign(_)
| ty::Str
| ty::Array(_, _)
| ty::Pat(_, _)
| ty::Slice(_)
| ty::RawPtr(_, _)
| ty::Ref(_, _, _)
| ty::FnDef(_, _)
| ty::FnPtr(..)
| ty::Dynamic(..)
| ty::Closure(..)
| ty::CoroutineClosure(..)
2023-10-19 16:06:43 +00:00
| ty::Coroutine(..)
| ty::CoroutineWitness(..)
| ty::Never
| ty::Tuple(_)
| ty::Param(_)
| ty::Placeholder(..)
| ty::Infer(ty::IntVar(_) | ty::FloatVar(_))
| ty::Error(_) => return,
2024-02-07 00:19:12 +00:00
ty::Infer(ty::FreshTy(_) | ty::FreshIntTy(_) | ty::FreshFloatTy(_)) | ty::Bound(..) => {
2024-06-17 17:59:08 -04:00
panic!("unexpected self type for `{goal:?}`")
2024-02-07 00:19:12 +00:00
}
2024-02-07 00:19:12 +00:00
ty::Infer(ty::TyVar(_)) => {
// If we hit infer when normalizing the self type of an alias,
// then bail with ambiguity. We should never encounter this on
// the *first* iteration of this recursive function.
if let Ok(result) =
self.evaluate_added_goals_and_make_canonical_response(Certainty::AMBIGUOUS)
{
2024-02-07 00:19:12 +00:00
candidates.push(Candidate { source: CandidateSource::AliasBound, result });
}
2024-02-07 00:19:12 +00:00
return;
}
2024-02-07 00:19:12 +00:00
ty::Alias(kind @ (ty::Projection | ty::Opaque), alias_ty) => (kind, alias_ty),
ty::Alias(ty::Inherent | ty::Weak, _) => {
2024-06-18 19:13:54 -04:00
self.cx().delay_bug(format!("could not normalize {self_ty:?}, it is not WF"));
return;
2024-02-07 00:19:12 +00:00
}
};
2024-06-18 19:13:54 -04:00
for assumption in
self.cx().item_bounds(alias_ty.def_id).iter_instantiated(self.cx(), alias_ty.args)
{
candidates.extend(G::probe_and_consider_implied_clause(
self,
CandidateSource::AliasBound,
goal,
assumption,
[],
));
2024-02-07 00:19:12 +00:00
}
if kind != ty::Projection {
return;
}
// Recurse on the self type of the projection.
match self.structurally_normalize_ty(goal.param_env, alias_ty.self_ty()) {
Ok(next_self_ty) => {
self.assemble_alias_bound_candidates_recur(next_self_ty, goal, candidates)
}
Err(NoSolution) => {}
}
}
2023-01-17 18:19:11 +00:00
#[instrument(level = "trace", skip_all)]
2024-06-18 19:13:54 -04:00
fn assemble_object_bound_candidates<G: GoalKind<D>>(
2023-01-17 18:19:11 +00:00
&mut self,
2024-06-17 17:59:08 -04:00
goal: Goal<I, G>,
candidates: &mut Vec<Candidate<I>>,
2023-01-17 18:19:11 +00:00
) {
2024-06-21 12:39:55 -04:00
let cx = self.cx();
if !cx.trait_may_be_implemented_via_object(goal.predicate.trait_def_id(cx)) {
2023-06-02 05:54:52 +00:00
return;
}
2023-01-17 18:19:11 +00:00
let self_ty = goal.predicate.self_ty();
2024-06-17 17:59:08 -04:00
let bounds = match self_ty.kind() {
2023-01-17 18:19:11 +00:00
ty::Bool
| ty::Char
| ty::Int(_)
| ty::Uint(_)
| ty::Float(_)
| ty::Adt(_, _)
| ty::Foreign(_)
| ty::Str
| ty::Array(_, _)
| ty::Pat(_, _)
2023-01-17 18:19:11 +00:00
| ty::Slice(_)
| ty::RawPtr(_, _)
2023-01-17 18:19:11 +00:00
| ty::Ref(_, _, _)
| ty::FnDef(_, _)
| ty::FnPtr(..)
2023-01-17 18:19:11 +00:00
| ty::Alias(..)
| ty::Closure(..)
| ty::CoroutineClosure(..)
2023-10-19 16:06:43 +00:00
| ty::Coroutine(..)
| ty::CoroutineWitness(..)
2023-01-17 18:19:11 +00:00
| ty::Never
| ty::Tuple(_)
| ty::Param(_)
| ty::Placeholder(..)
| ty::Infer(ty::IntVar(_) | ty::FloatVar(_))
2023-01-17 18:19:11 +00:00
| ty::Error(_) => return,
ty::Infer(ty::TyVar(_) | ty::FreshTy(_) | ty::FreshIntTy(_) | ty::FreshFloatTy(_))
2024-06-17 17:59:08 -04:00
| ty::Bound(..) => panic!("unexpected self type for `{goal:?}`"),
2023-01-17 18:19:11 +00:00
ty::Dynamic(bounds, ..) => bounds,
};
// Do not consider built-in object impls for non-object-safe types.
2024-06-21 12:39:55 -04:00
if bounds.principal_def_id().is_some_and(|def_id| !cx.trait_is_object_safe(def_id)) {
return;
}
2023-07-24 22:02:52 +00:00
// Consider all of the auto-trait and projection bounds, which don't
// need to be recorded as a `BuiltinImplSource::Object` since they don't
// really have a vtable base...
for bound in bounds.iter() {
2023-07-24 22:02:52 +00:00
match bound.skip_binder() {
ty::ExistentialPredicate::Trait(_) => {
// Skip principal
}
ty::ExistentialPredicate::Projection(_)
| ty::ExistentialPredicate::AutoTrait(_) => {
candidates.extend(G::probe_and_consider_object_bound_candidate(
2023-07-24 22:02:52 +00:00
self,
CandidateSource::BuiltinImpl(BuiltinImplSource::Misc),
2023-07-24 22:02:52 +00:00
goal,
2024-06-21 12:39:55 -04:00
bound.with_self_ty(cx, self_ty),
));
2023-07-24 22:02:52 +00:00
}
}
2023-07-24 22:02:52 +00:00
}
2023-07-24 22:02:52 +00:00
// FIXME: We only need to do *any* of this if we're considering a trait goal,
// since we don't need to look at any supertrait or anything if we are doing
// a projection goal.
if let Some(principal) = bounds.principal() {
2024-06-21 12:39:55 -04:00
let principal_trait_ref = principal.with_self_ty(cx, self_ty);
2024-07-06 12:37:04 -04:00
for (idx, assumption) in elaborate::supertraits(cx, principal_trait_ref).enumerate() {
candidates.extend(G::probe_and_consider_object_bound_candidate(
self,
CandidateSource::BuiltinImpl(BuiltinImplSource::Object(idx)),
2024-04-26 12:36:33 -04:00
goal,
2024-06-21 12:39:55 -04:00
assumption.upcast(cx),
));
}
2023-01-17 18:19:11 +00:00
}
}
2023-02-08 19:25:21 +00:00
2024-02-09 10:06:16 +01:00
/// In coherence we have to not only care about all impls we know about, but
/// also consider impls which may get added in a downstream or sibling crate
/// or which an upstream impl may add in a minor release.
///
/// To do so we add an ambiguous candidate in case such an unknown impl could
/// apply to the current goal.
#[instrument(level = "trace", skip_all)]
2024-06-18 19:13:54 -04:00
fn assemble_coherence_unknowable_candidates<G: GoalKind<D>>(
2023-03-21 16:26:23 +01:00
&mut self,
2024-06-17 17:59:08 -04:00
goal: Goal<I, G>,
candidates: &mut Vec<Candidate<I>>,
2023-03-21 16:26:23 +01:00
) {
2024-06-21 12:39:55 -04:00
let cx = self.cx();
candidates.extend(self.probe_trait_candidate(CandidateSource::CoherenceUnknowable).enter(
|ecx| {
2024-06-21 12:39:55 -04:00
let trait_ref = goal.predicate.trait_ref(cx);
2024-05-02 15:44:05 +00:00
if ecx.trait_ref_is_knowable(goal.param_env, trait_ref)? {
Err(NoSolution)
} else {
// While the trait bound itself may be unknowable, we may be able to
// prove that a super trait is not implemented. For this, we recursively
// prove the super trait bounds of the current goal.
//
// We skip the goal itself as that one would cycle.
let predicate: I::Predicate = trait_ref.upcast(cx);
ecx.add_goals(
GoalSource::Misc,
elaborate::elaborate(cx, [predicate])
.skip(1)
.map(|predicate| goal.with(cx, predicate)),
);
2024-05-02 15:44:05 +00:00
ecx.evaluate_added_goals_and_make_canonical_response(Certainty::AMBIGUOUS)
}
},
))
2023-03-21 16:26:23 +01:00
}
/// If there's a where-bound for the current goal, do not use any impl candidates
/// to prove the current goal. Most importantly, if there is a where-bound which does
/// not specify any associated types, we do not allow normalizing the associated type
/// by using an impl, even if it would apply.
///
/// <https://github.com/rust-lang/trait-system-refactor-initiative/issues/76>
// FIXME(@lcnr): The current structure here makes me unhappy and feels ugly. idk how
// to improve this however. However, this should make it fairly straightforward to refine
// the filtering going forward, so it seems alright-ish for now.
#[instrument(level = "debug", skip(self, goal))]
2024-06-18 19:13:54 -04:00
fn discard_impls_shadowed_by_env<G: GoalKind<D>>(
&mut self,
2024-06-17 17:59:08 -04:00
goal: Goal<I, G>,
candidates: &mut Vec<Candidate<I>>,
) {
2024-06-21 12:39:55 -04:00
let cx = self.cx();
2024-06-17 17:59:08 -04:00
let trait_goal: Goal<I, ty::TraitPredicate<I>> =
2024-06-21 12:39:55 -04:00
goal.with(cx, goal.predicate.trait_ref(cx));
2024-05-07 11:09:21 -04:00
let mut trait_candidates_from_env = vec![];
self.probe(|_| ProbeKind::ShadowedEnvProbing).enter(|ecx| {
ecx.assemble_param_env_candidates(trait_goal, &mut trait_candidates_from_env);
ecx.assemble_alias_bound_candidates(trait_goal, &mut trait_candidates_from_env);
});
if !trait_candidates_from_env.is_empty() {
let trait_env_result = self.merge_candidates(trait_candidates_from_env);
match trait_env_result.unwrap().value.certainty {
// If proving the trait goal succeeds by using the env,
// we freely drop all impl candidates.
//
// FIXME(@lcnr): It feels like this could easily hide
// a forced ambiguity candidate added earlier.
// This feels dangerous.
Certainty::Yes => {
candidates.retain(|c| match c.source {
2024-02-26 10:12:40 +01:00
CandidateSource::Impl(_) | CandidateSource::BuiltinImpl(_) => {
debug!(?c, "discard impl candidate");
2024-02-26 10:12:40 +01:00
false
}
CandidateSource::ParamEnv(_) | CandidateSource::AliasBound => true,
2024-06-17 17:59:08 -04:00
CandidateSource::CoherenceUnknowable => panic!("uh oh"),
});
}
// If it is still ambiguous we instead just force the whole goal
// to be ambig and wait for inference constraints. See
// tests/ui/traits/next-solver/env-shadows-impls/ambig-env-no-shadow.rs
Certainty::Maybe(cause) => {
debug!(?cause, "force ambiguity");
*candidates = self.forced_ambiguity(cause).into_iter().collect();
}
}
}
}
2023-03-29 15:36:17 +02:00
/// If there are multiple ways to prove a trait or projection goal, we have
/// to somehow try to merge the candidates into one. If that fails, we return
/// ambiguity.
#[instrument(level = "debug", skip(self), ret)]
2024-06-17 17:59:08 -04:00
pub(super) fn merge_candidates(&mut self, candidates: Vec<Candidate<I>>) -> QueryResult<I> {
2023-03-29 15:36:17 +02:00
// First try merging all candidates. This is complete and fully sound.
let responses = candidates.iter().map(|c| c.result).collect::<Vec<_>>();
if let Some(result) = self.try_merge_responses(&responses) {
return Ok(result);
} else {
self.flounder(&responses)
2023-02-08 19:25:21 +00:00
}
}
2022-12-19 07:01:38 +00:00
}