Skip to content

Incorrect promotion for projection in a const #65727

Closed
@ecstatic-morse

Description

@ecstatic-morse

Consider the following code:

Playground

const _: &i32 = {
    let x = &(5, false).0;
    x
};

fn main() {
    let _: &'static i32 = &(5, false).0;
}

The borrow of the tuple field in main is promoted out along with the tuple itself in accordance with the rules for rvalue static promotion. However, in the const, the mechanism for promotion does not remove Drop/StorageDead for the tuple, causing x to dangle. On stable, this is only caught during const-evaluation and results in a const_err warning. If this warning is supressed, an ICE occurs.

error: internal compiler error: tried to intern dangling pointer
 --> src/main.rs:2:1
  |
2 | / const _: &i32 = {
3 | |     let x = &(5, false).0;
4 | |     x
5 | | };
  | |__^

thread 'rustc' panicked at 'no errors encountered even though `delay_span_bug` issued', src/librustc_errors/lib.rs:361:17
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace.

On nightly, the MIR borrow checker prevents this from happening, and this code is rejected instead. It should be accepted in all contexts.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-const-evalArea: Constant evaluation, covers all const contexts (static, const fn, ...)C-bugCategory: This is a bug.T-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