Skip to content

Decide on guarantees for evaluation of associated consts and const blocks #124971

Closed
@RalfJung

Description

@RalfJung

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 the reference 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-const-evalArea: Constant evaluation, covers all const contexts (static, const fn, ...)T-langRelevant to the language team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions