Skip to content

Impl Trait fails to enforce static lifetime #134263

Closed
@alexeuler

Description

@alexeuler

The references to impl Trait's are not working as expected. This might be not a compiler bug per se but rather an language ergonomics issue.

I tried this code:

trait Merkle {}

trait Map {
    fn merkle(&self) -> impl Merkle + 'static;
}


fn get_merkle(
    m: &(impl Map + Clone + 'static),
) -> impl Merkle + 'static {
    let owned_map = m.clone();
    owned_map.merkle()
}

I expected it to compile and both merkle and get_merkle return a variable with a static lifetime (owned variable).

Instead, get the following error:

error[E0597]: `owned_map` does not live long enough
  --> src/lib.rs:12:5
   |
11 |     let owned_map = m.clone();
   |         --------- binding `owned_map` declared here
12 |     owned_map.merkle()
   |     ^^^^^^^^^---------
   |     |
   |     borrowed value does not live long enough
   |     argument requires that `owned_map` is borrowed for `'static`
13 | }
   | - `owned_map` dropped here while still borrowed

Meta

Same happens on nightly and beta channels

rustc --version --verbose:

rustc 1.83.0 (90b35a623 2024-11-26)
binary: rustc
commit-hash: 90b35a6239c3d8bdabc530a6a0816f7ff89a0aaf
commit-date: 2024-11-26
host: aarch64-apple-darwin
release: 1.83.0
LLVM version: 19.1.1
Backtrace

RUST_BACKTRACE=1 cargo build
   Compiling issue v0.1.0 (/private/tmp/issue)
error[E0597]: `owned_map` does not live long enough
  --> src/main.rs:9:5
   |
8  |     let owned_map: T = m.clone();
   |         --------- binding `owned_map` declared here
9  |     owned_map.merkle()
   |     ^^^^^^^^^---------
   |     |
   |     borrowed value does not live long enough
   |     argument requires that `owned_map` is borrowed for `'static`
10 | }
   | - `owned_map` dropped here while still borrowed

For more information about this error, try `rustc --explain E0597`.
error: could not compile `issue` (bin "issue") due to 1 previous error

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: This is a bug.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions