Skip to content

Commit

Permalink
Merge pull request #343 from yaahc/jane/intern-extend
Browse files Browse the repository at this point in the history
Add &self to intern lifetime
  • Loading branch information
nikomatsakis authored Mar 3, 2020
2 parents 177d713 + 0b1261c commit d6fd9ef
Show file tree
Hide file tree
Showing 17 changed files with 57 additions and 48 deletions.
4 changes: 3 additions & 1 deletion chalk-integration/src/lowering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1079,7 +1079,9 @@ impl LowerLifetime for Lifetime {
fn lower(&self, env: &Env) -> LowerResult<chalk_ir::Lifetime<ChalkIr>> {
match *self {
Lifetime::Id { name } => match env.lookup_lifetime(name)? {
LifetimeLookup::Parameter(d) => Ok(chalk_ir::LifetimeData::BoundVar(d).intern()),
LifetimeLookup::Parameter(d) => {
Ok(chalk_ir::LifetimeData::BoundVar(d).intern(env.interner()))
}
},
}
}
Expand Down
10 changes: 5 additions & 5 deletions chalk-ir/src/fold.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ pub trait Folder<I: Interner, TI: TargetInterner<I> = I> {
if self.forbid_free_vars() {
panic!("unexpected free variable with depth `{:?}`", depth)
} else {
Ok(LifetimeData::<TI>::BoundVar(depth + binders).intern())
Ok(LifetimeData::<TI>::BoundVar(depth + binders).intern(self.target_interner()))
}
}

Expand Down Expand Up @@ -169,7 +169,7 @@ pub trait Folder<I: Interner, TI: TargetInterner<I> = I> {
if self.forbid_free_placeholders() {
panic!("unexpected placeholder lifetime `{:?}`", universe)
} else {
Ok(universe.to_lifetime::<TI>())
Ok(universe.to_lifetime(self.target_interner()))
}
}

Expand All @@ -191,7 +191,7 @@ pub trait Folder<I: Interner, TI: TargetInterner<I> = I> {
if self.forbid_inference_vars() {
panic!("unexpected inference type `{:?}`", var)
} else {
Ok(var.to_ty::<TI>(self.target_interner()))
Ok(var.to_ty(self.target_interner()))
}
}

Expand All @@ -204,7 +204,7 @@ pub trait Folder<I: Interner, TI: TargetInterner<I> = I> {
if self.forbid_inference_vars() {
panic!("unexpected inference lifetime `'{:?}`", var)
} else {
Ok(var.to_lifetime::<TI>())
Ok(var.to_lifetime(self.target_interner()))
}
}

Expand Down Expand Up @@ -328,7 +328,7 @@ where
if *depth >= binders {
folder.fold_free_var_lifetime(depth - binders, binders)
} else {
Ok(LifetimeData::<TI>::BoundVar(*depth).intern())
Ok(LifetimeData::<TI>::BoundVar(*depth).intern(folder.target_interner()))
}
}
LifetimeData::InferenceVar(var) => folder.fold_inference_lifetime(*var, binders),
Expand Down
4 changes: 2 additions & 2 deletions chalk-ir/src/fold/shift.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ impl<I: Interner> Folder<I> for Shifter<'_, I> {
}

fn fold_free_var_lifetime(&mut self, depth: usize, binders: usize) -> Fallible<Lifetime<I>> {
Ok(LifetimeData::<I>::BoundVar(self.adjust(depth, binders)).intern())
Ok(LifetimeData::<I>::BoundVar(self.adjust(depth, binders)).intern(self.interner()))
}

fn interner(&self) -> &I {
Expand Down Expand Up @@ -157,7 +157,7 @@ impl<I: Interner> Folder<I> for DownShifter<'_, I> {
}

fn fold_free_var_lifetime(&mut self, depth: usize, binders: usize) -> Fallible<Lifetime<I>> {
Ok(LifetimeData::<I>::BoundVar(self.adjust(depth, binders)?).intern())
Ok(LifetimeData::<I>::BoundVar(self.adjust(depth, binders)?).intern(self.interner()))
}

fn interner(&self) -> &I {
Expand Down
5 changes: 4 additions & 1 deletion chalk-ir/src/fold/subst.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@ impl<I: Interner> Folder<I> for Subst<'_, '_, I> {

fn fold_free_var_lifetime(&mut self, depth: usize, binders: usize) -> Fallible<Lifetime<I>> {
if depth >= self.parameters.len() {
Ok(LifetimeData::<I>::BoundVar(depth - self.parameters.len() + binders).intern())
Ok(
LifetimeData::<I>::BoundVar(depth - self.parameters.len() + binders)
.intern(self.interner()),
)
} else {
match self.parameters[depth].data() {
ParameterKind::Lifetime(l) => Ok(l.shifted_in(self.interner(), binders)),
Expand Down
4 changes: 2 additions & 2 deletions chalk-ir/src/interner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ pub trait Interner: Debug + Copy + Eq + Ord + Hash {
/// normally invoked directly; instead, you invoke
/// `LifetimeData::intern` (which will ultimately call this
/// method).
fn intern_lifetime(lifetime: LifetimeData<Self>) -> Self::InternedLifetime;
fn intern_lifetime(&self, lifetime: LifetimeData<Self>) -> Self::InternedLifetime;

/// Lookup the `LifetimeData` that was interned to create a `InternedLifetime`.
fn lifetime_data(lifetime: &Self::InternedLifetime) -> &LifetimeData<Self>;
Expand Down Expand Up @@ -279,7 +279,7 @@ mod default {
ty
}

fn intern_lifetime(lifetime: LifetimeData<ChalkIr>) -> LifetimeData<ChalkIr> {
fn intern_lifetime(&self, lifetime: LifetimeData<ChalkIr>) -> LifetimeData<ChalkIr> {
lifetime
}

Expand Down
16 changes: 8 additions & 8 deletions chalk-ir/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -313,8 +313,8 @@ impl InferenceVar {
TyData::<I>::InferenceVar(self).intern(interner)
}

pub fn to_lifetime<I: Interner>(self) -> Lifetime<I> {
LifetimeData::<I>::InferenceVar(self).intern()
pub fn to_lifetime<I: Interner>(self, interner: &I) -> Lifetime<I> {
LifetimeData::<I>::InferenceVar(self).intern(interner)
}
}

Expand All @@ -332,9 +332,9 @@ pub struct Lifetime<I: Interner> {
}

impl<I: Interner> Lifetime<I> {
pub fn new(data: impl CastTo<LifetimeData<I>>) -> Self {
pub fn new(interner: &I, data: impl CastTo<LifetimeData<I>>) -> Self {
Lifetime {
interned: I::intern_lifetime(data.cast()),
interned: I::intern_lifetime(interner, data.cast()),
}
}

Expand Down Expand Up @@ -373,8 +373,8 @@ pub enum LifetimeData<I: Interner> {
}

impl<I: Interner> LifetimeData<I> {
pub fn intern(self) -> Lifetime<I> {
Lifetime::new(self)
pub fn intern(self, interner: &I) -> Lifetime<I> {
Lifetime::new(interner, self)
}
}

Expand All @@ -390,8 +390,8 @@ pub struct PlaceholderIndex {
}

impl PlaceholderIndex {
pub fn to_lifetime<I: Interner>(self) -> Lifetime<I> {
LifetimeData::<I>::Placeholder(self).intern()
pub fn to_lifetime<I: Interner>(self, interner: &I) -> Lifetime<I> {
LifetimeData::<I>::Placeholder(self).intern(interner)
}

pub fn to_ty<I: Interner>(self, interner: &I) -> Ty<I> {
Expand Down
6 changes: 3 additions & 3 deletions chalk-ir/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,15 @@ macro_rules! arg {
#[macro_export]
macro_rules! lifetime {
(infer $b:expr) => {
$crate::LifetimeData::InferenceVar($crate::InferenceVar::from($b)).intern()
$crate::LifetimeData::InferenceVar($crate::InferenceVar::from($b)).intern(&chalk_ir::interner::ChalkIr)
};

(bound $b:expr) => {
$crate::LifetimeData::BoundVar($b).intern()
$crate::LifetimeData::BoundVar($b).intern(&chalk_ir::interner::ChalkIr)
};

(placeholder $b:expr) => {
$crate::LifetimeData::Placeholder(PlaceholderIndex { ui: UniverseIndex { counter: $b }, idx: 0}).intern()
$crate::LifetimeData::Placeholder(PlaceholderIndex { ui: UniverseIndex { counter: $b }, idx: 0}).intern(&chalk_ir::interner::ChalkIr)
};

(expr $b:expr) => {
Expand Down
2 changes: 1 addition & 1 deletion chalk-rust-ir/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ impl<'a> ToParameter for (&'a ParameterKind<()>, usize) {
fn to_parameter<I: Interner>(&self, interner: &I) -> Parameter<I> {
let &(binder, index) = self;
match *binder {
ParameterKind::Lifetime(_) => LifetimeData::BoundVar(index).intern().cast(),
ParameterKind::Lifetime(_) => LifetimeData::BoundVar(index).intern(interner).cast(),
ParameterKind::Ty(_) => TyData::BoundVar(index).intern(interner).cast(),
}
}
Expand Down
2 changes: 1 addition & 1 deletion chalk-solve/src/infer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ impl<I: Interner> ParameterEnaVariableExt<I> for ParameterEnaVariable<I> {
fn to_parameter(self, interner: &I) -> Parameter<I> {
match self {
ParameterKind::Ty(v) => v.to_ty(interner).cast(),
ParameterKind::Lifetime(v) => v.to_lifetime().cast(),
ParameterKind::Lifetime(v) => v.to_lifetime(interner).cast(),
}
}
}
6 changes: 3 additions & 3 deletions chalk-solve/src/infer/canonicalize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ impl<I: Interner> Folder<I> for Canonicalizer<'_, I> {
_binders: usize,
) -> Fallible<Ty<I>> {
self.max_universe = max(self.max_universe, universe.ui);
Ok(universe.to_ty::<I>(self.interner()))
Ok(universe.to_ty(self.interner()))
}

fn fold_free_placeholder_lifetime(
Expand All @@ -116,7 +116,7 @@ impl<I: Interner> Folder<I> for Canonicalizer<'_, I> {
_binders: usize,
) -> Fallible<Lifetime<I>> {
self.max_universe = max(self.max_universe, universe.ui);
Ok(universe.to_lifetime::<I>())
Ok(universe.to_lifetime(self.interner()))
}

fn forbid_free_vars(&self) -> bool {
Expand Down Expand Up @@ -164,7 +164,7 @@ impl<I: Interner> Folder<I> for Canonicalizer<'_, I> {
let free_var = ParameterKind::Lifetime(self.table.unify.find(var));
let position = self.add(free_var);
debug!("not yet unified: position={:?}", position);
Ok(LifetimeData::BoundVar(position + binders).intern())
Ok(LifetimeData::BoundVar(position + binders).intern(self.interner()))
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions chalk-solve/src/infer/instantiate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,10 @@ impl<I: Interner> InferenceTable<I> {
let placeholder_idx = PlaceholderIndex { ui, idx };
match pk {
ParameterKind::Lifetime(()) => {
let lt = placeholder_idx.to_lifetime::<I>();
let lt = placeholder_idx.to_lifetime(interner);
lt.cast()
}
ParameterKind::Ty(()) => placeholder_idx.to_ty::<I>(interner).cast(),
ParameterKind::Ty(()) => placeholder_idx.to_ty(interner).cast(),
}
})
.collect();
Expand Down
2 changes: 1 addition & 1 deletion chalk-solve/src/infer/invert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ impl<I: Interner> Folder<I> for Inverter<'_, I> {
.inverted_lifetime
.entry(universe)
.or_insert_with(|| table.new_variable(universe.ui))
.to_lifetime()
.to_lifetime(self.interner())
.shifted_in(self.interner(), binders))
}

Expand Down
2 changes: 1 addition & 1 deletion chalk-solve/src/infer/normalize_deep.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ impl<I: Interner> Folder<I> for DeepNormalizer<'_, '_, I> {
let var = EnaVariable::from(var);
match self.table.probe_lifetime_var(var) {
Some(l) => Ok(l.fold_with(self, 0)?.shifted_in(self.interner(), binders)),
None => Ok(var.to_lifetime()), // FIXME shift
None => Ok(var.to_lifetime(self.interner())), // FIXME shift
}
}

Expand Down
10 changes: 5 additions & 5 deletions chalk-solve/src/infer/ucanonicalize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ impl<I: Interner> Folder<I> for UCollector<'_, '_, I> {
_binders: usize,
) -> Fallible<Lifetime<I>> {
self.universes.add(universe.ui);
Ok(universe.to_lifetime::<I>())
Ok(universe.to_lifetime(self.interner()))
}

fn forbid_inference_vars(&self) -> bool {
Expand Down Expand Up @@ -299,7 +299,7 @@ impl<I: Interner> Folder<I> for UMapToCanonical<'_, I> {
ui,
idx: universe0.idx,
}
.to_ty::<I>(self.interner()))
.to_ty(self.interner()))
}

fn fold_free_placeholder_lifetime(
Expand All @@ -312,7 +312,7 @@ impl<I: Interner> Folder<I> for UMapToCanonical<'_, I> {
ui: universe,
idx: universe0.idx,
}
.to_lifetime::<I>())
.to_lifetime(self.interner()))
}

fn interner(&self) -> &I {
Expand Down Expand Up @@ -344,7 +344,7 @@ impl<I: Interner> Folder<I> for UMapFromCanonical<'_, I> {
ui,
idx: universe0.idx,
}
.to_ty::<I>(self.interner()))
.to_ty(self.interner()))
}

fn fold_free_placeholder_lifetime(
Expand All @@ -357,7 +357,7 @@ impl<I: Interner> Folder<I> for UMapFromCanonical<'_, I> {
ui: universe,
idx: universe0.idx,
}
.to_lifetime::<I>())
.to_lifetime(self.interner()))
}

fn forbid_inference_vars(&self) -> bool {
Expand Down
16 changes: 9 additions & 7 deletions chalk-solve/src/infer/unify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ impl<'t, I: Interner> Unifier<'t, I> {
"unify_lifetime_lifetime: {:?} in {:?} can see {:?}; unifying",
var, var_ui, idx.ui
);
let v = LifetimeData::Placeholder(idx).intern();
let v = LifetimeData::Placeholder(idx).intern(self.interner);
self.table
.unify
.unify_var_value(var, InferenceValue::from(v))
Expand Down Expand Up @@ -398,7 +398,7 @@ impl<I: Interner> Folder<I> for OccursCheck<'_, '_, I> {
if self.universe_index < universe.ui {
Err(NoSolution)
} else {
Ok(universe.to_ty::<I>(self.interner())) // no need to shift, not relative to depth
Ok(universe.to_ty(self.interner())) // no need to shift, not relative to depth
}
}

Expand All @@ -422,13 +422,15 @@ impl<I: Interner> Folder<I> for OccursCheck<'_, '_, I> {
// exists<'x> forall<'b> ?T = Foo<'x>, where 'x = 'b

let tick_x = self.unifier.table.new_variable(self.universe_index);
self.unifier
.push_lifetime_eq_constraint(tick_x.to_lifetime(), ui.to_lifetime::<I>());
Ok(tick_x.to_lifetime())
self.unifier.push_lifetime_eq_constraint(
tick_x.to_lifetime(self.interner()),
ui.to_lifetime(self.interner()),
);
Ok(tick_x.to_lifetime(self.interner()))
} else {
// If the `ui` is higher than `self.universe_index`, then we can name
// this lifetime, no problem.
Ok(ui.to_lifetime::<I>()) // no need to shift, not relative to depth
Ok(ui.to_lifetime(self.interner())) // no need to shift, not relative to depth
}
}

Expand Down Expand Up @@ -493,7 +495,7 @@ impl<I: Interner> Folder<I> for OccursCheck<'_, '_, I> {
.unify_var_value(var, InferenceValue::Unbound(self.universe_index))
.unwrap();
}
Ok(var.to_lifetime())
Ok(var.to_lifetime(self.interner()))
}

InferenceValue::Bound(l) => {
Expand Down
6 changes: 3 additions & 3 deletions chalk-solve/src/infer/var.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,14 @@ impl<I: Interner> EnaVariable<I> {
/// method, naturally you should know from context that the kind
/// of this inference variable is a type (we can't check it).
pub(crate) fn to_ty(self, interner: &I) -> Ty<I> {
self.var.to_ty::<I>(interner)
self.var.to_ty(interner)
}

/// Convert this inference variable into a lifetime. When using this
/// method, naturally you should know from context that the kind
/// of this inference variable is a lifetime (we can't check it).
pub(crate) fn to_lifetime(self) -> Lifetime<I> {
self.var.to_lifetime::<I>()
pub(crate) fn to_lifetime(self, interner: &I) -> Lifetime<I> {
self.var.to_lifetime(interner)
}
}

Expand Down
6 changes: 4 additions & 2 deletions chalk-solve/src/solve/slg/aggregate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ fn merge_into_guidance<I: Interner>(
ParameterKind::Lifetime(_) => {
// Ignore the lifetimes from the substitution: we're just
// creating guidance here anyway.
return infer.new_variable(universe).to_lifetime().cast();
return infer.new_variable(universe).to_lifetime(interner).cast();
}
};

Expand Down Expand Up @@ -373,7 +373,9 @@ impl<I: Interner> AntiUnifier<'_, '_, I> {
}

fn new_lifetime_variable(&mut self) -> Lifetime<I> {
self.infer.new_variable(self.universe).to_lifetime()
self.infer
.new_variable(self.universe)
.to_lifetime(self.interner)
}
}

Expand Down

0 comments on commit d6fd9ef

Please sign in to comment.