Skip to content

E0605 suggests invalid cast #84598

Closed
@YaLTeR

Description

@YaLTeR

Given the following code: https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=46a86936d28608b95d4e28854b854d80

unsafe fn foo(f: *mut dyn FnOnce()) {
    (f as &dyn FnOnce())();
}

The current output is:

error[E0605]: non-primitive cast: `*mut (dyn FnOnce() + 'static)` as `&dyn FnOnce()`
 --> src/lib.rs:2:5
  |
2 |     (f as &dyn FnOnce())();
  |     ^^^^^^^^^^^^^^^^^^^^ invalid cast
  |
help: borrow the value for the cast to be valid
  |
2 |     (&f as &dyn FnOnce())();
  |      ^

However, this does not make a valid cast. The valid cast would use &*f as far as I can tell (it still can't be called but regardless).

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsC-bugCategory: This is a bug.D-invalid-suggestionDiagnostics: A structured suggestion resulting in incorrect code.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions