Skip to content

Commit 030aea8

Browse files
authored
Merge pull request #350 from yaahc/jane/intern-parameter-data
Add self param to parameter_data
2 parents 75718ac + e97cbed commit 030aea8

File tree

21 files changed

+266
-125
lines changed

21 files changed

+266
-125
lines changed

chalk-integration/src/lowering.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -732,7 +732,7 @@ impl LowerTraitRef for TraitRef {
732732
.lower(env)?;
733733

734734
let self_parameter = self.args[0].lower(env)?;
735-
Ok(without_self.as_trait_ref(interner, self_parameter.assert_ty_ref().clone()))
735+
Ok(without_self.as_trait_ref(interner, self_parameter.assert_ty_ref(interner).clone()))
736736
}
737737
}
738738

@@ -1389,8 +1389,10 @@ impl<T, L> Kinded for chalk_ir::ParameterKind<T, L> {
13891389
}
13901390
}
13911391

1392+
#[allow(unreachable_code, unused_variables)]
13921393
impl Kinded for chalk_ir::Parameter<ChalkIr> {
13931394
fn kind(&self) -> Kind {
1394-
self.data().kind()
1395+
let interner = &ChalkIr;
1396+
self.data(interner).kind()
13951397
}
13961398
}

chalk-integration/src/program.rs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +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, Lifetime, Parameter, ProgramClause, StructId, TraitId, Ty,
8-
TyData, TypeName,
7+
AliasTy, AssocTypeId, ImplId, Lifetime, Parameter, ParameterKind, ProgramClause, StructId,
8+
TraitId, Ty, TyData, TypeName,
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 {
@@ -216,7 +228,7 @@ impl RustIrDatabase<ChalkIr> for Program {
216228
self.impl_data.values().any(|impl_datum| {
217229
let impl_trait_ref = &impl_datum.binders.value.trait_ref;
218230
impl_trait_ref.trait_id == auto_trait_id
219-
&& match impl_trait_ref.self_type_parameter().data(interner) {
231+
&& match impl_trait_ref.self_type_parameter(interner).data(interner) {
220232
TyData::Apply(apply) => match apply.name {
221233
TypeName::Struct(id) => id == struct_id,
222234
_ => false,

chalk-ir/src/could_match.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ where
2020
interner: &'i I,
2121
};
2222

23-
impl<I: Interner> Zipper<I> for MatchZipper<'_, I> {
23+
impl<'i, I: Interner> Zipper<'i, I> for MatchZipper<'i, I> {
2424
fn zip_tys(&mut self, a: &Ty<I>, b: &Ty<I>) -> Fallible<()> {
2525
let could_match = match (a.data(self.interner), b.data(self.interner)) {
2626
(&TyData::Apply(ref a), &TyData::Apply(ref b)) => {
@@ -53,6 +53,10 @@ where
5353
{
5454
Zip::zip_with(self, &a.value, &b.value)
5555
}
56+
57+
fn interner(&self) -> &'i I {
58+
self.interner
59+
}
5660
}
5761
}
5862
}

chalk-ir/src/debug.rs

Lines changed: 7 additions & 9 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,15 +393,6 @@ impl<T: Debug, L: Debug> Debug for ParameterKind<T, L> {
386393
}
387394
}
388395

389-
impl<I: Interner> Debug for Parameter<I> {
390-
fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
391-
match self.data() {
392-
ParameterKind::Ty(n) => write!(fmt, "{:?}", n),
393-
ParameterKind::Lifetime(n) => write!(fmt, "{:?}", n),
394-
}
395-
}
396-
}
397-
398396
impl<I: Interner> Debug for Constraint<I> {
399397
fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
400398
match self {

chalk-ir/src/fold/boring_impls.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,11 @@ impl<I: Interner, TI: TargetInterner<I>> Fold<I, TI> for Parameter<I> {
122122
I: 'i,
123123
TI: 'i,
124124
{
125-
let data = self.data().fold_with(folder, binders)?;
126-
Ok(Parameter::new(folder.target_interner(), data))
125+
let interner = folder.interner();
126+
let target_interner = folder.target_interner();
127+
128+
let data = self.data(interner).fold_with(folder, binders)?;
129+
Ok(Parameter::new(target_interner, data))
127130
}
128131
}
129132

chalk-ir/src/fold/subst.rs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,28 +29,27 @@ impl<'i, I: Interner> Folder<'i, I> for Subst<'_, 'i, I> {
2929
}
3030

3131
fn fold_free_var_ty(&mut self, depth: usize, binders: usize) -> Fallible<Ty<I>> {
32+
let interner = self.interner();
3233
if depth >= self.parameters.len() {
33-
Ok(
34-
TyData::<I>::BoundVar(depth - self.parameters.len() + binders)
35-
.intern(self.interner()),
36-
)
34+
Ok(TyData::<I>::BoundVar(depth - self.parameters.len() + binders).intern(interner))
3735
} else {
38-
match self.parameters[depth].data() {
39-
ParameterKind::Ty(t) => Ok(t.shifted_in(self.interner(), binders)),
36+
match self.parameters[depth].data(interner) {
37+
ParameterKind::Ty(t) => Ok(t.shifted_in(interner, binders)),
4038
_ => panic!("mismatched kinds in substitution"),
4139
}
4240
}
4341
}
4442

4543
fn fold_free_var_lifetime(&mut self, depth: usize, binders: usize) -> Fallible<Lifetime<I>> {
44+
let interner = self.interner();
4645
if depth >= self.parameters.len() {
4746
Ok(
4847
LifetimeData::<I>::BoundVar(depth - self.parameters.len() + binders)
49-
.intern(self.interner()),
48+
.intern(interner),
5049
)
5150
} else {
52-
match self.parameters[depth].data() {
53-
ParameterKind::Lifetime(l) => Ok(l.shifted_in(self.interner(), binders)),
51+
match self.parameters[depth].data(interner) {
52+
ParameterKind::Lifetime(l) => Ok(l.shifted_in(interner, binders)),
5453
_ => panic!("mismatched kinds in substitution"),
5554
}
5655
}

chalk-ir/src/interner.rs

Lines changed: 24 additions & 2 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).
@@ -179,7 +191,7 @@ pub trait Interner: Debug + Copy + Eq + Ord + Hash {
179191
fn intern_parameter(&self, data: ParameterData<Self>) -> Self::InternedParameter;
180192

181193
/// Lookup the `LifetimeData` that was interned to create a `InternedLifetime`.
182-
fn parameter_data(lifetime: &Self::InternedParameter) -> &ParameterData<Self>;
194+
fn parameter_data<'a>(&self, lifetime: &'a Self::InternedParameter) -> &'a ParameterData<Self>;
183195

184196
/// Create an "interned" goal from `data`. This is not
185197
/// normally invoked directly; instead, you invoke
@@ -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
}
@@ -330,7 +349,10 @@ mod default {
330349
parameter
331350
}
332351

333-
fn parameter_data(parameter: &ParameterData<ChalkIr>) -> &ParameterData<ChalkIr> {
352+
fn parameter_data<'a>(
353+
&self,
354+
parameter: &'a ParameterData<ChalkIr>,
355+
) -> &'a ParameterData<ChalkIr> {
334356
parameter
335357
}
336358

chalk-ir/src/lib.rs

Lines changed: 37 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -416,16 +416,19 @@ impl<I: Interner> ApplicationTy<I> {
416416
Ty::new(interner, self)
417417
}
418418

419-
pub fn type_parameters<'a>(&'a self) -> impl Iterator<Item = Ty<I>> + 'a {
420-
self.substitution.iter().filter_map(|p| p.ty()).cloned()
419+
pub fn type_parameters<'a>(&'a self, interner: &'a I) -> impl Iterator<Item = Ty<I>> + 'a {
420+
self.substitution
421+
.iter()
422+
.filter_map(move |p| p.ty(interner))
423+
.cloned()
421424
}
422425

423-
pub fn first_type_parameter(&self) -> Option<Ty<I>> {
424-
self.type_parameters().next()
426+
pub fn first_type_parameter(&self, interner: &I) -> Option<Ty<I>> {
427+
self.type_parameters(interner).next()
425428
}
426429

427-
pub fn len_type_parameters(&self) -> usize {
428-
self.type_parameters().count()
430+
pub fn len_type_parameters(&self, interner: &I) -> usize {
431+
self.type_parameters(interner).count()
429432
}
430433
}
431434

@@ -511,41 +514,41 @@ impl<I: Interner> Parameter<I> {
511514
Parameter(interned)
512515
}
513516

514-
pub fn data(&self) -> &ParameterData<I> {
515-
I::parameter_data(&self.0)
517+
pub fn data(&self, interner: &I) -> &ParameterData<I> {
518+
I::parameter_data(interner, &self.0)
516519
}
517520

518-
pub fn assert_ty_ref(&self) -> &Ty<I> {
519-
self.as_ref().ty().unwrap()
521+
pub fn assert_ty_ref(&self, interner: &I) -> &Ty<I> {
522+
self.as_ref(interner).ty().unwrap()
520523
}
521524

522-
pub fn assert_lifetime_ref(&self) -> &Lifetime<I> {
523-
self.as_ref().lifetime().unwrap()
525+
pub fn assert_lifetime_ref(&self, interner: &I) -> &Lifetime<I> {
526+
self.as_ref(interner).lifetime().unwrap()
524527
}
525528

526-
pub fn as_ref(&self) -> ParameterKind<&Ty<I>, &Lifetime<I>> {
527-
match self.data() {
529+
pub fn as_ref(&self, interner: &I) -> ParameterKind<&Ty<I>, &Lifetime<I>> {
530+
match self.data(interner) {
528531
ParameterKind::Ty(t) => ParameterKind::Ty(t),
529532
ParameterKind::Lifetime(l) => ParameterKind::Lifetime(l),
530533
}
531534
}
532535

533-
pub fn is_ty(&self) -> bool {
534-
match self.data() {
536+
pub fn is_ty(&self, interner: &I) -> bool {
537+
match self.data(interner) {
535538
ParameterKind::Ty(_) => true,
536539
ParameterKind::Lifetime(_) => false,
537540
}
538541
}
539542

540-
pub fn ty(&self) -> Option<&Ty<I>> {
541-
match self.data() {
543+
pub fn ty(&self, interner: &I) -> Option<&Ty<I>> {
544+
match self.data(interner) {
542545
ParameterKind::Ty(t) => Some(t),
543546
_ => None,
544547
}
545548
}
546549

547-
pub fn lifetime(&self) -> Option<&Lifetime<I>> {
548-
match self.data() {
550+
pub fn lifetime(&self, interner: &I) -> Option<&Lifetime<I>> {
551+
match self.data(interner) {
549552
ParameterKind::Lifetime(t) => Some(t),
550553
_ => None,
551554
}
@@ -580,12 +583,15 @@ pub struct TraitRef<I: Interner> {
580583
}
581584

582585
impl<I: Interner> TraitRef<I> {
583-
pub fn type_parameters<'a>(&'a self) -> impl Iterator<Item = Ty<I>> + 'a {
584-
self.substitution.iter().filter_map(|p| p.ty()).cloned()
586+
pub fn type_parameters<'a>(&'a self, interner: &'a I) -> impl Iterator<Item = Ty<I>> + 'a {
587+
self.substitution
588+
.iter()
589+
.filter_map(move |p| p.ty(interner))
590+
.cloned()
585591
}
586592

587-
pub fn self_type_parameter(&self) -> Ty<I> {
588-
self.type_parameters().next().unwrap()
593+
pub fn self_type_parameter(&self, interner: &I) -> Ty<I> {
594+
self.type_parameters(interner).next().unwrap()
589595
}
590596

591597
pub fn from_env(self) -> FromEnv<I> {
@@ -1297,7 +1303,7 @@ impl<I: Interner> Substitution<I> {
12971303
pub fn is_identity_subst(&self, interner: &I) -> bool {
12981304
self.iter()
12991305
.zip(0..)
1300-
.all(|(parameter, index)| match parameter.data() {
1306+
.all(|(parameter, index)| match parameter.data(interner) {
13011307
ParameterKind::Ty(ty) => match ty.data(interner) {
13021308
TyData::BoundVar(depth) => index == *depth,
13031309
_ => false,
@@ -1392,15 +1398,17 @@ impl<'i, I: Interner> Folder<'i, I> for &SubstFolder<'i, I> {
13921398
}
13931399

13941400
fn fold_free_var_ty(&mut self, depth: usize, binders: usize) -> Fallible<Ty<I>> {
1401+
let interner = self.interner();
13951402
let ty = self.at(depth);
1396-
let ty = ty.assert_ty_ref();
1397-
Ok(ty.shifted_in(self.interner(), binders))
1403+
let ty = ty.assert_ty_ref(interner);
1404+
Ok(ty.shifted_in(interner, binders))
13981405
}
13991406

14001407
fn fold_free_var_lifetime(&mut self, depth: usize, binders: usize) -> Fallible<Lifetime<I>> {
1408+
let interner = self.interner();
14011409
let l = self.at(depth);
1402-
let l = l.assert_lifetime_ref();
1403-
Ok(l.shifted_in(self.interner(), binders))
1410+
let l = l.assert_lifetime_ref(interner);
1411+
Ok(l.shifted_in(interner, binders))
14041412
}
14051413

14061414
fn interner(&self) -> &'i I {

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, Parameter, StructId, TraitId, Ty};
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 {

0 commit comments

Comments
 (0)