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 FnOnce hidden type captures more than the closure itself #124413

Open
kuviman opened this issue Apr 26, 2024 · 1 comment
Open

impl FnOnce hidden type captures more than the closure itself #124413

kuviman opened this issue Apr 26, 2024 · 1 comment
Labels
C-bug Category: This is a bug. F-lifetime_capture_rules_2024 `#![feature(lifetime_capture_rules_2024)]` T-types Relevant to the types team, which will review and decide on the PR/issue.

Comments

@kuviman
Copy link

kuviman commented Apr 26, 2024

I tried this code:

trait FooTrait {}

struct Foo<'a> {
    value: i32,
    x: &'a mut dyn FooTrait,
}

impl<'a> Foo<'a> {
    fn works(&self) -> Box<dyn FnOnce() -> i32 + '_> {
        Box::new(|| self.value)
    }
    fn doent(&self) -> impl FnOnce() -> i32 + '_ {
        || self.value
    }
}

I expected both methods to compile, but only the dyn version does.

The error says:

error[E0700]: hidden type for `impl FnOnce() -> i32 + '_` captures lifetime that does not appear in bounds
  --> src/lib.rs:13:9
   |
8  | impl<'a> Foo<'a> {
   |      -- hidden type `{closure@src/lib.rs:13:9: 13:11}` captures the lifetime `'a` as defined here
...
12 |     fn doent(&self) -> impl FnOnce() -> i32 + '_ {
   |                        ------------------------- opaque type defined here
13 |         || self.value
   |         ^^^^^^^^^^^^^

The error says that the closure captures lifetime 'a, although the closure in the dyn version is the same and compiles successfully without capturing 'a

Tried both current stable and nightly

playground

@kuviman kuviman added the C-bug Category: This is a bug. label Apr 26, 2024
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Apr 26, 2024
@ryanavella
Copy link

It compiles on nightly if you add #![feature(lifetime_capture_rules_2024)], so this is probably the expected (if surprising) behavior of lifetime capture rules in return-position impl Traits. At least until the 2024 edition.

Related: #117587, rust-lang/rfcs#3498

@saethlin saethlin added T-types Relevant to the types team, which will review and decide on the PR/issue. F-lifetime_capture_rules_2024 `#![feature(lifetime_capture_rules_2024)]` and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels May 10, 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. F-lifetime_capture_rules_2024 `#![feature(lifetime_capture_rules_2024)]` T-types Relevant to the types team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

4 participants