Tracking issue for const fn pointers · Issue #63997 · rust-lang/rust (original) (raw)
Sub-tracking issue for #57563.
This tracks const fn
types and calling fn
types in const fn
.
From the RFC (https://github.com/oli-obk/rfcs/blob/const_generic_const_fn_bounds/text/0000-const-generic-const-fn-bounds.md#const-function-pointers):
const
function pointers
const fn foo(f: fn() -> i32) -> i32 { f() }
is illegal before and with this RFC. While we can change the language to allow this feature, two
questions make themselves known:
- fn pointers in constants
const F: fn() -> i32 = ...;
is already legal in Rust today, even though theF
doesn't need to be aconst
function. - Opt out bounds might seem unintuitive?
const fn foo(f: ?const fn() -> i32) -> i32 {
// not allowed to callf
here, because we can't guarantee that it points to aconst fn
}
const fn foo(f: fn() -> i32) -> i32 {
f()
}
Alternatively one can prefix function pointers to const
functions with const
:
const fn foo(f: const fn() -> i32) -> i32 { f() } const fn bar(f: fn() -> i32) -> i32 { f() // ERROR }
This opens up the curious situation of const
function pointers in non-const functions:
fn foo(f: const fn() -> i32) -> i32 { f() }
Which is useless except for ensuring some sense of "purity" of the function pointer ensuring that
subsequent calls will only modify global state if passed in via arguments.