Skip to content

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

Closed
@inanna-malick

Description

@inanna-malick

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

Metadata

Metadata

Assignees

Labels

A-borrow-checkerArea: The borrow checkerA-lifetimesArea: Lifetimes / regionsC-bugCategory: This is a bug.P-highHigh priorityT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.regression-from-stable-to-stablePerformance or correctness regression from one stable version to another.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions