Repository navigation
ReferencePropagation looks through field projections to find reborrow chains, but not when replacing #163777
Copy link
Copy link
Closed
Labels
A-mir-optArea: MIR optimizationsArea: MIR optimizationsC-bugCategory: This is a bug.Category: This is a bug.I-miscompileIssue: Correct Rust code lowers to incorrect machine codeIssue: Correct Rust code lowers to incorrect machine codeP-highHigh priorityHigh priorityT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.
Description
Activity
- addedT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.C-bugCategory: This is a bug.Category: This is a bug.A-mir-optArea: MIR optimizationsArea: MIR optimizationsI-miscompileIssue: Correct Rust code lowers to incorrect machine codeIssue: Correct Rust code lowers to incorrect machine code
on Oct 5, 2026 - addedneeds-triageThis issue may need triage. Remove when done. See docs forge.rust-lang.org/release/issue-triagingThis issue may need triage. Remove when done. See docs forge.rust-lang.org/release/issue-triagingI-prioritizeIssue needs a team member to assess the impact. Will be replaced by P-{low,medium,high,critical}Issue needs a team member to assess the impact. Will be replaced by P-{low,medium,high,critical}
on Oct 5, 2026 This might be a duplicate of #132898 ?
- removedI-prioritizeIssue needs a team member to assess the impact. Will be replaced by P-{low,medium,high,critical}Issue needs a team member to assess the impact. Will be replaced by P-{low,medium,high,critical}
on Oct 5, 2026 Assigning P-high (discussion on Zulip).
- removedneeds-triageThis issue may need triage. Remove when done. See docs forge.rust-lang.org/release/issue-triagingThis issue may need triage. Remove when done. See docs forge.rust-lang.org/release/issue-triaging
on Oct 5, 2026 - linked a pull request that will close this issuecheck is_indirect_first_projection when replacing in RefProp #163778
on Oct 6, 2026 - added 4 commits that reference this issue
on Oct 6, 2026 - added a commit that references this issue
on Oct 8, 2026 - added a commit that references this issue
on Oct 10, 2026
Metadata
Metadata
Assignees
Labels
A-mir-optArea: MIR optimizationsArea: MIR optimizationsC-bugCategory: This is a bug.Category: This is a bug.I-miscompileIssue: Correct Rust code lowers to incorrect machine codeIssue: Correct Rust code lowers to incorrect machine codeP-highHigh priorityHigh priorityT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.
When run with Miri this program runs fine, but when ReferencePropagation is enabled, it reports UB:
The transformation is:
fn maybe_dead_field_reborrow(_1: bool) -> i32 { let mut _0: i32; let mut _2: (i32, i32); let mut _3: &mut (i32, i32); let mut _4: &mut i32; bb0: { StorageLive(_2); _2 = (const 0_i32, const 0_i32); _3 = &mut _2; - ((*_3).0: i32) = const 1_i32; - _4 = &mut ((*_3).0: i32); + (_2.0: i32) = const 1_i32; switchInt(copy _1) -> [1: bb1, otherwise: bb2]; } bb1: { StorageDead(_2); goto -> bb2; } bb2: { - _0 = copy (*_4); + _0 = copy ((*_3).0: i32); return; } }This transformation is wrong because we create a reference to the field, then mutate the field directly, then try to read through the reference. The write invalidates the reference.
I'm not sure why I've never managed to provoke this with Rustlantis. I found this with an LLM, it's clearly inspired by this existing test case:
rust/tests/mir-opt/reference_prop.rs
Lines 735 to 778 in 260f1ac