Skip to content

Commit a61616a

Browse files
Move canonicalization code around
1 parent 7f89c7c commit a61616a

File tree

4 files changed

+47
-72
lines changed

4 files changed

+47
-72
lines changed

compiler/rustc_trait_selection/src/solve/eval_ctxt.rs

+6-55
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
11
use rustc_hir::def_id::DefId;
22
use rustc_infer::infer::at::ToTrace;
3-
use rustc_infer::infer::canonical::query_response::make_query_region_constraints;
4-
use rustc_infer::infer::canonical::{Canonical, CanonicalVarInfo, CanonicalVarValues};
3+
use rustc_infer::infer::canonical::CanonicalVarValues;
54
use rustc_infer::infer::type_variable::{TypeVariableOrigin, TypeVariableOriginKind};
65
use rustc_infer::infer::{
76
DefineOpaqueTypes, InferCtxt, InferOk, LateBoundRegionConversionTime, TyCtxtInferExt,
87
};
98
use rustc_infer::traits::query::NoSolution;
109
use rustc_infer::traits::ObligationCause;
1110
use rustc_middle::infer::unify_key::{ConstVariableOrigin, ConstVariableOriginKind};
12-
use rustc_middle::traits::solve::{
13-
CanonicalGoal, Certainty, ExternalConstraints, ExternalConstraintsData, MaybeCause, QueryResult,
14-
};
11+
use rustc_middle::traits::solve::{CanonicalGoal, Certainty, MaybeCause, QueryResult};
1512
use rustc_middle::ty::{
1613
self, Ty, TyCtxt, TypeFoldable, TypeSuperVisitable, TypeVisitable, TypeVisitableExt,
1714
TypeVisitor,
@@ -21,11 +18,12 @@ use std::ops::ControlFlow;
2118

2219
use crate::traits::specialization_graph;
2320

24-
use super::canonical::{CanonicalizeMode, Canonicalizer};
2521
use super::search_graph::{self, OverflowHandler};
2622
use super::SolverMode;
2723
use super::{search_graph::SearchGraph, Goal};
2824

25+
mod canonical;
26+
2927
pub struct EvalCtxt<'a, 'tcx> {
3028
/// The inference context that backs (mostly) inference and placeholder terms
3129
/// instantiated while solving goals.
@@ -414,7 +412,7 @@ impl<'tcx> EvalCtxt<'_, 'tcx> {
414412
if let &ty::Infer(ty::TyVar(vid)) = ty.kind() {
415413
match self.infcx.probe_ty_var(vid) {
416414
Ok(value) => bug!("resolved var in query: {goal:?} {value:?}"),
417-
Err(universe) => universe == self.universe(),
415+
Err(universe) => universe == self.infcx.universe(),
418416
}
419417
} else {
420418
false
@@ -424,7 +422,7 @@ impl<'tcx> EvalCtxt<'_, 'tcx> {
424422
if let ty::ConstKind::Infer(ty::InferConst::Var(vid)) = ct.kind() {
425423
match self.infcx.probe_const_var(vid) {
426424
Ok(value) => bug!("resolved var in query: {goal:?} {value:?}"),
427-
Err(universe) => universe == self.universe(),
425+
Err(universe) => universe == self.infcx.universe(),
428426
}
429427
} else {
430428
false
@@ -566,22 +564,6 @@ impl<'tcx> EvalCtxt<'_, 'tcx> {
566564
self.infcx.fresh_substs_for_item(DUMMY_SP, def_id)
567565
}
568566

569-
pub(super) fn universe(&self) -> ty::UniverseIndex {
570-
self.infcx.universe()
571-
}
572-
573-
pub(super) fn create_next_universe(&self) -> ty::UniverseIndex {
574-
self.infcx.create_next_universe()
575-
}
576-
577-
pub(super) fn instantiate_canonical_var(
578-
&self,
579-
cv_info: CanonicalVarInfo<'tcx>,
580-
universe_map: impl Fn(ty::UniverseIndex) -> ty::UniverseIndex,
581-
) -> ty::GenericArg<'tcx> {
582-
self.infcx.instantiate_canonical_var(DUMMY_SP, cv_info, universe_map)
583-
}
584-
585567
pub(super) fn translate_substs(
586568
&self,
587569
param_env: ty::ParamEnv<'tcx>,
@@ -621,35 +603,4 @@ impl<'tcx> EvalCtxt<'_, 'tcx> {
621603
crate::traits::wf::unnormalized_obligations(self.infcx, param_env, arg)
622604
.map(|obligations| obligations.into_iter().map(|obligation| obligation.into()))
623605
}
624-
625-
#[instrument(level = "debug", skip(self), ret)]
626-
pub(super) fn compute_external_query_constraints(
627-
&self,
628-
) -> Result<ExternalConstraints<'tcx>, NoSolution> {
629-
// Cannot use `take_registered_region_obligations` as we may compute the response
630-
// inside of a `probe` whenever we have multiple choices inside of the solver.
631-
let region_obligations = self.infcx.inner.borrow().region_obligations().to_owned();
632-
let region_constraints = self.infcx.with_region_constraints(|region_constraints| {
633-
make_query_region_constraints(
634-
self.tcx(),
635-
region_obligations
636-
.iter()
637-
.map(|r_o| (r_o.sup_type, r_o.sub_region, r_o.origin.to_constraint_category())),
638-
region_constraints,
639-
)
640-
});
641-
let opaque_types = self.infcx.clone_opaque_types_for_query_response();
642-
Ok(self
643-
.tcx()
644-
.mk_external_constraints(ExternalConstraintsData { region_constraints, opaque_types }))
645-
}
646-
647-
pub(super) fn canonicalize<T: TypeFoldable<TyCtxt<'tcx>>>(
648-
&self,
649-
canonicalize_mode: CanonicalizeMode,
650-
variables: &mut Vec<ty::GenericArg<'tcx>>,
651-
value: T,
652-
) -> Canonical<'tcx, T> {
653-
Canonicalizer::canonicalize(self.infcx, canonicalize_mode, variables, value)
654-
}
655606
}

compiler/rustc_trait_selection/src/solve/canonical/mod.rs renamed to compiler/rustc_trait_selection/src/solve/eval_ctxt/canonical.rs

+39-14
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,19 @@
88
/// section of the [rustc-dev-guide][c].
99
///
1010
/// [c]: https://rustc-dev-guide.rust-lang.org/solve/canonicalization.html
11-
pub use self::canonicalize::{CanonicalizeMode, Canonicalizer};
12-
1311
use super::{CanonicalGoal, Certainty, EvalCtxt, Goal};
14-
use super::{CanonicalResponse, QueryResult, Response};
12+
use crate::solve::canonicalize::{CanonicalizeMode, Canonicalizer};
13+
use crate::solve::{CanonicalResponse, QueryResult, Response};
14+
use rustc_infer::infer::canonical::query_response::make_query_region_constraints;
1515
use rustc_infer::infer::canonical::CanonicalVarValues;
1616
use rustc_infer::infer::canonical::{CanonicalExt, QueryRegionConstraints};
17-
use rustc_infer::traits::query::NoSolution;
18-
use rustc_infer::traits::solve::ExternalConstraintsData;
17+
use rustc_middle::traits::query::NoSolution;
18+
use rustc_middle::traits::solve::{ExternalConstraints, ExternalConstraintsData};
1919
use rustc_middle::ty::{self, GenericArgKind};
20+
use rustc_span::DUMMY_SP;
2021
use std::iter;
2122
use std::ops::Deref;
2223

23-
mod canonicalize;
24-
2524
impl<'tcx> EvalCtxt<'_, 'tcx> {
2625
/// Canonicalizes the goal remembering the original values
2726
/// for each bound variable.
@@ -30,7 +29,12 @@ impl<'tcx> EvalCtxt<'_, 'tcx> {
3029
goal: Goal<'tcx, ty::Predicate<'tcx>>,
3130
) -> (Vec<ty::GenericArg<'tcx>>, CanonicalGoal<'tcx>) {
3231
let mut orig_values = Default::default();
33-
let canonical_goal = self.canonicalize(CanonicalizeMode::Input, &mut orig_values, goal);
32+
let canonical_goal = Canonicalizer::canonicalize(
33+
self.infcx,
34+
CanonicalizeMode::Input,
35+
&mut orig_values,
36+
goal,
37+
);
3438
(orig_values, canonical_goal)
3539
}
3640

@@ -41,7 +45,7 @@ impl<'tcx> EvalCtxt<'_, 'tcx> {
4145
/// - `external_constraints`: additional constraints which aren't expressable
4246
/// using simple unification of inference variables.
4347
#[instrument(level = "debug", skip(self))]
44-
pub(super) fn evaluate_added_goals_and_make_canonical_response(
48+
pub(in crate::solve) fn evaluate_added_goals_and_make_canonical_response(
4549
&mut self,
4650
certainty: Certainty,
4751
) -> QueryResult<'tcx> {
@@ -51,14 +55,35 @@ impl<'tcx> EvalCtxt<'_, 'tcx> {
5155
let external_constraints = self.compute_external_query_constraints()?;
5256

5357
let response = Response { var_values: self.var_values, external_constraints, certainty };
54-
let canonical = self.canonicalize(
58+
let canonical = Canonicalizer::canonicalize(
59+
self.infcx,
5560
CanonicalizeMode::Response { max_input_universe: self.max_input_universe },
5661
&mut Default::default(),
5762
response,
5863
);
5964
Ok(canonical)
6065
}
6166

67+
#[instrument(level = "debug", skip(self), ret)]
68+
fn compute_external_query_constraints(&self) -> Result<ExternalConstraints<'tcx>, NoSolution> {
69+
// Cannot use `take_registered_region_obligations` as we may compute the response
70+
// inside of a `probe` whenever we have multiple choices inside of the solver.
71+
let region_obligations = self.infcx.inner.borrow().region_obligations().to_owned();
72+
let region_constraints = self.infcx.with_region_constraints(|region_constraints| {
73+
make_query_region_constraints(
74+
self.tcx(),
75+
region_obligations
76+
.iter()
77+
.map(|r_o| (r_o.sup_type, r_o.sub_region, r_o.origin.to_constraint_category())),
78+
region_constraints,
79+
)
80+
});
81+
let opaque_types = self.infcx.clone_opaque_types_for_query_response();
82+
Ok(self
83+
.tcx()
84+
.mk_external_constraints(ExternalConstraintsData { region_constraints, opaque_types }))
85+
}
86+
6287
/// After calling a canonical query, we apply the constraints returned
6388
/// by the query using this function.
6489
///
@@ -98,10 +123,10 @@ impl<'tcx> EvalCtxt<'_, 'tcx> {
98123
// FIXME: Longterm canonical queries should deal with all placeholders
99124
// created inside of the query directly instead of returning them to the
100125
// caller.
101-
let prev_universe = self.universe();
126+
let prev_universe = self.infcx.universe();
102127
let universes_created_in_query = response.max_universe.index() + 1;
103128
for _ in 0..universes_created_in_query {
104-
self.create_next_universe();
129+
self.infcx.create_next_universe();
105130
}
106131

107132
let var_values = response.value.var_values;
@@ -144,7 +169,7 @@ impl<'tcx> EvalCtxt<'_, 'tcx> {
144169
// A variable from inside a binder of the query. While ideally these shouldn't
145170
// exist at all (see the FIXME at the start of this method), we have to deal with
146171
// them for now.
147-
self.instantiate_canonical_var(info, |idx| {
172+
self.infcx.instantiate_canonical_var(DUMMY_SP, info, |idx| {
148173
ty::UniverseIndex::from(prev_universe.index() + idx.index())
149174
})
150175
} else if info.is_existential() {
@@ -158,7 +183,7 @@ impl<'tcx> EvalCtxt<'_, 'tcx> {
158183
if let Some(v) = opt_values[index] {
159184
v
160185
} else {
161-
self.instantiate_canonical_var(info, |_| prev_universe)
186+
self.infcx.instantiate_canonical_var(DUMMY_SP, info, |_| prev_universe)
162187
}
163188
} else {
164189
// For placeholders which were already part of the input, we simply map this

compiler/rustc_trait_selection/src/solve/mod.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,15 @@ use rustc_hir::def_id::DefId;
1515
use rustc_infer::infer::canonical::{Canonical, CanonicalVarValues};
1616
use rustc_infer::traits::query::NoSolution;
1717
use rustc_middle::traits::solve::{
18-
CanonicalGoal, CanonicalResponse, Certainty, ExternalConstraintsData, Goal, QueryResult,
19-
Response,
18+
CanonicalResponse, Certainty, ExternalConstraintsData, Goal, QueryResult, Response,
2019
};
2120
use rustc_middle::ty::{self, Ty, TyCtxt};
2221
use rustc_middle::ty::{
2322
CoercePredicate, RegionOutlivesPredicate, SubtypePredicate, TypeOutlivesPredicate,
2423
};
2524

2625
mod assembly;
27-
mod canonical;
26+
mod canonicalize;
2827
mod eval_ctxt;
2928
mod fulfill;
3029
mod project_goals;

0 commit comments

Comments
 (0)