Skip to content

Commit 7705322

Browse files
authored
Rollup merge of #115907 - RalfJung:interner-check, r=compiler-errors
nop_lift macros: ensure that we are using the right interner Right now someone could put down the wrong list name when using these macros, and everything would still build. Nothing does a type-check to ensure that the `$set` contains element of type `Self::Lifted`. Let's fix that. For lists this is fairly easy; for the other interners we need to unwrap some newtypes which makes this more complicated.
2 parents 9c50187 + 3b817b2 commit 7705322

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

Diff for: compiler/rustc_middle/src/ty/context.rs

+24
Original file line numberDiff line numberDiff line change
@@ -1214,6 +1214,25 @@ macro_rules! nop_lift {
12141214
impl<'a, 'tcx> Lift<'tcx> for $ty {
12151215
type Lifted = $lifted;
12161216
fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
1217+
// Assert that the set has the right type.
1218+
// Given an argument that has an interned type, the return type has the type of
1219+
// the corresponding interner set. This won't actually return anything, we're
1220+
// just doing this to compute said type!
1221+
fn _intern_set_ty_from_interned_ty<'tcx, Inner>(
1222+
_x: Interned<'tcx, Inner>,
1223+
) -> InternedSet<'tcx, Inner> {
1224+
unreachable!()
1225+
}
1226+
fn _type_eq<T>(_x: &T, _y: &T) {}
1227+
fn _test<'tcx>(x: $lifted, tcx: TyCtxt<'tcx>) {
1228+
// If `x` is a newtype around an `Interned<T>`, then `interner` is an
1229+
// interner of appropriate type. (Ideally we'd also check that `x` is a
1230+
// newtype with just that one field. Not sure how to do that.)
1231+
let interner = _intern_set_ty_from_interned_ty(x.0);
1232+
// Now check that this is the same type as `interners.$set`.
1233+
_type_eq(&interner, &tcx.interners.$set);
1234+
}
1235+
12171236
tcx.interners
12181237
.$set
12191238
.contains_pointer_to(&InternedInSet(&*self.0.0))
@@ -1230,6 +1249,11 @@ macro_rules! nop_list_lift {
12301249
impl<'a, 'tcx> Lift<'tcx> for &'a List<$ty> {
12311250
type Lifted = &'tcx List<$lifted>;
12321251
fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
1252+
// Assert that the set has the right type.
1253+
if false {
1254+
let _x: &InternedSet<'tcx, List<$lifted>> = &tcx.interners.$set;
1255+
}
1256+
12331257
if self.is_empty() {
12341258
return Some(List::empty());
12351259
}

0 commit comments

Comments
 (0)