Skip to content

What the function types are is vague #1971

@xmh0511

Description

@xmh0511

Call expressions - The Rust Reference says:

For non-function types, the expression f(...) uses the method on one of the following traits based on the function operand:

  • Fn or AsyncFn — shared reference.
  • FnMut or AsyncFnMut — mutable reference.
  • FnOnce or AsyncFnOnce — value.

The non-function types, as indicated by the link, refer to function item types.
Consider this example:

unsafe fn foo(i:i32){}
fn main(){
  let f = foo as unsafe fn(i32);
  unsafe{
    f(0);
 }
}

f is of type unsafe function pointer, fn - Rust says:

In addition, all safe function pointers implement Fn, FnMut, and FnOnce, because these traits are specially known to the compiler.

The wording emphasizes safe; in other words, unsafe function pointers don't implement Fn, FnMut, and FnOnce. Furthermore, the (unsafe)function pointers aren't function item types, so why is f(0) a valid call expression?

More details can be seen https://users.rust-lang.org/t/why-can-the-unsafe-function-pointer-be-as-the-function-operand/133182

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions