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

Return impl for<'a> Fn(&'a) incorrectly rejected for closures #105528

Open
verglasz opened this issue Dec 10, 2022 · 0 comments
Open

Return impl for<'a> Fn(&'a) incorrectly rejected for closures #105528

verglasz opened this issue Dec 10, 2022 · 0 comments
Labels
A-closures Area: Closures (`|…| { … }`) A-coercions Area: implicit and explicit `expr as Type` coercions C-bug Category: This is a bug. T-types Relevant to the types team, which will review and decide on the PR/issue.

Comments

@verglasz
Copy link

verglasz commented Dec 10, 2022

I ran into a compile error while writing some iterator adapters involving building (types containing) closures in a function returning (types containing) impl for<'a> Fn(&'a), which I narrowed down to the following example:

fn const_if_unit(input: bool) -> impl for<'a> FnOnce(&'a ()) {
    if input {
        |_| ()
    } else {
        |_| ()
    }
}

This function fails to typecheck, seemingly because the return type is inferred to only implement Fn(&()) rather than for <'a> Fn(&'a ()).
Note that replacing the body with an unconditional |_| () instead works as expected, and using functions rather than closures also compiles without errors, see playground for more examples.
I'm on Rust 1.65.0 but the playground shows the same happening in beta and nightly too.

Seems related to #99991 and maybe #98437 (that I could find), but they are about trait bounds in input types rather than return impl, and here it actually does work as long as the closure is returned unconditionally, though feel free to close as duplicate if it's ultimately the same root issue.

`cargo check` output

error[E0308]: mismatched types
 --> repro/src/main.rs:2:19
  |
2 |       if input {
  |  ___________________^
3 | |         |_| ()
4 | |     } else {
  | |_____^ one type is more general than the other
  |
  = note: expected trait `for<'b> FnOnce<(&'b u8,)>`
             found trait `FnOnce<(&u8,)>`

error: implementation of `FnOnce` is not general enough
 --> repro/src/main.rs:2:19
  |
2 |       if input {
  |  ___________________^
3 | |         |_| ()
4 | |     } else {
  | |_____^ implementation of `FnOnce` is not general enough
  |
  = note: `fn(&'2 u8)` must implement `FnOnce<(&'1 u8,)>`, for any lifetime `'1`...
  = note: ...but it actually implements `FnOnce<(&'2 u8,)>`, for some specific lifetime `'2`

error: higher-ranked lifetime error
 --> repro/src/main.rs:4:12
  |
4 |       } else {
  |  ____________^
5 | |         |_| ()
6 | |     }
  | |_____^

@verglasz verglasz added the C-bug Category: This is a bug. label Dec 10, 2022
@compiler-errors compiler-errors added A-closures Area: Closures (`|…| { … }`) A-coercions Area: implicit and explicit `expr as Type` coercions T-types Relevant to the types team, which will review and decide on the PR/issue. labels Dec 11, 2022
Dylan-DPC added a commit to Dylan-DPC/rust that referenced this issue Apr 14, 2023
…re, r=compiler-errors

suggest lifetime for closure parameter type when mismatch

This is a draft PR, will add test cases later and be ready for review.

This PR fixes rust-lang#105675 by adding a diagnostics suggestion. Also a partial fix to rust-lang#105528.

The following code will have a compile error now:

```
fn const_if_unit(input: bool) -> impl for<'a> FnOnce(&'a ()) -> usize {
    let x = |_| 1;
    x
}
```

Before this PR:

```
error[E0308]: mismatched types
 --> src/lib.rs:3:5
  |
3 |     x
  |     ^ one type is more general than the other
  |
  = note: expected trait `for<'a> FnOnce<(&'a (),)>`
             found trait `FnOnce<(&(),)>`
note: this closure does not fulfill the lifetime requirements
 --> src/lib.rs:2:13
  |
2 |     let x = |_| 1;
  |             ^^^

error: implementation of `FnOnce` is not general enough
 --> src/lib.rs:3:5
  |
3 |     x
  |     ^ implementation of `FnOnce` is not general enough
  |
  = note: closure with signature `fn(&'2 ()) -> usize` must implement `FnOnce<(&'1 (),)>`, for any lifetime `'1`...
  = note: ...but it actually implements `FnOnce<(&'2 (),)>`, for some specific lifetime `'2`

For more information about this error, try `rustc --explain E0308`.
error: could not compile `rust-test` due to 2 previous errors
```

After this PR:

```
error[E0308]: mismatched types
 --> src/lib.rs:3:5
  |
3 |     x
  |     ^ one type is more general than the other
  |
  = note: expected trait `for<'a> FnOnce<(&'a (),)>`
             found trait `FnOnce<(&(),)>`
note: this closure does not fulfill the lifetime requirements
 --> src/lib.rs:2:13
  |
2 |     let x = |_| 1;
  |             ^^^
help: consider changing the type of the closure parameters
  |
2 |     let x = |_: &_| 1;
  |             ~~~~~~~

error: implementation of `FnOnce` is not general enough
 --> src/lib.rs:3:5
  |
3 |     x
  |     ^ implementation of `FnOnce` is not general enough
  |
  = note: closure with signature `fn(&'2 ()) -> usize` must implement `FnOnce<(&'1 (),)>`, for any lifetime `'1`...
  = note: ...but it actually implements `FnOnce<(&'2 (),)>`, for some specific lifetime `'2`

For more information about this error, try `rustc --explain E0308`.
error: could not compile `rust-test` due to 2 previous errors
```

After applying the suggestion, it compiles. The suggestion might not always be correct as the generation procedure of that suggestion is quite simple...
bors added a commit to rust-lang-ci/rust that referenced this issue Apr 16, 2023
…, r=compiler-errors

suggest lifetime for closure parameter type when mismatch

This is a draft PR, will add test cases later and be ready for review.

This PR fixes rust-lang#105675 by adding a diagnostics suggestion. Also a partial fix to rust-lang#105528.

The following code will have a compile error now:

```
fn const_if_unit(input: bool) -> impl for<'a> FnOnce(&'a ()) -> usize {
    let x = |_| 1;
    x
}
```

Before this PR:

```
error[E0308]: mismatched types
 --> src/lib.rs:3:5
  |
3 |     x
  |     ^ one type is more general than the other
  |
  = note: expected trait `for<'a> FnOnce<(&'a (),)>`
             found trait `FnOnce<(&(),)>`
note: this closure does not fulfill the lifetime requirements
 --> src/lib.rs:2:13
  |
2 |     let x = |_| 1;
  |             ^^^

error: implementation of `FnOnce` is not general enough
 --> src/lib.rs:3:5
  |
3 |     x
  |     ^ implementation of `FnOnce` is not general enough
  |
  = note: closure with signature `fn(&'2 ()) -> usize` must implement `FnOnce<(&'1 (),)>`, for any lifetime `'1`...
  = note: ...but it actually implements `FnOnce<(&'2 (),)>`, for some specific lifetime `'2`

For more information about this error, try `rustc --explain E0308`.
error: could not compile `rust-test` due to 2 previous errors
```

After this PR:

```
error[E0308]: mismatched types
 --> src/lib.rs:3:5
  |
3 |     x
  |     ^ one type is more general than the other
  |
  = note: expected trait `for<'a> FnOnce<(&'a (),)>`
             found trait `FnOnce<(&(),)>`
note: this closure does not fulfill the lifetime requirements
 --> src/lib.rs:2:13
  |
2 |     let x = |_| 1;
  |             ^^^
help: consider changing the type of the closure parameters
  |
2 |     let x = |_: &_| 1;
  |             ~~~~~~~

error: implementation of `FnOnce` is not general enough
 --> src/lib.rs:3:5
  |
3 |     x
  |     ^ implementation of `FnOnce` is not general enough
  |
  = note: closure with signature `fn(&'2 ()) -> usize` must implement `FnOnce<(&'1 (),)>`, for any lifetime `'1`...
  = note: ...but it actually implements `FnOnce<(&'2 (),)>`, for some specific lifetime `'2`

For more information about this error, try `rustc --explain E0308`.
error: could not compile `rust-test` due to 2 previous errors
```

After applying the suggestion, it compiles. The suggestion might not always be correct as the generation procedure of that suggestion is quite simple...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-closures Area: Closures (`|…| { … }`) A-coercions Area: implicit and explicit `expr as Type` coercions C-bug Category: This is a bug. 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

2 participants