Skip to content

Commit 0c50db8

Browse files
committed
One universe many types
1 parent 895d534 commit 0c50db8

File tree

11 files changed

+50
-53
lines changed

11 files changed

+50
-53
lines changed

src/fold.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ pub trait UniversalFolder {
134134
///
135135
/// - `universe` is the universe of the `TypeName::ForAll` that was found
136136
/// - `binders` is the number of binders in scope
137-
fn fold_free_universal_ty(&mut self, universe: UniverseIndex, binders: usize) -> Fallible<Ty>;
137+
fn fold_free_universal_ty(&mut self, universe: UniversalIndex, binders: usize) -> Fallible<Ty>;
138138

139139
/// As with `fold_free_universal_ty`, but for lifetimes.
140140
fn fold_free_universal_lifetime(
@@ -150,8 +150,8 @@ pub trait UniversalFolder {
150150
pub trait IdentityUniversalFolder {}
151151

152152
impl<T: IdentityUniversalFolder> UniversalFolder for T {
153-
fn fold_free_universal_ty(&mut self, universe: UniverseIndex, _binders: usize) -> Fallible<Ty> {
154-
Ok(TypeName::ForAll(universe).to_ty())
153+
fn fold_free_universal_ty(&mut self, universe: UniversalIndex, _binders: usize) -> Fallible<Ty> {
154+
Ok(universe.to_ty())
155155
}
156156

157157
fn fold_free_universal_lifetime(

src/ir.rs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -138,21 +138,12 @@ pub enum TypeName {
138138
ItemId(ItemId),
139139

140140
/// skolemized form of a type parameter like `T`
141-
ForAll(UniverseIndex),
141+
ForAll(UniversalIndex),
142142

143143
/// an associated type like `Iterator::Item`; see `AssociatedType` for details
144144
AssociatedType(ItemId),
145145
}
146146

147-
impl TypeName {
148-
crate fn to_ty(self) -> Ty {
149-
Ty::Apply(ApplicationTy {
150-
name: self,
151-
parameters: vec![],
152-
})
153-
}
154-
}
155-
156147
/// An universe index is how a universally quantified parameter is
157148
/// represented when it's binder is moved into the environment.
158149
/// An example chain of transformations would be:
@@ -457,6 +448,13 @@ impl UniversalIndex {
457448
crate fn to_lifetime(self) -> Lifetime {
458449
Lifetime::ForAll(self)
459450
}
451+
452+
crate fn to_ty(self) -> Ty {
453+
Ty::Apply(ApplicationTy {
454+
name: TypeName::ForAll(self),
455+
parameters: vec![],
456+
})
457+
}
460458
}
461459

462460
#[derive(Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]

src/ir/debug.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ impl Debug for TypeName {
3737
fn fmt(&self, fmt: &mut Formatter) -> Result<(), Error> {
3838
match self {
3939
TypeName::ItemId(id) => write!(fmt, "{:?}", id),
40-
TypeName::ForAll(universe) => write!(fmt, "!{}", universe.counter),
40+
TypeName::ForAll(universe) => write!(fmt, "!{}_{}", universe.ui.counter, universe.idx),
4141
TypeName::AssociatedType(assoc_ty) => write!(fmt, "{:?}", assoc_ty),
4242
}
4343
}

src/ir/macros.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,5 +69,10 @@ macro_rules! lifetime {
6969
#[cfg(test)]
7070
macro_rules! ty_name {
7171
((item $n:expr)) => { ::ir::TypeName::ItemId(ItemId { index: $n }) };
72-
((skol $n:expr)) => { ::ir::TypeName::ForAll(UniverseIndex { counter: $n }) }
72+
((skol $n:expr)) => { ::ir::TypeName::ForAll(
73+
UniversalIndex {
74+
ui: UniverseIndex { counter: $n },
75+
idx: 0,
76+
})
77+
}
7378
}

src/solve/infer/canonicalize.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,9 @@ impl<'q> Canonicalizer<'q> {
9494
impl<'q> DefaultTypeFolder for Canonicalizer<'q> {}
9595

9696
impl<'q> UniversalFolder for Canonicalizer<'q> {
97-
fn fold_free_universal_ty(&mut self, universe: UniverseIndex, _binders: usize) -> Fallible<Ty> {
98-
self.max_universe = max(self.max_universe, universe);
99-
Ok(TypeName::ForAll(universe).to_ty())
97+
fn fold_free_universal_ty(&mut self, universe: UniversalIndex, _binders: usize) -> Fallible<Ty> {
98+
self.max_universe = max(self.max_universe, universe.ui);
99+
Ok(universe.to_ty())
100100
}
101101

102102
fn fold_free_universal_lifetime(

src/solve/infer/instantiate.rs

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -100,24 +100,18 @@ impl InferenceTable {
100100
T: Fold,
101101
{
102102
let (binders, value) = arg.split();
103-
let lifetime_ui = self.new_universe();
104-
let mut idx = 0;
103+
let ui = self.new_universe();
105104
let parameters: Vec<_> = binders
106105
.iter()
107-
.map(|pk| {
106+
.enumerate()
107+
.map(|(idx, pk)| {
108+
let universal_idx = UniversalIndex { ui, idx };
108109
match *pk {
109110
ParameterKind::Lifetime(()) => {
110-
let lt = Lifetime::ForAll(UniversalIndex { ui: lifetime_ui, idx });
111-
idx += 1;
111+
let lt = universal_idx.to_lifetime();
112112
ParameterKind::Lifetime(lt)
113113
}
114-
ParameterKind::Ty(()) => {
115-
let new_universe = self.new_universe();
116-
ParameterKind::Ty(Ty::Apply(ApplicationTy {
117-
name: TypeName::ForAll(new_universe),
118-
parameters: vec![],
119-
}))
120-
}
114+
ParameterKind::Ty(()) => ParameterKind::Ty(universal_idx.to_ty()),
121115
}
122116
})
123117
.collect();

src/solve/infer/invert.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ impl InferenceTable {
9797

9898
struct Inverter<'q> {
9999
table: &'q mut InferenceTable,
100-
inverted_ty: HashMap<UniverseIndex, InferenceVariable>,
100+
inverted_ty: HashMap<UniversalIndex, InferenceVariable>,
101101
inverted_lifetime: HashMap<UniversalIndex, InferenceVariable>,
102102
}
103103

@@ -114,12 +114,12 @@ impl<'q> Inverter<'q> {
114114
impl<'q> DefaultTypeFolder for Inverter<'q> {}
115115

116116
impl<'q> UniversalFolder for Inverter<'q> {
117-
fn fold_free_universal_ty(&mut self, universe: UniverseIndex, binders: usize) -> Fallible<Ty> {
117+
fn fold_free_universal_ty(&mut self, universe: UniversalIndex, binders: usize) -> Fallible<Ty> {
118118
let table = &mut self.table;
119119
Ok(
120120
self.inverted_ty
121121
.entry(universe)
122-
.or_insert_with(|| table.new_variable(universe))
122+
.or_insert_with(|| table.new_variable(universe.ui))
123123
.to_ty()
124124
.up_shift(binders),
125125
)

src/solve/infer/ucanonicalize.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -220,9 +220,9 @@ struct UCollector<'q> {
220220
impl<'q> DefaultTypeFolder for UCollector<'q> {}
221221

222222
impl<'q> UniversalFolder for UCollector<'q> {
223-
fn fold_free_universal_ty(&mut self, universe: UniverseIndex, _binders: usize) -> Fallible<Ty> {
224-
self.universes.add(universe);
225-
Ok(TypeName::ForAll(universe).to_ty())
223+
fn fold_free_universal_ty(&mut self, universe: UniversalIndex, _binders: usize) -> Fallible<Ty> {
224+
self.universes.add(universe.ui);
225+
Ok(universe.to_ty())
226226
}
227227

228228
fn fold_free_universal_lifetime(
@@ -246,11 +246,11 @@ impl<'q> DefaultTypeFolder for UMapToCanonical<'q> {}
246246
impl<'q> UniversalFolder for UMapToCanonical<'q> {
247247
fn fold_free_universal_ty(
248248
&mut self,
249-
universe0: UniverseIndex,
249+
universe0: UniversalIndex,
250250
_binders: usize,
251251
) -> Fallible<Ty> {
252-
let universe = self.universes.map_universe_to_canonical(universe0);
253-
Ok(TypeName::ForAll(universe).to_ty())
252+
let ui = self.universes.map_universe_to_canonical(universe0.ui);
253+
Ok(UniversalIndex { ui, idx: universe0.idx }.to_ty())
254254
}
255255

256256
fn fold_free_universal_lifetime(
@@ -274,11 +274,11 @@ impl<'q> DefaultTypeFolder for UMapFromCanonical<'q> {}
274274
impl<'q> UniversalFolder for UMapFromCanonical<'q> {
275275
fn fold_free_universal_ty(
276276
&mut self,
277-
universe0: UniverseIndex,
277+
universe0: UniversalIndex,
278278
_binders: usize,
279279
) -> Fallible<Ty> {
280-
let universe = self.universes.map_universe_from_canonical(universe0);
281-
Ok(TypeName::ForAll(universe).to_ty())
280+
let ui = self.universes.map_universe_from_canonical(universe0.ui);
281+
Ok(UniversalIndex { ui, idx: universe0.idx }.to_ty())
282282
}
283283

284284
fn fold_free_universal_lifetime(

src/solve/infer/unify.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -377,11 +377,11 @@ impl<'u, 't> OccursCheck<'u, 't> {
377377
impl<'u, 't> DefaultTypeFolder for OccursCheck<'u, 't> {}
378378

379379
impl<'u, 't> UniversalFolder for OccursCheck<'u, 't> {
380-
fn fold_free_universal_ty(&mut self, universe: UniverseIndex, _binders: usize) -> Fallible<Ty> {
381-
if self.universe_index < universe {
380+
fn fold_free_universal_ty(&mut self, universe: UniversalIndex, _binders: usize) -> Fallible<Ty> {
381+
if self.universe_index < universe.ui {
382382
Err(NoSolution)
383383
} else {
384-
Ok(TypeName::ForAll(universe).to_ty()) // no need to shift, not relative to depth
384+
Ok(universe.to_ty()) // no need to shift, not relative to depth
385385
}
386386
}
387387

src/solve/slg/test.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -483,7 +483,7 @@ fn subgoal_cycle_uninhabited() {
483483
Answer {
484484
subst: Canonical {
485485
value: ConstrainedSubst {
486-
subst: [?0 := !1],
486+
subst: [?0 := !1_0],
487487
constraints: []
488488
},
489489
binders: []

src/solve/test.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -476,7 +476,7 @@ fn normalize_basic() {
476476
}
477477
}
478478
} yields {
479-
"Unique; substitution [?0 := !1], lifetime constraints []"
479+
"Unique; substitution [?0 := !1_0], lifetime constraints []"
480480
}
481481

482482
goal {
@@ -506,7 +506,7 @@ fn normalize_basic() {
506506
}
507507
}
508508
} yields {
509-
"Unique; substitution [?0 := (Iterator::Item)<!1>]"
509+
"Unique; substitution [?0 := (Iterator::Item)<!1_0>]"
510510
}
511511

512512
goal {
@@ -518,7 +518,7 @@ fn normalize_basic() {
518518
}
519519
}
520520
} yields {
521-
"Unique; substitution [?0 := (Iterator::Item)<!1>]"
521+
"Unique; substitution [?0 := (Iterator::Item)<!1_0>]"
522522
}
523523

524524
goal {
@@ -581,7 +581,7 @@ fn normalize_gat1() {
581581
}
582582
}
583583
} yields {
584-
"Unique; substitution [?0 := Iter<'!2_0, !1>], lifetime constraints []"
584+
"Unique; substitution [?0 := Iter<'!2_0, !1_0>], lifetime constraints []"
585585
}
586586
}
587587
}
@@ -606,7 +606,7 @@ fn normalize_gat2() {
606606
}
607607
}
608608
} yields {
609-
"Unique; substitution [?0 := Span<'!1_0, !2>], lifetime constraints []"
609+
"Unique; substitution [?0 := Span<'!1_0, !1_1>], lifetime constraints []"
610610
}
611611

612612
goal {
@@ -664,7 +664,7 @@ fn normalize_gat_with_where_clause() {
664664
}
665665
}
666666
} yields {
667-
"Unique; substitution [?0 := Value<!1>]"
667+
"Unique; substitution [?0 := Value<!1_0>]"
668668
}
669669
}
670670
}
@@ -703,7 +703,7 @@ fn normalize_gat_with_where_clause2() {
703703
}
704704
}
705705
} yields {
706-
"Unique; substitution [?0 := !2]"
706+
"Unique; substitution [?0 := !1_1]"
707707
}
708708
}
709709
}

0 commit comments

Comments
 (0)