Skip to content

Commit 56c57ee

Browse files
committed
Get close but probably died trying
1 parent 8c3ba0f commit 56c57ee

File tree

9 files changed

+67
-15
lines changed

9 files changed

+67
-15
lines changed

chalk-integration/src/lowering.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1392,7 +1392,7 @@ impl<T, L> Kinded for chalk_ir::ParameterKind<T, L> {
13921392
#[allow(unreachable_code, unused_variables)]
13931393
impl Kinded for chalk_ir::Parameter<ChalkIr> {
13941394
fn kind(&self) -> Kind {
1395-
let interner = unimplemented!();
1395+
let interner = &ChalkIr;
13961396
self.data(interner).kind()
13971397
}
13981398
}

chalk-integration/src/program.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use chalk_ir::interner::ChalkIr;
55
use chalk_ir::tls;
66
use chalk_ir::{
77
AliasTy, AssocTypeId, ImplId, Lifetime, Parameter, ProgramClause, StructId, TraitId, Ty,
8-
TyData, TypeName,
8+
TyData, TypeName, ParameterKind,
99
};
1010
use chalk_rust_ir::{
1111
AssociatedTyDatum, AssociatedTyValue, AssociatedTyValueId, ImplDatum, ImplType, StructDatum,
@@ -135,6 +135,18 @@ impl tls::DebugContext for Program {
135135
let interner = self.interner();
136136
write!(fmt, "{:?}", lifetime.data(interner))
137137
}
138+
139+
fn debug_parameter(
140+
&self,
141+
parameter: &Parameter<ChalkIr>,
142+
fmt: &mut fmt::Formatter<'_>,
143+
) -> Result<(), fmt::Error> {
144+
let interner = self.interner();
145+
match parameter.data(interner) {
146+
ParameterKind::Ty(n) => write!(fmt, "{:?}", n),
147+
ParameterKind::Lifetime(n) => write!(fmt, "{:?}", n),
148+
}
149+
}
138150
}
139151

140152
impl RustIrDatabase<ChalkIr> for Program {

chalk-ir/src/could_match.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ where
5353
{
5454
Zip::zip_with(self, &a.value, &b.value)
5555
}
56+
57+
fn interner(&self) -> &I {
58+
self.interner
59+
}
5660
}
5761
}
5862
}

chalk-ir/src/debug.rs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,13 @@ impl<I: Interner> Debug for Lifetime<I> {
3333
}
3434
}
3535

36+
#[allow(unreachable_code, unused_variables)]
37+
impl<I: Interner> Debug for Parameter<I> {
38+
fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
39+
I::debug_parameter(self, fmt).unwrap_or_else(|| unimplemented!())
40+
}
41+
}
42+
3643
impl Display for UniverseIndex {
3744
fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
3845
write!(fmt, "U{}", self.counter)
@@ -386,17 +393,6 @@ impl<T: Debug, L: Debug> Debug for ParameterKind<T, L> {
386393
}
387394
}
388395

389-
#[allow(unreachable_code, unused_variables)]
390-
impl<I: Interner> Debug for Parameter<I> {
391-
fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
392-
let interner = unimplemented!();
393-
match self.data(interner) {
394-
ParameterKind::Ty(n) => write!(fmt, "{:?}", n),
395-
ParameterKind::Lifetime(n) => write!(fmt, "{:?}", n),
396-
}
397-
}
398-
}
399-
400396
impl<I: Interner> Debug for Constraint<I> {
401397
fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
402398
match self {

chalk-ir/src/interner.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,18 @@ pub trait Interner: Debug + Copy + Eq + Ord + Hash {
155155
fmt: &mut fmt::Formatter<'_>,
156156
) -> Option<fmt::Result>;
157157

158+
/// Prints the debug representation of an parameter. To get good
159+
/// results, this requires inspecting TLS, and is difficult to
160+
/// code without reference to a specific interner (and hence
161+
/// fully known types).
162+
///
163+
/// Returns `None` to fallback to the default debug output (e.g.,
164+
/// if no info about current program is available from TLS).
165+
fn debug_parameter(
166+
parameter: &Parameter<Self>,
167+
fmt: &mut fmt::Formatter<'_>,
168+
) -> Option<fmt::Result>;
169+
158170
/// Create an "interned" type from `ty`. This is not normally
159171
/// invoked directly; instead, you invoke `TyData::intern` (which
160172
/// will ultimately call this method).
@@ -307,6 +319,13 @@ mod default {
307319
.or_else(|| Some(write!(fmt, "{:?}", lifetime.interned)))
308320
}
309321

322+
fn debug_parameter(
323+
parameter: &Parameter<ChalkIr>,
324+
fmt: &mut fmt::Formatter<'_>,
325+
) -> Option<fmt::Result> {
326+
tls::with_current_program(|prog| Some(prog?.debug_parameter(parameter, fmt)))
327+
}
328+
310329
fn intern_ty(&self, ty: TyData<ChalkIr>) -> Arc<TyData<ChalkIr>> {
311330
Arc::new(ty)
312331
}

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, Lifetime, StructId, TraitId, Ty};
2+
use crate::{AliasTy, AssocTypeId, Lifetime, StructId, TraitId, Ty, Parameter};
33
use std::cell::RefCell;
44
use std::fmt;
55
use std::sync::Arc;
@@ -40,6 +40,12 @@ pub trait DebugContext {
4040
lifetime: &Lifetime<ChalkIr>,
4141
fmt: &mut fmt::Formatter<'_>,
4242
) -> Result<(), fmt::Error>;
43+
44+
fn debug_parameter(
45+
&self,
46+
parameter: &Parameter<ChalkIr>,
47+
fmt: &mut fmt::Formatter<'_>,
48+
) -> Result<(), fmt::Error>;
4349
}
4450

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

chalk-ir/src/zip.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ pub trait Zipper<I: Interner> {
3232
fn zip_binders<T>(&mut self, a: &Binders<T>, b: &Binders<T>) -> Fallible<()>
3333
where
3434
T: Zip<I> + Fold<I, I, Result = T>;
35+
36+
/// Retreives the interner from the underlying zipper object
37+
fn interner(&self) -> &I;
3538
}
3639

3740
impl<'f, Z, I> Zipper<I> for &'f mut Z
@@ -53,6 +56,10 @@ where
5356
{
5457
(**self).zip_binders(a, b)
5558
}
59+
60+
fn interner(&self) -> &I {
61+
Z::interner(*self)
62+
}
5663
}
5764

5865
/// The `Zip` trait walks two values, invoking the `Zipper` methods where
@@ -323,7 +330,7 @@ impl<T: Zip<I>, L: Zip<I>, I: Interner> Zip<I> for ParameterKind<T, L> {
323330
#[allow(unreachable_code, unused_variables)]
324331
impl<I: Interner> Zip<I> for Parameter<I> {
325332
fn zip_with<Z: Zipper<I>>(zipper: &mut Z, a: &Self, b: &Self) -> Fallible<()> {
326-
let interner = unimplemented!();
333+
let interner = zipper.interner();
327334
Zip::zip_with(zipper, a.data(interner), b.data(interner))
328335
}
329336
}

chalk-solve/src/infer/unify.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,10 @@ impl<I: Interner> Zipper<I> for Unifier<'_, I> {
364364

365365
self.unify_binders(a, b)
366366
}
367+
368+
fn interner(&self) -> &I {
369+
self.interner
370+
}
367371
}
368372

369373
struct OccursCheck<'u, 't, I: Interner> {

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -464,4 +464,8 @@ impl<I: Interner> Zipper<I> for AnswerSubstitutor<'_, I> {
464464
self.pending_binders -= pending.binders.len();
465465
Ok(())
466466
}
467+
468+
fn interner(&self) -> &I {
469+
self.interner
470+
}
467471
}

0 commit comments

Comments
 (0)