Check that non-overwrite accesses to downcast projections are dominated by variant checks. #59578
Labels
A-codegen
Area: Code generation
A-mir
Area: Mid-level IR (MIR) - https://blog.rust-lang.org/2016/04/19/MIR.html
C-enhancement
Category: An issue proposing an enhancement or a PR with one.
T-compiler
Relevant to the compiler team, which will review and decide on the PR/issue.
Pattern-matching in Rust, e.g.:
turns into this MIR (slightly cleaned up):
We already have a dominator tree for MIR, so we can build on top of that and compute the known variants for places (in this case,
Ok
andErr
for_1
).Then we can just check that any read/borrow/etc. access (any access with does not fully overwrite the previous value, really) within a downcast (e.g.
(_1 as Ok).0
) is dominated by a variant check for that variant (i.e._1
beingOk
, via_2
beingdiscriminant(_1)
).That said, the kind of dataflow borrowck already needs to do might easily include this too (e.g. treating
(_1 as Ok)
as initialized iff_1
is initialized anddiscriminant(_1) == 0
was checked).(Also tempting: moving
Discriminant
intoOperand
to be able to get rid of the_2
and haveswitchInt(discriminant(_1))
directly)cc @rust-lang/wg-compiler-nll @oli
The text was updated successfully, but these errors were encountered: