Skip to content

Commit f1ad7f0

Browse files
Rollup merge of #108110 - compiler-errors:new-solver-less-infcx, r=lcnr
Move some `InferCtxt` methods to `EvalCtxt` in new solver Moving towards eventually making the `InferCtxt` within `EvalCtxt` private, so that we make sure not to do anything strange in the solver. This doesn't finish this work yet, just gets it started. r? ``@lcnr``
2 parents f9216b7 + 7596998 commit f1ad7f0

File tree

7 files changed

+228
-226
lines changed

7 files changed

+228
-226
lines changed

compiler/rustc_trait_selection/src/solve/assembly.rs

+6-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
//! Code shared by trait and projection goals for candidate assembly.
22
3-
use super::infcx_ext::InferCtxtExt;
43
#[cfg(doc)]
54
use super::trait_goals::structural_traits::*;
65
use super::{CanonicalResponse, Certainty, EvalCtxt, Goal, MaybeCause, QueryResult};
@@ -206,7 +205,7 @@ impl<'tcx> EvalCtxt<'_, 'tcx> {
206205
&mut self,
207206
goal: Goal<'tcx, G>,
208207
) -> Vec<Candidate<'tcx>> {
209-
debug_assert_eq!(goal, self.infcx.resolve_vars_if_possible(goal));
208+
debug_assert_eq!(goal, self.resolve_vars_if_possible(goal));
210209

211210
// HACK: `_: Trait` is ambiguous, because it may be satisfied via a builtin rule,
212211
// object bound, alias bound, etc. We are unable to determine this until we can at
@@ -250,25 +249,25 @@ impl<'tcx> EvalCtxt<'_, 'tcx> {
250249
let &ty::Alias(ty::Projection, projection_ty) = goal.predicate.self_ty().kind() else {
251250
return
252251
};
253-
self.infcx.probe(|_| {
254-
let normalized_ty = self.infcx.next_ty_infer();
252+
self.probe(|this| {
253+
let normalized_ty = this.next_ty_infer();
255254
let normalizes_to_goal = goal.with(
256255
tcx,
257256
ty::Binder::dummy(ty::ProjectionPredicate {
258257
projection_ty,
259258
term: normalized_ty.into(),
260259
}),
261260
);
262-
let normalization_certainty = match self.evaluate_goal(normalizes_to_goal) {
261+
let normalization_certainty = match this.evaluate_goal(normalizes_to_goal) {
263262
Ok((_, certainty)) => certainty,
264263
Err(NoSolution) => return,
265264
};
266-
let normalized_ty = self.infcx.resolve_vars_if_possible(normalized_ty);
265+
let normalized_ty = this.resolve_vars_if_possible(normalized_ty);
267266

268267
// NOTE: Alternatively we could call `evaluate_goal` here and only have a `Normalized` candidate.
269268
// This doesn't work as long as we use `CandidateSource` in winnowing.
270269
let goal = goal.with(tcx, goal.predicate.with_self_ty(tcx, normalized_ty));
271-
let normalized_candidates = self.assemble_and_evaluate_candidates(goal);
270+
let normalized_candidates = this.assemble_and_evaluate_candidates(goal);
272271
for mut normalized_candidate in normalized_candidates {
273272
normalized_candidate.result =
274273
normalized_candidate.result.unchecked_map(|mut response| {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
use rustc_hir::def_id::DefId;
2+
use rustc_infer::infer::at::ToTrace;
3+
use rustc_infer::infer::canonical::CanonicalVarValues;
4+
use rustc_infer::infer::type_variable::{TypeVariableOrigin, TypeVariableOriginKind};
5+
use rustc_infer::infer::{InferCtxt, InferOk, LateBoundRegionConversionTime};
6+
use rustc_infer::traits::query::NoSolution;
7+
use rustc_infer::traits::ObligationCause;
8+
use rustc_middle::infer::unify_key::{ConstVariableOrigin, ConstVariableOriginKind};
9+
use rustc_middle::ty::{self, ir::TypeVisitor, Ty, TyCtxt, TypeFoldable, TypeSuperVisitable};
10+
use rustc_span::DUMMY_SP;
11+
use std::ops::ControlFlow;
12+
13+
use super::search_graph::SearchGraph;
14+
use super::Goal;
15+
16+
pub struct EvalCtxt<'a, 'tcx> {
17+
// FIXME: should be private.
18+
pub(super) infcx: &'a InferCtxt<'tcx>,
19+
20+
pub(super) var_values: CanonicalVarValues<'tcx>,
21+
22+
pub(super) search_graph: &'a mut SearchGraph<'tcx>,
23+
24+
/// This field is used by a debug assertion in [`EvalCtxt::evaluate_goal`],
25+
/// see the comment in that method for more details.
26+
pub in_projection_eq_hack: bool,
27+
}
28+
29+
impl<'tcx> EvalCtxt<'_, 'tcx> {
30+
pub(super) fn probe<T>(&mut self, f: impl FnOnce(&mut EvalCtxt<'_, 'tcx>) -> T) -> T {
31+
self.infcx.probe(|_| f(self))
32+
}
33+
34+
pub(super) fn tcx(&self) -> TyCtxt<'tcx> {
35+
self.infcx.tcx
36+
}
37+
38+
pub(super) fn next_ty_infer(&self) -> Ty<'tcx> {
39+
self.infcx.next_ty_var(TypeVariableOrigin {
40+
kind: TypeVariableOriginKind::MiscVariable,
41+
span: DUMMY_SP,
42+
})
43+
}
44+
45+
pub(super) fn next_const_infer(&self, ty: Ty<'tcx>) -> ty::Const<'tcx> {
46+
self.infcx.next_const_var(
47+
ty,
48+
ConstVariableOrigin { kind: ConstVariableOriginKind::MiscVariable, span: DUMMY_SP },
49+
)
50+
}
51+
52+
/// Is the projection predicate is of the form `exists<T> <Ty as Trait>::Assoc = T`.
53+
///
54+
/// This is the case if the `term` is an inference variable in the innermost universe
55+
/// and does not occur in any other part of the predicate.
56+
pub(super) fn term_is_fully_unconstrained(
57+
&self,
58+
goal: Goal<'tcx, ty::ProjectionPredicate<'tcx>>,
59+
) -> bool {
60+
let term_is_infer = match goal.predicate.term.unpack() {
61+
ty::TermKind::Ty(ty) => {
62+
if let &ty::Infer(ty::TyVar(vid)) = ty.kind() {
63+
match self.infcx.probe_ty_var(vid) {
64+
Ok(value) => bug!("resolved var in query: {goal:?} {value:?}"),
65+
Err(universe) => universe == self.universe(),
66+
}
67+
} else {
68+
false
69+
}
70+
}
71+
ty::TermKind::Const(ct) => {
72+
if let ty::ConstKind::Infer(ty::InferConst::Var(vid)) = ct.kind() {
73+
match self.infcx.probe_const_var(vid) {
74+
Ok(value) => bug!("resolved var in query: {goal:?} {value:?}"),
75+
Err(universe) => universe == self.universe(),
76+
}
77+
} else {
78+
false
79+
}
80+
}
81+
};
82+
83+
// Guard against `<T as Trait<?0>>::Assoc = ?0>`.
84+
struct ContainsTerm<'tcx> {
85+
term: ty::Term<'tcx>,
86+
}
87+
impl<'tcx> TypeVisitor<TyCtxt<'tcx>> for ContainsTerm<'tcx> {
88+
type BreakTy = ();
89+
fn visit_ty(&mut self, t: Ty<'tcx>) -> ControlFlow<Self::BreakTy> {
90+
if t.needs_infer() {
91+
if ty::Term::from(t) == self.term {
92+
ControlFlow::Break(())
93+
} else {
94+
t.super_visit_with(self)
95+
}
96+
} else {
97+
ControlFlow::Continue(())
98+
}
99+
}
100+
101+
fn visit_const(&mut self, c: ty::Const<'tcx>) -> ControlFlow<Self::BreakTy> {
102+
if c.needs_infer() {
103+
if ty::Term::from(c) == self.term {
104+
ControlFlow::Break(())
105+
} else {
106+
c.super_visit_with(self)
107+
}
108+
} else {
109+
ControlFlow::Continue(())
110+
}
111+
}
112+
}
113+
114+
let mut visitor = ContainsTerm { term: goal.predicate.term };
115+
116+
term_is_infer
117+
&& goal.predicate.projection_ty.visit_with(&mut visitor).is_continue()
118+
&& goal.param_env.visit_with(&mut visitor).is_continue()
119+
}
120+
121+
#[instrument(level = "debug", skip(self, param_env), ret)]
122+
pub(super) fn eq<T: ToTrace<'tcx>>(
123+
&self,
124+
param_env: ty::ParamEnv<'tcx>,
125+
lhs: T,
126+
rhs: T,
127+
) -> Result<Vec<Goal<'tcx, ty::Predicate<'tcx>>>, NoSolution> {
128+
self.infcx
129+
.at(&ObligationCause::dummy(), param_env)
130+
.eq(lhs, rhs)
131+
.map(|InferOk { value: (), obligations }| {
132+
obligations.into_iter().map(|o| o.into()).collect()
133+
})
134+
.map_err(|e| {
135+
debug!(?e, "failed to equate");
136+
NoSolution
137+
})
138+
}
139+
140+
pub(super) fn instantiate_binder_with_infer<T: TypeFoldable<'tcx> + Copy>(
141+
&self,
142+
value: ty::Binder<'tcx, T>,
143+
) -> T {
144+
self.infcx.instantiate_binder_with_fresh_vars(
145+
DUMMY_SP,
146+
LateBoundRegionConversionTime::HigherRankedType,
147+
value,
148+
)
149+
}
150+
151+
pub(super) fn instantiate_binder_with_placeholders<T: TypeFoldable<'tcx> + Copy>(
152+
&self,
153+
value: ty::Binder<'tcx, T>,
154+
) -> T {
155+
self.infcx.instantiate_binder_with_placeholders(value)
156+
}
157+
158+
pub(super) fn resolve_vars_if_possible<T>(&self, value: T) -> T
159+
where
160+
T: TypeFoldable<'tcx>,
161+
{
162+
self.infcx.resolve_vars_if_possible(value)
163+
}
164+
165+
pub(super) fn fresh_substs_for_item(&self, def_id: DefId) -> ty::SubstsRef<'tcx> {
166+
self.infcx.fresh_substs_for_item(DUMMY_SP, def_id)
167+
}
168+
169+
pub(super) fn universe(&self) -> ty::UniverseIndex {
170+
self.infcx.universe()
171+
}
172+
}

compiler/rustc_trait_selection/src/solve/infcx_ext.rs

-77
This file was deleted.

compiler/rustc_trait_selection/src/solve/mod.rs

+7-23
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,15 @@ use crate::solve::search_graph::OverflowHandler;
3535
use crate::traits::ObligationCause;
3636

3737
mod assembly;
38+
mod eval_ctxt;
3839
mod fulfill;
39-
mod infcx_ext;
4040
mod project_goals;
4141
mod search_graph;
4242
mod trait_goals;
4343

44+
pub use eval_ctxt::EvalCtxt;
4445
pub use fulfill::FulfillmentCtxt;
4546

46-
use self::infcx_ext::InferCtxtExt;
47-
4847
/// A goal is a statement, i.e. `predicate`, we want to prove
4948
/// given some assumptions, i.e. `param_env`.
5049
///
@@ -180,22 +179,7 @@ impl<'tcx> InferCtxtEvalExt<'tcx> for InferCtxt<'tcx> {
180179
}
181180
}
182181

183-
struct EvalCtxt<'a, 'tcx> {
184-
infcx: &'a InferCtxt<'tcx>,
185-
var_values: CanonicalVarValues<'tcx>,
186-
187-
search_graph: &'a mut search_graph::SearchGraph<'tcx>,
188-
189-
/// This field is used by a debug assertion in [`EvalCtxt::evaluate_goal`],
190-
/// see the comment in that method for more details.
191-
in_projection_eq_hack: bool,
192-
}
193-
194182
impl<'a, 'tcx> EvalCtxt<'a, 'tcx> {
195-
fn tcx(&self) -> TyCtxt<'tcx> {
196-
self.infcx.tcx
197-
}
198-
199183
/// The entry point of the solver.
200184
///
201185
/// This function deals with (coinductive) cycles, overflow, and caching
@@ -427,7 +411,7 @@ impl<'a, 'tcx> EvalCtxt<'a, 'tcx> {
427411

428412
let evaluate_normalizes_to = |ecx: &mut EvalCtxt<'_, 'tcx>, alias, other| {
429413
debug!("evaluate_normalizes_to(alias={:?}, other={:?})", alias, other);
430-
let r = ecx.infcx.probe(|_| {
414+
let r = ecx.probe(|ecx| {
431415
let (_, certainty) = ecx.evaluate_goal(goal.with(
432416
tcx,
433417
ty::Binder::dummy(ty::ProjectionPredicate {
@@ -462,10 +446,10 @@ impl<'a, 'tcx> EvalCtxt<'a, 'tcx> {
462446
// Evaluate all 3 potential candidates for the alias' being equal
463447
candidates.push(evaluate_normalizes_to(self, alias_lhs, goal.predicate.1));
464448
candidates.push(evaluate_normalizes_to(self, alias_rhs, goal.predicate.0));
465-
candidates.push(self.infcx.probe(|_| {
449+
candidates.push(self.probe(|this| {
466450
debug!("compute_alias_eq_goal: alias defids are equal, equating substs");
467-
let nested_goals = self.infcx.eq(goal.param_env, alias_lhs, alias_rhs)?;
468-
self.evaluate_all_and_make_canonical_response(nested_goals)
451+
let nested_goals = this.eq(goal.param_env, alias_lhs, alias_rhs)?;
452+
this.evaluate_all_and_make_canonical_response(nested_goals)
469453
}));
470454

471455
debug!(?candidates);
@@ -481,7 +465,7 @@ impl<'a, 'tcx> EvalCtxt<'a, 'tcx> {
481465
goal: Goal<'tcx, (ty::Const<'tcx>, Ty<'tcx>)>,
482466
) -> QueryResult<'tcx> {
483467
let (ct, ty) = goal.predicate;
484-
let nested_goals = self.infcx.eq(goal.param_env, ct.ty(), ty)?;
468+
let nested_goals = self.eq(goal.param_env, ct.ty(), ty)?;
485469
self.evaluate_all_and_make_canonical_response(nested_goals)
486470
}
487471
}

0 commit comments

Comments
 (0)