Description
Original title: Fn item types should be zero-sized
Per RFC 401, every fn in Rust should have its own unique, zero-sized type. This fits with the overall design of closures, which also have their own unique types (but those types are not zero-sized, as they carry the environment). None of these types have any explicit syntax at present. This has been a longstanding plan, but until recently it was not fully implemented. This is changing with PR #31710, which means that some existing code will need to be adjusted to account for the new behavior. This issue attempts to summarize how things work and describe what needs to be changed.
Zero-sized fn types
What all of this means is that, if you have a function declaration foo
:
// for the purposes of this discussion, all of these different kinds of `fn` declarations are equivalent:
fn foo(x: i32) { ... }
extern "C" fn foo(x: i32);
impl i32 { fn foo(x: self) { ... } }
the type of foo
is not fn(i32)
, as one might expect. Rather, it is a unique, zero-sized marker type that I will just write as typeof(foo)
. However, typeof(foo)
can be coerced to a function pointer fn(i32)
, so you rarely notice this:
let x: fn(i32) = foo; // OK, coerces
The reason that this matter is that the type fn(i32)
is not specify to any particular function: it's a function pointer. So calling x()
results in a virtual call, whereas foo()
is statically dispatched, because the type of foo
tells us precisely what function is being called.
Impact on users
As noted above, coercions mean that most code will continue to work just fine before and after this issue is fully fixed. However, you can tell the difference in a few scenarios. Perhaps the most prominent is using transmute to convert a fn item into a fn pointer. This is often done as part of an FFI:
extern "C" fn foo(userdata: Box<i32>) {
...
}
let f: extern "C" fn(*mut i32) = transmute(foo);
callback(f);
Here, transmute is being used to convert the types of the fn arguments. This pattern is now incorrect because, because the type of foo
is a function item (typeof(foo)
), which is zero-sized, and the target type (fn()
) is a function pointer, which is not zero-sized. For now, this pattern still works due to some special-cased code in the compiler, but that code is expected to be removed. For future compatibility, this pattern should be rewritten. There are a few possible ways to do this:
- change the original fn declaration to match the expected signature, and do the cast in the fn body (perhaps the best);
- cast the fn item fo a fn pointer before calling transmute, as shown here:
let f: extern "C" fn(*mut i32) = transmute(foo as extern "C" fn(_))
let f: extern "C" fn(*mut i32) = transmute(foo as usize) /* works too */
The same applies to transmutes to *mut fn()
, which were observed frequently in practice. Note though that use of this type is generally incorrect. The intention is typically to describe a function pointer, but just fn()
alone suffices for that. *mut fn()
is a pointer to a fn pointer. (Since these values are typically just passed to C code, however, this rarely makes a difference in practice.)
Current status and implementation trail
- PR Implement unique types per fn item, rather than having all fn items have fn pointer type #19891 laid the groundwork with fresh, unique types per fn item, but left the back-end work undone
- PR Distinguish fn item types to allow reification from nothing to fn pointers. #31710 completes the work.