From 86691a19e84f74481a1ff8574c146ac5150047bb Mon Sep 17 00:00:00 2001 From: Alan Wu Date: Fri, 3 Feb 2023 15:25:57 -0500 Subject: [PATCH 1/2] Clarify that unnamed constants are always evaluated at compile time See https://github.com/rust-lang/rust/issues/93838 It seems like everyone is onboard with blessing this behavior as stable. --- src/items/constant-items.md | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/items/constant-items.md b/src/items/constant-items.md index bf315932f..258c0ce5d 100644 --- a/src/items/constant-items.md +++ b/src/items/constant-items.md @@ -89,6 +89,17 @@ m!(const _: () = ();); // const _: () = (); ``` +Unnamed constants are always [evaluated][const_eval] at compile-time to surface +panics. This happens even within an unused function: + +```rust,compile_fail +fn unused_generic_function() { + // A failing compile-time assertion + const _: () = assert!(usize::BITS == 0); +} +``` + +[const_eval]: ../const_eval.md [associated constant]: ../items/associated-items.md#associated-constants [constant value]: ../const_eval.md#constant-expressions [free]: ../glossary.md#free-item From 021889f26215721860a153e692909eda7cfd7a6e Mon Sep 17 00:00:00 2001 From: Alan Wu Date: Tue, 14 Feb 2023 18:31:49 -0500 Subject: [PATCH 2/2] Adjust language to also cover named constants Especially since it seems that there is no test in rust-lang/rust that covers the unamed constant case (even though the implementation covers it). https://github.com/rust-lang/rust/issues/93838#issuecomment-1430524383 --- src/items/constant-items.md | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/items/constant-items.md b/src/items/constant-items.md index 258c0ce5d..85d3e015d 100644 --- a/src/items/constant-items.md +++ b/src/items/constant-items.md @@ -89,10 +89,15 @@ m!(const _: () = ();); // const _: () = (); ``` -Unnamed constants are always [evaluated][const_eval] at compile-time to surface +## Evaluation + +[Free][free] constants are always [evaluated][const_eval] at compile-time to surface panics. This happens even within an unused function: ```rust,compile_fail +// Compile-time panic +const PANIC: () = std::unimplemented!(); + fn unused_generic_function() { // A failing compile-time assertion const _: () = assert!(usize::BITS == 0);