Skip to content

promote_consts experiment: do not promote !Freeze shared references #142287

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion compiler/rustc_mir_transform/src/promote_consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,19 @@ impl<'tcx> Validator<'_, 'tcx> {
}

BorrowKind::Shared => {
let has_mut_interior = self.qualif_local::<qualifs::HasMutInterior>(place.local);
// 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_mut_interior = match ty.kind() {
// Empty arrays have no interior mutability no matter their element type.
ty::Array(_elem, count)
if count.try_to_target_usize(self.tcx).is_some_and(|v| v == 0) =>
{
false
}
// Fallback to checking `Freeze`.
_ => !ty.is_freeze(self.tcx, self.typing_env),
};
if has_mut_interior {
return Err(Unpromotable);
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_pattern_analysis/src/pat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ impl<'p, Cx: PatCx> PatOrWild<'p, Cx> {
}
pub(crate) fn ctor(self) -> &'p Constructor<Cx> {
match self {
PatOrWild::Wild => &Wildcard,
PatOrWild::Wild => const { &Wildcard },
Copy link
Member Author

@RalfJung RalfJung Jun 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not even the compiler builds without some changes.^^

What happens here is that by making the const expression &Wildcard, the lifetime extension rules kick in, and those still do value-based reasoning and therefore allow this constant without ever involving promotion.

PatOrWild::Pat(pat) => pat.ctor(),
}
}
Expand Down
Loading