Skip to content

Commit 1ac0d00

Browse files
authored
Merge pull request #354 from yaahc/jane/intern-substitution-data
Add self param to Interner::substitution_data
2 parents b7ed5e0 + 886553e commit 1ac0d00

File tree

17 files changed

+288
-153
lines changed

17 files changed

+288
-153
lines changed

chalk-integration/src/lowering.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -949,7 +949,7 @@ impl LowerAliasTy for AliasTy {
949949
}
950950
}
951951

952-
args.extend(trait_substitution.iter().cloned());
952+
args.extend(trait_substitution.iter(interner).cloned());
953953

954954
Ok(chalk_ir::AliasTy {
955955
associated_ty_id: lookup.id,

chalk-integration/src/program.rs

Lines changed: 60 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ use chalk_ir::debug::Angle;
44
use chalk_ir::interner::ChalkIr;
55
use chalk_ir::tls;
66
use chalk_ir::{
7-
AliasTy, AssocTypeId, Goal, GoalData, Goals, ImplId, Lifetime, Parameter, ParameterKind,
8-
ProgramClause, ProgramClauseImplication, StructId, TraitId, Ty, TyData, TypeName,
7+
debug::SeparatorTraitRef, AliasTy, ApplicationTy, AssocTypeId, Goal, GoalData, Goals, ImplId,
8+
Lifetime, Parameter, ParameterKind, ProgramClause, ProgramClauseImplication, StructId,
9+
Substitution, TraitId, Ty, TyData, TypeName,
910
};
1011
use chalk_rust_ir::{
1112
AssociatedTyDatum, AssociatedTyValue, AssociatedTyValueId, ImplDatum, ImplType, StructDatum,
@@ -215,6 +216,61 @@ impl tls::DebugContext for Program {
215216
}
216217
write!(fmt, "{:?}", conditions[conds - 1])
217218
}
219+
220+
fn debug_application_ty(
221+
&self,
222+
application_ty: &ApplicationTy<ChalkIr>,
223+
fmt: &mut fmt::Formatter<'_>,
224+
) -> Result<(), fmt::Error> {
225+
let interner = self.interner();
226+
let ApplicationTy { name, substitution } = application_ty;
227+
write!(fmt, "{:?}{:?}", name, substitution.with_angle(interner))
228+
}
229+
230+
fn debug_substitution(
231+
&self,
232+
substitution: &Substitution<ChalkIr>,
233+
fmt: &mut fmt::Formatter<'_>,
234+
) -> Result<(), fmt::Error> {
235+
let interner = self.interner();
236+
let mut first = true;
237+
238+
write!(fmt, "[")?;
239+
240+
for (index, value) in substitution.iter(interner).enumerate() {
241+
if first {
242+
first = false;
243+
} else {
244+
write!(fmt, ", ")?;
245+
}
246+
247+
write!(fmt, "?{} := {:?}", index, value)?;
248+
}
249+
250+
write!(fmt, "]")?;
251+
252+
Ok(())
253+
}
254+
255+
fn debug_separator_trait_ref(
256+
&self,
257+
separator_trait_ref: &SeparatorTraitRef<ChalkIr>,
258+
fmt: &mut fmt::Formatter<'_>,
259+
) -> Result<(), fmt::Error> {
260+
let interner = self.interner();
261+
let parameters = separator_trait_ref
262+
.trait_ref
263+
.substitution
264+
.parameters(interner);
265+
write!(
266+
fmt,
267+
"{:?}{}{:?}{:?}",
268+
parameters[0],
269+
separator_trait_ref.separator,
270+
separator_trait_ref.trait_ref.trait_id,
271+
Angle(&parameters[1..])
272+
)
273+
}
218274
}
219275

220276
impl RustIrDatabase<ChalkIr> for Program {
@@ -263,11 +319,11 @@ impl RustIrDatabase<ChalkIr> for Program {
263319
.filter(|(_, impl_datum)| {
264320
let trait_ref = &impl_datum.binders.value.trait_ref;
265321
trait_id == trait_ref.trait_id && {
266-
assert_eq!(trait_ref.substitution.len(), parameters.len());
322+
assert_eq!(trait_ref.substitution.len(interner), parameters.len());
267323
<[_] as CouldMatch<[_]>>::could_match(
268324
&parameters,
269325
interner,
270-
&trait_ref.substitution.parameters(),
326+
&trait_ref.substitution.parameters(interner),
271327
)
272328
}
273329
})

chalk-ir/src/could_match.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,16 @@ where
2222

2323
impl<'i, I: Interner> Zipper<'i, I> for MatchZipper<'i, I> {
2424
fn zip_tys(&mut self, a: &Ty<I>, b: &Ty<I>) -> Fallible<()> {
25-
let could_match = match (a.data(self.interner), b.data(self.interner)) {
25+
let interner = self.interner;
26+
let could_match = match (a.data(interner), b.data(interner)) {
2627
(&TyData::Apply(ref a), &TyData::Apply(ref b)) => {
2728
let names_could_match = a.name == b.name;
2829

2930
names_could_match
3031
&& a.substitution
31-
.iter()
32-
.zip(&b.substitution)
33-
.all(|(p_a, p_b)| p_a.could_match(self.interner, &p_b))
32+
.iter(interner)
33+
.zip(b.substitution.iter(interner))
34+
.all(|(p_a, p_b)| p_a.could_match(interner, &p_b))
3435
}
3536

3637
_ => true,

chalk-ir/src/debug.rs

Lines changed: 52 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -23,37 +23,78 @@ impl<I: Interner> Debug for AssocTypeId<I> {
2323

2424
impl<I: Interner> Debug for Ty<I> {
2525
fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
26-
I::debug_ty(self, fmt).unwrap_or_else(|| unimplemented!())
26+
I::debug_ty(self, fmt)
27+
.unwrap_or_else(|| unimplemented!("cannot format Ty without setting Program in tls"))
2728
}
2829
}
2930

3031
impl<I: Interner> Debug for Lifetime<I> {
3132
fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
32-
I::debug_lifetime(self, fmt).unwrap_or_else(|| unimplemented!())
33+
I::debug_lifetime(self, fmt).unwrap_or_else(|| {
34+
unimplemented!("cannot format Lifetime without setting Program in tls")
35+
})
3336
}
3437
}
3538

3639
impl<I: Interner> Debug for Parameter<I> {
3740
fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
38-
I::debug_parameter(self, fmt).unwrap_or_else(|| unimplemented!())
41+
I::debug_parameter(self, fmt).unwrap_or_else(|| {
42+
unimplemented!("cannot format Parameter without setting Program in tls")
43+
})
3944
}
4045
}
4146

4247
impl<I: Interner> Debug for Goal<I> {
4348
fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
44-
I::debug_goal(self, fmt).unwrap_or_else(|| unimplemented!())
49+
I::debug_goal(self, fmt)
50+
.unwrap_or_else(|| unimplemented!("cannot format Goal without setting Program in tls"))
4551
}
4652
}
4753

4854
impl<I: Interner> Debug for Goals<I> {
4955
fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
50-
I::debug_goals(self, fmt).unwrap_or_else(|| unimplemented!())
56+
I::debug_goals(self, fmt)
57+
.unwrap_or_else(|| unimplemented!("cannot format Goals without setting Program in tls"))
5158
}
5259
}
5360

5461
impl<I: Interner> Debug for ProgramClauseImplication<I> {
5562
fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
56-
I::debug_program_clause_implication(self, fmt).unwrap_or_else(|| unimplemented!())
63+
I::debug_program_clause_implication(self, fmt).unwrap_or_else(|| {
64+
unimplemented!("cannot format ProgramClauseImplication without setting Program in tls")
65+
})
66+
}
67+
}
68+
69+
impl<I: Interner> Debug for ApplicationTy<I> {
70+
fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
71+
I::debug_application_ty(self, fmt).unwrap_or_else(|| {
72+
unimplemented!("cannot format ApplicationTy without setting Program in tls")
73+
})
74+
}
75+
}
76+
77+
impl<I: Interner> Debug for SeparatorTraitRef<'_, I> {
78+
fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
79+
I::debug_separator_trait_ref(self, fmt).unwrap_or_else(|| {
80+
unimplemented!("cannot format Substitution without setting Program in tls")
81+
})
82+
}
83+
}
84+
85+
impl<I: Interner> Debug for AliasTy<I> {
86+
fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
87+
I::debug_alias(self, fmt).unwrap_or_else(|| {
88+
unimplemented!("cannot format AliasTy without setting Program in tls")
89+
})
90+
}
91+
}
92+
93+
impl<I: Interner> Display for Substitution<I> {
94+
fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
95+
I::debug_substitution(self, fmt).unwrap_or_else(|| {
96+
unimplemented!("cannot format Substitution without setting Program in tls")
97+
})
5798
}
5899
}
59100

@@ -135,13 +176,6 @@ impl Debug for PlaceholderIndex {
135176
}
136177
}
137178

138-
impl<I: Interner> Debug for ApplicationTy<I> {
139-
fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
140-
let ApplicationTy { name, substitution } = self;
141-
write!(fmt, "{:?}{:?}", name, substitution.with_angle())
142-
}
143-
}
144-
145179
impl<I: Interner> TraitRef<I> {
146180
/// Returns a "Debuggable" type that prints like `P0 as Trait<P1..>`
147181
pub fn with_as(&self) -> impl std::fmt::Debug + '_ {
@@ -166,36 +200,9 @@ impl<I: Interner> Debug for TraitRef<I> {
166200
}
167201
}
168202

169-
struct SeparatorTraitRef<'me, I: Interner> {
170-
trait_ref: &'me TraitRef<I>,
171-
separator: &'me str,
172-
}
173-
174-
impl<I: Interner> Debug for SeparatorTraitRef<'_, I> {
175-
fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
176-
let parameters = self.trait_ref.substitution.parameters();
177-
write!(
178-
fmt,
179-
"{:?}{}{:?}{:?}",
180-
parameters[0],
181-
self.separator,
182-
self.trait_ref.trait_id,
183-
Angle(&parameters[1..])
184-
)
185-
}
186-
}
187-
188-
impl<I: Interner> Debug for AliasTy<I> {
189-
fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
190-
I::debug_alias(self, fmt).unwrap_or_else(|| {
191-
write!(
192-
fmt,
193-
"({:?}){:?}",
194-
self.associated_ty_id,
195-
self.substitution.with_angle()
196-
)
197-
})
198-
}
203+
pub struct SeparatorTraitRef<'me, I: Interner> {
204+
pub trait_ref: &'me TraitRef<I>,
205+
pub separator: &'me str,
199206
}
200207

201208
pub struct Angle<'a, T>(pub &'a [T]);
@@ -374,8 +381,8 @@ impl<I: Interner> Display for ConstrainedSubst<I> {
374381
impl<I: Interner> Substitution<I> {
375382
/// Displays the substitution in the form `< P0, .. Pn >`, or (if
376383
/// the substitution is empty) as an empty string.
377-
pub fn with_angle(&self) -> Angle<'_, Parameter<I>> {
378-
Angle(self.parameters())
384+
pub fn with_angle(&self, interner: &I) -> Angle<'_, Parameter<I>> {
385+
Angle(self.parameters(interner))
379386
}
380387
}
381388

@@ -384,25 +391,3 @@ impl<I: Interner> Debug for Substitution<I> {
384391
Display::fmt(self, fmt)
385392
}
386393
}
387-
388-
impl<I: Interner> Display for Substitution<I> {
389-
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> {
390-
let mut first = true;
391-
392-
write!(f, "[")?;
393-
394-
for (index, value) in self.iter().enumerate() {
395-
if first {
396-
first = false;
397-
} else {
398-
write!(f, ", ")?;
399-
}
400-
401-
write!(f, "?{} := {:?}", index, value)?;
402-
}
403-
404-
write!(f, "]")?;
405-
406-
Ok(())
407-
}
408-
}

chalk-ir/src/fold/boring_impls.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,9 +141,10 @@ impl<I: Interner, TI: TargetInterner<I>> Fold<I, TI> for Substitution<I> {
141141
I: 'i,
142142
TI: 'i,
143143
{
144-
let interner = folder.target_interner();
145-
let folded = self.iter().map(|p| p.fold_with(folder, binders));
146-
Ok(Substitution::from_fallible(interner, folded)?)
144+
let interner = folder.interner();
145+
let target_interner = folder.target_interner();
146+
let folded = self.iter(interner).map(|p| p.fold_with(folder, binders));
147+
Ok(Substitution::from_fallible(target_interner, folded)?)
147148
}
148149
}
149150

0 commit comments

Comments
 (0)