[Pre-RFC] Function types


#1

Summary

Currently, each unboxed closure definition generates its own unique type, functions however do not exhibit the same behavior. This RFC proposes the addition of a unique compiler-generated zero-sized anonymous type for each function definition.

Motivation

This allows a normal function to be used in place where any unboxed closure would, with the exact same generated code as if the function was an unboxed closure with no captures. As it stands the function would instead be passed as a pointer, which leads to pointless indirection and missed optimizations.

As this causes a function to be equivalent (bar the coercions to fn() and calling syntax) to a struct with no fields implementing Fn, FnMut and FnOnce, it may lead to further simplification of the language and the compiler.

Detailed design

Each function shall have its own anonymous, zero-sized type, henceforth referred to as a “function type”. Each function type should implement Fn, FnMut and FnOnce. Any reference of a function as a literal should result in the instantiation of that functions associated type. Every function type should also be coercible to an appropriate fn() type.

Drawbacks

The superfluous type information may lead to slowdowns during compilation. This is also introduces extra coercion rules which may be undesirable.

Alternatives

None currently proposed.

Unresolved questions

None.