Skip to content

Commit 084186b

Browse files
committed
Switch Vec<ParameterKind<()>> to ParameterKinds<I>
1 parent 501b5e2 commit 084186b

File tree

27 files changed

+299
-188
lines changed

27 files changed

+299
-188
lines changed

chalk-engine/src/lib.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -278,5 +278,3 @@ impl Minimums {
278278
min(self.positive, self.negative)
279279
}
280280
}
281-
282-

chalk-integration/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ pub mod program;
1111
pub mod program_environment;
1212
pub mod query;
1313

14+
use chalk_ir::interner::ChalkIr;
1415
pub use chalk_ir::interner::{Identifier, RawId};
1516
use chalk_ir::Binders;
1617

@@ -24,5 +25,5 @@ pub enum TypeSort {
2425
pub struct TypeKind {
2526
pub sort: TypeSort,
2627
pub name: Identifier,
27-
pub binders: Binders<()>,
28+
pub binders: Binders<ChalkIr, ()>,
2829
}

chalk-integration/src/lowering.rs

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use chalk_ir::cast::{Cast, Caster};
22
use chalk_ir::interner::ChalkIr;
33
use chalk_ir::{
4-
self, AssocTypeId, BoundVar, DebruijnIndex, ImplId, QuantifiedWhereClauses, StructId,
5-
Substitution, TraitId,
4+
self, AssocTypeId, BoundVar, DebruijnIndex, ImplId, ParameterKinds, QuantifiedWhereClauses,
5+
StructId, Substitution, TraitId,
66
};
77
use chalk_parse::ast::*;
88
use chalk_rust_ir as rust_ir;
@@ -161,16 +161,17 @@ impl<'k> Env<'k> {
161161
})
162162
}
163163

164-
fn in_binders<I, T, OP>(&self, binders: I, op: OP) -> LowerResult<chalk_ir::Binders<T>>
164+
fn in_binders<I, T, OP>(&self, binders: I, op: OP) -> LowerResult<chalk_ir::Binders<ChalkIr, T>>
165165
where
166166
I: IntoIterator<Item = chalk_ir::ParameterKind<Ident>>,
167167
I::IntoIter: ExactSizeIterator,
168168
OP: FnOnce(&Self) -> LowerResult<T>,
169169
{
170170
let binders: Vec<_> = binders.into_iter().collect();
171171
let env = self.introduce(binders.iter().cloned())?;
172+
let interner = &ChalkIr;
172173
Ok(chalk_ir::Binders {
173-
binders: binders.anonymize(),
174+
binders: ParameterKinds::from(interner, binders.anonymize()),
174175
value: op(&env)?,
175176
})
176177
}
@@ -525,11 +526,12 @@ trait LowerWhereClauses {
525526

526527
impl LowerTypeKind for StructDefn {
527528
fn lower_type_kind(&self) -> LowerResult<TypeKind> {
529+
let interner = &ChalkIr;
528530
Ok(TypeKind {
529531
sort: TypeSort::Struct,
530532
name: self.name.str,
531533
binders: chalk_ir::Binders {
532-
binders: self.all_parameters().anonymize(),
534+
binders: ParameterKinds::from(interner, self.all_parameters().anonymize()),
533535
value: (),
534536
},
535537
})
@@ -544,13 +546,14 @@ impl LowerWhereClauses for StructDefn {
544546

545547
impl LowerTypeKind for TraitDefn {
546548
fn lower_type_kind(&self) -> LowerResult<TypeKind> {
549+
let interner = &ChalkIr;
547550
let binders: Vec<_> = self.parameter_kinds.iter().map(|p| p.lower()).collect();
548551
Ok(TypeKind {
549552
sort: TypeSort::Trait,
550553
name: self.name.str,
551554
binders: chalk_ir::Binders {
552555
// for the purposes of the *type*, ignore `Self`:
553-
binders: binders.anonymize(),
556+
binders: ParameterKinds::from(interner, binders.anonymize()),
554557
value: (),
555558
},
556559
})
@@ -757,6 +760,7 @@ trait LowerTraitBound {
757760

758761
impl LowerTraitBound for TraitBound {
759762
fn lower(&self, env: &Env) -> LowerResult<rust_ir::TraitBound<ChalkIr>> {
763+
let interner = &ChalkIr;
760764
let trait_id = env.lookup_trait(self.trait_name)?;
761765

762766
let k = env.trait_kind(trait_id);
@@ -770,15 +774,15 @@ impl LowerTraitBound for TraitBound {
770774
.map(|a| Ok(a.lower(env)?))
771775
.collect::<LowerResult<Vec<_>>>()?;
772776

773-
if parameters.len() != k.binders.len() {
777+
if parameters.len() != k.binders.len(interner) {
774778
Err(RustIrError::IncorrectNumberOfTypeParameters {
775779
identifier: self.trait_name,
776-
expected: k.binders.len(),
780+
expected: k.binders.len(interner),
777781
actual: parameters.len(),
778782
})?;
779783
}
780784

781-
for (binder, param) in k.binders.binders.iter().zip(parameters.iter()) {
785+
for (binder, param) in k.binders.binders.iter(interner).zip(parameters.iter()) {
782786
if binder.kind() != param.kind() {
783787
Err(RustIrError::IncorrectTraitParameterKind {
784788
identifier: self.trait_name,
@@ -984,10 +988,10 @@ impl LowerTy for Ty {
984988
Ty::Id { name } => match env.lookup_type(name)? {
985989
TypeLookup::Struct(id) => {
986990
let k = env.struct_kind(id);
987-
if k.binders.len() > 0 {
991+
if k.binders.len(interner) > 0 {
988992
Err(RustIrError::IncorrectNumberOfTypeParameters {
989993
identifier: name,
990-
expected: k.binders.len(),
994+
expected: k.binders.len(interner),
991995
actual: 0,
992996
})
993997
} else {
@@ -1031,10 +1035,10 @@ impl LowerTy for Ty {
10311035
};
10321036

10331037
let k = env.struct_kind(id);
1034-
if k.binders.len() != args.len() {
1038+
if k.binders.len(interner) != args.len() {
10351039
Err(RustIrError::IncorrectNumberOfTypeParameters {
10361040
identifier: name,
1037-
expected: k.binders.len(),
1041+
expected: k.binders.len(interner),
10381042
actual: args.len(),
10391043
})?;
10401044
}
@@ -1044,7 +1048,7 @@ impl LowerTy for Ty {
10441048
args.iter().map(|t| Ok(t.lower(env)?)),
10451049
)?;
10461050

1047-
for (param, arg) in k.binders.binders.iter().zip(args.iter()) {
1051+
for (param, arg) in k.binders.binders.iter(interner).zip(args.iter()) {
10481052
if param.kind() != arg.kind() {
10491053
Err(RustIrError::IncorrectParameterKind {
10501054
identifier: name,
@@ -1207,7 +1211,10 @@ impl LowerClause for Clause {
12071211
let clauses = implications
12081212
.into_iter()
12091213
.map(
1210-
|implication: chalk_ir::Binders<chalk_ir::ProgramClauseImplication<ChalkIr>>| {
1214+
|implication: chalk_ir::Binders<
1215+
ChalkIr,
1216+
chalk_ir::ProgramClauseImplication<ChalkIr>,
1217+
>| {
12111218
chalk_ir::ProgramClauseData::ForAll(implication).intern(interner)
12121219
},
12131220
)
@@ -1273,14 +1280,16 @@ pub trait LowerGoal<A> {
12731280

12741281
impl LowerGoal<LoweredProgram> for Goal {
12751282
fn lower(&self, program: &LoweredProgram) -> LowerResult<chalk_ir::Goal<ChalkIr>> {
1283+
let interner = &ChalkIr;
12761284
let associated_ty_lookups: BTreeMap<_, _> = program
12771285
.associated_ty_data
12781286
.iter()
12791287
.map(|(&associated_ty_id, datum)| {
12801288
let trait_datum = &program.trait_data[&datum.trait_id];
1281-
let num_trait_params = trait_datum.binders.len();
1282-
let num_addl_params = datum.binders.len() - num_trait_params;
1283-
let addl_parameter_kinds = datum.binders.binders[..num_addl_params].to_owned();
1289+
let num_trait_params = trait_datum.binders.len(interner);
1290+
let num_addl_params = datum.binders.len(interner) - num_trait_params;
1291+
let addl_parameter_kinds =
1292+
datum.binders.binders.as_slice(interner)[..num_addl_params].to_owned();
12841293
let lookup = AssociatedTyLookup {
12851294
id: associated_ty_id,
12861295
addl_parameter_kinds,

chalk-integration/src/program.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,15 @@ impl tls::DebugContext for Program {
158158
write!(fmt, "{:?}", parameter_kinds.as_slice(interner))
159159
}
160160

161+
fn debug_parameter_kinds_with_angles(
162+
&self,
163+
parameter_kinds: &chalk_ir::ParameterKinds<ChalkIr>,
164+
fmt: &mut fmt::Formatter<'_>,
165+
) -> Result<(), fmt::Error> {
166+
let interner = self.interner();
167+
write!(fmt, "{:?}", parameter_kinds.inner_debug(interner))
168+
}
169+
161170
fn debug_parameter_kinds_with_universe_index(
162171
&self,
163172
parameter_kinds: &chalk_ir::ParameterKindsWithUniverseIndex<ChalkIr>,

chalk-ir/src/cast.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ impl<I: Interner> CastTo<Goal<I>> for EqGoal<I> {
140140
}
141141
}
142142

143-
impl<T: CastTo<Goal<I>>, I: Interner> CastTo<Goal<I>> for Binders<T> {
143+
impl<T: CastTo<Goal<I>>, I: Interner> CastTo<Goal<I>> for Binders<I, T> {
144144
fn cast_to(self, interner: &I) -> Goal<I> {
145145
GoalData::Quantified(
146146
QuantifierKind::ForAll,
@@ -194,7 +194,7 @@ where
194194
}
195195
}
196196

197-
impl<T, I> CastTo<ProgramClause<I>> for Binders<T>
197+
impl<T, I> CastTo<ProgramClause<I>> for Binders<I, T>
198198
where
199199
T: CastTo<DomainGoal<I>>,
200200
I: Interner,
@@ -214,7 +214,7 @@ impl<I: Interner> CastTo<ProgramClause<I>> for ProgramClauseImplication<I> {
214214
}
215215
}
216216

217-
impl<I: Interner> CastTo<ProgramClause<I>> for Binders<ProgramClauseImplication<I>> {
217+
impl<I: Interner> CastTo<ProgramClause<I>> for Binders<I, ProgramClauseImplication<I>> {
218218
fn cast_to(self, interner: &I) -> ProgramClause<I> {
219219
ProgramClauseData::ForAll(self).intern(interner)
220220
}

chalk-ir/src/could_match.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ where
4848
Ok(())
4949
}
5050

51-
fn zip_binders<T>(&mut self, a: &Binders<T>, b: &Binders<T>) -> Fallible<()>
51+
fn zip_binders<T>(&mut self, a: &Binders<I, T>, b: &Binders<I, T>) -> Fallible<()>
5252
where
5353
T: Zip<I>,
5454
{

chalk-ir/src/debug.rs

Lines changed: 64 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -187,22 +187,64 @@ impl<I: Interner> Debug for LifetimeData<I> {
187187
}
188188
}
189189

190+
impl<I: Interner> ParameterKinds<I> {
191+
fn debug(&self) -> ParameterKindsDebug<'_, I> {
192+
ParameterKindsDebug(self)
193+
}
194+
195+
pub fn inner_debug<'a>(&'a self, interner: &'a I) -> ParameterKindsInnerDebug<'a, I> {
196+
ParameterKindsInnerDebug {
197+
parameter_kinds: self,
198+
interner,
199+
}
200+
}
201+
}
202+
203+
struct ParameterKindsDebug<'a, I: Interner>(&'a ParameterKinds<I>);
204+
205+
impl<'a, I: Interner> Debug for ParameterKindsDebug<'a, I> {
206+
fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
207+
I::debug_parameter_kinds_with_angles(self.0, fmt)
208+
.unwrap_or_else(|| write!(fmt, "{:?}", self.0.interned))
209+
}
210+
}
211+
212+
pub struct ParameterKindsInnerDebug<'a, I: Interner> {
213+
parameter_kinds: &'a ParameterKinds<I>,
214+
interner: &'a I,
215+
}
216+
217+
impl<'a, I: Interner> Debug for ParameterKindsInnerDebug<'a, I> {
218+
fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
219+
// NB: We always print the `for<>`, even if it is empty,
220+
// because it may affect the debruijn indices of things
221+
// contained within. For example, `for<> { ^1.0 }` is very
222+
// different from `^1.0` in terms of what variable is being
223+
// referenced.
224+
write!(fmt, "<")?;
225+
for (index, binder) in self.parameter_kinds.iter(self.interner).enumerate() {
226+
if index > 0 {
227+
write!(fmt, ", ")?;
228+
}
229+
match *binder {
230+
ParameterKind::Ty(()) => write!(fmt, "type")?,
231+
ParameterKind::Lifetime(()) => write!(fmt, "lifetime")?,
232+
}
233+
}
234+
write!(fmt, ">")
235+
}
236+
}
237+
190238
impl<I: Interner> Debug for GoalData<I> {
191239
fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
192240
match self {
193-
GoalData::Quantified(qkind, ref subgoal) => {
194-
write!(fmt, "{:?}<", qkind)?;
195-
for (index, binder) in subgoal.binders.iter().enumerate() {
196-
if index > 0 {
197-
write!(fmt, ", ")?;
198-
}
199-
match *binder {
200-
ParameterKind::Ty(()) => write!(fmt, "type")?,
201-
ParameterKind::Lifetime(()) => write!(fmt, "lifetime")?,
202-
}
203-
}
204-
write!(fmt, "> {{ {:?} }}", subgoal.value)
205-
}
241+
GoalData::Quantified(qkind, ref subgoal) => write!(
242+
fmt,
243+
"{:?}{:?} {{ {:?} }}",
244+
qkind,
245+
subgoal.binders.debug(),
246+
subgoal.value
247+
),
206248
GoalData::Implies(ref wc, ref g) => write!(fmt, "if ({:?}) {{ {:?} }}", wc, g),
207249
GoalData::All(ref goals) => write!(fmt, "all{:?}", goals),
208250
GoalData::Not(ref g) => write!(fmt, "not {{ {:?} }}", g),
@@ -511,30 +553,13 @@ impl<I: Interner> Debug for EqGoal<I> {
511553
}
512554
}
513555

514-
impl<T: Debug> Debug for Binders<T> {
556+
impl<I: Interner, T: Debug> Debug for Binders<I, T> {
515557
fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
516558
let Binders {
517559
ref binders,
518560
ref value,
519561
} = *self;
520-
521-
// NB: We always print the `for<>`, even if it is empty,
522-
// because it may affect the debruijn indices of things
523-
// contained within. For example, `for<> { ^1.0 }` is very
524-
// different from `^1.0` in terms of what variable is being
525-
// referenced.
526-
527-
write!(fmt, "for<")?;
528-
for (index, binder) in binders.iter().enumerate() {
529-
if index > 0 {
530-
write!(fmt, ", ")?;
531-
}
532-
match *binder {
533-
ParameterKind::Ty(()) => write!(fmt, "type")?,
534-
ParameterKind::Lifetime(()) => write!(fmt, "lifetime")?,
535-
}
536-
}
537-
write!(fmt, "> ")?;
562+
write!(fmt, "for{:?} ", binders.debug())?;
538563
Debug::fmt(value, fmt)
539564
}
540565
}
@@ -570,13 +595,15 @@ impl<I: Interner, T: Display> Canonical<I, T> {
570595
}
571596
}
572597

573-
pub trait DisplayWithInterner<I: Interner> {
574-
fn fmt_with_interner(&self, interner: &I, f: &mut Formatter<'_>) -> Result<(), Error>;
598+
pub struct CanonicalDisplay<'a, I: Interner, T> {
599+
canonical: &'a Canonical<I, T>,
600+
interner: &'a I,
575601
}
576602

577-
impl<I: Interner, T: Display> DisplayWithInterner<I> for Canonical<I, T>{
578-
fn fmt_with_interner(&self, interner: &I, f: &mut Formatter<'_>) -> Result<(), Error> {
579-
let Canonical { binders, value } = self;
603+
impl<'a, I: Interner, T: Display> Display for CanonicalDisplay<'a, I, T> {
604+
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> {
605+
let Canonical { binders, value } = self.canonical;
606+
let interner = self.interner;
580607
let binders = binders.as_slice(interner);
581608
if binders.is_empty() {
582609
// Ordinarily, we try to print all binder levels, if they
@@ -603,17 +630,6 @@ impl<I: Interner, T: Display> DisplayWithInterner<I> for Canonical<I, T>{
603630
}
604631
}
605632

606-
pub struct CanonicalDisplay<'a, I: Interner, T> {
607-
canonical: &'a Canonical<I, T>,
608-
interner: &'a I,
609-
}
610-
611-
impl<'a, I: Interner, T: Display> Display for CanonicalDisplay<'a, I, T> {
612-
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> {
613-
DisplayWithInterner::fmt_with_interner(self.canonical, self.interner, f)
614-
}
615-
}
616-
617633
impl<T: Debug, L: Debug> Debug for ParameterKind<T, L> {
618634
fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
619635
match *self {

0 commit comments

Comments
 (0)