Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

failed to evaluate constant #67612

Closed
Mark-Simulacrum opened this issue Dec 25, 2019 · 3 comments · Fixed by #67630
Closed

failed to evaluate constant #67612

Mark-Simulacrum opened this issue Dec 25, 2019 · 3 comments · Fixed by #67630
Assignees
Labels
A-const-eval Area: Constant evaluation (MIR interpretation) P-high High priority regression-from-stable-to-beta Performance or correctness regression from stable to beta. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@Mark-Simulacrum
Copy link
Member

root: window - 2 (0 gh, 2 crates.io) detected crates which regressed due to this

cc @oli-obk @wesleywiser

@Mark-Simulacrum Mark-Simulacrum added T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. regression-from-stable-to-beta Performance or correctness regression from stable to beta. labels Dec 25, 2019
@jonas-schievink jonas-schievink added the A-const-eval Area: Constant evaluation (MIR interpretation) label Dec 25, 2019
@oli-obk
Copy link
Contributor

oli-obk commented Dec 26, 2019

:( static mut is being used there.

The regression is caused by #66587

@oli-obk
Copy link
Contributor

oli-obk commented Dec 26, 2019

Minimal repro:

extern "C" {
    static X: i32;
}

static mut FOO: *const &'static i32 = [unsafe { &X }].as_ptr();

the MIR is

static mut  FOO: *const &'static i32 = {
    let mut _0: *const &'static i32;     // return place in scope 0 at /home/oli/rustc/rust6/src/test/mir-opt/const-promotion-extern-static.rs:9:17: 9:36
    let mut _1: &[&'static i32];         // in scope 0 at /home/oli/rustc/rust6/src/test/mir-opt/const-promotion-extern-static.rs:9:39: 9:54
    let mut _2: &[&'static i32; 1];      // in scope 0 at /home/oli/rustc/rust6/src/test/mir-opt/const-promotion-extern-static.rs:9:39: 9:54
    let _3: [&'static i32; 1];           // in scope 0 at /home/oli/rustc/rust6/src/test/mir-opt/const-promotion-extern-static.rs:9:39: 9:54
    let mut _4: &'static i32;            // in scope 0 at /home/oli/rustc/rust6/src/test/mir-opt/const-promotion-extern-static.rs:9:40: 9:53
    let _5: *const i32;                  // in scope 0 at /home/oli/rustc/rust6/src/test/mir-opt/const-promotion-extern-static.rs:9:50: 9:51
    scope 1 {
    }

    bb0: {
        StorageLive(_1);                 // bb0[0]: scope 0 at /home/oli/rustc/rust6/src/test/mir-opt/const-promotion-extern-static.rs:9:39: 9:54
        StorageLive(_2);                 // bb0[1]: scope 0 at /home/oli/rustc/rust6/src/test/mir-opt/const-promotion-extern-static.rs:9:39: 9:54
        StorageLive(_3);                 // bb0[2]: scope 0 at /home/oli/rustc/rust6/src/test/mir-opt/const-promotion-extern-static.rs:9:39: 9:54
        StorageLive(_4);                 // bb0[3]: scope 0 at /home/oli/rustc/rust6/src/test/mir-opt/const-promotion-extern-static.rs:9:40: 9:53
        StorageLive(_5);                 // bb0[4]: scope 1 at /home/oli/rustc/rust6/src/test/mir-opt/const-promotion-extern-static.rs:9:50: 9:51
        _5 = const Value(Scalar(AllocId(1).0x0)) : *const i32; // bb0[5]: scope 1 at /home/oli/rustc/rust6/src/test/mir-opt/const-promotion-extern-static.rs:9:50: 9:51
                                         // ty::Const
                                         // + ty: *const i32
                                         // + val: Value(Scalar(AllocId(1).0x0))
                                         // mir::Constant
                                         // + span: /home/oli/rustc/rust6/src/test/mir-opt/const-promotion-extern-static.rs:9:50: 9:51
                                         // + literal: Const { ty: *const i32, val: Value(Scalar(AllocId(1).0x0)) }
        _4 = &(*_5);                     // bb0[6]: scope 1 at /home/oli/rustc/rust6/src/test/mir-opt/const-promotion-extern-static.rs:9:49: 9:51
        _3 = [move _4];                  // bb0[7]: scope 0 at /home/oli/rustc/rust6/src/test/mir-opt/const-promotion-extern-static.rs:9:39: 9:54
        _2 = &_3;                        // bb0[8]: scope 0 at /home/oli/rustc/rust6/src/test/mir-opt/const-promotion-extern-static.rs:9:39: 9:54
        _1 = move _2 as &[&'static i32] (Pointer(Unsize)); // bb0[9]: scope 0 at /home/oli/rustc/rust6/src/test/mir-opt/const-promotion-extern-static.rs:9:39: 9:54
        _0 = const core::slice::<impl [&'static i32]>::as_ptr(move _1) -> [return: bb2, unwind: bb1]; // bb0[10]: scope 0 at /home/oli/rustc/rust6/src/test/mir-opt/const-promotion-extern-static.rs:9:39: 9:63
                                         // ty::Const
                                         // + ty: for<'r> fn(&'r [&'static i32]) -> *const &'static i32 {core::slice::<impl [&'static i32]>::as_ptr}
                                         // + val: Value(Scalar(<ZST>))
                                         // mir::Constant
                                         // + span: /home/oli/rustc/rust6/src/test/mir-opt/const-promotion-extern-static.rs:9:55: 9:61
                                         // + literal: Const { ty: for<'r> fn(&'r [&'static i32]) -> *const &'static i32 {core::slice::<impl [&'static i32]>::as_ptr}, val: Value(Scalar(<ZST>)) }
    }

    bb1 (cleanup): {
        resume;                          // bb1[0]: scope 0 at /home/oli/rustc/rust6/src/test/mir-opt/const-promotion-extern-static.rs:9:1: 9:64
    }

    bb2: {
        StorageDead(_5);                 // bb2[0]: scope 0 at /home/oli/rustc/rust6/src/test/mir-opt/const-promotion-extern-static.rs:9:62: 9:63
        StorageDead(_3);                 // bb2[1]: scope 0 at /home/oli/rustc/rust6/src/test/mir-opt/const-promotion-extern-static.rs:9:62: 9:63
        return;                          // bb2[2]: scope 0 at /home/oli/rustc/rust6/src/test/mir-opt/const-promotion-extern-static.rs:9:1: 9:64
    }
}

The StorageDead(_3) deallocates the array that the as_ptr call gets a raw pointer to. That's where the dangling pointer error comes from.

I guess we used to promote this array but due to the deref (since statics are now *CONST_PTR_TO_STATIC) don't do this anymore

@pnkfelix
Copy link
Member

pnkfelix commented Jan 2, 2020

triage: P-high. (Has PR, yay.)

@pnkfelix pnkfelix added the P-high High priority label Jan 2, 2020
@bors bors closed this as completed in 60bef14 Jan 9, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-const-eval Area: Constant evaluation (MIR interpretation) P-high High priority regression-from-stable-to-beta Performance or correctness regression from stable to beta. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants