From bd9a46eaee5bf732a8db95d33ba230e2109fb1a6 Mon Sep 17 00:00:00 2001 From: Alan Wu Date: Fri, 24 Dec 2021 17:41:52 -0500 Subject: [PATCH] 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]: https://github.com/rust-lang/rust/issues/89006 [^1]: https://github.com/rust-lang/rust/issues/91877#issuecomment-995026270 [^2]: https://github.com/rust-lang/rust/issues/91877#issuecomment-995977016 [^3]: https://github.com/rust-lang/rust/issues/71800 [^4]: https://github.com/rust-lang/rust/issues/51999#issuecomment-832086617 --- src/const_eval.md | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/const_eval.md b/src/const_eval.md index 93d2fe8b6..4485e0a79 100644 --- a/src/const_eval.md +++ b/src/const_eval.md @@ -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 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