2022-12-19 07:01:38 +00:00
|
|
|
//! Code shared by trait and projection goals for candidate assembly.
|
|
|
|
|
2023-03-21 16:26:23 +01:00
|
|
|
use super::{EvalCtxt, SolverMode};
|
2023-12-18 07:49:46 +01:00
|
|
|
use crate::solve::GoalSource;
|
2023-03-21 16:26:23 +01:00
|
|
|
use crate::traits::coherence;
|
2022-12-19 07:01:38 +00:00
|
|
|
use rustc_hir::def_id::DefId;
|
|
|
|
use rustc_infer::traits::query::NoSolution;
|
2023-09-11 11:34:57 +02:00
|
|
|
use rustc_middle::traits::solve::inspect::ProbeKind;
|
|
|
|
use rustc_middle::traits::solve::{
|
2024-02-09 10:40:26 +01:00
|
|
|
CandidateSource, CanonicalResponse, Certainty, Goal, MaybeCause, QueryResult,
|
2023-09-11 11:34:57 +02:00
|
|
|
};
|
2023-07-24 22:02:52 +00:00
|
|
|
use rustc_middle::traits::BuiltinImplSource;
|
2023-07-18 18:07:42 +02:00
|
|
|
use rustc_middle::ty::fast_reject::{SimplifiedType, TreatParams};
|
2022-12-19 07:01:38 +00:00
|
|
|
use rustc_middle::ty::{self, Ty, TyCtxt};
|
2023-07-18 18:07:42 +02:00
|
|
|
use rustc_middle::ty::{fast_reject, TypeFoldable};
|
2023-07-24 22:02:52 +00:00
|
|
|
use rustc_middle::ty::{ToPredicate, TypeVisitableExt};
|
2024-02-10 23:49:16 +00:00
|
|
|
use rustc_span::{ErrorGuaranteed, DUMMY_SP};
|
2022-12-19 07:01:38 +00:00
|
|
|
use std::fmt::Debug;
|
|
|
|
|
2023-03-29 15:44:23 +02:00
|
|
|
pub(super) mod structural_traits;
|
|
|
|
|
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(Debug, Clone)]
|
2023-01-17 11:47:47 +01:00
|
|
|
pub(super) struct Candidate<'tcx> {
|
|
|
|
pub(super) source: CandidateSource,
|
2022-12-19 07:01:38 +00:00
|
|
|
pub(super) result: CanonicalResponse<'tcx>,
|
|
|
|
}
|
|
|
|
|
2023-01-27 20:45:03 +01:00
|
|
|
/// Methods used to assemble candidates for either trait or projection goals.
|
2023-04-22 03:11:25 +00:00
|
|
|
pub(super) trait GoalKind<'tcx>:
|
|
|
|
TypeFoldable<TyCtxt<'tcx>> + Copy + Eq + std::fmt::Display
|
|
|
|
{
|
2022-12-19 07:01:38 +00:00
|
|
|
fn self_ty(self) -> Ty<'tcx>;
|
|
|
|
|
2023-03-21 16:26:23 +01:00
|
|
|
fn trait_ref(self, tcx: TyCtxt<'tcx>) -> ty::TraitRef<'tcx>;
|
|
|
|
|
2022-12-19 07:01:38 +00:00
|
|
|
fn with_self_ty(self, tcx: TyCtxt<'tcx>, self_ty: Ty<'tcx>) -> Self;
|
|
|
|
|
|
|
|
fn trait_def_id(self, tcx: TyCtxt<'tcx>) -> DefId;
|
|
|
|
|
2023-07-18 18:07:42 +02: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`]).
|
2023-04-22 04:51:35 +00:00
|
|
|
fn probe_and_match_goal_against_assumption(
|
|
|
|
ecx: &mut EvalCtxt<'_, 'tcx>,
|
|
|
|
goal: Goal<'tcx, Self>,
|
2023-06-16 06:27:41 +00:00
|
|
|
assumption: ty::Clause<'tcx>,
|
2023-04-22 04:51:35 +00:00
|
|
|
then: impl FnOnce(&mut EvalCtxt<'_, 'tcx>) -> QueryResult<'tcx>,
|
|
|
|
) -> QueryResult<'tcx>;
|
|
|
|
|
2023-07-18 18:07:42 +02:00
|
|
|
/// 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.
|
2023-02-16 03:04:08 +00:00
|
|
|
fn consider_implied_clause(
|
2023-01-17 11:47:47 +01:00
|
|
|
ecx: &mut EvalCtxt<'_, 'tcx>,
|
|
|
|
goal: Goal<'tcx, Self>,
|
2023-06-16 06:27:41 +00:00
|
|
|
assumption: ty::Clause<'tcx>,
|
2023-02-16 03:04:08 +00:00
|
|
|
requirements: impl IntoIterator<Item = Goal<'tcx, ty::Predicate<'tcx>>>,
|
2023-04-22 04:51:35 +00:00
|
|
|
) -> QueryResult<'tcx> {
|
|
|
|
Self::probe_and_match_goal_against_assumption(ecx, goal, assumption, |ecx| {
|
2023-12-18 07:49:46 +01:00
|
|
|
// FIXME(-Znext-solver=coinductive): check whether this should be
|
|
|
|
// `GoalSource::ImplWhereBound` for any caller.
|
|
|
|
ecx.add_goals(GoalSource::Misc, requirements);
|
2023-04-22 04:51:35 +00:00
|
|
|
ecx.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
|
|
|
|
})
|
|
|
|
}
|
2023-02-10 02:10:42 +00:00
|
|
|
|
2023-07-18 18:07:42 +02:00
|
|
|
/// 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.
|
2023-02-22 01:11:57 +00:00
|
|
|
fn consider_object_bound_candidate(
|
|
|
|
ecx: &mut EvalCtxt<'_, 'tcx>,
|
|
|
|
goal: Goal<'tcx, Self>,
|
2023-06-16 06:27:41 +00:00
|
|
|
assumption: ty::Clause<'tcx>,
|
2023-04-22 04:51:35 +00:00
|
|
|
) -> QueryResult<'tcx> {
|
|
|
|
Self::probe_and_match_goal_against_assumption(ecx, goal, assumption, |ecx| {
|
|
|
|
let tcx = ecx.tcx();
|
|
|
|
let ty::Dynamic(bounds, _, _) = *goal.predicate.self_ty().kind() else {
|
|
|
|
bug!("expected object type in `consider_object_bound_candidate`");
|
|
|
|
};
|
2023-12-18 07:49:46 +01:00
|
|
|
// FIXME(-Znext-solver=coinductive): Should this be `GoalSource::ImplWhereBound`?
|
|
|
|
ecx.add_goals(
|
|
|
|
GoalSource::Misc,
|
|
|
|
structural_traits::predicates_for_object_candidate(
|
|
|
|
ecx,
|
|
|
|
goal.param_env,
|
|
|
|
goal.predicate.trait_ref(tcx),
|
|
|
|
bounds,
|
|
|
|
),
|
|
|
|
);
|
2023-04-22 04:51:35 +00:00
|
|
|
ecx.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
|
|
|
|
})
|
|
|
|
}
|
2023-02-22 01:11:57 +00:00
|
|
|
|
2023-02-16 03:04:08 +00:00
|
|
|
fn consider_impl_candidate(
|
2023-02-10 02:10:42 +00:00
|
|
|
ecx: &mut EvalCtxt<'_, 'tcx>,
|
|
|
|
goal: Goal<'tcx, Self>,
|
2023-02-16 03:04:08 +00:00
|
|
|
impl_def_id: DefId,
|
2023-11-20 15:01:31 +01:00
|
|
|
) -> Result<Candidate<'tcx>, 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-18 18:07:42 +02:00
|
|
|
///
|
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.
|
2023-07-18 18:07:42 +02:00
|
|
|
fn consider_error_guaranteed_candidate(
|
|
|
|
ecx: &mut EvalCtxt<'_, 'tcx>,
|
|
|
|
guar: ErrorGuaranteed,
|
|
|
|
) -> QueryResult<'tcx>;
|
|
|
|
|
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(
|
|
|
|
ecx: &mut EvalCtxt<'_, 'tcx>,
|
|
|
|
goal: Goal<'tcx, Self>,
|
|
|
|
) -> QueryResult<'tcx>;
|
|
|
|
|
2023-07-18 18:07:42 +02: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(
|
|
|
|
ecx: &mut EvalCtxt<'_, 'tcx>,
|
|
|
|
goal: Goal<'tcx, Self>,
|
|
|
|
) -> QueryResult<'tcx>;
|
|
|
|
|
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(
|
2023-01-17 11:47:47 +01:00
|
|
|
ecx: &mut EvalCtxt<'_, 'tcx>,
|
|
|
|
goal: Goal<'tcx, Self>,
|
2023-01-17 19:29:52 +00:00
|
|
|
) -> QueryResult<'tcx>;
|
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(
|
|
|
|
ecx: &mut EvalCtxt<'_, 'tcx>,
|
|
|
|
goal: Goal<'tcx, Self>,
|
|
|
|
) -> QueryResult<'tcx>;
|
2023-01-18 23:21:12 +00:00
|
|
|
|
2023-07-18 18:07:42 +02: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(
|
2023-01-18 23:21:12 +00:00
|
|
|
ecx: &mut EvalCtxt<'_, 'tcx>,
|
|
|
|
goal: Goal<'tcx, Self>,
|
|
|
|
) -> QueryResult<'tcx>;
|
2023-01-19 01:20:34 +00:00
|
|
|
|
2023-07-18 18:07:42 +02:00
|
|
|
/// 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(
|
|
|
|
ecx: &mut EvalCtxt<'_, 'tcx>,
|
|
|
|
goal: Goal<'tcx, Self>,
|
|
|
|
) -> QueryResult<'tcx>;
|
|
|
|
|
2023-07-18 18:07:42 +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.
|
2023-01-19 01:20:34 +00:00
|
|
|
fn consider_builtin_fn_trait_candidates(
|
|
|
|
ecx: &mut EvalCtxt<'_, 'tcx>,
|
|
|
|
goal: Goal<'tcx, Self>,
|
|
|
|
kind: ty::ClosureKind,
|
|
|
|
) -> QueryResult<'tcx>;
|
|
|
|
|
2024-01-24 22:27:25 +00:00
|
|
|
/// 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(
|
|
|
|
ecx: &mut EvalCtxt<'_, 'tcx>,
|
|
|
|
goal: Goal<'tcx, Self>,
|
|
|
|
kind: ty::ClosureKind,
|
|
|
|
) -> QueryResult<'tcx>;
|
|
|
|
|
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.
|
2024-01-24 22:27:25 +00:00
|
|
|
fn consider_builtin_async_fn_kind_helper_candidate(
|
|
|
|
ecx: &mut EvalCtxt<'_, 'tcx>,
|
|
|
|
goal: Goal<'tcx, Self>,
|
|
|
|
) -> QueryResult<'tcx>;
|
|
|
|
|
2023-07-18 18:07:42 +02:00
|
|
|
/// `Tuple` is implemented if the `Self` type is a tuple.
|
2023-01-19 01:20:34 +00:00
|
|
|
fn consider_builtin_tuple_candidate(
|
|
|
|
ecx: &mut EvalCtxt<'_, 'tcx>,
|
|
|
|
goal: Goal<'tcx, Self>,
|
|
|
|
) -> QueryResult<'tcx>;
|
2023-01-24 23:24:25 +00:00
|
|
|
|
2023-07-18 18:07:42 +02: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(
|
|
|
|
ecx: &mut EvalCtxt<'_, 'tcx>,
|
|
|
|
goal: Goal<'tcx, Self>,
|
|
|
|
) -> QueryResult<'tcx>;
|
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
|
2023-07-18 18:07:42 +02:00
|
|
|
/// that was computed during type-checking.
|
2023-01-24 23:38:20 +00:00
|
|
|
fn consider_builtin_future_candidate(
|
|
|
|
ecx: &mut EvalCtxt<'_, 'tcx>,
|
|
|
|
goal: Goal<'tcx, Self>,
|
|
|
|
) -> QueryResult<'tcx>;
|
|
|
|
|
2023-10-20 22:48:37 +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(
|
|
|
|
ecx: &mut EvalCtxt<'_, 'tcx>,
|
|
|
|
goal: Goal<'tcx, Self>,
|
|
|
|
) -> QueryResult<'tcx>;
|
|
|
|
|
2024-03-22 00:48:36 +09:00
|
|
|
/// A coroutine (that comes from a `gen` desugaring) is known to implement
|
|
|
|
/// `FusedIterator`
|
|
|
|
fn consider_builtin_fused_iterator_candidate(
|
|
|
|
ecx: &mut EvalCtxt<'_, 'tcx>,
|
|
|
|
goal: Goal<'tcx, Self>,
|
|
|
|
) -> QueryResult<'tcx>;
|
|
|
|
|
2023-11-28 18:18:19 +00:00
|
|
|
fn consider_builtin_async_iterator_candidate(
|
|
|
|
ecx: &mut EvalCtxt<'_, 'tcx>,
|
|
|
|
goal: Goal<'tcx, Self>,
|
|
|
|
) -> QueryResult<'tcx>;
|
|
|
|
|
2023-10-20 22:48:37 +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(
|
2023-01-24 23:38:20 +00:00
|
|
|
ecx: &mut EvalCtxt<'_, 'tcx>,
|
|
|
|
goal: Goal<'tcx, Self>,
|
|
|
|
) -> QueryResult<'tcx>;
|
2023-01-23 22:33:59 +00:00
|
|
|
|
2023-01-27 20:45:03 +01:00
|
|
|
fn consider_builtin_discriminant_kind_candidate(
|
|
|
|
ecx: &mut EvalCtxt<'_, 'tcx>,
|
|
|
|
goal: Goal<'tcx, Self>,
|
|
|
|
) -> QueryResult<'tcx>;
|
2023-03-22 17:13:00 +00:00
|
|
|
|
2024-02-13 12:31:41 +03:00
|
|
|
fn consider_builtin_async_destruct_candidate(
|
|
|
|
ecx: &mut EvalCtxt<'_, 'tcx>,
|
|
|
|
goal: Goal<'tcx, Self>,
|
|
|
|
) -> QueryResult<'tcx>;
|
|
|
|
|
2023-03-22 17:13:00 +00:00
|
|
|
fn consider_builtin_destruct_candidate(
|
|
|
|
ecx: &mut EvalCtxt<'_, 'tcx>,
|
|
|
|
goal: Goal<'tcx, Self>,
|
|
|
|
) -> QueryResult<'tcx>;
|
2023-04-09 00:09:53 +00:00
|
|
|
|
|
|
|
fn consider_builtin_transmute_candidate(
|
|
|
|
ecx: &mut EvalCtxt<'_, 'tcx>,
|
|
|
|
goal: Goal<'tcx, Self>,
|
|
|
|
) -> QueryResult<'tcx>;
|
2023-07-24 22:02:52 +00:00
|
|
|
|
|
|
|
/// Consider (possibly several) candidates to upcast or unsize a type to another
|
2023-08-14 23:37:06 +00:00
|
|
|
/// 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.
|
2023-08-14 23:37:06 +00:00
|
|
|
fn consider_structural_builtin_unsize_candidates(
|
2023-07-24 22:02:52 +00:00
|
|
|
ecx: &mut EvalCtxt<'_, 'tcx>,
|
|
|
|
goal: Goal<'tcx, Self>,
|
|
|
|
) -> Vec<(CanonicalResponse<'tcx>, BuiltinImplSource)>;
|
2023-01-17 11:47:47 +01:00
|
|
|
}
|
2023-01-17 20:24:58 +00:00
|
|
|
|
2023-01-17 11:47:47 +01:00
|
|
|
impl<'tcx> EvalCtxt<'_, 'tcx> {
|
|
|
|
pub(super) fn assemble_and_evaluate_candidates<G: GoalKind<'tcx>>(
|
|
|
|
&mut self,
|
2023-01-17 10:21:30 +01:00
|
|
|
goal: Goal<'tcx, G>,
|
2023-01-17 11:47:47 +01:00
|
|
|
) -> Vec<Candidate<'tcx>> {
|
2024-02-01 12:34:38 +01:00
|
|
|
let Ok(normalized_self_ty) =
|
|
|
|
self.structurally_normalize_ty(goal.param_env, goal.predicate.self_ty())
|
2024-01-08 15:29:44 +01:00
|
|
|
else {
|
2024-02-01 12:34:38 +01:00
|
|
|
return vec![];
|
2024-01-08 15:29:44 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
if normalized_self_ty.is_ty_var() {
|
|
|
|
debug!("self type has been normalized to infer");
|
2024-02-09 10:40:26 +01:00
|
|
|
return self.forced_ambiguity(MaybeCause::Ambiguity);
|
2023-11-20 13:57:44 +01:00
|
|
|
}
|
2023-07-18 18:07:42 +02:00
|
|
|
|
2024-01-08 15:29:44 +01:00
|
|
|
let goal =
|
|
|
|
goal.with(self.tcx(), goal.predicate.with_self_ty(self.tcx(), normalized_self_ty));
|
2024-03-10 22:32:55 -04:00
|
|
|
// 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
|
|
|
|
2024-01-08 15:29:44 +01:00
|
|
|
let mut candidates = vec![];
|
2022-12-19 07:01:38 +00:00
|
|
|
|
2023-07-18 18:07:42 +02:00
|
|
|
self.assemble_non_blanket_impl_candidates(goal, &mut candidates);
|
2022-12-19 07:01:38 +00:00
|
|
|
|
2023-01-17 11:47:47 +01:00
|
|
|
self.assemble_builtin_impl_candidates(goal, &mut candidates);
|
2022-12-19 07:01:38 +00:00
|
|
|
|
2023-01-17 11:47:47 +01: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);
|
|
|
|
|
2024-01-08 15:29:44 +01:00
|
|
|
self.assemble_blanket_impl_candidates(goal, &mut candidates);
|
|
|
|
|
|
|
|
self.assemble_param_env_candidates(goal, &mut candidates);
|
|
|
|
|
2024-02-09 10:40:26 +01:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
2024-01-08 15:29:44 +01:00
|
|
|
|
|
|
|
candidates
|
2022-12-19 07:01:38 +00:00
|
|
|
}
|
|
|
|
|
2024-02-09 10:40:26 +01:00
|
|
|
fn forced_ambiguity(&mut self, cause: MaybeCause) -> Vec<Candidate<'tcx>> {
|
|
|
|
let source = CandidateSource::BuiltinImpl(BuiltinImplSource::Misc);
|
|
|
|
let certainty = Certainty::Maybe(cause);
|
2024-04-04 07:47:13 +02:00
|
|
|
// 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.
|
|
|
|
//
|
|
|
|
// FIXME: Add a test for this. It seems to be necessary for typenum but
|
|
|
|
// is incredibly hard to minimize as it may rely on being inside of a
|
|
|
|
// trait solver cycle.
|
|
|
|
let result = self.evaluate_added_goals_and_make_canonical_response(certainty);
|
2024-02-09 10:40:26 +01:00
|
|
|
let mut dummy_probe = self.inspect.new_probe();
|
2024-04-04 07:47:13 +02:00
|
|
|
dummy_probe.probe_kind(ProbeKind::TraitCandidate { source, result });
|
2024-02-09 10:40:26 +01:00
|
|
|
self.inspect.finish_probe(dummy_probe);
|
2024-04-04 07:47:13 +02:00
|
|
|
if let Ok(result) = result { vec![Candidate { source, result }] } else { vec![] }
|
2024-02-09 10:40:26 +01:00
|
|
|
}
|
|
|
|
|
2023-03-25 20:10:41 +00:00
|
|
|
#[instrument(level = "debug", skip_all)]
|
2023-07-18 18:07:42 +02:00
|
|
|
fn assemble_non_blanket_impl_candidates<G: GoalKind<'tcx>>(
|
2023-01-17 11:47:47 +01:00
|
|
|
&mut self,
|
|
|
|
goal: Goal<'tcx, G>,
|
|
|
|
candidates: &mut Vec<Candidate<'tcx>>,
|
|
|
|
) {
|
|
|
|
let tcx = self.tcx();
|
2023-07-18 18:07:42 +02:00
|
|
|
let self_ty = goal.predicate.self_ty();
|
|
|
|
let trait_impls = tcx.trait_impls_of(goal.predicate.trait_def_id(tcx));
|
|
|
|
let mut consider_impls_for_simplified_type = |simp| {
|
|
|
|
if let Some(impls_for_type) = trait_impls.non_blanket_impls().get(&simp) {
|
|
|
|
for &impl_def_id in impls_for_type {
|
2024-02-13 19:20:13 +00:00
|
|
|
// 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.
|
|
|
|
if tcx.defaultness(impl_def_id).is_default() {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-07-18 18:07:42 +02:00
|
|
|
match G::consider_impl_candidate(self, goal, impl_def_id) {
|
2023-11-20 15:01:31 +01:00
|
|
|
Ok(candidate) => candidates.push(candidate),
|
2023-07-18 18:07:42 +02:00
|
|
|
Err(NoSolution) => (),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
match self_ty.kind() {
|
|
|
|
ty::Bool
|
|
|
|
| ty::Char
|
|
|
|
| ty::Int(_)
|
|
|
|
| ty::Uint(_)
|
|
|
|
| ty::Float(_)
|
|
|
|
| ty::Adt(_, _)
|
|
|
|
| ty::Foreign(_)
|
|
|
|
| ty::Str
|
|
|
|
| ty::Array(_, _)
|
2023-02-02 13:57:36 +00:00
|
|
|
| ty::Pat(_, _)
|
2023-07-18 18:07:42 +02:00
|
|
|
| ty::Slice(_)
|
2024-03-21 17:11:06 -04:00
|
|
|
| ty::RawPtr(_, _)
|
2023-07-18 18:07:42 +02:00
|
|
|
| ty::Ref(_, _, _)
|
|
|
|
| ty::FnDef(_, _)
|
|
|
|
| ty::FnPtr(_)
|
|
|
|
| ty::Dynamic(_, _, _)
|
2024-01-24 18:01:56 +00:00
|
|
|
| ty::Closure(..)
|
|
|
|
| ty::CoroutineClosure(..)
|
2023-12-21 01:52:10 +00:00
|
|
|
| ty::Coroutine(_, _)
|
2023-07-18 18:07:42 +02:00
|
|
|
| ty::Never
|
|
|
|
| ty::Tuple(_) => {
|
|
|
|
let simp =
|
|
|
|
fast_reject::simplify_type(tcx, self_ty, TreatParams::ForLookup).unwrap();
|
|
|
|
consider_impls_for_simplified_type(simp);
|
|
|
|
}
|
|
|
|
|
|
|
|
// HACK: For integer and float variables we have to manually look at all impls
|
|
|
|
// which have some integer or float as a self type.
|
|
|
|
ty::Infer(ty::IntVar(_)) => {
|
|
|
|
use ty::IntTy::*;
|
|
|
|
use ty::UintTy::*;
|
|
|
|
// This causes a compiler error if any new integer kinds are added.
|
|
|
|
let (I8 | I16 | I32 | I64 | I128 | Isize): ty::IntTy;
|
|
|
|
let (U8 | U16 | U32 | U64 | U128 | Usize): ty::UintTy;
|
|
|
|
let possible_integers = [
|
|
|
|
// signed integers
|
|
|
|
SimplifiedType::Int(I8),
|
|
|
|
SimplifiedType::Int(I16),
|
|
|
|
SimplifiedType::Int(I32),
|
|
|
|
SimplifiedType::Int(I64),
|
|
|
|
SimplifiedType::Int(I128),
|
|
|
|
SimplifiedType::Int(Isize),
|
|
|
|
// unsigned integers
|
|
|
|
SimplifiedType::Uint(U8),
|
|
|
|
SimplifiedType::Uint(U16),
|
|
|
|
SimplifiedType::Uint(U32),
|
|
|
|
SimplifiedType::Uint(U64),
|
|
|
|
SimplifiedType::Uint(U128),
|
|
|
|
SimplifiedType::Uint(Usize),
|
|
|
|
];
|
|
|
|
for simp in possible_integers {
|
|
|
|
consider_impls_for_simplified_type(simp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ty::Infer(ty::FloatVar(_)) => {
|
|
|
|
// This causes a compiler error if any new float kinds are added.
|
2024-02-28 03:44:23 -05:00
|
|
|
let (ty::FloatTy::F16 | ty::FloatTy::F32 | ty::FloatTy::F64 | ty::FloatTy::F128);
|
2023-07-18 18:07:42 +02:00
|
|
|
let possible_floats = [
|
2024-02-28 03:44:23 -05:00
|
|
|
SimplifiedType::Float(ty::FloatTy::F16),
|
2023-07-18 18:07:42 +02:00
|
|
|
SimplifiedType::Float(ty::FloatTy::F32),
|
|
|
|
SimplifiedType::Float(ty::FloatTy::F64),
|
2024-02-28 03:44:23 -05:00
|
|
|
SimplifiedType::Float(ty::FloatTy::F128),
|
2023-07-18 18:07:42 +02:00
|
|
|
];
|
|
|
|
|
|
|
|
for simp in possible_floats {
|
|
|
|
consider_impls_for_simplified_type(simp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// The only traits applying to aliases and placeholders are blanket impls.
|
|
|
|
//
|
|
|
|
// Impls which apply to an alias after normalization are handled by
|
|
|
|
// `assemble_candidates_after_normalizing_self_ty`.
|
|
|
|
ty::Alias(_, _) | ty::Placeholder(..) | ty::Error(_) => (),
|
|
|
|
|
|
|
|
// FIXME: These should ideally not exist as a self type. It would be nice for
|
2023-10-19 21:46:28 +00:00
|
|
|
// the builtin auto trait impls of coroutines to instead directly recurse
|
2023-07-18 18:07:42 +02:00
|
|
|
// into the witness.
|
2023-10-19 16:06:43 +00:00
|
|
|
ty::CoroutineWitness(..) => (),
|
2023-07-18 18:07:42 +02:00
|
|
|
|
|
|
|
// These variants should not exist as a self type.
|
|
|
|
ty::Infer(ty::TyVar(_) | ty::FreshTy(_) | ty::FreshIntTy(_) | ty::FreshFloatTy(_))
|
|
|
|
| ty::Param(_)
|
|
|
|
| ty::Bound(_, _) => bug!("unexpected self type: {self_ty}"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-29 19:27:04 +01:00
|
|
|
#[instrument(level = "debug", skip_all)]
|
2023-07-18 18:07:42 +02:00
|
|
|
fn assemble_blanket_impl_candidates<G: GoalKind<'tcx>>(
|
|
|
|
&mut self,
|
|
|
|
goal: Goal<'tcx, G>,
|
|
|
|
candidates: &mut Vec<Candidate<'tcx>>,
|
|
|
|
) {
|
|
|
|
let tcx = self.tcx();
|
|
|
|
let trait_impls = tcx.trait_impls_of(goal.predicate.trait_def_id(tcx));
|
|
|
|
for &impl_def_id in trait_impls.blanket_impls() {
|
2024-02-13 19:20:13 +00:00
|
|
|
// 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.
|
|
|
|
if tcx.defaultness(impl_def_id).is_default() {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-07-18 18:07:42 +02:00
|
|
|
match G::consider_impl_candidate(self, goal, impl_def_id) {
|
2023-11-20 15:01:31 +01:00
|
|
|
Ok(candidate) => candidates.push(candidate),
|
2023-01-17 11:47:47 +01:00
|
|
|
Err(NoSolution) => (),
|
2023-07-18 18:07:42 +02:00
|
|
|
}
|
|
|
|
}
|
2022-12-19 07:01:38 +00:00
|
|
|
}
|
2023-01-17 11:47:47 +01:00
|
|
|
|
2023-03-25 20:10:41 +00:00
|
|
|
#[instrument(level = "debug", skip_all)]
|
2023-01-17 11:47:47 +01:00
|
|
|
fn assemble_builtin_impl_candidates<G: GoalKind<'tcx>>(
|
|
|
|
&mut self,
|
|
|
|
goal: Goal<'tcx, G>,
|
|
|
|
candidates: &mut Vec<Candidate<'tcx>>,
|
|
|
|
) {
|
2023-07-18 18:07:42 +02:00
|
|
|
let tcx = self.tcx();
|
|
|
|
let lang_items = tcx.lang_items();
|
|
|
|
let trait_def_id = goal.predicate.trait_def_id(tcx);
|
2023-04-12 18:56:19 +00:00
|
|
|
|
|
|
|
// 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.
|
2023-07-18 18:07:42 +02:00
|
|
|
let result = if let Err(guar) = goal.predicate.error_reported() {
|
|
|
|
G::consider_error_guaranteed_candidate(self, guar)
|
|
|
|
} else if tcx.trait_is_auto(trait_def_id) {
|
2023-01-17 20:16:30 +00:00
|
|
|
G::consider_auto_trait_candidate(self, goal)
|
2023-07-18 18:07:42 +02:00
|
|
|
} else if tcx.trait_is_alias(trait_def_id) {
|
2023-01-17 20:16:30 +00:00
|
|
|
G::consider_trait_alias_candidate(self, goal)
|
|
|
|
} else if lang_items.sized_trait() == Some(trait_def_id) {
|
2023-01-17 11:47:47 +01:00
|
|
|
G::consider_builtin_sized_candidate(self, goal)
|
2023-01-17 20:24:58 +00:00
|
|
|
} else if lang_items.copy_trait() == Some(trait_def_id)
|
|
|
|
|| lang_items.clone_trait() == Some(trait_def_id)
|
|
|
|
{
|
|
|
|
G::consider_builtin_copy_clone_candidate(self, goal)
|
2023-02-07 18:02:20 +00:00
|
|
|
} else if lang_items.pointer_like() == Some(trait_def_id) {
|
|
|
|
G::consider_builtin_pointer_like_candidate(self, goal)
|
2022-07-20 14:32:58 +02:00
|
|
|
} else if lang_items.fn_ptr_trait() == Some(trait_def_id) {
|
|
|
|
G::consider_builtin_fn_ptr_trait_candidate(self, goal)
|
2023-01-19 01:20:34 +00:00
|
|
|
} else if let Some(kind) = self.tcx().fn_trait_kind_from_def_id(trait_def_id) {
|
|
|
|
G::consider_builtin_fn_trait_candidates(self, goal, kind)
|
2024-01-24 22:27:25 +00:00
|
|
|
} else if let Some(kind) = self.tcx().async_fn_trait_kind_from_def_id(trait_def_id) {
|
|
|
|
G::consider_builtin_async_fn_trait_candidates(self, goal, kind)
|
|
|
|
} else if lang_items.async_fn_kind_helper() == Some(trait_def_id) {
|
|
|
|
G::consider_builtin_async_fn_kind_helper_candidate(self, goal)
|
2023-01-19 01:20:34 +00:00
|
|
|
} else if lang_items.tuple_trait() == Some(trait_def_id) {
|
|
|
|
G::consider_builtin_tuple_candidate(self, goal)
|
2023-01-24 23:24:25 +00:00
|
|
|
} else if lang_items.pointee_trait() == Some(trait_def_id) {
|
|
|
|
G::consider_builtin_pointee_candidate(self, goal)
|
2023-01-24 23:38:20 +00:00
|
|
|
} else if lang_items.future_trait() == Some(trait_def_id) {
|
|
|
|
G::consider_builtin_future_candidate(self, goal)
|
2023-10-20 22:48:37 +00:00
|
|
|
} else if lang_items.iterator_trait() == Some(trait_def_id) {
|
|
|
|
G::consider_builtin_iterator_candidate(self, goal)
|
2024-03-22 00:48:36 +09:00
|
|
|
} else if lang_items.fused_iterator_trait() == Some(trait_def_id) {
|
|
|
|
G::consider_builtin_fused_iterator_candidate(self, goal)
|
2023-11-28 18:18:19 +00:00
|
|
|
} else if lang_items.async_iterator_trait() == Some(trait_def_id) {
|
|
|
|
G::consider_builtin_async_iterator_candidate(self, goal)
|
2023-10-30 23:35:35 +00:00
|
|
|
} else if lang_items.coroutine_trait() == Some(trait_def_id) {
|
2023-10-19 21:46:28 +00:00
|
|
|
G::consider_builtin_coroutine_candidate(self, goal)
|
2023-01-27 20:45:03 +01:00
|
|
|
} else if lang_items.discriminant_kind_trait() == Some(trait_def_id) {
|
|
|
|
G::consider_builtin_discriminant_kind_candidate(self, goal)
|
2024-02-13 12:31:41 +03:00
|
|
|
} else if lang_items.async_destruct_trait() == Some(trait_def_id) {
|
|
|
|
G::consider_builtin_async_destruct_candidate(self, goal)
|
2023-03-22 17:13:00 +00:00
|
|
|
} else if lang_items.destruct_trait() == Some(trait_def_id) {
|
|
|
|
G::consider_builtin_destruct_candidate(self, goal)
|
2023-04-09 00:09:53 +00:00
|
|
|
} else if lang_items.transmute_trait() == Some(trait_def_id) {
|
|
|
|
G::consider_builtin_transmute_candidate(self, goal)
|
2023-01-17 11:47:47 +01:00
|
|
|
} else {
|
|
|
|
Err(NoSolution)
|
|
|
|
};
|
|
|
|
|
2023-01-17 19:29:52 +00:00
|
|
|
match result {
|
2023-06-21 01:22:43 +00:00
|
|
|
Ok(result) => candidates.push(Candidate {
|
|
|
|
source: CandidateSource::BuiltinImpl(BuiltinImplSource::Misc),
|
|
|
|
result,
|
|
|
|
}),
|
2023-01-17 11:47:47 +01:00
|
|
|
Err(NoSolution) => (),
|
|
|
|
}
|
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<_>>`
|
|
|
|
if lang_items.unsize_trait() == Some(trait_def_id) {
|
2023-08-14 23:37:06 +00:00
|
|
|
for (result, source) in G::consider_structural_builtin_unsize_candidates(self, goal) {
|
2023-07-20 18:36:34 +00:00
|
|
|
candidates.push(Candidate { source: CandidateSource::BuiltinImpl(source), result });
|
2023-01-23 23:56:54 +00:00
|
|
|
}
|
|
|
|
}
|
2023-01-17 11:47:47 +01:00
|
|
|
}
|
|
|
|
|
2023-03-25 20:10:41 +00:00
|
|
|
#[instrument(level = "debug", skip_all)]
|
2023-01-17 11:47:47 +01:00
|
|
|
fn assemble_param_env_candidates<G: GoalKind<'tcx>>(
|
|
|
|
&mut self,
|
|
|
|
goal: Goal<'tcx, G>,
|
|
|
|
candidates: &mut Vec<Candidate<'tcx>>,
|
|
|
|
) {
|
|
|
|
for (i, assumption) in goal.param_env.caller_bounds().iter().enumerate() {
|
2023-06-22 18:17:13 +00:00
|
|
|
match G::consider_implied_clause(self, goal, assumption, []) {
|
|
|
|
Ok(result) => {
|
|
|
|
candidates.push(Candidate { source: CandidateSource::ParamEnv(i), result })
|
2023-01-17 11:47:47 +01:00
|
|
|
}
|
2023-06-22 18:17:13 +00:00
|
|
|
Err(NoSolution) => (),
|
2023-01-17 11:47:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-25 20:10:41 +00:00
|
|
|
#[instrument(level = "debug", skip_all)]
|
2023-01-17 11:47:47 +01:00
|
|
|
fn assemble_alias_bound_candidates<G: GoalKind<'tcx>>(
|
|
|
|
&mut self,
|
|
|
|
goal: Goal<'tcx, G>,
|
|
|
|
candidates: &mut Vec<Candidate<'tcx>>,
|
|
|
|
) {
|
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);
|
|
|
|
});
|
|
|
|
}
|
2023-01-17 11:47:47 +01:00
|
|
|
|
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.
|
|
|
|
fn assemble_alias_bound_candidates_recur<G: GoalKind<'tcx>>(
|
|
|
|
&mut self,
|
|
|
|
self_ty: Ty<'tcx>,
|
|
|
|
goal: Goal<'tcx, G>,
|
|
|
|
candidates: &mut Vec<Candidate<'tcx>>,
|
|
|
|
) {
|
|
|
|
let (kind, alias_ty) = match *self_ty.kind() {
|
2023-01-17 11:47:47 +01:00
|
|
|
ty::Bool
|
|
|
|
| ty::Char
|
|
|
|
| ty::Int(_)
|
|
|
|
| ty::Uint(_)
|
|
|
|
| ty::Float(_)
|
|
|
|
| ty::Adt(_, _)
|
|
|
|
| ty::Foreign(_)
|
|
|
|
| ty::Str
|
|
|
|
| ty::Array(_, _)
|
2023-02-02 13:57:36 +00:00
|
|
|
| ty::Pat(_, _)
|
2023-01-17 11:47:47 +01:00
|
|
|
| ty::Slice(_)
|
2024-03-21 17:11:06 -04:00
|
|
|
| ty::RawPtr(_, _)
|
2023-01-17 11:47:47 +01:00
|
|
|
| ty::Ref(_, _, _)
|
|
|
|
| ty::FnDef(_, _)
|
|
|
|
| ty::FnPtr(_)
|
|
|
|
| ty::Dynamic(..)
|
|
|
|
| ty::Closure(..)
|
2024-01-24 18:01:56 +00:00
|
|
|
| ty::CoroutineClosure(..)
|
2023-10-19 16:06:43 +00:00
|
|
|
| ty::Coroutine(..)
|
|
|
|
| ty::CoroutineWitness(..)
|
2023-01-17 11:47:47 +01:00
|
|
|
| ty::Never
|
|
|
|
| ty::Tuple(_)
|
|
|
|
| ty::Param(_)
|
|
|
|
| ty::Placeholder(..)
|
2023-01-25 00:38:34 +00:00
|
|
|
| ty::Infer(ty::IntVar(_) | ty::FloatVar(_))
|
2023-01-17 11:47:47 +01:00
|
|
|
| ty::Error(_) => return,
|
2024-02-07 00:19:12 +00:00
|
|
|
ty::Infer(ty::FreshTy(_) | ty::FreshIntTy(_) | ty::FreshFloatTy(_)) | ty::Bound(..) => {
|
|
|
|
bug!("unexpected self type for `{goal:?}`")
|
|
|
|
}
|
2024-02-01 23:48:04 +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-01 23:48:04 +00:00
|
|
|
{
|
2024-02-07 00:19:12 +00:00
|
|
|
candidates.push(Candidate { source: CandidateSource::AliasBound, result });
|
2024-02-01 23:48:04 +00:00
|
|
|
}
|
2024-02-07 00:19:12 +00:00
|
|
|
return;
|
|
|
|
}
|
2024-02-01 23:48:04 +00:00
|
|
|
|
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-02-10 23:49:16 +00:00
|
|
|
self.tcx().sess.dcx().span_delayed_bug(
|
|
|
|
DUMMY_SP,
|
|
|
|
format!("could not normalize {self_ty}, it is not WF"),
|
|
|
|
);
|
|
|
|
return;
|
2024-02-07 00:19:12 +00:00
|
|
|
}
|
2023-01-17 11:47:47 +01:00
|
|
|
};
|
|
|
|
|
2023-07-11 22:35:29 +01:00
|
|
|
for assumption in
|
|
|
|
self.tcx().item_bounds(alias_ty.def_id).instantiate(self.tcx(), alias_ty.args)
|
2023-01-17 11:47:47 +01:00
|
|
|
{
|
2024-02-03 01:20:59 +00:00
|
|
|
match G::consider_implied_clause(self, goal, assumption, []) {
|
2023-06-19 20:46:46 +00:00
|
|
|
Ok(result) => {
|
2024-02-07 00:19:12 +00:00
|
|
|
candidates.push(Candidate { source: CandidateSource::AliasBound, result });
|
2023-01-17 11:47:47 +01:00
|
|
|
}
|
2024-02-07 00:19:12 +00:00
|
|
|
Err(NoSolution) => {}
|
2023-01-17 11:47:47 +01:00
|
|
|
}
|
2024-02-07 00:19:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if kind != ty::Projection {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-02-01 12:34:38 +01:00
|
|
|
// 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)
|
2023-01-17 11:47:47 +01:00
|
|
|
}
|
2024-02-01 12:34:38 +01:00
|
|
|
Err(NoSolution) => {}
|
2023-01-17 11:47:47 +01:00
|
|
|
}
|
|
|
|
}
|
2023-01-17 18:19:11 +00:00
|
|
|
|
2023-03-25 20:10:41 +00:00
|
|
|
#[instrument(level = "debug", skip_all)]
|
2023-01-17 18:19:11 +00:00
|
|
|
fn assemble_object_bound_candidates<G: GoalKind<'tcx>>(
|
|
|
|
&mut self,
|
|
|
|
goal: Goal<'tcx, G>,
|
|
|
|
candidates: &mut Vec<Candidate<'tcx>>,
|
|
|
|
) {
|
2023-06-02 05:54:52 +00:00
|
|
|
let tcx = self.tcx();
|
|
|
|
if !tcx.trait_def(goal.predicate.trait_def_id(tcx)).implement_via_object {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-01-17 18:19:11 +00:00
|
|
|
let self_ty = goal.predicate.self_ty();
|
|
|
|
let bounds = match *self_ty.kind() {
|
|
|
|
ty::Bool
|
|
|
|
| ty::Char
|
|
|
|
| ty::Int(_)
|
|
|
|
| ty::Uint(_)
|
|
|
|
| ty::Float(_)
|
|
|
|
| ty::Adt(_, _)
|
|
|
|
| ty::Foreign(_)
|
|
|
|
| ty::Str
|
|
|
|
| ty::Array(_, _)
|
2023-02-02 13:57:36 +00:00
|
|
|
| ty::Pat(_, _)
|
2023-01-17 18:19:11 +00:00
|
|
|
| ty::Slice(_)
|
2024-03-21 17:11:06 -04:00
|
|
|
| ty::RawPtr(_, _)
|
2023-01-17 18:19:11 +00:00
|
|
|
| ty::Ref(_, _, _)
|
|
|
|
| ty::FnDef(_, _)
|
|
|
|
| ty::FnPtr(_)
|
|
|
|
| ty::Alias(..)
|
|
|
|
| ty::Closure(..)
|
2024-01-24 18:01:56 +00:00
|
|
|
| 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(..)
|
2023-01-25 00:38:34 +00:00
|
|
|
| ty::Infer(ty::IntVar(_) | ty::FloatVar(_))
|
2023-01-17 18:19:11 +00:00
|
|
|
| ty::Error(_) => return,
|
2023-01-25 00:38:34 +00:00
|
|
|
ty::Infer(ty::TyVar(_) | ty::FreshTy(_) | ty::FreshIntTy(_) | ty::FreshFloatTy(_))
|
|
|
|
| ty::Bound(..) => bug!("unexpected self type for `{goal:?}`"),
|
2023-01-17 18:19:11 +00:00
|
|
|
ty::Dynamic(bounds, ..) => bounds,
|
|
|
|
};
|
|
|
|
|
2023-08-15 00:05:15 +00:00
|
|
|
// Do not consider built-in object impls for non-object-safe types.
|
|
|
|
if bounds.principal_def_id().is_some_and(|def_id| !tcx.check_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 {
|
|
|
|
match bound.skip_binder() {
|
|
|
|
ty::ExistentialPredicate::Trait(_) => {
|
|
|
|
// Skip principal
|
|
|
|
}
|
|
|
|
ty::ExistentialPredicate::Projection(_)
|
|
|
|
| ty::ExistentialPredicate::AutoTrait(_) => {
|
|
|
|
match G::consider_object_bound_candidate(
|
|
|
|
self,
|
|
|
|
goal,
|
|
|
|
bound.with_self_ty(tcx, self_ty),
|
|
|
|
) {
|
|
|
|
Ok(result) => candidates.push(Candidate {
|
|
|
|
source: CandidateSource::BuiltinImpl(BuiltinImplSource::Misc),
|
|
|
|
result,
|
|
|
|
}),
|
|
|
|
Err(NoSolution) => (),
|
|
|
|
}
|
|
|
|
}
|
2023-03-27 19:41:15 +00:00
|
|
|
}
|
2023-07-24 22:02:52 +00:00
|
|
|
}
|
2023-03-27 19:41:15 +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() {
|
|
|
|
let principal_trait_ref = principal.with_self_ty(tcx, self_ty);
|
|
|
|
self.walk_vtable(principal_trait_ref, |ecx, assumption, vtable_base, _| {
|
|
|
|
match G::consider_object_bound_candidate(ecx, goal, assumption.to_predicate(tcx)) {
|
|
|
|
Ok(result) => candidates.push(Candidate {
|
|
|
|
source: CandidateSource::BuiltinImpl(BuiltinImplSource::Object {
|
|
|
|
vtable_base,
|
|
|
|
}),
|
|
|
|
result,
|
|
|
|
}),
|
|
|
|
Err(NoSolution) => (),
|
|
|
|
}
|
|
|
|
});
|
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.
|
2023-03-25 20:10:41 +00:00
|
|
|
#[instrument(level = "debug", skip_all)]
|
2023-03-21 16:26:23 +01:00
|
|
|
fn assemble_coherence_unknowable_candidates<G: GoalKind<'tcx>>(
|
|
|
|
&mut self,
|
|
|
|
goal: Goal<'tcx, G>,
|
|
|
|
candidates: &mut Vec<Candidate<'tcx>>,
|
|
|
|
) {
|
2023-08-04 12:17:28 +02:00
|
|
|
let tcx = self.tcx();
|
2023-09-11 11:34:57 +02:00
|
|
|
let result = self.probe_misc_candidate("coherence unknowable").enter(|ecx| {
|
2023-08-04 12:17:28 +02:00
|
|
|
let trait_ref = goal.predicate.trait_ref(tcx);
|
2024-02-01 12:34:38 +01:00
|
|
|
let lazily_normalize_ty = |ty| ecx.structurally_normalize_ty(goal.param_env, ty);
|
2023-08-04 12:17:28 +02:00
|
|
|
|
2024-02-01 12:34:38 +01:00
|
|
|
match coherence::trait_ref_is_knowable(tcx, trait_ref, lazily_normalize_ty)? {
|
|
|
|
Ok(()) => Err(NoSolution),
|
|
|
|
Err(_) => {
|
2023-08-04 12:17:28 +02:00
|
|
|
ecx.evaluate_added_goals_and_make_canonical_response(Certainty::AMBIGUOUS)
|
2023-03-21 16:26:23 +01:00
|
|
|
}
|
|
|
|
}
|
2023-08-04 12:17:28 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
match result {
|
|
|
|
Ok(result) => candidates.push(Candidate {
|
|
|
|
source: CandidateSource::BuiltinImpl(BuiltinImplSource::Misc),
|
|
|
|
result,
|
|
|
|
}),
|
|
|
|
Err(NoSolution) => {}
|
2023-03-21 16:26:23 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-09 10:40:26 +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.
|
2024-02-26 10:12:40 +01:00
|
|
|
#[instrument(level = "debug", skip(self, goal))]
|
2024-02-09 10:40:26 +01:00
|
|
|
fn discard_impls_shadowed_by_env<G: GoalKind<'tcx>>(
|
|
|
|
&mut self,
|
|
|
|
goal: Goal<'tcx, G>,
|
|
|
|
candidates: &mut Vec<Candidate<'tcx>>,
|
|
|
|
) {
|
|
|
|
let tcx = self.tcx();
|
|
|
|
let trait_goal: Goal<'tcx, ty::TraitPredicate<'tcx>> =
|
|
|
|
goal.with(tcx, goal.predicate.trait_ref(tcx));
|
|
|
|
let mut trait_candidates_from_env = Vec::new();
|
|
|
|
self.assemble_param_env_candidates(trait_goal, &mut trait_candidates_from_env);
|
|
|
|
self.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");
|
|
|
|
false
|
|
|
|
}
|
2024-02-09 10:40:26 +01:00
|
|
|
CandidateSource::ParamEnv(_) | CandidateSource::AliasBound => true,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
// 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) => {
|
2024-02-26 10:12:40 +01:00
|
|
|
debug!(?cause, "force ambiguity");
|
2024-02-09 10:40:26 +01:00
|
|
|
*candidates = self.forced_ambiguity(cause);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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.
|
2023-02-08 19:25:21 +00:00
|
|
|
#[instrument(level = "debug", skip(self), ret)]
|
2023-03-21 16:26:23 +01:00
|
|
|
pub(super) fn merge_candidates(
|
2023-02-08 19:25:21 +00:00
|
|
|
&mut self,
|
2024-02-02 22:45:25 +00:00
|
|
|
candidates: Vec<Candidate<'tcx>>,
|
2023-02-08 19:25:21 +00:00
|
|
|
) -> QueryResult<'tcx> {
|
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);
|
2024-02-09 10:40:26 +01:00
|
|
|
} else {
|
|
|
|
self.flounder(&responses)
|
2023-02-08 19:25:21 +00:00
|
|
|
}
|
|
|
|
}
|
2022-12-19 07:01:38 +00:00
|
|
|
}
|