Skip to content

Commit 775aa38

Browse files
committed
Switch Vec<ParameterKind<()>> to ParameterKinds<I>.
1 parent 43d059d commit 775aa38

File tree

27 files changed

+296
-161
lines changed

27 files changed

+296
-161
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: 9 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, HasInterner};
1415
pub use chalk_ir::interner::{Identifier, RawId};
1516
use chalk_ir::Binders;
1617

@@ -20,9 +21,16 @@ pub enum TypeSort {
2021
Trait,
2122
}
2223

24+
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
25+
pub struct Unit;
26+
27+
impl HasInterner for Unit {
28+
type Interner = ChalkIr;
29+
}
30+
2331
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
2432
pub struct TypeKind {
2533
pub sort: TypeSort,
2634
pub name: Identifier,
27-
pub binders: Binders<()>,
35+
pub binders: Binders<Unit>,
2836
}

chalk-integration/src/lowering.rs

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use chalk_ir::cast::{Cast, Caster};
2-
use chalk_ir::interner::ChalkIr;
2+
use chalk_ir::interner::{ChalkIr, HasInterner};
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;
@@ -165,11 +165,16 @@ impl<'k> Env<'k> {
165165
where
166166
I: IntoIterator<Item = chalk_ir::ParameterKind<Ident>>,
167167
I::IntoIter: ExactSizeIterator,
168+
T: HasInterner<Interner = ChalkIr>,
168169
OP: FnOnce(&Self) -> LowerResult<T>,
169170
{
171+
let interner = &ChalkIr;
170172
let binders: Vec<_> = binders.into_iter().collect();
171173
let env = self.introduce(binders.iter().cloned())?;
172-
Ok(chalk_ir::Binders::new(binders.anonymize(), op(&env)?))
174+
Ok(chalk_ir::Binders::new(
175+
ParameterKinds::from(interner, binders.anonymize()),
176+
op(&env)?,
177+
))
173178
}
174179
}
175180

@@ -522,10 +527,14 @@ trait LowerWhereClauses {
522527

523528
impl LowerTypeKind for StructDefn {
524529
fn lower_type_kind(&self) -> LowerResult<TypeKind> {
530+
let interner = &ChalkIr;
525531
Ok(TypeKind {
526532
sort: TypeSort::Struct,
527533
name: self.name.str,
528-
binders: chalk_ir::Binders::new(self.all_parameters().anonymize(), ()),
534+
binders: chalk_ir::Binders::new(
535+
ParameterKinds::from(interner, self.all_parameters().anonymize()),
536+
crate::Unit,
537+
),
529538
})
530539
}
531540
}
@@ -538,14 +547,15 @@ impl LowerWhereClauses for StructDefn {
538547

539548
impl LowerTypeKind for TraitDefn {
540549
fn lower_type_kind(&self) -> LowerResult<TypeKind> {
550+
let interner = &ChalkIr;
541551
let binders: Vec<_> = self.parameter_kinds.iter().map(|p| p.lower()).collect();
542552
Ok(TypeKind {
543553
sort: TypeSort::Trait,
544554
name: self.name.str,
545555
binders: chalk_ir::Binders::new(
546556
// for the purposes of the *type*, ignore `Self`:
547-
binders.anonymize(),
548-
(),
557+
ParameterKinds::from(interner, binders.anonymize()),
558+
crate::Unit,
549559
),
550560
})
551561
}
@@ -751,6 +761,7 @@ trait LowerTraitBound {
751761

752762
impl LowerTraitBound for TraitBound {
753763
fn lower(&self, env: &Env) -> LowerResult<rust_ir::TraitBound<ChalkIr>> {
764+
let interner = &ChalkIr;
754765
let trait_id = env.lookup_trait(self.trait_name)?;
755766

756767
let k = env.trait_kind(trait_id);
@@ -764,15 +775,15 @@ impl LowerTraitBound for TraitBound {
764775
.map(|a| Ok(a.lower(env)?))
765776
.collect::<LowerResult<Vec<_>>>()?;
766777

767-
if parameters.len() != k.binders.len() {
778+
if parameters.len() != k.binders.len(interner) {
768779
Err(RustIrError::IncorrectNumberOfTypeParameters {
769780
identifier: self.trait_name,
770-
expected: k.binders.len(),
781+
expected: k.binders.len(interner),
771782
actual: parameters.len(),
772783
})?;
773784
}
774785

775-
for (binder, param) in k.binders.binders.iter().zip(parameters.iter()) {
786+
for (binder, param) in k.binders.binders.iter(interner).zip(parameters.iter()) {
776787
if binder.kind() != param.kind() {
777788
Err(RustIrError::IncorrectTraitParameterKind {
778789
identifier: self.trait_name,
@@ -978,10 +989,10 @@ impl LowerTy for Ty {
978989
Ty::Id { name } => match env.lookup_type(name)? {
979990
TypeLookup::Struct(id) => {
980991
let k = env.struct_kind(id);
981-
if k.binders.len() > 0 {
992+
if k.binders.len(interner) > 0 {
982993
Err(RustIrError::IncorrectNumberOfTypeParameters {
983994
identifier: name,
984-
expected: k.binders.len(),
995+
expected: k.binders.len(interner),
985996
actual: 0,
986997
})
987998
} else {
@@ -1025,10 +1036,10 @@ impl LowerTy for Ty {
10251036
};
10261037

10271038
let k = env.struct_kind(id);
1028-
if k.binders.len() != args.len() {
1039+
if k.binders.len(interner) != args.len() {
10291040
Err(RustIrError::IncorrectNumberOfTypeParameters {
10301041
identifier: name,
1031-
expected: k.binders.len(),
1042+
expected: k.binders.len(interner),
10321043
actual: args.len(),
10331044
})?;
10341045
}
@@ -1038,7 +1049,7 @@ impl LowerTy for Ty {
10381049
args.iter().map(|t| Ok(t.lower(env)?)),
10391050
)?;
10401051

1041-
for (param, arg) in k.binders.binders.iter().zip(args.iter()) {
1052+
for (param, arg) in k.binders.binders.iter(interner).zip(args.iter()) {
10421053
if param.kind() != arg.kind() {
10431054
Err(RustIrError::IncorrectParameterKind {
10441055
identifier: name,
@@ -1267,14 +1278,16 @@ pub trait LowerGoal<A> {
12671278

12681279
impl LowerGoal<LoweredProgram> for Goal {
12691280
fn lower(&self, program: &LoweredProgram) -> LowerResult<chalk_ir::Goal<ChalkIr>> {
1281+
let interner = &ChalkIr;
12701282
let associated_ty_lookups: BTreeMap<_, _> = program
12711283
.associated_ty_data
12721284
.iter()
12731285
.map(|(&associated_ty_id, datum)| {
12741286
let trait_datum = &program.trait_data[&datum.trait_id];
1275-
let num_trait_params = trait_datum.binders.len();
1276-
let num_addl_params = datum.binders.len() - num_trait_params;
1277-
let addl_parameter_kinds = datum.binders.binders[..num_addl_params].to_owned();
1287+
let num_trait_params = trait_datum.binders.len(interner);
1288+
let num_addl_params = datum.binders.len(interner) - num_trait_params;
1289+
let addl_parameter_kinds =
1290+
datum.binders.binders.as_slice(interner)[..num_addl_params].to_owned();
12781291
let lookup = AssociatedTyLookup {
12791292
id: associated_ty_id,
12801293
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<I: Interner, T: HasInterner<Interner = I> + CastTo<Goal<I>>> CastTo<Goal<I>> for Binders<T> {
144144
fn cast_to(self, interner: &I) -> Goal<I> {
145145
GoalData::Quantified(
146146
QuantifierKind::ForAll,
@@ -194,10 +194,10 @@ where
194194
}
195195
}
196196

197-
impl<T, I> CastTo<ProgramClause<I>> for Binders<T>
197+
impl<I, T> CastTo<ProgramClause<I>> for Binders<T>
198198
where
199-
T: CastTo<DomainGoal<I>>,
200199
I: Interner,
200+
T: HasInterner<Interner = I> + CastTo<DomainGoal<I>>,
201201
{
202202
fn cast_to(self, interner: &I) -> ProgramClause<I> {
203203
ProgramClauseData::ForAll(self.map(|bound| ProgramClauseImplication {

chalk-ir/src/could_match.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ where
5050

5151
fn zip_binders<T>(&mut self, a: &Binders<T>, b: &Binders<T>) -> Fallible<()>
5252
where
53-
T: Zip<I>,
53+
T: HasInterner + Zip<I>,
5454
{
5555
Zip::zip_with(self, &a.value, &b.value)
5656
}

chalk-ir/src/debug.rs

Lines changed: 57 additions & 43 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<T: HasInterner + Debug> Debug for Binders<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
}
@@ -605,17 +630,6 @@ impl<'a, T: HasInterner + Display> Display for CanonicalDisplay<'a, T> {
605630
}
606631
}
607632

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

chalk-ir/src/fold/binder_impls.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ impl<I: Interner, TI: TargetInterner<I>> Fold<I, TI> for Fn<I> {
3030

3131
impl<T, I: Interner, TI: TargetInterner<I>> Fold<I, TI> for Binders<T>
3232
where
33-
T: Fold<I, TI>,
33+
T: HasInterner<Interner = I> + Fold<I, TI>,
34+
<T as Fold<I, TI>>::Result: HasInterner<Interner = TI>,
3435
I: Interner,
3536
{
3637
type Result = Binders<T::Result>;
@@ -48,10 +49,10 @@ where
4849
value: self_value,
4950
} = self;
5051
let value = self_value.fold_with(folder, outer_binder.shifted_in())?;
51-
Ok(Binders {
52-
binders: self_binders.clone(),
53-
value: value,
54-
})
52+
let binders = ParameterKinds {
53+
interned: TI::transfer_parameter_kinds(self_binders.interned().clone()),
54+
};
55+
Ok(Binders::new(binders, value))
5556
}
5657
}
5758

0 commit comments

Comments
 (0)