-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Stabilize guaranteed compile time evaluation of unnamed constant items #93838
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
We discussed this in today's @rust-lang/lang meeting. We'd be fine with stabilizing such a guarantee. We'd want to make sure that the language used to make the guarantee still allows the compiler to optimize away code (e.g. anything that doesn't have a visible side effect such as a panic). |
Do we already have tests that confirm this behavior? If so, this may just need a reference PR that we can FCP. |
(just passing this on to the compiler team to double-check that people there are okay with what's being proposed here. And if firmer language is required to be checked against the rust-lang/reference, then we need to check that nominations there show up on the T-compiler agenda.) |
I think we should clarify that this is true even for unused generic functions: fn foo<T>() {
const _: () = {
panic!("Const eval!");
};
}
Since (as @XrXr mentioned) unnamed const items cannot use generics, we evaluate unnamed const items in the body of unused generic functions. |
I think this is related: struct A();
impl A {
const _1: [(); panic!("1")] = [];
const _2: () = panic!("2");
}
const _3: () = panic!("3"); Currently |
See rust-lang/rust#93838 I think at this point everyone is onboard with blessing this behavior as stable.
See rust-lang/rust#93838 It seems like everyone is onboard with blessing this behavior as stable.
See rust-lang/rust#93838 It seems like everyone is onboard with blessing this behavior as stable.
I submitted PR to the reference rust-lang/reference#1328 to document the timing.
@joshtriplett can I ask for a review of the language? |
@MauriceKayser all of those are named constant items so is out of scope for this issue. I also find it odd that associated constants are not always evaluated. From my understanding the timing there is related to how In any case, it's not in-scope for this issue. |
The only related test I found is There are many tests that implicitly depend on the definition being evaluated, though. Some examples:
Should I expand |
Especially since it seems that there is no test in rust-lang/rust that covers the unamed constant case (even though the implementation covers it). rust-lang/rust#93838 (comment)
Now that rust-lang/reference#1328 is merged, I'm going to go ahead and close this. This functionality is probably stable enough even without any explicit confirmation through an FCP, RFC or what have you. Weakening this guarantee would probably break a lot of crates in the ecosystem, so I trust that such change will not happen without careful consideration. Thanks everyone for your attention! |
See rust-lang/rust#93838 It seems like everyone is onboard with blessing this behavior as stable.
Especially since it seems that there is no test in rust-lang/rust that covers the unamed constant case (even though the implementation covers it). rust-lang/rust#93838 (comment)
This is a request to stabilize the implementation fact that the definition of unnamed constant items are always evaluated at compile time (through CTFE). There is no associated RFC as this feature is already available in stable releases and this is mostly a request for using an FCP to reach consensus for future support.
The stabilization of
const-panic
introduced a way for CTFE to emit hard compile errors. This allows users to perform compile time assertions using familiar language constructs and a growing set ofconst
APIs. Beforeconst-panic
, users could emit theconst_err
lint, which was less dependable since whether lints fail builds is configurable.Authors of compile time assertions that utilize
const-panic
would probably like to have confidence that what they wrote is in fact evaluated byrustc
during compilation. Currently, users can ensure evaluation by utilizing const contexts that are inputs to the type checker:This trick relies on the fact that type checking is guaranteed to happen during compilation. Utilizing this to initiate constant evaluation feels more cumbersome than necessary.
This report aims to stabilize a more obvious way to initiate compile time evaluation.
What is being stabilized
The current
rustc
behavior that unnamed constant definitions are evaluated at least once at compile time. Note that compile time evaluation happens even when the function enclosing the unnamed constant is unused:Stabilizing this seems unlikely to cause maintenance issues in the future due to the constrained nature of unnamed constants. However, I defer to people with
rustc
maintenance experience to judge this claim.What is not being stabilized
The evaluation timing of named constant items. Named constant items can be associated constants, which could use generics. Unnamed constants cannot be associated constants and cannot use generic parameters. While it might be useful to also stabilize the evaluation timing of named constant items, they require more consideration and are out of scope for this report.
I would like to thank Ralf Jung, Oli Scherer, and Eric Huss for the generous amount of guidance I received. Here is the Zulip discussion leading up to this stabilization request.
The text was updated successfully, but these errors were encountered: