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

Hidden type for impl trait captures lifetime when it's not supposed to #108512

Closed
Neo-Zhixing opened this issue Feb 26, 2023 · 2 comments
Closed
Labels
C-bug Category: This is a bug.

Comments

@Neo-Zhixing
Copy link
Contributor

Neo-Zhixing commented Feb 26, 2023

I tried this code:

trait MyTrait {
}
struct MyTraitStruct;
impl MyTrait for MyTraitStruct {

}
fn inner<F>(writer: F) -> impl MyTrait + 'static
where F: FnOnce(&mut [u8])  {
    MyTraitStruct
}
fn outer(data: &[u8]) -> impl MyTrait + 'static {
    inner(|dst| {
        dst.copy_from_slice(data)
    })
}

I expected to see this compile.

Instead, I get this error:

error[E0700]: hidden type for `impl MyTrait + 'static` captures lifetime that does not appear in bounds
   --> core\src\pipeline\sbt.rs:222:5
    |
221 |   fn outer(data: &[u8]) -> impl MyTrait + 'static {
    |                  ----- hidden type `impl MyTrait + 'static` captures the anonymous lifetime defined here
222 | /     inner(|dst| {
223 | |         dst.copy_from_slice(data)
224 | |     })
    | |______^

For more information about this error, try `rustc --explain E0700`.

The writer argument is just a impl FnOnce(&mut [u8]) with no lifetime bounds.
The lifetime there is a HRTB, the full trait def is for<'a> FnOnce(&'a mut [u8])
It says the closure needs to accept any mutable byte slice, regardless of the slices lifetime.
It should have no impact on the lifetime of the closure.

Meta

rustc --version --verbose:

rustc 1.69.0-nightly (34e6673a0 2023-02-25)
binary: rustc
commit-hash: 34e6673a0473e90ef01a18eb575392c9e3859747
commit-date: 2023-02-25
host: x86_64-pc-windows-msvc
release: 1.69.0-nightly
LLVM version: 15.0.7
@Neo-Zhixing Neo-Zhixing added the C-bug Category: This is a bug. label Feb 26, 2023
@aliemjay
Copy link
Member

This is #8217.

As a workaround, you can use #![feature(type_alias_impl_trait)] as described in #82171 (comment)

@Neo-Zhixing
Copy link
Contributor Author

Thanks for the pointer!
Close in favor of #82171

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

2 participants