about summary refs log tree commit diff
path: root/compiler/rustc_next_trait_solver/src/resolve.rs
diff options
context:
space:
mode:
authorMichael Goulet <michael@errs.io>2024-05-19 13:04:44 -0400
committerMichael Goulet <michael@errs.io>2024-05-20 13:57:58 -0400
commitb0f1afd1fc29646d9c2d383fb4d1e86477dde73e (patch)
treed438cfcab59f34fb1276adfacbd1dfa5ff80a048 /compiler/rustc_next_trait_solver/src/resolve.rs
parent569fb43aa09eb4008155dcf0a2f7082c3afddefa (diff)
downloadrust-b0f1afd1fc29646d9c2d383fb4d1e86477dde73e.tar.gz
rust-b0f1afd1fc29646d9c2d383fb4d1e86477dde73e.zip
Rework var resolution in InferCtxtLike, uplift EagerResolver
Diffstat (limited to 'compiler/rustc_next_trait_solver/src/resolve.rs')
-rw-r--r--compiler/rustc_next_trait_solver/src/resolve.rs83
1 files changed, 83 insertions, 0 deletions
diff --git a/compiler/rustc_next_trait_solver/src/resolve.rs b/compiler/rustc_next_trait_solver/src/resolve.rs
new file mode 100644
index 00000000000..1333b4aa7d8
--- /dev/null
+++ b/compiler/rustc_next_trait_solver/src/resolve.rs
@@ -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
+                }
+            }
+        }
+    }
+}