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

diagnostics for capturing closures that are coerced to fn pointers are not great #71895

Closed
nikomatsakis opened this issue May 4, 2020 · 1 comment · Fixed by #78901
Closed
Assignees
Labels
A-closures Area: Closures (`|…| { … }`) A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@nikomatsakis
Copy link
Contributor

nikomatsakis commented May 4, 2020

Given this program (playground):

fn main() {
    let y = 22;
    let x: fn() = || drop(y);
}

the error message is currently:

error[E0308]: mismatched types
 --> src/main.rs:3:19
  |
3 |     let x: fn() = || drop(y);
  |            ----   ^^^^^^^^^^ expected fn pointer, found closure
  |            |
  |            expected due to this
  |
  = note: expected fn pointer `fn()`
                found closure `[closure@src/main.rs:3:19: 3:29 y:_]`

The problem here is that closures can be coerced to fn types, but only if they don't capture any variables (and this one does capture y) -- see #39817 for more details. I think that we ought to give an error like:

error[E0308]: closure cannot be forced to `fn()` type because it captures `y`
 --> src/main.rs:3:19
  |
3 |     let x: fn() = || drop(y);
  |            ----           ^ `y` captured due to use here
  |            |
  |            expected due to this
  |
  = note: closures can only be coerced to `fn` types if they do not capture any variables
@nikomatsakis nikomatsakis added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels May 4, 2020
@jonas-schievink jonas-schievink added the C-enhancement Category: An issue proposing an enhancement or a PR with one. label May 4, 2020
@estebank estebank added the A-closures Area: Closures (`|…| { … }`) label May 5, 2020
@arora-aman
Copy link
Member

@rustbot claim

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-closures Area: Closures (`|…| { … }`) A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants