Skip to content

Commit

Permalink
review
Browse files Browse the repository at this point in the history
  • Loading branch information
lcnr committed Oct 2, 2024
1 parent a7b1144 commit 4ce959e
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 22 deletions.
6 changes: 4 additions & 2 deletions compiler/rustc_infer/src/infer/relate/type_relating.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@ use crate::infer::{DefineOpaqueTypes, InferCtxt, SubregionOrigin};

/// Enforce that `a` is equal to or a subtype of `b`.
pub struct TypeRelating<'combine, 'a, 'tcx> {
// Partially mutable.
// Immutable except for the `InferCtxt` and the
// resulting nested `goals`.
fields: &'combine mut CombineFields<'a, 'tcx>,

// Immutable fields.
// Immutable field.
structurally_relate_aliases: StructurallyRelateAliases,
// Mutable field.
ambient_variance: ty::Variance,

/// The cache has only tracks the `ambient_variance` as its the
Expand Down
17 changes: 9 additions & 8 deletions compiler/rustc_middle/src/ty/fold.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,16 +210,17 @@ where
debug_assert!(!ty.has_vars_bound_above(ty::INNERMOST));
ty::fold::shift_vars(self.tcx, ty, self.current_index.as_u32())
}
_ if t.has_vars_bound_at_or_above(self.current_index) => {
if let Some(&ty) = self.cache.get(&(self.current_index, t)) {
return ty;
_ => {
if !t.has_vars_bound_at_or_above(self.current_index) {
t
} else if let Some(&t) = self.cache.get(&(self.current_index, t)) {
t
} else {
let res = t.super_fold_with(self);
assert!(self.cache.insert((self.current_index, t), res));
res
}

let res = t.super_fold_with(self);
assert!(self.cache.insert((self.current_index, t), res));
res
}
_ => t,
}
}

Expand Down
12 changes: 7 additions & 5 deletions compiler/rustc_next_trait_solver/src/canonicalizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,19 @@ pub enum CanonicalizeMode {

pub struct Canonicalizer<'a, D: SolverDelegate<Interner = I>, I: Interner> {
delegate: &'a D,

// Immutable field.
canonicalize_mode: CanonicalizeMode,

// Mutable fields.
variables: &'a mut Vec<I::GenericArg>,
variable_lookup_table: HashMap<I::GenericArg, usize>,

primitive_var_infos: Vec<CanonicalVarInfo<I>>,
variable_lookup_table: HashMap<I::GenericArg, usize>,
binder_index: ty::DebruijnIndex,

/// We only use the debruijn index during lookup as all other fields
/// should not be impacted by whether a type is folded once or multiple
/// times.
/// We only use the debruijn index during lookup. We don't need to
/// track the `variables` as each generic arg only results in a single
/// bound variable regardless of how many times it is encountered.
cache: HashMap<(ty::DebruijnIndex, I::Ty), I::Ty>,
}

Expand Down
2 changes: 2 additions & 0 deletions compiler/rustc_next_trait_solver/src/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ where
I: Interner,
{
delegate: &'a D,
/// We're able to use a cache here as the folder does not have any
/// mutable state.
cache: DelayedMap<I::Ty, I::Ty>,
}

Expand Down
15 changes: 8 additions & 7 deletions compiler/rustc_next_trait_solver/src/solve/eval_ctxt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1057,16 +1057,17 @@ where
);
infer_ty
}
_ if ty.has_aliases() => {
if let Some(&entry) = self.cache.get(&ty) {
_ => {
if !ty.has_aliases() {
ty
} else if let Some(&entry) = self.cache.get(&ty) {
return entry;
} else {
let res = ty.super_fold_with(self);
assert!(self.cache.insert(ty, res).is_none());
res
}

let res = ty.super_fold_with(self);
assert!(self.cache.insert(ty, res).is_none());
res
}
_ => ty,
}
}

Expand Down

0 comments on commit 4ce959e

Please sign in to comment.