diff --git a/compiler/rustc_mir_transform/src/promote_consts.rs b/compiler/rustc_mir_transform/src/promote_consts.rs index 2e11da4d585ef..7aff68de22f92 100644 --- a/compiler/rustc_mir_transform/src/promote_consts.rs +++ b/compiler/rustc_mir_transform/src/promote_consts.rs @@ -393,6 +393,24 @@ impl<'tcx> Validator<'_, 'tcx> { if has_mut_interior { return Err(Unpromotable); } + // Let's just see what happens if we reject anything `!Freeze`... + // (Except ZST which definitely can't have interior mut) + let ty = place.ty(self.body, self.tcx).ty; + let has_interior_mut = match ty.kind() { + // Empty arrays have no interior mutability no matter their element type. + ty::Array(_elem, count) + if count + .try_eval_target_usize(self.tcx, self.param_env) + .is_some_and(|v| v == 0) => + { + false + } + // Fallback to checking `Freeze`. + _ => !ty.is_freeze(self.tcx, self.param_env), + }; + if has_interior_mut { + return Err(Unpromotable); + } } // FIXME: consider changing this to only promote &mut [] for default borrows,