PlaceMention-style UB not detected when in closure
use std::mem::transmute;
fn main() {
let r: &&u32 = unsafe {
let x = 42;
transmute(&&x)
};
// no UB detected
let f = || { let _ = **r; };
f();
// UB due to the inner deref
let _ = **r;
}
Found this when prompted by @Nadrieril:
In fact the other calls to
{try_}to_placein match MIR lowering are potential places where we mess things up. E.g. here, we want to add aPlaceMentionto the scrutinee which we can't do if it's not captured, could that have weird consequences maybe?
Now that https://github.com/rust-lang/rust/pull/138961 has landed... @Nadrieril is there some idea you have for making the two cases above behave consistently? Closure capturing and lowering is largely a black box to me, and this issue is more about what exact MIR gets built for the program than about what Miri does, I think.
The good news is that this means there also is no de-facto UB here as codegen sees the same MIR that Miri sees. I should find a way to reflect that in the labels...
Actually I think that's the correct behavior per the rules in the Reference. I filed an issue to the Reference.