Skip to content

Commit c10ed5b

Browse files
committed
Add sections about panics during constant evaluation
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)
1 parent 06f9e61 commit c10ed5b

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

src/const_eval.md

+15
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,21 @@ Conversely, the following are possible in a const function, but not in a const c
9999
* Use of generic type and lifetime parameters.
100100
* Const contexts do allow limited use of [const generic parameters].
101101

102+
## Panics
103+
104+
Compile time evaluation of constant expressions that that panic can cause
105+
compilation errors.
106+
107+
## Evaluation timing
108+
109+
Not all const contexts that appear in the source code necessarily trigger
110+
compile time constant evaluation. For example, the compiler can only evaluate
111+
constant expressions that use [const generic parameters] once concrete values are
112+
known.
113+
114+
Top level constants are guaranteed to be evaluated at compile time, even when
115+
the constants are unused.
116+
102117
[arithmetic]: expressions/operator-expr.md#arithmetic-and-logical-binary-operators
103118
[array expressions]: expressions/array-expr.md
104119
[array indexing]: expressions/array-expr.md#array-and-slice-indexing-expressions

0 commit comments

Comments
 (0)