-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
Decide on guarantees for evaluation of associated consts and const blocks #124971
Comments
This should be enough for our needs. Thanks for your work on this! cc @jswrenn |
I don't understand: isn't the entire purpose of |
Those two statements are in contradiction, aren't they? const if false {
const { panic!(); } // this is in dead code
} So we can't just say "const in dead code are evaluated". We have to say "const in dead code are evaluated, except ...". It's not okay to make a blanket statement and then restrict it elsewhere -- that makes the blanket statement just false. |
Theoretically a However, it is still the case that "syntactically mentioned" is determined before any I'd personally be quite happy to see a written guarantee that if a What I'd prefer not to see (yet) is any (new) guarantees of when |
Yeah that was my thinking as well, which is why proposed exactly that.
Agreed as well, which is why my proposal does not make any such guarantees. |
@rustbot labels -I-lang-nominated We discussed this today in lang triage. People agreed that this is a reasonable and minimal first step, and rust-lang/reference#1497 is now in FCP. |
rust-lang/reference#1497 landed so this can be closed. |
The point of this issue is to ask t-lang to decide on our guarantees for which constants will definitely be evaluated by the compiler: rust-lang/reference#1497. There's no
I-lang-nominated
in thereference
repo so we need this companion issue.The text added to the reference should be self-explaining: we guarantee that if a const block expression, or path expression denoting an associated const, is executed at runtime, then the corresponding constant has successfully evaluated, even if their value is entirely ignored by the following code. We do not say anything about constants occurring in dead code.
This is weaker than the guarantee we have been promoting so far, where all constants in a function were guaranteed to be evaluated if the function gets called, even if they occur in dead code. However, there is desire for something like
const if
which only evaluates constants in the branch that was actually taken, so long-term we probably don't want to guarantee all constants in a function to be evaluated. But when a const block / associated const occurs in live code, surely we want to guarantee that it is evaluated, and we don't want the compiler to optimize away the const entirely (including the panics it contains) just because their return value is not used.If it turns out we need a stronger guarantee that applies to all constants, even in dead code, we can always strengthen this again in the future. But this minimal first guarantee should be fairly uncontroversial I hope.
@joshlf since you were asking about this guarantee, I hope this is strong enough for your needs. Looking at your example in #112090 I think it should be fine.
Cc @rust-lang/opsem @rust-lang/wg-const-eval
The text was updated successfully, but these errors were encountered: