Skip to content

Commit cd92edd

Browse files
committed
Get interner into lifetime Debug impl
1 parent 4dd5382 commit cd92edd

File tree

8 files changed

+57
-23
lines changed

8 files changed

+57
-23
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 & 8 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,14 +93,6 @@ impl<I: Interner> Debug for Fn<I> {
8793
}
8894
}
8995

90-
#[allow(unreachable_code, unused_variables)]
91-
impl<I: Interner> Debug for Lifetime<I> {
92-
fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
93-
let interner = unimplemented!();
94-
write!(fmt, "{:?}", self.data(interner))
95-
}
96-
}
97-
9896
impl<I: Interner> Debug for LifetimeData<I> {
9997
fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
10098
match self {

chalk-ir/src/interner.rs

Lines changed: 26 additions & 3 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).
@@ -282,8 +295,15 @@ 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)))
287307
}
288308

289309
fn intern_ty(&self, ty: TyData<ChalkIr>) -> Arc<TyData<ChalkIr>> {
@@ -298,7 +318,10 @@ mod default {
298318
lifetime
299319
}
300320

301-
fn lifetime_data<'a>(&self, lifetime: &'a LifetimeData<ChalkIr>) -> &'a LifetimeData<ChalkIr> {
321+
fn lifetime_data<'a>(
322+
&self,
323+
lifetime: &'a LifetimeData<ChalkIr>,
324+
) -> &'a LifetimeData<ChalkIr> {
302325
lifetime
303326
}
304327

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: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,11 @@ 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, interner: &I, leaf: &Lifetime<I>) -> Option<Lifetime<I>> {
146+
pub(crate) fn normalize_lifetime(
147+
&mut self,
148+
interner: &I,
149+
leaf: &Lifetime<I>,
150+
) -> Option<Lifetime<I>> {
147151
let var = EnaVariable::from(leaf.inference_var(interner)?);
148152
let v1 = self.probe_lifetime_var(var)?;
149153
assert!(!v1.needs_shift(interner));

chalk-solve/src/infer/unify.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -484,7 +484,6 @@ where
484484
var: InferenceVar,
485485
binders: usize,
486486
) -> Fallible<Lifetime<I>> {
487-
let interner = self.interner();
488487
// a free existentially bound region; find the
489488
// inference variable it corresponds to
490489
let interner = self.interner();

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -391,9 +391,7 @@ impl<I: Interner> AntiUnifier<'_, '_, I> {
391391

392392
fn new_lifetime_variable(&mut self) -> Lifetime<I> {
393393
let interner = self.interner;
394-
self.infer
395-
.new_variable(self.universe)
396-
.to_lifetime(interner)
394+
self.infer.new_variable(self.universe).to_lifetime(interner)
397395
}
398396
}
399397

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -365,11 +365,7 @@ impl<I: Interner> Zipper<I> for AnswerSubstitutor<'_, I> {
365365
// resulting answer that the subgoal found and unify it with
366366
// the value from our "pending subgoal".
367367
if let TyData::BoundVar(answer_depth) = answer.data(interner) {
368-
if self.unify_free_answer_var(
369-
interner,
370-
*answer_depth,
371-
ParameterKind::Ty(pending),
372-
)? {
368+
if self.unify_free_answer_var(interner, *answer_depth, ParameterKind::Ty(pending))? {
373369
return Ok(());
374370
}
375371
}

0 commit comments

Comments
 (0)