Change:
<ins> error[E0716]: temporary value dropped while borrowed --> $DIR/qualif-union.rs:27:26 | LL |     let _: &'static _ = &C1; |            ----------    ^^ creates a temporary which is freed while still in use |            | |            type annotation requires that borrow lasts for `'static` ... LL | } | - temporary value is freed at the end of this statement error[E0716]: temporary value dropped while borrowed --> $DIR/qualif-union.rs:28:26 | LL |     let _: &'static _ = &C2; |            ----------    ^^ creates a temporary which is freed while still in use |            | |            type annotation requires that borrow lasts for `'static` ... LL | } | - temporary value is freed at the end of this statement error[E0716]: temporary value dropped while borrowed --> $DIR/qualif-union.rs:29:26 | LL |     let _: &'static _ = &C3; |            ----------    ^^ creates a temporary which is freed while still in use |            | |            type annotation requires that borrow lasts for `'static` ... LL | } | - temporary value is freed at the end of this statement error[E0716]: temporary value dropped while borrowed --> $DIR/qualif-union.rs:30:26 | LL |     let _: &'static _ = &C4; |            ----------    ^^ creates a temporary which is freed while still in use |            | |            type annotation requires that borrow lasts for `'static` LL |     let _: &'static _ = &C5; LL | } | - temporary value is freed at the end of this statement error[E0716]: temporary value dropped while borrowed --> $DIR/qualif-union.rs:31:26 | LL |     let _: &'static _ = &C5; |            ----------    ^^ creates a temporary which is freed while still in use |            | |            type annotation requires that borrow lasts for `'static` LL | } | - temporary value is freed at the end of this statement error: aborting due to 5 previous errors For more information about this error, try `rustc --explain E0716`. </ins>

Discussion:
For example, after laundering interior mutability through a union, a resulting constant undergoes promotion: label +A-const-eval +F-untagged_unions
Yea, unions should not be "more powerful" than a plain transmute imo. cc
I assume with transmute one can also produce a similar example as the OP? So in which sense are unions more powerful than transmute, Why does this example behave any different than the following?
The union local initially starts without interior mutability qualif. This persist after the first assignment, since u32 does not have interior mutability The move from the cell variant is qualified based on the qualification of the local, so the overall constant is consider not to have interior mutability. The use of the constant in the main function is promoted and fails dynamic checks.
I totally forgot promotion of looks into the definition of ... that'll continue to haunt us for a while I think. :/ Yeah, the analysis clearly is wrong for reading from union fields. But even for values like , I have no idea what the wider consequences are (given that the type is considered interior mutable).
The example can be also modified by moving union access to the runtime code, in which case it will compile successfully, with promoted being placed in .rodata section: In the context of static analysis, I think it would make sense to use strictly type based analysis for unions, in which case the first assignment to a union local would already qualify the local as having interior mutability.
But also, there's supposed to be a 2nd line of defense in our interning code that is somehow failing.
Turns out interning relies in validity checking for this: The example in the OP was caught by validity, so that is good. But the 2nd example was not caught, because we never actually hit an UnsafeCell -- we stop at the union boundary. So maybe the union part of the visitor should also have the kind of ?
do we have an established approach to testing that this second line of defense works? Carefully crafted constant with undefined behaviour might do its job in this case (not sure yet), but maybe we have better alternatives?
We have -Zunleash-the-miri-inside-of-you but that only helps for checks, not for promoted restrictions... I don't think we have anything for promoteds (and it seems non-trivial, we would need a way to force something to be promoted despite it violating the usual conditions).