2022-12-19 07:01:38 +00:00
|
|
|
//! Code shared by trait and projection goals for candidate assembly.
|
|
|
|
|
2023-03-27 21:48:43 +00:00
|
|
|
use super::search_graph::OverflowHandler;
|
2023-03-21 16:26:23 +01:00
|
|
|
use super::{EvalCtxt, SolverMode};
|
2023-03-29 15:36:17 +02:00
|
|
|
use crate::solve::CanonicalResponseExt;
|
2023-03-21 16:26:23 +01:00
|
|
|
use crate::traits::coherence;
|
2023-03-27 19:41:15 +00:00
|
|
|
use rustc_data_structures::fx::FxIndexSet;
|
2022-12-19 07:01:38 +00:00
|
|
|
use rustc_hir::def_id::DefId;
|
|
|
|
use rustc_infer::traits::query::NoSolution;
|
2023-04-06 23:11:19 +00:00
|
|
|
use rustc_infer::traits::util::elaborate;
|
2023-02-15 02:08:05 +00:00
|
|
|
use rustc_middle::traits::solve::{CanonicalResponse, Certainty, Goal, MaybeCause, QueryResult};
|
2023-03-07 04:39:17 +00:00
|
|
|
use rustc_middle::ty::fast_reject::TreatProjections;
|
2022-12-19 07:01:38 +00:00
|
|
|
use rustc_middle::ty::TypeFoldable;
|
|
|
|
use rustc_middle::ty::{self, Ty, TyCtxt};
|
|
|
|
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-17 11:47:47 +01:00
|
|
|
/// Possible ways the given goal can be proven.
|
|
|
|
#[derive(Debug, Clone, Copy)]
|
|
|
|
pub(super) enum CandidateSource {
|
|
|
|
/// A user written impl.
|
|
|
|
///
|
|
|
|
/// ## Examples
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// fn main() {
|
|
|
|
/// let x: Vec<u32> = Vec::new();
|
|
|
|
/// // This uses the impl from the standard library to prove `Vec<T>: Clone`.
|
|
|
|
/// let y = x.clone();
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
Impl(DefId),
|
|
|
|
/// A builtin impl generated by the compiler. When adding a new special
|
|
|
|
/// trait, try to use actual impls whenever possible. Builtin impls should
|
|
|
|
/// only be used in cases where the impl cannot be manually be written.
|
|
|
|
///
|
|
|
|
/// Notable examples are auto traits, `Sized`, and `DiscriminantKind`.
|
|
|
|
/// For a list of all traits with builtin impls, check out the
|
|
|
|
/// [`EvalCtxt::assemble_builtin_impl_candidates`] method. Not
|
|
|
|
BuiltinImpl,
|
|
|
|
/// An assumption from the environment.
|
|
|
|
///
|
|
|
|
/// More precicely we've used the `n-th` assumption in the `param_env`.
|
|
|
|
///
|
|
|
|
/// ## Examples
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// fn is_clone<T: Clone>(x: T) -> (T, T) {
|
|
|
|
/// // This uses the assumption `T: Clone` from the `where`-bounds
|
|
|
|
/// // to prove `T: Clone`.
|
|
|
|
/// (x.clone(), x)
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
ParamEnv(usize),
|
|
|
|
/// If the self type is an alias type, e.g. an opaque type or a projection,
|
|
|
|
/// we know the bounds on that alias to hold even without knowing its concrete
|
|
|
|
/// underlying type.
|
|
|
|
///
|
|
|
|
/// More precisely this candidate is using the `n-th` bound in the `item_bounds` of
|
|
|
|
/// the self type.
|
|
|
|
///
|
|
|
|
/// ## Examples
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// trait Trait {
|
|
|
|
/// type Assoc: Clone;
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// fn foo<T: Trait>(x: <T as Trait>::Assoc) {
|
|
|
|
/// // We prove `<T as Trait>::Assoc` by looking at the bounds on `Assoc` in
|
|
|
|
/// // in the trait definition.
|
|
|
|
/// let _y = x.clone();
|
|
|
|
/// }
|
|
|
|
/// ```
|
2023-01-28 06:00:27 -06:00
|
|
|
AliasBound,
|
2023-01-17 11:47:47 +01:00
|
|
|
}
|
2022-12-19 07:01:38 +00:00
|
|
|
|
2023-01-27 20:45:03 +01:00
|
|
|
/// Methods used to assemble candidates for either trait or projection goals.
|
2023-02-22 02:18:40 +00:00
|
|
|
pub(super) trait GoalKind<'tcx>: TypeFoldable<TyCtxt<'tcx>> + Copy + Eq {
|
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-02-16 03:04:08 +00: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.
|
|
|
|
fn consider_implied_clause(
|
2023-01-17 11:47:47 +01:00
|
|
|
ecx: &mut EvalCtxt<'_, 'tcx>,
|
|
|
|
goal: Goal<'tcx, Self>,
|
2023-01-17 20:16:30 +00:00
|
|
|
assumption: ty::Predicate<'tcx>,
|
2023-02-16 03:04:08 +00:00
|
|
|
requirements: impl IntoIterator<Item = Goal<'tcx, ty::Predicate<'tcx>>>,
|
|
|
|
) -> QueryResult<'tcx>;
|
2023-02-10 02:10:42 +00:00
|
|
|
|
2023-02-22 01:11:57 +00: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.
|
|
|
|
fn consider_object_bound_candidate(
|
|
|
|
ecx: &mut EvalCtxt<'_, 'tcx>,
|
|
|
|
goal: Goal<'tcx, Self>,
|
|
|
|
assumption: ty::Predicate<'tcx>,
|
|
|
|
) -> QueryResult<'tcx>;
|
|
|
|
|
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-01-17 19:29:52 +00:00
|
|
|
) -> QueryResult<'tcx>;
|
2022-12-19 07:01:38 +00:00
|
|
|
|
2023-01-27 04:31:51 +00:00
|
|
|
// A type implements an `auto trait` if its components do as well. These components
|
|
|
|
// are given by built-in rules from [`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-01-27 04:31:51 +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(
|
|
|
|
ecx: &mut EvalCtxt<'_, 'tcx>,
|
|
|
|
goal: Goal<'tcx, Self>,
|
|
|
|
) -> QueryResult<'tcx>;
|
|
|
|
|
2023-01-27 04:31:51 +00:00
|
|
|
// A type is `Copy` or `Clone` if its components are `Sized`. These components
|
|
|
|
// are given by built-in rules from [`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-01-27 04:31:51 +00:00
|
|
|
// A type is `Copy` or `Clone` if its components are `Copy` or `Clone`. These
|
|
|
|
// components are given by built-in rules from [`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-02-07 18:02:20 +00:00
|
|
|
// A type is `PointerLike` if we can compute its layout, and that layout
|
2023-01-27 04:31:51 +00:00
|
|
|
// 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
|
|
|
|
2022-07-20 14:32:58 +02:00
|
|
|
// A type is a `FnPtr` if it is of `FnPtr` type.
|
|
|
|
fn consider_builtin_fn_ptr_trait_candidate(
|
|
|
|
ecx: &mut EvalCtxt<'_, 'tcx>,
|
|
|
|
goal: Goal<'tcx, Self>,
|
|
|
|
) -> QueryResult<'tcx>;
|
|
|
|
|
2023-01-27 04:31:51 +00: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>;
|
|
|
|
|
2023-01-27 04:31:51 +00: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-01-27 04:31:51 +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(
|
|
|
|
ecx: &mut EvalCtxt<'_, 'tcx>,
|
|
|
|
goal: Goal<'tcx, Self>,
|
|
|
|
) -> QueryResult<'tcx>;
|
2023-01-24 23:38:20 +00:00
|
|
|
|
2023-01-27 04:31:51 +00:00
|
|
|
// A generator (that comes from an `async` desugaring) is known to implement
|
|
|
|
// `Future<Output = O>`, where `O` is given by the generator's return type
|
|
|
|
// 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-01-27 04:31:51 +00:00
|
|
|
// A generator (that doesn't come from an `async` desugaring) is known to
|
|
|
|
// implement `Generator<R, Yield = Y, Return = O>`, given the resume, yield,
|
|
|
|
// and return types of the generator computed during type-checking.
|
2023-01-24 23:38:20 +00:00
|
|
|
fn consider_builtin_generator_candidate(
|
|
|
|
ecx: &mut EvalCtxt<'_, 'tcx>,
|
|
|
|
goal: Goal<'tcx, Self>,
|
|
|
|
) -> QueryResult<'tcx>;
|
2023-01-23 22:33:59 +00:00
|
|
|
|
2023-01-23 23:56:54 +00:00
|
|
|
// The most common forms of unsizing are array to slice, and concrete (Sized)
|
|
|
|
// type into a `dyn Trait`. ADTs and Tuples can also have their final field
|
|
|
|
// unsized if it's generic.
|
2023-01-23 22:33:59 +00:00
|
|
|
fn consider_builtin_unsize_candidate(
|
|
|
|
ecx: &mut EvalCtxt<'_, 'tcx>,
|
|
|
|
goal: Goal<'tcx, Self>,
|
|
|
|
) -> QueryResult<'tcx>;
|
2023-01-23 23:56:54 +00:00
|
|
|
|
|
|
|
// `dyn Trait1` can be unsized to `dyn Trait2` if they are the same trait, or
|
|
|
|
// if `Trait2` is a (transitive) supertrait of `Trait2`.
|
2023-01-25 17:02:16 +00:00
|
|
|
fn consider_builtin_dyn_upcast_candidates(
|
2023-01-23 23:56:54 +00:00
|
|
|
ecx: &mut EvalCtxt<'_, 'tcx>,
|
|
|
|
goal: Goal<'tcx, Self>,
|
|
|
|
) -> Vec<CanonicalResponse<'tcx>>;
|
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
|
|
|
|
|
|
|
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-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>> {
|
2023-02-16 02:28:10 +00:00
|
|
|
debug_assert_eq!(goal, self.resolve_vars_if_possible(goal));
|
2023-01-19 15:32:20 +00:00
|
|
|
|
2023-01-19 03:26:54 +00:00
|
|
|
// HACK: `_: Trait` is ambiguous, because it may be satisfied via a builtin rule,
|
|
|
|
// object bound, alias bound, etc. We are unable to determine this until we can at
|
|
|
|
// least structually resolve the type one layer.
|
|
|
|
if goal.predicate.self_ty().is_ty_var() {
|
|
|
|
return vec![Candidate {
|
|
|
|
source: CandidateSource::BuiltinImpl,
|
2023-03-16 14:58:26 +00:00
|
|
|
result: self
|
|
|
|
.evaluate_added_goals_and_make_canonical_response(Certainty::AMBIGUOUS)
|
|
|
|
.unwrap(),
|
2023-01-19 03:26:54 +00:00
|
|
|
}];
|
|
|
|
}
|
|
|
|
|
2023-01-17 11:47:47 +01:00
|
|
|
let mut candidates = Vec::new();
|
2022-12-19 07:01:38 +00:00
|
|
|
|
2023-01-17 11:47:47 +01:00
|
|
|
self.assemble_candidates_after_normalizing_self_ty(goal, &mut candidates);
|
2022-12-19 07:01:38 +00:00
|
|
|
|
2023-01-17 11:47:47 +01:00
|
|
|
self.assemble_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_param_env_candidates(goal, &mut candidates);
|
|
|
|
|
|
|
|
self.assemble_alias_bound_candidates(goal, &mut candidates);
|
|
|
|
|
2023-01-17 18:19:11 +00:00
|
|
|
self.assemble_object_bound_candidates(goal, &mut candidates);
|
|
|
|
|
2023-03-21 16:26:23 +01:00
|
|
|
self.assemble_coherence_unknowable_candidates(goal, &mut candidates);
|
|
|
|
|
2023-01-17 11:47:47 +01:00
|
|
|
candidates
|
2022-12-19 07:01:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// If the self type of a goal is a projection, computing the relevant candidates is difficult.
|
|
|
|
///
|
|
|
|
/// To deal with this, we first try to normalize the self type and add the candidates for the normalized
|
2023-03-21 16:26:23 +01:00
|
|
|
/// self type to the list of candidates in case that succeeds. We also have to consider candidates with the
|
|
|
|
/// projection as a self type as well
|
2023-03-25 20:10:41 +00:00
|
|
|
#[instrument(level = "debug", skip_all)]
|
2023-01-17 11:47:47 +01:00
|
|
|
fn assemble_candidates_after_normalizing_self_ty<G: GoalKind<'tcx>>(
|
|
|
|
&mut self,
|
|
|
|
goal: Goal<'tcx, G>,
|
|
|
|
candidates: &mut Vec<Candidate<'tcx>>,
|
|
|
|
) {
|
|
|
|
let tcx = self.tcx();
|
2022-12-19 07:01:38 +00:00
|
|
|
// FIXME: We also have to normalize opaque types, not sure where to best fit that in.
|
|
|
|
let &ty::Alias(ty::Projection, projection_ty) = goal.predicate.self_ty().kind() else {
|
|
|
|
return
|
|
|
|
};
|
2023-03-16 14:58:26 +00:00
|
|
|
|
2023-03-27 21:48:43 +00:00
|
|
|
let normalized_self_candidates: Result<_, NoSolution> = self.probe(|ecx| {
|
|
|
|
ecx.with_incremented_depth(
|
|
|
|
|ecx| {
|
|
|
|
let result = ecx.evaluate_added_goals_and_make_canonical_response(
|
|
|
|
Certainty::Maybe(MaybeCause::Overflow),
|
|
|
|
)?;
|
|
|
|
Ok(vec![Candidate { source: CandidateSource::BuiltinImpl, result }])
|
|
|
|
},
|
|
|
|
|ecx| {
|
|
|
|
let normalized_ty = ecx.next_ty_infer();
|
|
|
|
let normalizes_to_goal = goal.with(
|
|
|
|
tcx,
|
|
|
|
ty::Binder::dummy(ty::ProjectionPredicate {
|
|
|
|
projection_ty,
|
|
|
|
term: normalized_ty.into(),
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
ecx.add_goal(normalizes_to_goal);
|
|
|
|
let _ = ecx.try_evaluate_added_goals()?;
|
|
|
|
let normalized_ty = ecx.resolve_vars_if_possible(normalized_ty);
|
|
|
|
// NOTE: Alternatively we could call `evaluate_goal` here and only
|
|
|
|
// have a `Normalized` candidate. This doesn't work as long as we
|
|
|
|
// use `CandidateSource` in winnowing.
|
|
|
|
let goal = goal.with(tcx, goal.predicate.with_self_ty(tcx, normalized_ty));
|
|
|
|
Ok(ecx.assemble_and_evaluate_candidates(goal))
|
|
|
|
},
|
|
|
|
)
|
2023-03-16 14:58:26 +00:00
|
|
|
});
|
2023-03-27 21:48:43 +00:00
|
|
|
|
|
|
|
if let Ok(normalized_self_candidates) = normalized_self_candidates {
|
|
|
|
candidates.extend(normalized_self_candidates);
|
|
|
|
}
|
2022-12-19 07:01:38 +00:00
|
|
|
}
|
|
|
|
|
2023-03-25 20:10:41 +00:00
|
|
|
#[instrument(level = "debug", skip_all)]
|
2023-01-17 11:47:47 +01:00
|
|
|
fn assemble_impl_candidates<G: GoalKind<'tcx>>(
|
|
|
|
&mut self,
|
|
|
|
goal: Goal<'tcx, G>,
|
|
|
|
candidates: &mut Vec<Candidate<'tcx>>,
|
|
|
|
) {
|
|
|
|
let tcx = self.tcx();
|
2023-03-07 04:39:17 +00:00
|
|
|
tcx.for_each_relevant_impl_treating_projections(
|
2023-01-17 10:21:30 +01:00
|
|
|
goal.predicate.trait_def_id(tcx),
|
2022-12-19 07:01:38 +00:00
|
|
|
goal.predicate.self_ty(),
|
2023-03-07 04:39:17 +00:00
|
|
|
TreatProjections::NextSolverLookup,
|
2023-01-17 19:29:52 +00:00
|
|
|
|impl_def_id| match G::consider_impl_candidate(self, goal, impl_def_id) {
|
2023-01-17 11:47:47 +01:00
|
|
|
Ok(result) => candidates
|
|
|
|
.push(Candidate { source: CandidateSource::Impl(impl_def_id), result }),
|
|
|
|
Err(NoSolution) => (),
|
|
|
|
},
|
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>>,
|
|
|
|
) {
|
|
|
|
let lang_items = self.tcx().lang_items();
|
|
|
|
let trait_def_id = goal.predicate.trait_def_id(self.tcx());
|
2023-01-17 20:16:30 +00:00
|
|
|
let result = if self.tcx().trait_is_auto(trait_def_id) {
|
|
|
|
G::consider_auto_trait_candidate(self, goal)
|
|
|
|
} else if self.tcx().trait_is_alias(trait_def_id) {
|
|
|
|
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)
|
|
|
|
} 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)
|
|
|
|
} else if lang_items.gen_trait() == Some(trait_def_id) {
|
|
|
|
G::consider_builtin_generator_candidate(self, goal)
|
2023-01-23 22:33:59 +00:00
|
|
|
} else if lang_items.unsize_trait() == Some(trait_def_id) {
|
|
|
|
G::consider_builtin_unsize_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)
|
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-01-17 11:47:47 +01:00
|
|
|
Ok(result) => {
|
|
|
|
candidates.push(Candidate { source: CandidateSource::BuiltinImpl, result })
|
|
|
|
}
|
|
|
|
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-01-25 17:02:16 +00:00
|
|
|
for result in G::consider_builtin_dyn_upcast_candidates(self, goal) {
|
2023-01-23 23:56:54 +00:00
|
|
|
candidates.push(Candidate { source: CandidateSource::BuiltinImpl, result });
|
|
|
|
}
|
|
|
|
}
|
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-02-16 03:04:08 +00:00
|
|
|
match G::consider_implied_clause(self, goal, assumption, []) {
|
2023-01-17 11:47:47 +01:00
|
|
|
Ok(result) => {
|
|
|
|
candidates.push(Candidate { source: CandidateSource::ParamEnv(i), result })
|
|
|
|
}
|
|
|
|
Err(NoSolution) => (),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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>>,
|
|
|
|
) {
|
|
|
|
let alias_ty = match goal.predicate.self_ty().kind() {
|
|
|
|
ty::Bool
|
|
|
|
| ty::Char
|
|
|
|
| ty::Int(_)
|
|
|
|
| ty::Uint(_)
|
|
|
|
| ty::Float(_)
|
|
|
|
| ty::Adt(_, _)
|
|
|
|
| ty::Foreign(_)
|
|
|
|
| ty::Str
|
|
|
|
| ty::Array(_, _)
|
|
|
|
| ty::Slice(_)
|
|
|
|
| ty::RawPtr(_)
|
|
|
|
| ty::Ref(_, _, _)
|
|
|
|
| ty::FnDef(_, _)
|
|
|
|
| ty::FnPtr(_)
|
|
|
|
| ty::Dynamic(..)
|
|
|
|
| ty::Closure(..)
|
|
|
|
| ty::Generator(..)
|
|
|
|
| ty::GeneratorWitness(_)
|
2022-10-01 14:56:24 +02:00
|
|
|
| ty::GeneratorWitnessMIR(..)
|
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,
|
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 11:47:47 +01:00
|
|
|
ty::Alias(_, alias_ty) => alias_ty,
|
|
|
|
};
|
|
|
|
|
2023-02-08 02:46:22 +00:00
|
|
|
for assumption in self.tcx().item_bounds(alias_ty.def_id).subst(self.tcx(), alias_ty.substs)
|
2023-01-17 11:47:47 +01:00
|
|
|
{
|
2023-02-16 03:04:08 +00:00
|
|
|
match G::consider_implied_clause(self, goal, assumption, []) {
|
2023-01-17 11:47:47 +01:00
|
|
|
Ok(result) => {
|
2023-01-28 06:00:27 -06:00
|
|
|
candidates.push(Candidate { source: CandidateSource::AliasBound, result })
|
2023-01-17 11:47:47 +01:00
|
|
|
}
|
|
|
|
Err(NoSolution) => (),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
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>>,
|
|
|
|
) {
|
|
|
|
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(_, _)
|
|
|
|
| ty::Slice(_)
|
|
|
|
| ty::RawPtr(_)
|
|
|
|
| ty::Ref(_, _, _)
|
|
|
|
| ty::FnDef(_, _)
|
|
|
|
| ty::FnPtr(_)
|
|
|
|
| ty::Alias(..)
|
|
|
|
| ty::Closure(..)
|
|
|
|
| ty::Generator(..)
|
|
|
|
| ty::GeneratorWitness(_)
|
2022-10-01 14:56:24 +02:00
|
|
|
| ty::GeneratorWitnessMIR(..)
|
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,
|
|
|
|
};
|
|
|
|
|
|
|
|
let tcx = self.tcx();
|
2023-03-27 19:41:15 +00:00
|
|
|
let own_bounds: FxIndexSet<_> =
|
|
|
|
bounds.iter().map(|bound| bound.with_self_ty(tcx, self_ty)).collect();
|
2023-02-02 21:22:02 +00:00
|
|
|
for assumption in elaborate(tcx, own_bounds.iter().copied())
|
|
|
|
// we only care about bounds that match the `Self` type
|
|
|
|
.filter_only_self()
|
|
|
|
{
|
2023-03-27 19:41:15 +00:00
|
|
|
// FIXME: Predicates are fully elaborated in the object type's existential bounds
|
|
|
|
// list. We want to only consider these pre-elaborated projections, and not other
|
|
|
|
// projection predicates that we reach by elaborating the principal trait ref,
|
|
|
|
// since that'll cause ambiguity.
|
|
|
|
//
|
|
|
|
// We can remove this when we have implemented intersections in responses.
|
|
|
|
if assumption.to_opt_poly_projection_pred().is_some()
|
|
|
|
&& !own_bounds.contains(&assumption)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2023-03-26 20:33:54 +00:00
|
|
|
match G::consider_object_bound_candidate(self, goal, assumption) {
|
2023-01-17 18:19:11 +00:00
|
|
|
Ok(result) => {
|
|
|
|
candidates.push(Candidate { source: CandidateSource::BuiltinImpl, result })
|
|
|
|
}
|
|
|
|
Err(NoSolution) => (),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-02-08 19:25:21 +00:00
|
|
|
|
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>>,
|
|
|
|
) {
|
|
|
|
match self.solver_mode() {
|
|
|
|
SolverMode::Normal => return,
|
|
|
|
SolverMode::Coherence => {
|
|
|
|
let trait_ref = goal.predicate.trait_ref(self.tcx());
|
|
|
|
match coherence::trait_ref_is_knowable(self.tcx(), trait_ref) {
|
|
|
|
Ok(()) => {}
|
|
|
|
Err(_) => match self
|
|
|
|
.evaluate_added_goals_and_make_canonical_response(Certainty::AMBIGUOUS)
|
|
|
|
{
|
|
|
|
Ok(result) => candidates
|
|
|
|
.push(Candidate { source: CandidateSource::BuiltinImpl, result }),
|
|
|
|
// FIXME: This will be reachable at some point if we're in
|
|
|
|
// `assemble_candidates_after_normalizing_self_ty` and we get a
|
|
|
|
// universe error. We'll deal with it at this point.
|
|
|
|
Err(NoSolution) => bug!("coherence candidate resulted in NoSolution"),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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,
|
|
|
|
mut candidates: Vec<Candidate<'tcx>>,
|
|
|
|
) -> 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);
|
2023-02-08 19:25:21 +00:00
|
|
|
}
|
|
|
|
|
2023-03-29 15:36:17 +02:00
|
|
|
// We then check whether we should prioritize `ParamEnv` candidates.
|
|
|
|
//
|
|
|
|
// Doing so is incomplete and would therefore be unsound during coherence.
|
|
|
|
match self.solver_mode() {
|
|
|
|
SolverMode::Coherence => (),
|
|
|
|
// Prioritize `ParamEnv` candidates only if they do not guide inference.
|
|
|
|
//
|
|
|
|
// This is still incomplete as we may add incorrect region bounds.
|
|
|
|
SolverMode::Normal => {
|
|
|
|
let param_env_responses = candidates
|
|
|
|
.iter()
|
|
|
|
.filter(|c| matches!(c.source, CandidateSource::ParamEnv(_)))
|
|
|
|
.map(|c| c.result)
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
if let Some(result) = self.try_merge_responses(¶m_env_responses) {
|
|
|
|
if result.has_only_region_constraints() {
|
|
|
|
return Ok(result);
|
2023-02-08 19:25:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-03-29 15:36:17 +02:00
|
|
|
self.flounder(&responses)
|
2023-02-08 19:25:21 +00:00
|
|
|
}
|
2022-12-19 07:01:38 +00:00
|
|
|
}
|