Skip to content

Commit 901e6d5

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 + deba557 commit 901e6d5

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
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
}

compiler/rustc_pattern_analysis/src/pat.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ impl<'p, Cx: PatCx> PatOrWild<'p, Cx> {
192192
}
193193
pub(crate) fn ctor(self) -> &'p Constructor<Cx> {
194194
match self {
195-
PatOrWild::Wild => &Wildcard,
195+
PatOrWild::Wild => const { &Wildcard },
196196
PatOrWild::Pat(pat) => pat.ctor(),
197197
}
198198
}

0 commit comments

Comments
 (0)