1
Fork 0

Rework var resolution in InferCtxtLike, uplift EagerResolver

This commit is contained in:
Michael Goulet 2024-05-19 13:04:44 -04:00
parent 569fb43aa0
commit b0f1afd1fc
12 changed files with 204 additions and 165 deletions

View file

@ -268,7 +268,7 @@ impl<Infcx: InferCtxtLike<Interner = I>, I: Interner> TypeFolder<I>
ty::ReVar(vid) => {
assert_eq!(
self.infcx.opportunistic_resolve_lt_var(vid),
None,
r,
"region vid should have been resolved fully before canonicalization"
);
match self.canonicalize_mode {
@ -302,13 +302,8 @@ impl<Infcx: InferCtxtLike<Interner = I>, I: Interner> TypeFolder<I>
ty::Infer(i) => match i {
ty::TyVar(vid) => {
assert_eq!(
self.infcx.root_ty_var(vid),
vid,
"ty vid should have been resolved fully before canonicalization"
);
assert_eq!(
self.infcx.probe_ty_var(vid),
None,
self.infcx.opportunistic_resolve_ty_var(vid),
t,
"ty vid should have been resolved fully before canonicalization"
);
@ -318,10 +313,24 @@ impl<Infcx: InferCtxtLike<Interner = I>, I: Interner> TypeFolder<I>
.unwrap_or_else(|| panic!("ty var should have been resolved: {t:?}")),
))
}
ty::IntVar(_) => CanonicalVarKind::Ty(CanonicalTyVarKind::Int),
ty::FloatVar(_) => CanonicalVarKind::Ty(CanonicalTyVarKind::Float),
ty::IntVar(vid) => {
assert_eq!(
self.infcx.opportunistic_resolve_int_var(vid),
t,
"ty vid should have been resolved fully before canonicalization"
);
CanonicalVarKind::Ty(CanonicalTyVarKind::Int)
}
ty::FloatVar(vid) => {
assert_eq!(
self.infcx.opportunistic_resolve_float_var(vid),
t,
"ty vid should have been resolved fully before canonicalization"
);
CanonicalVarKind::Ty(CanonicalTyVarKind::Float)
}
ty::FreshTy(_) | ty::FreshIntTy(_) | ty::FreshFloatTy(_) => {
todo!()
panic!("fresh vars not expected in canonicalization")
}
},
ty::Placeholder(placeholder) => match self.canonicalize_mode {
@ -387,14 +396,11 @@ impl<Infcx: InferCtxtLike<Interner = I>, I: Interner> TypeFolder<I>
let kind = match c.kind() {
ty::ConstKind::Infer(i) => match i {
ty::InferConst::Var(vid) => {
// We compare `kind`s here because we've folded the `ty` with `RegionsToStatic`
// so we'll get a mismatch in types if it actually changed any regions.
assert_eq!(
self.infcx.root_ct_var(vid),
vid,
"region vid should have been resolved fully before canonicalization"
);
assert_eq!(
self.infcx.probe_ct_var(vid),
None,
self.infcx.opportunistic_resolve_ct_var(vid, ty).kind(),
c.kind(),
"region vid should have been resolved fully before canonicalization"
);
CanonicalVarKind::Const(self.infcx.universe_of_ct(vid).unwrap(), ty)

View file

@ -1,2 +1,3 @@
pub mod canonicalizer;
pub mod resolve;
pub mod solve;

View file

@ -0,0 +1,83 @@
use rustc_type_ir::fold::{TypeFoldable, TypeFolder, TypeSuperFoldable};
use rustc_type_ir::inherent::*;
use rustc_type_ir::visit::TypeVisitableExt;
use rustc_type_ir::{self as ty, InferCtxtLike, Interner};
///////////////////////////////////////////////////////////////////////////
// EAGER RESOLUTION
/// Resolves ty, region, and const vars to their inferred values or their root vars.
pub struct EagerResolver<
'a,
Infcx: InferCtxtLike<Interner = I>,
I: Interner = <Infcx as InferCtxtLike>::Interner,
> {
infcx: &'a Infcx,
}
impl<'a, Infcx: InferCtxtLike> EagerResolver<'a, Infcx> {
pub fn new(infcx: &'a Infcx) -> Self {
EagerResolver { infcx }
}
}
impl<Infcx: InferCtxtLike<Interner = I>, I: Interner> TypeFolder<I> for EagerResolver<'_, Infcx> {
fn interner(&self) -> I {
self.infcx.interner()
}
fn fold_ty(&mut self, t: I::Ty) -> I::Ty {
match t.kind() {
ty::Infer(ty::TyVar(vid)) => {
let resolved = self.infcx.opportunistic_resolve_ty_var(vid);
if t != resolved && resolved.has_infer() {
resolved.fold_with(self)
} else {
resolved
}
}
ty::Infer(ty::IntVar(vid)) => self.infcx.opportunistic_resolve_int_var(vid),
ty::Infer(ty::FloatVar(vid)) => self.infcx.opportunistic_resolve_float_var(vid),
_ => {
if t.has_infer() {
t.super_fold_with(self)
} else {
t
}
}
}
}
fn fold_region(&mut self, r: I::Region) -> I::Region {
match r.kind() {
ty::ReVar(vid) => self.infcx.opportunistic_resolve_lt_var(vid),
_ => r,
}
}
fn fold_const(&mut self, c: I::Const) -> I::Const {
match c.kind() {
ty::ConstKind::Infer(ty::InferConst::Var(vid)) => {
let ty = c.ty().fold_with(self);
let resolved = self.infcx.opportunistic_resolve_ct_var(vid, ty);
if c != resolved && resolved.has_infer() {
resolved.fold_with(self)
} else {
resolved
}
}
ty::ConstKind::Infer(ty::InferConst::EffectVar(vid)) => {
let bool = Ty::new_bool(self.infcx.interner());
debug_assert_eq!(c.ty(), bool);
self.infcx.opportunistic_resolve_effect_var(vid, bool)
}
_ => {
if c.has_infer() {
c.super_fold_with(self)
} else {
c
}
}
}
}
}