-
-
Notifications
You must be signed in to change notification settings - Fork 14.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 RFCPG-const-traitsProject group: Const traitsProject group: Const traitsS-tracking-needs-design-proposalStatus: This needs a clear design proposal and then a meeting with the team.Status: This needs a clear design proposal and then a meeting with the team.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
Fdoesn't need to be aconstfunction. -
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.
Reactions are currently unavailable
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 RFCPG-const-traitsProject group: Const traitsProject group: Const traitsS-tracking-needs-design-proposalStatus: This needs a clear design proposal and then a meeting with the team.Status: This needs a clear design proposal and then a meeting with the team.T-langRelevant to the language teamRelevant to the language team