Skip to content

ScalarReplacementOfAggregates doesn't handle ManuallyDrop/MaybeDangling #164176

Closed

Description

@saethlin
use std::mem::ManuallyDrop;
fn main() {
    let mut v = 0u8;
    let p = &raw mut v;
    let mut md = ManuallyDrop::new(unsafe { &mut *p });
    unsafe {
        ManuallyDrop::drop(&mut md);
        *p = 1;
    }
    let _keep = md;
}

Runs fine when run with:

RUSTFLAGS='-Zmir-enable-passes=+Inline' cargo +nightly miri run

But reports UB when run with:

RUSTFLAGS='-Zmir-enable-passes=+Inline,+ScalarReplacementOfAggregates' cargo +nightly miri run

Bisection points to #150446, of course. I think we were should have updated a lot of MIR optimizations to handle MaybeDangling, because they've been doing a lot more aliasing analysis than I thought.

Note that the change here with #150446 is not new UB, it's less. This program (with optimizations off) used to encounter UB and now it doesn't. We changed the semantics to permit some programs that were previously forbidden and MIR opts still assume those programs are forbidden.

Found by an LLM.

Activity

  1. added
    T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
    C-bugCategory: This is a bug.
    A-mir-optArea: MIR optimizations
    I-miscompileIssue: Correct Rust code lowers to incorrect machine code
    on Oct 11, 2026
  2. added
    needs-triageThis issue may need triage. Remove when done. See docs forge.rust-lang.org/release/issue-triaging
    I-prioritizeIssue needs a team member to assess the impact. Will be replaced by P-{low,medium,high,critical}
    on Oct 11, 2026
  3. apiraino commented on Oct 13, 2026

    @apiraino
    Contributor
  4. assigned and unassigned on Oct 19, 2026
  5. added a commit that references this issue on Oct 20, 2026
    cd2a75d
  6. added a commit that references this issue on Oct 20, 2026
    31e7937
  7. removed
    needs-triageThis issue may need triage. Remove when done. See docs forge.rust-lang.org/release/issue-triaging
    on Oct 20, 2026
  8. removed
    I-prioritizeIssue needs a team member to assess the impact. Will be replaced by P-{low,medium,high,critical}
    on Oct 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-mir-optArea: MIR optimizationsC-bugCategory: This is a bug.I-miscompileIssue: Correct Rust code lowers to incorrect machine codeT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions