rust-lang / rust-lang/reference
What the function types are is vague
Nobody has claimed this yet.
- 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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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