rust-lang/rust

regression - temporary value dropped while borrowed with static slice (as used by rust-phf)

inanna-malick opened this issue · 8 comments

The below code compiles in 1.40.0 but not in 1.42.0 (stable). (minimized example from rust-phf)

pub enum Slice<T: 'static> {
    Static(&'static [T]),
}

pub struct Map<K: 'static, V: 'static> {
    pub entries: Slice<(K, V)>,
}

static CONTENT : & 'static [ u8 ] = b"a";

pub static CONTENT_MAP: Map<&'static str, &'static [u8]> = {
    Map {
        entries: Slice::Static(&[
            ("content", CONTENT),
        ]),
    }
};

When built in 1.42.0, it produces the following error:

error[E0716]: temporary value dropped while borrowed
  --> src/lib.rs:13:33
   |
13 |            entries: Slice::Static(&[
   |   ________________________________-^
   |  |________________________________|
   | ||
14 | ||             ("content", CONTENT),
15 | ||         ]),
   | ||         ^
   | ||_________|
   | |__________creates a temporary which is freed while still in use
   |            cast requires that borrow lasts for `'static`
16 |        }
17 |    };
   |    - temporary value is freed at the end of this statement

The same error occurs using nightly.

There's an issue from January mentioning this build error on the rust-phf repo (seems to occur in 1.41.0+), but no followup discussion/work that I could find. rust-phf/rust-phf#187

Notes:

  • the same error occurs if Slice is a struct instead of an enum
  • the error does not occur if Slice is removed and the &'static [T] is stored directly on Map

Minimized:

pub struct Slice<T: 'static>(&'static [T]);

static CONTENT: &'static [u8] = b"";

pub static CONTENT_MAP: Slice<&'static [u8]> = {
    Slice(&[
        CONTENT,
    ])
};

CONTENT must be a static for this to happen, with a const it doesn't happen.

This happens on both 1.41.0 and 1.41.1, so it's not fallout from #69145 (which fixed #69114 in that release). There's no entry in the release notes or blog post for 1.41.0 that indicates that this is expected fallout.

cc @matthewjasper anyways re: borrowck

regressed nightly: nightly-2019-11-24
searched commits: from 5fa0af2 to 0c987c5
regressed commit: a449535

@rustbot modify labels to -E-needs-bisection

Nice, thanks @steffahn! Looks like #66587 is the cause then.

This issue was briefly discussed in our pre triage meeting. Tagging as P-high and unnominating.

So... the bug is fairly simple, albeit not easy to fix. The problem is that any use of a static anywhere like

let x = STATIC;

becomes

const STATIC_REF: &'static Type = &STATIC;
let tmp = STATIC_REF;
let x = *tmp;

This has various positive effects on rustc, making many implementation things easier, but it also means that it broke promoting reading from statics within other statics, because we will never ever support promoting * beyond &* reborrows. So now we need to special case *STATIC_REF, which isn't too bad by itself, but just from the deref it is fairly hard to figure out that tmp is only assigned STATIC_REF, so this is the special case we're allowed to deref.