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

Invalid cast error when trying to cast a closure to a function pointer should mention captured variables #128078

Closed
divergentdave opened this issue Jul 22, 2024 · 0 comments · Fixed by #128082
Assignees
Labels
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.

Comments

@divergentdave
Copy link
Contributor

Code

fn main() {
    let captured = "x";
    let closure = || { captured.to_string() };
    let fn_ptr = closure as fn() -> String;
    let _ = fn_ptr();
}

Current output

error[E0605]: non-primitive cast: `{closure@src/main.rs:3:19: 3:21}` as `fn() -> String`
 --> src/main.rs:4:18
  |
4 |     let fn_ptr = closure as fn() -> String;
  |                  ^^^^^^^^^^^^^^^^^^^^^^^^^ invalid cast

For more information about this error, try `rustc --explain E0605`.
error: could not compile `playground` (bin "playground") due to 1 previous error

Desired output

error[E0605]: non-primitive cast: `{closure@src/main.rs:3:19: 3:21}` as `fn() -> String`
 --> src/main.rs:4:18
  |
4 |     let fn_ptr = closure as fn() -> String;
  |                  ^^^^^^^^^^^^^^^^^^^^^^^^^ invalid cast
help: this closure cannot be cast to a function pointer because it captures a variable
  |
2 |     let captured = "x";
  |         ^^^^^^^- captured outer variable
3 |     let closure = || { captured.to_string() }; 
  |                   -- captured by this Fn closure

For more information about this error, try `rustc --explain E0605`.
error: could not compile `playground` (bin "playground") due to 1 previous error

Rationale and extra context

I forgot a closure was capturing a variable while I was refactoring and setting up an array of function pointers. I had to double check the reference to figure out what the error was referring to.

Other cases

No response

Rust Version

rustc 1.79.0 (129f3b996 2024-06-10)
binary: rustc
commit-hash: 129f3b9964af4d4a709d1383930ade12dfe7c081
commit-date: 2024-06-10
host: x86_64-unknown-linux-gnu
release: 1.79.0
LLVM version: 18.1.7

Anything else?

No response

@divergentdave divergentdave 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 Jul 22, 2024
@compiler-errors compiler-errors self-assigned this Jul 22, 2024
@bors bors closed this as completed in c2ba4b1 Jul 23, 2024
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Jul 23, 2024
Rollup merge of rust-lang#128082 - compiler-errors:closure-cap, r=estebank

Note closure captures when reporting cast to fn ptr failed

Fixes rust-lang#128078

We already had logic to point out a closure having captures when that's possibly the source of a coercion error to `fn()`, but we weren't reporting it during an explicit `as` cast.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
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.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants