Skip to content

Commit 75718ac

Browse files
authored
Merge pull request #349 from yaahc/jane/intern-lifetime-data
Add self param to lifetime_data
2 parents 703ffca + 4291e16 commit 75718ac

File tree

10 files changed

+94
-47
lines changed

10 files changed

+94
-47
lines changed

chalk-integration/src/program.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ use chalk_ir::debug::Angle;
44
use chalk_ir::interner::ChalkIr;
55
use chalk_ir::tls;
66
use chalk_ir::{
7-
AliasTy, AssocTypeId, ImplId, Parameter, ProgramClause, StructId, TraitId, Ty, TyData, TypeName,
7+
AliasTy, AssocTypeId, ImplId, Lifetime, Parameter, ProgramClause, StructId, TraitId, Ty,
8+
TyData, TypeName,
89
};
910
use chalk_rust_ir::{
1011
AssociatedTyDatum, AssociatedTyValue, AssociatedTyValueId, ImplDatum, ImplType, StructDatum,
@@ -125,6 +126,15 @@ impl tls::DebugContext for Program {
125126
let interner = self.interner();
126127
write!(fmt, "{:?}", ty.data(interner))
127128
}
129+
130+
fn debug_lifetime(
131+
&self,
132+
lifetime: &Lifetime<ChalkIr>,
133+
fmt: &mut fmt::Formatter<'_>,
134+
) -> Result<(), fmt::Error> {
135+
let interner = self.interner();
136+
write!(fmt, "{:?}", lifetime.data(interner))
137+
}
128138
}
129139

130140
impl RustIrDatabase<ChalkIr> for Program {

chalk-ir/src/debug.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@ impl<I: Interner> Debug for Ty<I> {
2727
}
2828
}
2929

30+
impl<I: Interner> Debug for Lifetime<I> {
31+
fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
32+
I::debug_lifetime(self, fmt).unwrap_or_else(|| unimplemented!())
33+
}
34+
}
35+
3036
impl Display for UniverseIndex {
3137
fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
3238
write!(fmt, "U{}", self.counter)
@@ -87,12 +93,6 @@ impl<I: Interner> Debug for Fn<I> {
8793
}
8894
}
8995

90-
impl<I: Interner> Debug for Lifetime<I> {
91-
fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
92-
write!(fmt, "{:?}", self.data())
93-
}
94-
}
95-
9696
impl<I: Interner> Debug for LifetimeData<I> {
9797
fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
9898
match self {

chalk-ir/src/fold.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -365,12 +365,14 @@ where
365365
I: 'i,
366366
TI: 'i,
367367
{
368-
match self.data() {
368+
let interner = folder.interner();
369+
let target_interner = folder.target_interner();
370+
match self.data(interner) {
369371
LifetimeData::BoundVar(depth) => {
370372
if *depth >= binders {
371373
folder.fold_free_var_lifetime(depth - binders, binders)
372374
} else {
373-
Ok(LifetimeData::<TI>::BoundVar(*depth).intern(folder.target_interner()))
375+
Ok(LifetimeData::<TI>::BoundVar(*depth).intern(target_interner))
374376
}
375377
}
376378
LifetimeData::InferenceVar(var) => folder.fold_inference_lifetime(*var, binders),

chalk-ir/src/interner.rs

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use crate::AliasTy;
22
use crate::AssocTypeId;
33
use crate::Goal;
44
use crate::GoalData;
5+
use crate::Lifetime;
56
use crate::LifetimeData;
67
use crate::Parameter;
78
use crate::ParameterData;
@@ -142,6 +143,18 @@ pub trait Interner: Debug + Copy + Eq + Ord + Hash {
142143
/// if no info about current program is available from TLS).
143144
fn debug_ty(ty: &Ty<Self>, fmt: &mut fmt::Formatter<'_>) -> Option<fmt::Result>;
144145

146+
/// Prints the debug representation of an lifetime. To get good
147+
/// results, this requires inspecting TLS, and is difficult to
148+
/// code without reference to a specific interner (and hence
149+
/// fully known types).
150+
///
151+
/// Returns `None` to fallback to the default debug output (e.g.,
152+
/// if no info about current program is available from TLS).
153+
fn debug_lifetime(
154+
lifetime: &Lifetime<Self>,
155+
fmt: &mut fmt::Formatter<'_>,
156+
) -> Option<fmt::Result>;
157+
145158
/// Create an "interned" type from `ty`. This is not normally
146159
/// invoked directly; instead, you invoke `TyData::intern` (which
147160
/// will ultimately call this method).
@@ -157,7 +170,7 @@ pub trait Interner: Debug + Copy + Eq + Ord + Hash {
157170
fn intern_lifetime(&self, lifetime: LifetimeData<Self>) -> Self::InternedLifetime;
158171

159172
/// Lookup the `LifetimeData` that was interned to create a `InternedLifetime`.
160-
fn lifetime_data(lifetime: &Self::InternedLifetime) -> &LifetimeData<Self>;
173+
fn lifetime_data<'a>(&self, lifetime: &'a Self::InternedLifetime) -> &'a LifetimeData<Self>;
161174

162175
/// Create an "interned" parameter from `data`. This is not
163176
/// normally invoked directly; instead, you invoke
@@ -282,8 +295,16 @@ mod default {
282295
tls::with_current_program(|prog| Some(prog?.debug_alias(alias, fmt)))
283296
}
284297

285-
fn debug_ty(alias: &Ty<ChalkIr>, fmt: &mut fmt::Formatter<'_>) -> Option<fmt::Result> {
286-
tls::with_current_program(|prog| Some(prog?.debug_ty(alias, fmt)))
298+
fn debug_ty(ty: &Ty<ChalkIr>, fmt: &mut fmt::Formatter<'_>) -> Option<fmt::Result> {
299+
tls::with_current_program(|prog| Some(prog?.debug_ty(ty, fmt)))
300+
}
301+
302+
fn debug_lifetime(
303+
lifetime: &Lifetime<ChalkIr>,
304+
fmt: &mut fmt::Formatter<'_>,
305+
) -> Option<fmt::Result> {
306+
tls::with_current_program(|prog| Some(prog?.debug_lifetime(lifetime, fmt)))
307+
.or_else(|| Some(write!(fmt, "{:?}", lifetime.interned)))
287308
}
288309

289310
fn intern_ty(&self, ty: TyData<ChalkIr>) -> Arc<TyData<ChalkIr>> {
@@ -298,7 +319,10 @@ mod default {
298319
lifetime
299320
}
300321

301-
fn lifetime_data(lifetime: &LifetimeData<ChalkIr>) -> &LifetimeData<ChalkIr> {
322+
fn lifetime_data<'a>(
323+
&self,
324+
lifetime: &'a LifetimeData<ChalkIr>,
325+
) -> &'a LifetimeData<ChalkIr> {
302326
lifetime
303327
}
304328

chalk-ir/src/lib.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -342,13 +342,13 @@ impl<I: Interner> Lifetime<I> {
342342
}
343343
}
344344

345-
pub fn data(&self) -> &LifetimeData<I> {
346-
I::lifetime_data(&self.interned)
345+
pub fn data(&self, interner: &I) -> &LifetimeData<I> {
346+
I::lifetime_data(interner, &self.interned)
347347
}
348348

349349
/// If this is a `Lifetime::InferenceVar(d)`, returns `Some(d)` else `None`.
350-
pub fn inference_var(&self) -> Option<InferenceVar> {
351-
if let LifetimeData::InferenceVar(depth) = self.data() {
350+
pub fn inference_var(&self, interner: &I) -> Option<InferenceVar> {
351+
if let LifetimeData::InferenceVar(depth) = self.data(interner) {
352352
Some(*depth)
353353
} else {
354354
None
@@ -357,8 +357,8 @@ impl<I: Interner> Lifetime<I> {
357357

358358
/// True if this lifetime is a "bound" lifetime, and hence
359359
/// needs to be shifted across binders. Meant for debug assertions.
360-
pub fn needs_shift(&self) -> bool {
361-
match self.data() {
360+
pub fn needs_shift(&self, interner: &I) -> bool {
361+
match self.data(interner) {
362362
LifetimeData::BoundVar(_) => true,
363363
LifetimeData::InferenceVar(_) => false,
364364
LifetimeData::Placeholder(_) => false,
@@ -1302,7 +1302,7 @@ impl<I: Interner> Substitution<I> {
13021302
TyData::BoundVar(depth) => index == *depth,
13031303
_ => false,
13041304
},
1305-
ParameterKind::Lifetime(lifetime) => match lifetime.data() {
1305+
ParameterKind::Lifetime(lifetime) => match lifetime.data(interner) {
13061306
LifetimeData::BoundVar(depth) => index == *depth,
13071307
_ => false,
13081308
},

chalk-ir/src/tls.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::interner::ChalkIr;
2-
use crate::{AliasTy, AssocTypeId, StructId, TraitId, Ty};
2+
use crate::{AliasTy, AssocTypeId, Lifetime, StructId, TraitId, Ty};
33
use std::cell::RefCell;
44
use std::fmt;
55
use std::sync::Arc;
@@ -34,6 +34,12 @@ pub trait DebugContext {
3434
) -> Result<(), fmt::Error>;
3535

3636
fn debug_ty(&self, ty: &Ty<ChalkIr>, fmt: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error>;
37+
38+
fn debug_lifetime(
39+
&self,
40+
lifetime: &Lifetime<ChalkIr>,
41+
fmt: &mut fmt::Formatter<'_>,
42+
) -> Result<(), fmt::Error>;
3743
}
3844

3945
pub fn with_current_program<R>(op: impl FnOnce(Option<&Arc<dyn DebugContext>>) -> R) -> R {

chalk-solve/src/infer.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -143,10 +143,14 @@ impl<I: Interner> InferenceTable<I> {
143143

144144
/// If `leaf` represents an inference variable `X`, and `X` is bound,
145145
/// returns `Some(v)` where `v` is the value to which `X` is bound.
146-
pub(crate) fn normalize_lifetime(&mut self, leaf: &Lifetime<I>) -> Option<Lifetime<I>> {
147-
let var = EnaVariable::from(leaf.inference_var()?);
146+
pub(crate) fn normalize_lifetime(
147+
&mut self,
148+
interner: &I,
149+
leaf: &Lifetime<I>,
150+
) -> Option<Lifetime<I>> {
151+
let var = EnaVariable::from(leaf.inference_var(interner)?);
148152
let v1 = self.probe_lifetime_var(var)?;
149-
assert!(!v1.needs_shift());
153+
assert!(!v1.needs_shift(interner));
150154
Some(v1)
151155
}
152156

chalk-solve/src/infer/unify.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ pub(crate) struct UnificationResult<I: Interner> {
5454
}
5555

5656
impl<'t, I: Interner> Unifier<'t, I> {
57-
#[allow(unreachable_code)]
5857
fn new(
5958
interner: &'t I,
6059
table: &'t mut InferenceTable<I>,
@@ -270,15 +269,15 @@ impl<'t, I: Interner> Unifier<'t, I> {
270269
fn unify_lifetime_lifetime(&mut self, a: &Lifetime<I>, b: &Lifetime<I>) -> Fallible<()> {
271270
let interner = self.interner;
272271

273-
if let Some(n_a) = self.table.normalize_lifetime(a) {
272+
if let Some(n_a) = self.table.normalize_lifetime(interner, a) {
274273
return self.unify_lifetime_lifetime(&n_a, b);
275-
} else if let Some(n_b) = self.table.normalize_lifetime(b) {
274+
} else if let Some(n_b) = self.table.normalize_lifetime(interner, b) {
276275
return self.unify_lifetime_lifetime(a, &n_b);
277276
}
278277

279278
debug_heading!("unify_lifetime_lifetime({:?}, {:?})", a, b);
280279

281-
match (a.data(), b.data()) {
280+
match (a.data(interner), b.data(interner)) {
282281
(&LifetimeData::InferenceVar(var_a), &LifetimeData::InferenceVar(var_b)) => {
283282
let var_a = EnaVariable::from(var_a);
284283
let var_b = EnaVariable::from(var_b);
@@ -510,7 +509,7 @@ where
510509
InferenceValue::Bound(l) => {
511510
let l = l.lifetime().unwrap();
512511
let l = l.fold_with(self, binders)?;
513-
assert!(!l.needs_shift());
512+
assert!(!l.needs_shift(interner));
514513
Ok(l.clone())
515514
}
516515
}

chalk-solve/src/solve/slg/aggregate.rs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,7 @@ impl<I: Interner> AntiUnifier<'_, '_, I> {
257257
apply1: &ApplicationTy<I>,
258258
apply2: &ApplicationTy<I>,
259259
) -> Ty<I> {
260+
let interner = self.interner;
260261
let ApplicationTy {
261262
name: name1,
262263
substitution: substitution1,
@@ -268,7 +269,7 @@ impl<I: Interner> AntiUnifier<'_, '_, I> {
268269

269270
self.aggregate_name_and_substs(name1, substitution1, name2, substitution2)
270271
.map(|(&name, substitution)| {
271-
TyData::Apply(ApplicationTy { name, substitution }).intern(self.interner)
272+
TyData::Apply(ApplicationTy { name, substitution }).intern(interner)
272273
})
273274
.unwrap_or_else(|| self.new_variable())
274275
}
@@ -278,14 +279,16 @@ impl<I: Interner> AntiUnifier<'_, '_, I> {
278279
index1: &PlaceholderIndex,
279280
index2: &PlaceholderIndex,
280281
) -> Ty<I> {
282+
let interner = self.interner;
281283
if index1 != index2 {
282284
self.new_variable()
283285
} else {
284-
TyData::Placeholder(index1.clone()).intern(self.interner)
286+
TyData::Placeholder(index1.clone()).intern(interner)
285287
}
286288
}
287289

288290
fn aggregate_alias_tys(&mut self, alias1: &AliasTy<I>, alias2: &AliasTy<I>) -> Ty<I> {
291+
let interner = self.interner;
289292
let AliasTy {
290293
associated_ty_id: name1,
291294
substitution: substitution1,
@@ -301,7 +304,7 @@ impl<I: Interner> AntiUnifier<'_, '_, I> {
301304
associated_ty_id,
302305
substitution,
303306
})
304-
.intern(self.interner)
307+
.intern(interner)
305308
})
306309
.unwrap_or_else(|| self.new_variable())
307310
}
@@ -359,7 +362,8 @@ impl<I: Interner> AntiUnifier<'_, '_, I> {
359362
}
360363

361364
fn aggregate_lifetimes(&mut self, l1: &Lifetime<I>, l2: &Lifetime<I>) -> Lifetime<I> {
362-
match (l1.data(), l2.data()) {
365+
let interner = self.interner;
366+
match (l1.data(interner), l2.data(interner)) {
363367
(LifetimeData::InferenceVar(_), _) | (_, LifetimeData::InferenceVar(_)) => {
364368
self.new_lifetime_variable()
365369
}
@@ -381,13 +385,13 @@ impl<I: Interner> AntiUnifier<'_, '_, I> {
381385
}
382386

383387
fn new_variable(&mut self) -> Ty<I> {
384-
self.infer.new_variable(self.universe).to_ty(self.interner)
388+
let interner = self.interner;
389+
self.infer.new_variable(self.universe).to_ty(interner)
385390
}
386391

387392
fn new_lifetime_variable(&mut self) -> Lifetime<I> {
388-
self.infer
389-
.new_variable(self.universe)
390-
.to_lifetime(self.interner)
393+
let interner = self.interner;
394+
self.infer.new_variable(self.universe).to_lifetime(interner)
391395
}
392396
}
393397

chalk-solve/src/solve/slg/resolvent.rs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,8 @@ impl<I: Interner> AnswerSubstitutor<'_, I> {
355355
impl<I: Interner> Zipper<I> for AnswerSubstitutor<'_, I> {
356356
fn zip_tys(&mut self, answer: &Ty<I>, pending: &Ty<I>) -> Fallible<()> {
357357
let interner = self.interner;
358-
if let Some(pending) = self.table.normalize_shallow(self.interner, pending) {
358+
359+
if let Some(pending) = self.table.normalize_shallow(interner, pending) {
359360
return Zip::zip_with(self, answer, &pending);
360361
}
361362

@@ -364,11 +365,7 @@ impl<I: Interner> Zipper<I> for AnswerSubstitutor<'_, I> {
364365
// resulting answer that the subgoal found and unify it with
365366
// the value from our "pending subgoal".
366367
if let TyData::BoundVar(answer_depth) = answer.data(interner) {
367-
if self.unify_free_answer_var(
368-
self.interner,
369-
*answer_depth,
370-
ParameterKind::Ty(pending),
371-
)? {
368+
if self.unify_free_answer_var(interner, *answer_depth, ParameterKind::Ty(pending))? {
372369
return Ok(());
373370
}
374371
}
@@ -417,21 +414,22 @@ impl<I: Interner> Zipper<I> for AnswerSubstitutor<'_, I> {
417414
}
418415

419416
fn zip_lifetimes(&mut self, answer: &Lifetime<I>, pending: &Lifetime<I>) -> Fallible<()> {
420-
if let Some(pending) = self.table.normalize_lifetime(pending) {
417+
let interner = self.interner;
418+
if let Some(pending) = self.table.normalize_lifetime(interner, pending) {
421419
return Zip::zip_with(self, answer, &pending);
422420
}
423421

424-
if let LifetimeData::BoundVar(answer_depth) = answer.data() {
422+
if let LifetimeData::BoundVar(answer_depth) = answer.data(interner) {
425423
if self.unify_free_answer_var(
426-
self.interner,
424+
interner,
427425
*answer_depth,
428426
ParameterKind::Lifetime(pending),
429427
)? {
430428
return Ok(());
431429
}
432430
}
433431

434-
match (answer.data(), pending.data()) {
432+
match (answer.data(interner), pending.data(interner)) {
435433
(LifetimeData::BoundVar(answer_depth), LifetimeData::BoundVar(pending_depth)) => {
436434
self.assert_matching_vars(*answer_depth, *pending_depth)
437435
}

0 commit comments

Comments
 (0)