Skip to content

Commit 3f10dc7

Browse files
authored
Rollup merge of rust-lang#120128 - oli-obk:smir_internal_lift, r=celinval
Make stable_mir::with_tables sound See the first commit for the actual soundness fix. The rest is just fallout from that and is entirely safe code. Includes most of rust-lang#120120 The major difference to rust-lang#120120 is that we don't need an unsafe trait, as we can now rely on the type system (the only unsafe part, and the actual source of the unsoundness was in `with_tables`) r? `@celinval`
2 parents 9652b15 + 6cd6539 commit 3f10dc7

File tree

14 files changed

+497
-378
lines changed

14 files changed

+497
-378
lines changed

compiler/rustc_arena/src/lib.rs

+13
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,19 @@ impl DroplessArena {
484484
}
485485
}
486486

487+
/// Used by `Lift` to check whether this slice is allocated
488+
/// in this arena.
489+
#[inline]
490+
pub fn contains_slice<T>(&self, slice: &[T]) -> bool {
491+
for chunk in self.chunks.borrow_mut().iter_mut() {
492+
let ptr = slice.as_ptr().cast::<u8>().cast_mut();
493+
if chunk.start() <= ptr && chunk.end() >= ptr {
494+
return true;
495+
}
496+
}
497+
false
498+
}
499+
487500
/// Allocates a string slice that is copied into the `DroplessArena`, returning a
488501
/// reference to it. Will panic if passed an empty string.
489502
///

compiler/rustc_middle/src/mir/consts.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ impl<'tcx> ConstValue<'tcx> {
195195
/// Constants
196196
197197
#[derive(Clone, Copy, PartialEq, Eq, TyEncodable, TyDecodable, Hash, HashStable, Debug)]
198-
#[derive(TypeFoldable, TypeVisitable)]
198+
#[derive(TypeFoldable, TypeVisitable, Lift)]
199199
pub enum Const<'tcx> {
200200
/// This constant came from the type system.
201201
///
@@ -456,7 +456,7 @@ impl<'tcx> Const<'tcx> {
456456

457457
/// An unevaluated (potentially generic) constant used in MIR.
458458
#[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord, TyEncodable, TyDecodable)]
459-
#[derive(Hash, HashStable, TypeFoldable, TypeVisitable)]
459+
#[derive(Hash, HashStable, TypeFoldable, TypeVisitable, Lift)]
460460
pub struct UnevaluatedConst<'tcx> {
461461
pub def: DefId,
462462
pub args: GenericArgsRef<'tcx>,

compiler/rustc_middle/src/ty/context.rs

+22-1
Original file line numberDiff line numberDiff line change
@@ -1416,6 +1416,7 @@ nop_lift! {const_; Const<'a> => Const<'tcx>}
14161416
nop_lift! {const_allocation; ConstAllocation<'a> => ConstAllocation<'tcx>}
14171417
nop_lift! {predicate; Predicate<'a> => Predicate<'tcx>}
14181418
nop_lift! {predicate; Clause<'a> => Clause<'tcx>}
1419+
nop_lift! {layout; Layout<'a> => Layout<'tcx>}
14191420

14201421
nop_list_lift! {type_lists; Ty<'a> => Ty<'tcx>}
14211422
nop_list_lift! {poly_existential_predicates; PolyExistentialPredicate<'a> => PolyExistentialPredicate<'tcx>}
@@ -1424,8 +1425,28 @@ nop_list_lift! {bound_variable_kinds; ty::BoundVariableKind => ty::BoundVariable
14241425
// This is the impl for `&'a GenericArgs<'a>`.
14251426
nop_list_lift! {args; GenericArg<'a> => GenericArg<'tcx>}
14261427

1428+
macro_rules! nop_slice_lift {
1429+
($ty:ty => $lifted:ty) => {
1430+
impl<'a, 'tcx> Lift<'tcx> for &'a [$ty] {
1431+
type Lifted = &'tcx [$lifted];
1432+
fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
1433+
if self.is_empty() {
1434+
return Some(&[]);
1435+
}
1436+
tcx.interners
1437+
.arena
1438+
.dropless
1439+
.contains_slice(self)
1440+
.then(|| unsafe { mem::transmute(self) })
1441+
}
1442+
}
1443+
};
1444+
}
1445+
1446+
nop_slice_lift! {ty::ValTree<'a> => ty::ValTree<'tcx>}
1447+
14271448
TrivialLiftImpls! {
1428-
ImplPolarity,
1449+
ImplPolarity, Promoted
14291450
}
14301451

14311452
macro_rules! sty_debug_print {

0 commit comments

Comments
 (0)