Skip to content

Commit 3ef27d8

Browse files
committed
allow canonicalized regions to carry universe and track max-universe
But.. we don't really use it for anything right now.
1 parent 784746f commit 3ef27d8

File tree

8 files changed

+61
-41
lines changed

8 files changed

+61
-41
lines changed

src/librustc/ich/impls_ty.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1268,7 +1268,7 @@ for traits::VtableGeneratorData<'gcx, N> where N: HashStable<StableHashingContex
12681268

12691269
impl_stable_hash_for!(
12701270
impl<'tcx, V> for struct infer::canonical::Canonical<'tcx, V> {
1271-
variables, value
1271+
max_universe, variables, value
12721272
}
12731273
);
12741274

@@ -1284,7 +1284,7 @@ impl_stable_hash_for!(struct infer::canonical::CanonicalVarInfo {
12841284

12851285
impl_stable_hash_for!(enum infer::canonical::CanonicalVarKind {
12861286
Ty(k),
1287-
Region
1287+
Region(ui),
12881288
});
12891289

12901290
impl_stable_hash_for!(enum infer::canonical::CanonicalTyVarKind {

src/librustc/infer/canonical/canonicalizer.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,8 @@ impl<'cx, 'gcx, 'tcx> TypeFolder<'gcx, 'tcx> for Canonicalizer<'cx, 'gcx, 'tcx>
261261
| ty::ReScope(_)
262262
| ty::RePlaceholder(..)
263263
| ty::ReEmpty
264-
| ty::ReErased => self.canonicalize_region_mode.canonicalize_free_region(self, r),
264+
| ty::ReErased => self.canonicalize_region_mode
265+
.canonicalize_free_region(self, r),
265266

266267
ty::ReClosureBound(..) | ty::ReCanonical(_) => {
267268
bug!("canonical region encountered during canonicalization")
@@ -353,6 +354,7 @@ impl<'cx, 'gcx, 'tcx> Canonicalizer<'cx, 'gcx, 'tcx> {
353354
if !value.has_type_flags(needs_canonical_flags) {
354355
let out_value = gcx.lift(value).unwrap();
355356
let canon_value = Canonical {
357+
max_universe: ty::UniverseIndex::ROOT,
356358
variables: List::empty(),
357359
value: out_value,
358360
};
@@ -383,7 +385,14 @@ impl<'cx, 'gcx, 'tcx> Canonicalizer<'cx, 'gcx, 'tcx> {
383385

384386
let canonical_variables = tcx.intern_canonical_var_infos(&canonicalizer.variables);
385387

388+
let max_universe = canonical_variables
389+
.iter()
390+
.map(|cvar| cvar.universe())
391+
.max()
392+
.unwrap_or(ty::UniverseIndex::ROOT);
393+
386394
Canonical {
395+
max_universe,
387396
variables: canonical_variables,
388397
value: out_value,
389398
}
@@ -451,8 +460,10 @@ impl<'cx, 'gcx, 'tcx> Canonicalizer<'cx, 'gcx, 'tcx> {
451460
}
452461

453462
fn canonical_var_for_region(&mut self, r: ty::Region<'tcx>) -> ty::Region<'tcx> {
463+
// TODO: root is not always what we want here, but we'll
464+
// address that in a later commit.
454465
let info = CanonicalVarInfo {
455-
kind: CanonicalVarKind::Region,
466+
kind: CanonicalVarKind::Region(ty::UniverseIndex::ROOT),
456467
};
457468
let b = self.canonical_var(info, r.into());
458469
debug_assert_eq!(ty::INNERMOST, b.level);

src/librustc/infer/canonical/mod.rs

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ mod substitute;
5353
/// numbered starting from 0 in order of first appearance.
5454
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, RustcDecodable, RustcEncodable)]
5555
pub struct Canonical<'gcx, V> {
56+
pub max_universe: ty::UniverseIndex,
5657
pub variables: CanonicalVarInfos<'gcx>,
5758
pub value: V,
5859
}
@@ -95,6 +96,12 @@ pub struct CanonicalVarInfo {
9596
pub kind: CanonicalVarKind,
9697
}
9798

99+
impl CanonicalVarInfo {
100+
pub fn universe(self) -> ty::UniverseIndex {
101+
self.kind.universe()
102+
}
103+
}
104+
98105
/// Describes the "kind" of the canonical variable. This is a "kind"
99106
/// in the type-theory sense of the term -- i.e., a "meta" type system
100107
/// that analyzes type-like values.
@@ -104,7 +111,22 @@ pub enum CanonicalVarKind {
104111
Ty(CanonicalTyVarKind),
105112

106113
/// Region variable `'?R`.
107-
Region,
114+
Region(ty::UniverseIndex),
115+
}
116+
117+
118+
impl CanonicalVarKind {
119+
pub fn universe(self) -> ty::UniverseIndex {
120+
match self {
121+
// At present, we don't support higher-ranked
122+
// quantification over types, so all type variables are in
123+
// the root universe.
124+
CanonicalVarKind::Ty(_) => ty::UniverseIndex::ROOT,
125+
126+
// Region variables can be created in sub-universes.
127+
CanonicalVarKind::Region(ui) => ui,
128+
}
129+
}
108130
}
109131

110132
/// Rust actually has more than one category of type variables;
@@ -220,8 +242,8 @@ impl<'gcx, V> Canonical<'gcx, V> {
220242
/// let b: Canonical<'tcx, (T, Ty<'tcx>)> = a.unchecked_map(|v| (v, ty));
221243
/// ```
222244
pub fn unchecked_map<W>(self, map_op: impl FnOnce(V) -> W) -> Canonical<'gcx, W> {
223-
let Canonical { variables, value } = self;
224-
Canonical { variables, value: map_op(value) }
245+
let Canonical { max_universe, variables, value } = self;
246+
Canonical { max_universe, variables, value: map_op(value) }
225247
}
226248
}
227249

@@ -293,8 +315,8 @@ impl<'cx, 'gcx, 'tcx> InferCtxt<'cx, 'gcx, 'tcx> {
293315
ty.into()
294316
}
295317

296-
CanonicalVarKind::Region => self
297-
.next_region_var(RegionVariableOrigin::MiscVariable(span))
318+
CanonicalVarKind::Region(ui) => self
319+
.next_region_var_in_universe(RegionVariableOrigin::MiscVariable(span), ui)
298320
.into(),
299321
}
300322
}
@@ -314,6 +336,7 @@ CloneTypeFoldableImpls! {
314336

315337
BraceStructTypeFoldableImpl! {
316338
impl<'tcx, C> TypeFoldable<'tcx> for Canonical<'tcx, C> {
339+
max_universe,
317340
variables,
318341
value,
319342
} where C: TypeFoldable<'tcx>
@@ -322,7 +345,7 @@ BraceStructTypeFoldableImpl! {
322345
BraceStructLiftImpl! {
323346
impl<'a, 'tcx, T> Lift<'tcx> for Canonical<'a, T> {
324347
type Lifted = Canonical<'tcx, T::Lifted>;
325-
variables, value
348+
max_universe, variables, value
326349
} where T: Lift<'tcx>
327350
}
328351

src/librustc/traits/query/type_op/implied_outlives_bounds.rs

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -38,19 +38,13 @@ impl<'gcx: 'tcx, 'tcx> super::QueryTypeOp<'gcx, 'tcx> for ImpliedOutlivesBounds<
3838
tcx: TyCtxt<'_, 'gcx, 'tcx>,
3939
canonicalized: Canonicalized<'gcx, ParamEnvAnd<'tcx, Self>>,
4040
) -> Fallible<CanonicalizedQueryResponse<'gcx, Self::QueryResponse>> {
41-
// FIXME the query should take a `ImpliedOutlivesBounds`
42-
let Canonical {
43-
variables,
44-
value:
45-
ParamEnvAnd {
46-
param_env,
47-
value: ImpliedOutlivesBounds { ty },
48-
},
49-
} = canonicalized;
50-
let canonicalized = Canonical {
51-
variables,
52-
value: param_env.and(ty),
53-
};
41+
// FIXME this `unchecked_map` is only necessary because the
42+
// query is defined as taking a `ParamEnvAnd<Ty>`; it should
43+
// take a `ImpliedOutlivesBounds` instead
44+
let canonicalized = canonicalized.unchecked_map(|ParamEnvAnd { param_env, value }| {
45+
let ImpliedOutlivesBounds { ty } = value;
46+
param_env.and(ty)
47+
});
5448

5549
tcx.implied_outlives_bounds(canonicalized)
5650
}

src/librustc/traits/query/type_op/outlives.rs

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -59,18 +59,10 @@ where
5959
// FIXME convert to the type expected by the `dropck_outlives`
6060
// query. This should eventually be fixed by changing the
6161
// *underlying query*.
62-
let Canonical {
63-
variables,
64-
value:
65-
ParamEnvAnd {
66-
param_env,
67-
value: DropckOutlives { dropped_ty },
68-
},
69-
} = canonicalized;
70-
let canonicalized = Canonical {
71-
variables,
72-
value: param_env.and(dropped_ty),
73-
};
62+
let canonicalized = canonicalized.unchecked_map(|ParamEnvAnd { param_env, value }| {
63+
let DropckOutlives { dropped_ty } = value;
64+
param_env.and(dropped_ty)
65+
});
7466

7567
tcx.dropck_outlives(canonicalized)
7668
}

src/test/mir-opt/basic_assignment.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ fn main() {
5656
// StorageLive(_4);
5757
// _4 = std::option::Option<std::boxed::Box<u32>>::None;
5858
// FakeRead(ForLet, _4);
59-
// AscribeUserType(_4, o, UserTypeProjection { base: Ty(Canonical { variables: [], value: std::option::Option<std::boxed::Box<u32>> }), projs: [] });
59+
// AscribeUserType(_4, o, UserTypeProjection { base: Ty(Canonical { max_universe: U0, variables: [], value: std::option::Option<std::boxed::Box<u32>> }), projs: [] });
6060
// StorageLive(_5);
6161
// StorageLive(_6);
6262
// _6 = move _4;

src/test/ui/nll/user-annotations/dump-adt-brace-struct.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error: user substs: Canonical { variables: [], value: UserSubsts { substs: [u32], user_self_ty: None } }
1+
error: user substs: Canonical { max_universe: U0, variables: [], value: UserSubsts { substs: [u32], user_self_ty: None } }
22
--> $DIR/dump-adt-brace-struct.rs:28:5
33
|
44
LL | SomeStruct::<u32> { t: 22 }; //~ ERROR [u32]

src/test/ui/nll/user-annotations/dump-fn-method.stderr

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
1-
error: user substs: Canonical { variables: [], value: UserSubsts { substs: [u32], user_self_ty: None } }
1+
error: user substs: Canonical { max_universe: U0, variables: [], value: UserSubsts { substs: [u32], user_self_ty: None } }
22
--> $DIR/dump-fn-method.rs:36:13
33
|
44
LL | let x = foo::<u32>; //~ ERROR [u32]
55
| ^^^^^^^^^^
66

7-
error: user substs: Canonical { variables: [CanonicalVarInfo { kind: Ty(General) }, CanonicalVarInfo { kind: Ty(General) }], value: UserSubsts { substs: [?0, u32, ?1], user_self_ty: None } }
7+
error: user substs: Canonical { max_universe: U0, variables: [CanonicalVarInfo { kind: Ty(General) }, CanonicalVarInfo { kind: Ty(General) }], value: UserSubsts { substs: [?0, u32, ?1], user_self_ty: None } }
88
--> $DIR/dump-fn-method.rs:42:13
99
|
1010
LL | let x = <_ as Bazoom<u32>>::method::<_>; //~ ERROR [?0, u32, ?1]
1111
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1212

13-
error: user substs: Canonical { variables: [], value: UserSubsts { substs: [u8, u16, u32], user_self_ty: None } }
13+
error: user substs: Canonical { max_universe: U0, variables: [], value: UserSubsts { substs: [u8, u16, u32], user_self_ty: None } }
1414
--> $DIR/dump-fn-method.rs:46:13
1515
|
1616
LL | let x = <u8 as Bazoom<u16>>::method::<u32>; //~ ERROR [u8, u16, u32]
1717
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1818

19-
error: user substs: Canonical { variables: [CanonicalVarInfo { kind: Ty(General) }, CanonicalVarInfo { kind: Ty(General) }], value: UserSubsts { substs: [?0, ?1, u32], user_self_ty: None } }
19+
error: user substs: Canonical { max_universe: U0, variables: [CanonicalVarInfo { kind: Ty(General) }, CanonicalVarInfo { kind: Ty(General) }], value: UserSubsts { substs: [?0, ?1, u32], user_self_ty: None } }
2020
--> $DIR/dump-fn-method.rs:54:5
2121
|
2222
LL | y.method::<u32>(44, 66); //~ ERROR [?0, ?1, u32]

0 commit comments

Comments
 (0)