Skip to content

Commit

Permalink
Add sections about panics during constant evaluation
Browse files Browse the repository at this point in the history
Since const panics are now [stabilized], I figure it would be good to
document some of its gotchas. I couldn't really find documents for
the specifics I ran into. I am no const eval expert, and what I
wrote is basically a paraphrase of Ralf's comments[^1][^2],
but I hope to get the ball rolling for adding some docs.

I deliberately chose to not mention the guarantee about syntactically
referenced constants in functions that require code generation as it
seems hard to explain completely without some ambiguity. It also seems
to me that the rules are not completely stable[^3][^4] yet.

[stabilized]: rust-lang/rust#89006
[^1]: rust-lang/rust#91877 (comment)
[^2]: rust-lang/rust#91877 (comment)
[^3]: rust-lang/rust#71800
[^4]: rust-lang/rust#51999 (comment)
  • Loading branch information
XrXr committed Dec 24, 2021
1 parent 06f9e61 commit c10ed5b
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/const_eval.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,21 @@ Conversely, the following are possible in a const function, but not in a const c
* Use of generic type and lifetime parameters.
* Const contexts do allow limited use of [const generic parameters].

## Panics

Compile time evaluation of constant expressions that that panic can cause
compilation errors.

## Evaluation timing

Not all const contexts that appear in the source code necessarily trigger
compile time constant evaluation. For example, the compiler can only evaluate
constant expressions that use [const generic parameters] once concrete values are
known.

Top level constants are guaranteed to be evaluated at compile time, even when
the constants are unused.

[arithmetic]: expressions/operator-expr.md#arithmetic-and-logical-binary-operators
[array expressions]: expressions/array-expr.md
[array indexing]: expressions/array-expr.md#array-and-slice-indexing-expressions
Expand Down

0 comments on commit c10ed5b

Please sign in to comment.