-
Notifications
You must be signed in to change notification settings - Fork 13.5k
Open
Labels
A-const-evalArea: Constant evaluation, covers all const contexts (static, const fn, ...)Area: Constant evaluation, covers all const contexts (static, const fn, ...)C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFCCategory: An issue tracking the progress of sth. like the implementation of an RFCS-tracking-needs-summaryStatus: It's hard to tell what's been done and what hasn't! Someone should do some investigation.Status: It's hard to tell what's been done and what hasn't! Someone should do some investigation.T-langRelevant to the language teamRelevant to the language team
Description
Sub-tracking issue for #57563.
This tracks const fn
types and calling fn
types in const fn
.
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 the
F
doesn't need to be aconst
function. -
Opt out bounds might seem unintuitive?
const fn foo(f: ?const fn() -> i32) -> i32 { // not allowed to call `f` here, because we can't guarantee that it points to a `const 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.
nicklauri, Avi-D-coder, tema3210, WGH-, ajruckman and 14 more
Metadata
Metadata
Assignees
Labels
A-const-evalArea: Constant evaluation, covers all const contexts (static, const fn, ...)Area: Constant evaluation, covers all const contexts (static, const fn, ...)C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFCCategory: An issue tracking the progress of sth. like the implementation of an RFCS-tracking-needs-summaryStatus: It's hard to tell what's been done and what hasn't! Someone should do some investigation.Status: It's hard to tell what's been done and what hasn't! Someone should do some investigation.T-langRelevant to the language teamRelevant to the language team