What the function types are is vague

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

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

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

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.

Description

A-functions

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

Dominant language
Rust
Stars
1.6k
Forks
618
Avg merge
5h 48m
Merged PRs (30d)
3

Getting set up

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.

More from rust-lang/reference

All issues in rust-lang/reference

Similar issues

More Rust issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.