rust-lang / rust-lang/reference

What the function types are is vague

Open
#1,971 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-functions
Dominant language
Rust
Stars
1.6k
Forks
607
PR merge metrics
PR metrics pending

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the Rust Reference call-expressions section and the standard-library fn documentation linked in the issue, then compare their descriptions with the unsafe function-pointer example. Clarify what makes the call expression valid and distinguish function item types from safe and unsafe function pointers; done when the Reference wording resolves the stated ambiguity.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
documentation
Issue type
Documentation
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.