Skip to content

Commit f355369

Browse files
Comment stuff in the new solver
1 parent 77e24f9 commit f355369

File tree

8 files changed

+63
-16
lines changed

8 files changed

+63
-16
lines changed

compiler/rustc_trait_selection/src/solve/alias_relate.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
1+
//! Implements the `AliasRelate` goal, which is used to unify two aliases in the
2+
//! new solver, which uses "lazy normalization".
3+
//!
4+
//! This goal, e.g. `A alias-relate B`, may be satisfied by one of three branches:
5+
//! * normalizes-to: If `A` is a projection, we can prove the equivalent
6+
//! projection predicate with B as the right-hand side of the projection.
7+
//! This goal is computed in both directions, if both are aliases.
8+
//! * subst-relate: Equate `A` and `B` by their substs, if they're both
9+
//! aliases with the same def-id.
10+
//! * bidirectional-normalizes-to: If `A` and `B` are both projections, and both
11+
//! may apply, then we can compute the "intersection" of both normalizes-to by
12+
//! performing them together. This is used specifically to resolve ambiguities.
113
use super::{EvalCtxt, SolverMode};
214
use rustc_infer::traits::query::NoSolution;
315
use rustc_middle::traits::solve::{Certainty, Goal, QueryResult};
@@ -115,6 +127,8 @@ impl<'tcx> EvalCtxt<'_, 'tcx> {
115127
})
116128
}
117129

130+
// Computes the normalizes-to branch, with side-effects. This must be performed
131+
// in a probe in order to not taint the evaluation context.
118132
fn normalizes_to_inner(
119133
&mut self,
120134
param_env: ty::ParamEnv<'tcx>,
@@ -124,9 +138,13 @@ impl<'tcx> EvalCtxt<'_, 'tcx> {
124138
invert: Invert,
125139
) -> Result<(), NoSolution> {
126140
let other = match direction {
127-
// This is purely an optimization.
141+
// This is purely an optimization. No need to instantiate a new
142+
// infer var and equate the RHS to it.
128143
ty::AliasRelationDirection::Equate => other,
129144

145+
// Instantiate an infer var and subtype our RHS to it, so that we
146+
// properly represent a subtype relation between the LHS and RHS
147+
// of the goal.
130148
ty::AliasRelationDirection::Subtype => {
131149
let fresh = self.next_term_infer_of_kind(other);
132150
let (sub, sup) = match invert {

compiler/rustc_trait_selection/src/solve/assembly/structural_traits.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//! Code which is used by built-in goals that match "structurally", such a auto
2+
//! traits, `Copy`/`Clone`.
13
use rustc_data_structures::fx::FxHashMap;
24
use rustc_hir::{def_id::DefId, Movability, Mutability};
35
use rustc_infer::traits::query::NoSolution;

compiler/rustc_trait_selection/src/solve/eval_ctxt/canonical.rs

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
/// Canonicalization is used to separate some goal from its context,
2-
/// throwing away unnecessary information in the process.
3-
///
4-
/// This is necessary to cache goals containing inference variables
5-
/// and placeholders without restricting them to the current `InferCtxt`.
6-
///
7-
/// Canonicalization is fairly involved, for more details see the relevant
8-
/// section of the [rustc-dev-guide][c].
9-
///
10-
/// [c]: https://rustc-dev-guide.rust-lang.org/solve/canonicalization.html
1+
//! Canonicalization is used to separate some goal from its context,
2+
//! throwing away unnecessary information in the process.
3+
//!
4+
//! This is necessary to cache goals containing inference variables
5+
//! and placeholders without restricting them to the current `InferCtxt`.
6+
//!
7+
//! Canonicalization is fairly involved, for more details see the relevant
8+
//! section of the [rustc-dev-guide][c].
9+
//!
10+
//! [c]: https://rustc-dev-guide.rust-lang.org/solve/canonicalization.html
1111
use super::{CanonicalInput, Certainty, EvalCtxt, Goal};
1212
use crate::solve::canonicalize::{CanonicalizeMode, Canonicalizer};
1313
use crate::solve::{CanonicalResponse, QueryResult, Response};
@@ -135,6 +135,13 @@ impl<'tcx> EvalCtxt<'_, 'tcx> {
135135
)
136136
}
137137

138+
/// Computes the region constraints and *new* opaque types registered when
139+
/// proving a goal.
140+
///
141+
/// If an opaque was already constrained before proving this goal, then the
142+
/// external constraints do not need to record that opaque, since if it is
143+
/// further constrained by inference, that will be passed back in the var
144+
/// values.
138145
#[instrument(level = "debug", skip(self), ret)]
139146
fn compute_external_query_constraints(&self) -> Result<ExternalConstraints<'tcx>, NoSolution> {
140147
// We only check for leaks from universes which were entered inside

compiler/rustc_trait_selection/src/solve/inherent_projection.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
//! Computes a normalizes-to (projection) goal for inherent associated types,
2+
//! `#![feature(inherent_associated_type)]`. Since astconv already determines
3+
//! which impl the IAT is being projected from, we just:
4+
//! 1. instantiate substs,
5+
//! 2. equate the self type, and
6+
//! 3. instantiate and register where clauses.
17
use rustc_middle::traits::solve::{Certainty, Goal, QueryResult};
28
use rustc_middle::ty;
39

compiler/rustc_trait_selection/src/solve/mod.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
1-
//! The new trait solver, currently still WIP.
1+
//! The next-generation trait solver, currently still WIP.
22
//!
3-
//! As a user of the trait system, you can use `TyCtxt::evaluate_goal` to
4-
//! interact with this solver.
3+
//! As a user of rust, you can use `-Ztrait-solver=next` or `next-coherence`
4+
//! to enable the new trait solver always, or just within coherence, respectively.
5+
//!
6+
//! As a developer of rustc, you probably shouldn't be using the new trait
7+
//! solver without asking the trait-system-refactor-initiative, but it can
8+
//! be enabled with `InferCtxtBuilder::with_next_trait_solver`. This will
9+
//! ensure that trait solving using that inference context will be routed
10+
//! to the new trait solver.
511
//!
612
//! For a high-level overview of how this solver works, check out the relevant
713
//! section of the rustc-dev-guide.
814
//!
915
//! FIXME(@lcnr): Write that section. If you read this before then ask me
1016
//! about it on zulip.
11-
1217
use rustc_hir::def_id::DefId;
1318
use rustc_infer::infer::canonical::{Canonical, CanonicalVarValues};
1419
use rustc_infer::traits::query::NoSolution;

compiler/rustc_trait_selection/src/solve/normalize.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ pub(crate) fn deeply_normalize<'tcx, T: TypeFoldable<TyCtxt<'tcx>>>(
2828
/// its input to be already fully resolved.
2929
///
3030
/// Additionally takes a list of universes which represents the binders which have been
31-
/// entered before passing `value` to the function.
31+
/// entered before passing `value` to the function. This is currently needed for
32+
/// `normalize_erasing_regions`, which skips binders as it walks through a type.
3233
pub(crate) fn deeply_normalize_with_skipped_universes<'tcx, T: TypeFoldable<TyCtxt<'tcx>>>(
3334
at: At<'_, 'tcx>,
3435
value: T,

compiler/rustc_trait_selection/src/solve/opaques.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
//! Computes a normalizes-to (projection) goal for opaque types. This goal
2+
//! behaves differently depending on the param-env's reveal mode and whether
3+
//! the opaque is in a defining scope.
14
use rustc_middle::traits::query::NoSolution;
25
use rustc_middle::traits::solve::{Certainty, Goal, QueryResult};
36
use rustc_middle::traits::Reveal;

compiler/rustc_trait_selection/src/solve/weak_types.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
//! Computes a normalizes-to (projection) goal for inherent associated types,
2+
//! `#![feature(lazy_type_alias)]` and `#![feature(type_alias_impl_trait)]`.
3+
//!
4+
//! Since a weak alias is not ambiguous, this just computes the `type_of` of
5+
//! the alias and registers any where-clause predicates on the type alias.
16
use rustc_middle::traits::solve::{Certainty, Goal, QueryResult};
27
use rustc_middle::ty;
38

0 commit comments

Comments
 (0)