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

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

Closed
inanna-malick opened this issue Mar 30, 2020 · 8 comments · Fixed by #74945
Assignees
Labels
A-borrow-checker Area: The borrow checker A-lifetimes Area: Lifetimes / regions C-bug Category: This is a bug. P-high High priority regression-from-stable-to-stable Performance or correctness regression from one stable version to another. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@inanna-malick
Copy link

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

@inanna-malick inanna-malick added the C-bug Category: This is a bug. label Mar 30, 2020
@jonas-schievink jonas-schievink added I-nominated regression-from-stable-to-stable Performance or correctness regression from one stable version to another. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Mar 30, 2020
@inanna-malick
Copy link
Author

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

@jonas-schievink
Copy link
Contributor

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.

@jonas-schievink
Copy link
Contributor

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

@jonas-schievink jonas-schievink added A-borrow-checker Area: The borrow checker A-lifetimes Area: Lifetimes / regions E-needs-bisection Call for participation: This issue needs bisection: https://github.com/rust-lang/cargo-bisect-rustc labels Mar 30, 2020
@steffahn
Copy link
Member

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

@rustbot modify labels to -E-needs-bisection

@rustbot rustbot removed the E-needs-bisection Call for participation: This issue needs bisection: https://github.com/rust-lang/cargo-bisect-rustc label Mar 30, 2020
@jonas-schievink
Copy link
Contributor

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

@oli-cosmian
Copy link

@rustbot assign @oli-obk

@oli-obk oli-obk self-assigned this Mar 31, 2020
@spastorino
Copy link
Member

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

@spastorino spastorino added P-high High priority and removed I-nominated labels Apr 1, 2020
@oli-obk
Copy link
Contributor

oli-obk commented May 8, 2020

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-borrow-checker Area: The borrow checker A-lifetimes Area: Lifetimes / regions C-bug Category: This is a bug. P-high High priority regression-from-stable-to-stable Performance or correctness regression from one stable version to another. 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.

7 participants