Skip to content

Commit

Permalink
Rollup merge of rust-lang#105661 - lcnr:evaluate-new, r=compiler-errors
Browse files Browse the repository at this point in the history
implement the skeleton of the updated trait solver

cc ``@rust-lang/initiative-trait-system-refactor``

This is mostly following the architecture discussed in the types team meetup.

After discussing the desired changes for the trait solver, we encountered cyclic dependencies between them. Most notably between changing evaluate to be canonical and returning inference constraints. We cannot canonicalize evaluate without returning inference constraints due to coinductive cycles. However, caching inference constraints also relies on canonicalization. Implementing both of these changes at once in-place is not feasible.

This somewhat closely mirrors the current `evaluate` implementation with the following notable differences:
- it moves `project` into the core solver, allowing us to correctly deal with coinductive projections (will be required for implied bounds, perfect derive)
- it changes trait solver overflow to be non-fatal (required to backcompat breakage from changes to the iteration order of nested goals, deferred projection equality, generally very useful)
- it returns inference constraints and canonicalizes inputs and outputs (required for a lot things, most notably merging fulfill and evaluate, and deferred projection equality)
- it is implemented to work with lazy normalization

A lot of things aren't yet implemented, but the remaining FIXMEs should all be fairly self-contained and parallelizable. If the architecture looks correct and is what we want here, I would like to quickly merge this and then split the work.

r? ``@compiler-errors`` / ``@rust-lang/types`` :3
  • Loading branch information
fee1-dead authored Dec 21, 2022
2 parents fd3c544 + 750bf36 commit 4fc45e5
Show file tree
Hide file tree
Showing 22 changed files with 1,538 additions and 87 deletions.
6 changes: 5 additions & 1 deletion compiler/rustc_infer/src/infer/canonical/query_response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,11 @@ impl<'tcx> InferCtxt<'tcx> {
})
}

fn take_opaque_types_for_query_response(&self) -> Vec<(Ty<'tcx>, Ty<'tcx>)> {
/// FIXME: This method should only be used for canonical queries and therefore be private.
///
/// As the new solver does canonicalization slightly differently, this is also used there
/// for now. This should hopefully change fairly soon.
pub fn take_opaque_types_for_query_response(&self) -> Vec<(Ty<'tcx>, Ty<'tcx>)> {
self.inner
.borrow_mut()
.opaque_type_storage
Expand Down
10 changes: 10 additions & 0 deletions compiler/rustc_middle/src/infer/canonical.rs
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,16 @@ impl<'tcx, V> Canonical<'tcx, V> {
let Canonical { max_universe, variables, value } = self;
Canonical { max_universe, variables, value: map_op(value) }
}

/// Allows you to map the `value` of a canonical while keeping the same set of
/// bound variables.
///
/// **WARNING:** This function is very easy to mis-use, hence the name! See
/// the comment of [Canonical::unchecked_map] for more details.
pub fn unchecked_rebind<W>(self, value: W) -> Canonical<'tcx, W> {
let Canonical { max_universe, variables, value: _ } = self;
Canonical { max_universe, variables, value }
}
}

pub type QueryOutlivesConstraint<'tcx> = (
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_middle/src/traits/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ pub type CanonicalTypeOpProvePredicateGoal<'tcx> =
pub type CanonicalTypeOpNormalizeGoal<'tcx, T> =
Canonical<'tcx, ty::ParamEnvAnd<'tcx, type_op::Normalize<T>>>;

#[derive(Copy, Clone, Debug, HashStable)]
#[derive(Copy, Clone, Debug, HashStable, PartialEq, Eq)]
pub struct NoSolution;

pub type Fallible<T> = Result<T, NoSolution>;
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_middle/src/traits/specialization_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ impl Iterator for Ancestors<'_> {
}

/// Information about the most specialized definition of an associated item.
#[derive(Debug)]
pub struct LeafDef {
/// The associated item described by this `LeafDef`.
pub item: ty::AssocItem,
Expand Down
41 changes: 29 additions & 12 deletions compiler/rustc_middle/src/ty/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,17 @@ impl<'tcx> Predicate<'tcx> {
self
}

#[instrument(level = "debug", skip(tcx), ret)]
pub fn is_coinductive(self, tcx: TyCtxt<'tcx>) -> bool {
match self.kind().skip_binder() {
ty::PredicateKind::Clause(ty::Clause::Trait(data)) => {
tcx.trait_is_coinductive(data.def_id())
}
ty::PredicateKind::WellFormed(_) => true,
_ => false,
}
}

/// Whether this projection can be soundly normalized.
///
/// Wf predicates must not be normalized, as normalization
Expand Down Expand Up @@ -1018,6 +1029,24 @@ pub struct ProjectionPredicate<'tcx> {
pub term: Term<'tcx>,
}

impl<'tcx> ProjectionPredicate<'tcx> {
pub fn self_ty(self) -> Ty<'tcx> {
self.projection_ty.self_ty()
}

pub fn with_self_ty(self, tcx: TyCtxt<'tcx>, self_ty: Ty<'tcx>) -> ProjectionPredicate<'tcx> {
Self { projection_ty: self.projection_ty.with_self_ty(tcx, self_ty), ..self }
}

pub fn trait_def_id(self, tcx: TyCtxt<'tcx>) -> DefId {
self.projection_ty.trait_def_id(tcx)
}

pub fn def_id(self) -> DefId {
self.projection_ty.def_id
}
}

pub type PolyProjectionPredicate<'tcx> = Binder<'tcx, ProjectionPredicate<'tcx>>;

impl<'tcx> PolyProjectionPredicate<'tcx> {
Expand Down Expand Up @@ -1054,18 +1083,6 @@ impl<'tcx> PolyProjectionPredicate<'tcx> {
}
}

impl<'tcx> ProjectionPredicate<'tcx> {
pub fn with_self_ty(self, tcx: TyCtxt<'tcx>, self_ty: Ty<'tcx>) -> Self {
Self {
projection_ty: tcx.mk_alias_ty(
self.projection_ty.def_id,
[self_ty.into()].into_iter().chain(self.projection_ty.substs.iter().skip(1)),
),
..self
}
}
}

pub trait ToPolyTraitRef<'tcx> {
fn to_poly_trait_ref(&self) -> PolyTraitRef<'tcx>;
}
Expand Down
12 changes: 8 additions & 4 deletions compiler/rustc_middle/src/ty/sty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1169,7 +1169,7 @@ pub struct AliasTy<'tcx> {
}

impl<'tcx> AliasTy<'tcx> {
pub fn trait_def_id(&self, tcx: TyCtxt<'tcx>) -> DefId {
pub fn trait_def_id(self, tcx: TyCtxt<'tcx>) -> DefId {
match tcx.def_kind(self.def_id) {
DefKind::AssocTy | DefKind::AssocConst => tcx.parent(self.def_id),
DefKind::ImplTraitPlaceholder => {
Expand All @@ -1183,7 +1183,7 @@ impl<'tcx> AliasTy<'tcx> {
/// For example, if this is a projection of `<T as StreamingIterator>::Item<'a>`,
/// then this function would return a `T: Iterator` trait reference and `['a]` as the own substs
pub fn trait_ref_and_own_substs(
&self,
self,
tcx: TyCtxt<'tcx>,
) -> (ty::TraitRef<'tcx>, &'tcx [ty::GenericArg<'tcx>]) {
debug_assert!(matches!(tcx.def_kind(self.def_id), DefKind::AssocTy | DefKind::AssocConst));
Expand All @@ -1202,14 +1202,18 @@ impl<'tcx> AliasTy<'tcx> {
/// WARNING: This will drop the substs for generic associated types
/// consider calling [Self::trait_ref_and_own_substs] to get those
/// as well.
pub fn trait_ref(&self, tcx: TyCtxt<'tcx>) -> ty::TraitRef<'tcx> {
pub fn trait_ref(self, tcx: TyCtxt<'tcx>) -> ty::TraitRef<'tcx> {
let def_id = self.trait_def_id(tcx);
tcx.mk_trait_ref(def_id, self.substs.truncate_to(tcx, tcx.generics_of(def_id)))
}

pub fn self_ty(&self) -> Ty<'tcx> {
pub fn self_ty(self) -> Ty<'tcx> {
self.substs.type_at(0)
}

pub fn with_self_ty(self, tcx: TyCtxt<'tcx>, self_ty: Ty<'tcx>) -> Self {
tcx.mk_alias_ty(self.def_id, [self_ty.into()].into_iter().chain(self.substs.iter().skip(1)))
}
}

#[derive(Copy, Clone, Debug, TypeFoldable, TypeVisitable, Lift)]
Expand Down
4 changes: 4 additions & 0 deletions compiler/rustc_middle/src/ty/subst.rs
Original file line number Diff line number Diff line change
Expand Up @@ -573,6 +573,10 @@ impl<T> EarlyBinder<T> {
pub fn rebind<U>(&self, value: U) -> EarlyBinder<U> {
EarlyBinder(value)
}

pub fn skip_binder(self) -> T {
self.0
}
}

impl<T> EarlyBinder<Option<T>> {
Expand Down
2 changes: 2 additions & 0 deletions compiler/rustc_trait_selection/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#![feature(let_chains)]
#![feature(if_let_guard)]
#![feature(never_type)]
#![feature(result_option_inspect)]
#![feature(type_alias_impl_trait)]
#![recursion_limit = "512"] // For rustdoc

Expand All @@ -37,4 +38,5 @@ extern crate smallvec;
pub mod autoderef;
pub mod errors;
pub mod infer;
pub mod solve;
pub mod traits;
150 changes: 150 additions & 0 deletions compiler/rustc_trait_selection/src/solve/assembly.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
//! Code shared by trait and projection goals for candidate assembly.

use super::infcx_ext::InferCtxtExt;
use super::{
fixme_instantiate_canonical_query_response, CanonicalGoal, CanonicalResponse, Certainty,
EvalCtxt, Goal,
};
use rustc_hir::def_id::DefId;
use rustc_infer::infer::TyCtxtInferExt;
use rustc_infer::infer::{
canonical::{CanonicalVarValues, OriginalQueryValues},
InferCtxt,
};
use rustc_infer::traits::query::NoSolution;
use rustc_middle::ty::TypeFoldable;
use rustc_middle::ty::{self, Ty, TyCtxt};
use rustc_span::DUMMY_SP;
use std::fmt::Debug;

/// 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`.
///
/// For the list of possible candidates, please look at the documentation of
/// [super::trait_goals::CandidateSource] and [super::project_goals::CandidateSource].
#[derive(Debug, Clone)]
pub(super) struct Candidate<'tcx, G: GoalKind<'tcx>> {
pub(super) source: G::CandidateSource,
pub(super) result: CanonicalResponse<'tcx>,
}

pub(super) trait GoalKind<'tcx>: TypeFoldable<'tcx> + Copy {
type CandidateSource: Debug + Copy;

fn self_ty(self) -> Ty<'tcx>;

fn with_self_ty(self, tcx: TyCtxt<'tcx>, self_ty: Ty<'tcx>) -> Self;

fn trait_def_id(self, tcx: TyCtxt<'tcx>) -> DefId;

fn consider_impl_candidate(
acx: &mut AssemblyCtxt<'_, 'tcx, Self>,
goal: Goal<'tcx, Self>,
impl_def_id: DefId,
);
}

/// An abstraction which correctly deals with the canonical results for candidates.
///
/// It also deduplicates the behavior between trait and projection predicates.
pub(super) struct AssemblyCtxt<'a, 'tcx, G: GoalKind<'tcx>> {
pub(super) cx: &'a mut EvalCtxt<'tcx>,
pub(super) infcx: &'a InferCtxt<'tcx>,
var_values: CanonicalVarValues<'tcx>,
candidates: Vec<Candidate<'tcx, G>>,
}

impl<'a, 'tcx, G: GoalKind<'tcx>> AssemblyCtxt<'a, 'tcx, G> {
pub(super) fn assemble_and_evaluate_candidates(
cx: &'a mut EvalCtxt<'tcx>,
goal: CanonicalGoal<'tcx, G>,
) -> Vec<Candidate<'tcx, G>> {
let (ref infcx, goal, var_values) =
cx.tcx.infer_ctxt().build_with_canonical(DUMMY_SP, &goal);
let mut acx = AssemblyCtxt { cx, infcx, var_values, candidates: Vec::new() };

acx.assemble_candidates_after_normalizing_self_ty(goal);

acx.assemble_impl_candidates(goal);

acx.candidates
}

pub(super) fn try_insert_candidate(
&mut self,
source: G::CandidateSource,
certainty: Certainty,
) {
match self.infcx.make_canonical_response(self.var_values.clone(), certainty) {
Ok(result) => self.candidates.push(Candidate { source, result }),
Err(NoSolution) => debug!(?source, ?certainty, "failed leakcheck"),
}
}

/// 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
/// self type to the list of candidates in case that succeeds. Note that we can't just eagerly return in
/// this case as projections as self types add `
fn assemble_candidates_after_normalizing_self_ty(&mut self, goal: Goal<'tcx, G>) {
let tcx = self.cx.tcx;
// 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
};
self.infcx.probe(|_| {
let normalized_ty = self.infcx.next_ty_infer();
let normalizes_to_goal = goal.with(
tcx,
ty::Binder::dummy(ty::ProjectionPredicate {
projection_ty,
term: normalized_ty.into(),
}),
);
let normalization_certainty =
match self.cx.evaluate_goal(&self.infcx, normalizes_to_goal) {
Ok((_, certainty)) => certainty,
Err(NoSolution) => return,
};

// 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 both winnowing and to resolve associated items.
let goal = goal.with(tcx, goal.predicate.with_self_ty(tcx, normalized_ty));
let mut orig_values = OriginalQueryValues::default();
let goal = self.infcx.canonicalize_query(goal, &mut orig_values);
let normalized_candidates =
AssemblyCtxt::assemble_and_evaluate_candidates(self.cx, goal);

// Map each candidate from being canonical wrt the current inference context to being
// canonical wrt the caller.
for Candidate { source, result } in normalized_candidates {
self.infcx.probe(|_| {
let candidate_certainty = fixme_instantiate_canonical_query_response(
&self.infcx,
&orig_values,
result,
);

// FIXME: This is a bit scary if the `normalizes_to_goal` overflows.
//
// If we have an ambiguous candidate it hides that normalization
// caused an overflow which may cause issues.
self.try_insert_candidate(
source,
normalization_certainty.unify_and(candidate_certainty),
)
})
}
})
}

fn assemble_impl_candidates(&mut self, goal: Goal<'tcx, G>) {
self.cx.tcx.for_each_relevant_impl(
goal.predicate.trait_def_id(self.cx.tcx),
goal.predicate.self_ty(),
|impl_def_id| G::consider_impl_candidate(self, goal, impl_def_id),
);
}
}
Loading

0 comments on commit 4fc45e5

Please sign in to comment.