rust/compiler/rustc_next_trait_solver/src/resolve.rs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

90 lines
2.8 KiB
Rust
Raw Normal View History

2024-09-30 10:18:55 +02:00
use rustc_type_ir::data_structures::DelayedMap;
use rustc_type_ir::inherent::*;
use rustc_type_ir::visit::TypeVisitableExt;
2025-03-13 16:59:55 +00:00
use rustc_type_ir::{
self as ty, InferCtxtLike, Interner, TypeFoldable, TypeFolder, TypeSuperFoldable,
};
2024-06-18 19:13:54 -04:00
use crate::delegate::SolverDelegate;
///////////////////////////////////////////////////////////////////////////
// EAGER RESOLUTION
/// Resolves ty, region, and const vars to their inferred values or their root vars.
2024-06-18 19:13:54 -04:00
pub struct EagerResolver<'a, D, I = <D as SolverDelegate>::Interner>
2024-06-04 16:02:36 -04:00
where
2024-06-18 19:13:54 -04:00
D: SolverDelegate<Interner = I>,
2024-06-04 16:02:36 -04:00
I: Interner,
{
2024-06-18 19:13:54 -04:00
delegate: &'a D,
2024-10-02 14:39:43 +02:00
/// We're able to use a cache here as the folder does not have any
/// mutable state.
2024-09-30 10:18:55 +02:00
cache: DelayedMap<I::Ty, I::Ty>,
}
2024-06-18 19:13:54 -04:00
impl<'a, D: SolverDelegate> EagerResolver<'a, D> {
pub fn new(delegate: &'a D) -> Self {
2024-09-30 10:18:55 +02:00
EagerResolver { delegate, cache: Default::default() }
}
}
2024-06-18 19:13:54 -04:00
impl<D: SolverDelegate<Interner = I>, I: Interner> TypeFolder<I> for EagerResolver<'_, D> {
fn cx(&self) -> I {
self.delegate.cx()
}
fn fold_ty(&mut self, t: I::Ty) -> I::Ty {
match t.kind() {
ty::Infer(ty::TyVar(vid)) => {
2024-06-18 19:13:54 -04:00
let resolved = self.delegate.opportunistic_resolve_ty_var(vid);
if t != resolved && resolved.has_infer() {
resolved.fold_with(self)
} else {
resolved
}
}
2024-06-18 19:13:54 -04:00
ty::Infer(ty::IntVar(vid)) => self.delegate.opportunistic_resolve_int_var(vid),
ty::Infer(ty::FloatVar(vid)) => self.delegate.opportunistic_resolve_float_var(vid),
_ => {
if t.has_infer() {
2024-09-30 10:18:55 +02:00
if let Some(&ty) = self.cache.get(&t) {
return ty;
}
let res = t.super_fold_with(self);
assert!(self.cache.insert(t, res));
res
} else {
t
}
}
}
}
fn fold_region(&mut self, r: I::Region) -> I::Region {
match r.kind() {
2024-06-18 19:13:54 -04:00
ty::ReVar(vid) => self.delegate.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)) => {
2024-06-18 19:13:54 -04:00
let resolved = self.delegate.opportunistic_resolve_ct_var(vid);
if c != resolved && resolved.has_infer() {
resolved.fold_with(self)
} else {
resolved
}
}
_ => {
if c.has_infer() {
c.super_fold_with(self)
} else {
c
}
}
}
}
}