Skip to content

Commit 51087fb

Browse files
Auto merge of #142287 - RalfJung:no-promote-im, r=<try>
promote_consts: do not promote !Freeze shared references This is a crater experiment to measure the fallout from not promoting shared references to `!Freeze` types. IOW, this switches promotion from value-based reasoning to type-based reasoning about interior mutability. Landing this PR would fix rust-lang/unsafe-code-guidelines#493 but I doubt we can get away with that.
2 parents 40daf23 + 19a8ea3 commit 51087fb

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

compiler/rustc_mir_transform/src/promote_consts.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,19 @@ impl<'tcx> Validator<'_, 'tcx> {
397397
}
398398

399399
BorrowKind::Shared => {
400-
let has_mut_interior = self.qualif_local::<qualifs::HasMutInterior>(place.local);
400+
// Let's just see what happens if we reject anything `!Freeze`...
401+
// (Except ZST which definitely can't have interior mut)
402+
let ty = place.ty(self.body, self.tcx).ty;
403+
let has_mut_interior = match ty.kind() {
404+
// Empty arrays have no interior mutability no matter their element type.
405+
ty::Array(_elem, count)
406+
if count.try_to_target_usize(self.tcx).is_some_and(|v| v == 0) =>
407+
{
408+
false
409+
}
410+
// Fallback to checking `Freeze`.
411+
_ => !ty.is_freeze(self.tcx, self.typing_env),
412+
};
401413
if has_mut_interior {
402414
return Err(Unpromotable);
403415
}

0 commit comments

Comments
 (0)