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

Impl Trait fails to enforce static lifetime #134263

Closed
alexeuler opened this issue Dec 13, 2024 · 2 comments
Closed

Impl Trait fails to enforce static lifetime #134263

alexeuler opened this issue Dec 13, 2024 · 2 comments
Labels
C-bug Category: This is a bug.

Comments

@alexeuler
Copy link

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

@alexeuler alexeuler added the C-bug Category: This is a bug. label Dec 13, 2024
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Dec 13, 2024
@compiler-errors
Copy link
Member

dupe of #117210 (comment)

@alexeuler
Copy link
Author

Closing it, as it is a duplicate of #117210 (comment)

@jieyouxu jieyouxu removed the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Dec 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: This is a bug.
Projects
None yet
Development

No branches or pull requests

4 participants