let _ = <access to unsafe field>
currently type-checks · Issue #54003 · rust-lang/rust (original) (raw)
The following example passes the unsafe checker, but probably should not:
// #![feature(nll)]
union Foo { x: u32, y: u64 }
fn main() { let foo = Foo { x: 22 }; let _ = foo.x; }
The problem here is that let _ = foo.x
is a no-op -- foo.x
is a place expression, and the _
pattern just plain ignores it. This means that no MIR is generated. The unsafe checker runs on MIR.
But I think that we expect an error here nonetheless.
This is a regression that was introduced in the move to doing unsafe checking on MIR. I'm not sure the best way to fix: I tend to think we should do unsafe checking on HAIR, but that would be a fairly large effort. We could certainly generate a "dummy access" as we do with matches. (But we want to be careful about borrow checker interactions -- see #53114).
I'm nominating this for lang-team discussion — we do want an unsafe error here, right?