Skip to content

Commit 7c2244a

Browse files
authored
Rollup merge of rust-lang#118259 - spastorino:move-eager-resolver-to-infer, r=compiler-errors
Move EagerResolution to rustc_infer::infer::resolve `EagerResolver` fits better in `rustc_infer::infer::resolver`. Started to disentagle rust-lang#118118 that has a lot of unrelated things. r? `@compiler-errors` `@lcnr`
2 parents 969a773 + 798d2cb commit 7c2244a

File tree

2 files changed

+87
-80
lines changed

2 files changed

+87
-80
lines changed

compiler/rustc_infer/src/infer/resolve.rs

+82
Original file line numberDiff line numberDiff line change
@@ -260,3 +260,85 @@ impl<'a, 'tcx> FallibleTypeFolder<TyCtxt<'tcx>> for FullTypeResolver<'a, 'tcx> {
260260
}
261261
}
262262
}
263+
264+
///////////////////////////////////////////////////////////////////////////
265+
// EAGER RESOLUTION
266+
267+
/// Resolves ty, region, and const vars to their inferred values or their root vars.
268+
pub struct EagerResolver<'a, 'tcx> {
269+
infcx: &'a InferCtxt<'tcx>,
270+
}
271+
272+
impl<'a, 'tcx> EagerResolver<'a, 'tcx> {
273+
pub fn new(infcx: &'a InferCtxt<'tcx>) -> Self {
274+
EagerResolver { infcx }
275+
}
276+
}
277+
278+
impl<'tcx> TypeFolder<TyCtxt<'tcx>> for EagerResolver<'_, 'tcx> {
279+
fn interner(&self) -> TyCtxt<'tcx> {
280+
self.infcx.tcx
281+
}
282+
283+
fn fold_ty(&mut self, t: Ty<'tcx>) -> Ty<'tcx> {
284+
match *t.kind() {
285+
ty::Infer(ty::TyVar(vid)) => match self.infcx.probe_ty_var(vid) {
286+
Ok(t) => t.fold_with(self),
287+
Err(_) => Ty::new_var(self.infcx.tcx, self.infcx.root_var(vid)),
288+
},
289+
ty::Infer(ty::IntVar(vid)) => self.infcx.opportunistic_resolve_int_var(vid),
290+
ty::Infer(ty::FloatVar(vid)) => self.infcx.opportunistic_resolve_float_var(vid),
291+
_ => {
292+
if t.has_infer() {
293+
t.super_fold_with(self)
294+
} else {
295+
t
296+
}
297+
}
298+
}
299+
}
300+
301+
fn fold_region(&mut self, r: ty::Region<'tcx>) -> ty::Region<'tcx> {
302+
match *r {
303+
ty::ReVar(vid) => self
304+
.infcx
305+
.inner
306+
.borrow_mut()
307+
.unwrap_region_constraints()
308+
.opportunistic_resolve_var(self.infcx.tcx, vid),
309+
_ => r,
310+
}
311+
}
312+
313+
fn fold_const(&mut self, c: ty::Const<'tcx>) -> ty::Const<'tcx> {
314+
match c.kind() {
315+
ty::ConstKind::Infer(ty::InferConst::Var(vid)) => {
316+
// FIXME: we need to fold the ty too, I think.
317+
match self.infcx.probe_const_var(vid) {
318+
Ok(c) => c.fold_with(self),
319+
Err(_) => {
320+
ty::Const::new_var(self.infcx.tcx, self.infcx.root_const_var(vid), c.ty())
321+
}
322+
}
323+
}
324+
ty::ConstKind::Infer(ty::InferConst::EffectVar(vid)) => {
325+
debug_assert_eq!(c.ty(), self.infcx.tcx.types.bool);
326+
match self.infcx.probe_effect_var(vid) {
327+
Some(c) => c.as_const(self.infcx.tcx),
328+
None => ty::Const::new_infer(
329+
self.infcx.tcx,
330+
ty::InferConst::EffectVar(self.infcx.root_effect_var(vid)),
331+
self.infcx.tcx.types.bool,
332+
),
333+
}
334+
}
335+
_ => {
336+
if c.has_infer() {
337+
c.super_fold_with(self)
338+
} else {
339+
c
340+
}
341+
}
342+
}
343+
}
344+
}

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

+5-80
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,15 @@ use rustc_index::IndexVec;
1818
use rustc_infer::infer::canonical::query_response::make_query_region_constraints;
1919
use rustc_infer::infer::canonical::CanonicalVarValues;
2020
use rustc_infer::infer::canonical::{CanonicalExt, QueryRegionConstraints};
21+
use rustc_infer::infer::resolve::EagerResolver;
2122
use rustc_infer::infer::{DefineOpaqueTypes, InferCtxt, InferOk};
2223
use rustc_middle::infer::canonical::Canonical;
2324
use rustc_middle::traits::query::NoSolution;
2425
use rustc_middle::traits::solve::{
2526
ExternalConstraintsData, MaybeCause, PredefinedOpaquesData, QueryInput,
2627
};
2728
use rustc_middle::traits::ObligationCause;
28-
use rustc_middle::ty::{
29-
self, BoundVar, GenericArgKind, Ty, TyCtxt, TypeFoldable, TypeFolder, TypeSuperFoldable,
30-
TypeVisitableExt,
31-
};
29+
use rustc_middle::ty::{self, BoundVar, GenericArgKind, Ty, TyCtxt, TypeFoldable};
3230
use rustc_span::DUMMY_SP;
3331
use std::iter;
3432
use std::ops::Deref;
@@ -58,7 +56,7 @@ impl<'tcx> EvalCtxt<'_, 'tcx> {
5856
) -> (Vec<ty::GenericArg<'tcx>>, CanonicalInput<'tcx, T>) {
5957
let opaque_types = self.infcx.clone_opaque_types_for_query_response();
6058
let (goal, opaque_types) =
61-
(goal, opaque_types).fold_with(&mut EagerResolver { infcx: self.infcx });
59+
(goal, opaque_types).fold_with(&mut EagerResolver::new(self.infcx));
6260

6361
let mut orig_values = Default::default();
6462
let canonical_goal = Canonicalizer::canonicalize(
@@ -115,7 +113,7 @@ impl<'tcx> EvalCtxt<'_, 'tcx> {
115113
let external_constraints = self.compute_external_query_constraints()?;
116114

117115
let (var_values, mut external_constraints) =
118-
(var_values, external_constraints).fold_with(&mut EagerResolver { infcx: self.infcx });
116+
(var_values, external_constraints).fold_with(&mut EagerResolver::new(self.infcx));
119117
// Remove any trivial region constraints once we've resolved regions
120118
external_constraints
121119
.region_constraints
@@ -364,86 +362,13 @@ impl<'tcx> EvalCtxt<'_, 'tcx> {
364362
}
365363
}
366364

367-
/// Resolves ty, region, and const vars to their inferred values or their root vars.
368-
struct EagerResolver<'a, 'tcx> {
369-
infcx: &'a InferCtxt<'tcx>,
370-
}
371-
372-
impl<'tcx> TypeFolder<TyCtxt<'tcx>> for EagerResolver<'_, 'tcx> {
373-
fn interner(&self) -> TyCtxt<'tcx> {
374-
self.infcx.tcx
375-
}
376-
377-
fn fold_ty(&mut self, t: Ty<'tcx>) -> Ty<'tcx> {
378-
match *t.kind() {
379-
ty::Infer(ty::TyVar(vid)) => match self.infcx.probe_ty_var(vid) {
380-
Ok(t) => t.fold_with(self),
381-
Err(_) => Ty::new_var(self.infcx.tcx, self.infcx.root_var(vid)),
382-
},
383-
ty::Infer(ty::IntVar(vid)) => self.infcx.opportunistic_resolve_int_var(vid),
384-
ty::Infer(ty::FloatVar(vid)) => self.infcx.opportunistic_resolve_float_var(vid),
385-
_ => {
386-
if t.has_infer() {
387-
t.super_fold_with(self)
388-
} else {
389-
t
390-
}
391-
}
392-
}
393-
}
394-
395-
fn fold_region(&mut self, r: ty::Region<'tcx>) -> ty::Region<'tcx> {
396-
match *r {
397-
ty::ReVar(vid) => self
398-
.infcx
399-
.inner
400-
.borrow_mut()
401-
.unwrap_region_constraints()
402-
.opportunistic_resolve_var(self.infcx.tcx, vid),
403-
_ => r,
404-
}
405-
}
406-
407-
fn fold_const(&mut self, c: ty::Const<'tcx>) -> ty::Const<'tcx> {
408-
match c.kind() {
409-
ty::ConstKind::Infer(ty::InferConst::Var(vid)) => {
410-
// FIXME: we need to fold the ty too, I think.
411-
match self.infcx.probe_const_var(vid) {
412-
Ok(c) => c.fold_with(self),
413-
Err(_) => {
414-
ty::Const::new_var(self.infcx.tcx, self.infcx.root_const_var(vid), c.ty())
415-
}
416-
}
417-
}
418-
ty::ConstKind::Infer(ty::InferConst::EffectVar(vid)) => {
419-
debug_assert_eq!(c.ty(), self.infcx.tcx.types.bool);
420-
match self.infcx.probe_effect_var(vid) {
421-
Some(c) => c.as_const(self.infcx.tcx),
422-
None => ty::Const::new_infer(
423-
self.infcx.tcx,
424-
ty::InferConst::EffectVar(self.infcx.root_effect_var(vid)),
425-
self.infcx.tcx.types.bool,
426-
),
427-
}
428-
}
429-
_ => {
430-
if c.has_infer() {
431-
c.super_fold_with(self)
432-
} else {
433-
c
434-
}
435-
}
436-
}
437-
}
438-
}
439-
440365
impl<'tcx> inspect::ProofTreeBuilder<'tcx> {
441366
pub fn make_canonical_state<T: TypeFoldable<TyCtxt<'tcx>>>(
442367
ecx: &EvalCtxt<'_, 'tcx>,
443368
data: T,
444369
) -> inspect::CanonicalState<'tcx, T> {
445370
let state = inspect::State { var_values: ecx.var_values, data };
446-
let state = state.fold_with(&mut EagerResolver { infcx: ecx.infcx });
371+
let state = state.fold_with(&mut EagerResolver::new(ecx.infcx));
447372
Canonicalizer::canonicalize(
448373
ecx.infcx,
449374
CanonicalizeMode::Response { max_input_universe: ecx.max_input_universe },

0 commit comments

Comments
 (0)