Skip to content

Promotion creates invalid constants that are never used in invalid ways #67534

Closed
@oli-obk

Description

@oli-obk

Namely, we can have promoteds with interior mutable types but in an immutable allocation.
#67000 makes this explicit by introducing the ignore_interior_mut_in_const_validation field on the const interner.

We should instead rewrite such promoteds to just contain the parts of the promoted that is accessed. So

let x: &'static i32 = &[(Cell::new(42), 1), Cell::new(42), 2)][runtime_index].1;

currently produces

const PROMOTED: &[(Cell<i32>, i32); 2] = &[(Cell::new(42), 1), Cell::new(42), 2)];
let x: &'static i32 = &PROMOTED[runtime_index].1;

And we should be producing

const PROMOTED: &[i32; 2] = &[1, 2];
let x: &'static i32 = &PROMOTED[runtime_index];

This is definitely a nontrivial transformation, especially since it needs to be done on the generic promoted and not the computed final value (generic e.g. if we had T::CELL_VALUE instead of Cell::new(42)).
The reason it needs to be done on the MIR is that we also need to change the projection for obtaining the value of x, which we have no control over if we did it on the final constant.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-const-evalArea: Constant evaluation, covers all const contexts (static, const fn, ...)C-bugCategory: This is a bug.I-unsoundIssue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/SoundnessP-highHigh priorityT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions