@@ -293,22 +293,35 @@ type that the definition has to implement.
293293An * associated constant definition* defines a constant associated with a
294294type. It is written the same as a [ constant item] .
295295
296- Unlike [ free] constants, associated constant definitions undergo
297- [ constant evaluation] only when referenced.
296+ Associated constant definitions undergo [ constant evaluation] only when
297+ referenced. Further, definitions that include [ generic parameters] are
298+ evaluated after monomorphization.
298299
299- ``` rust
300+ ``` rust,compile_fail
300301struct Struct;
302+ struct GenericStruct<const ID: i32>;
301303
302304impl Struct {
303- const ID : i32 = 1 ;
304305 // Definition not immediately evaluated
305306 const PANIC: () = panic!("compile-time panic");
306307}
307308
309+ impl<const ID: i32> GenericStruct<ID> {
310+ // Definition not immediately evaluated
311+ const NON_ZERO: () = if ID == 0 {
312+ panic!("contradiction")
313+ };
314+ }
315+
308316fn main() {
309- assert_eq! (1 , Struct :: ID );
310317 // Referencing Struct::PANIC causes compilation error
311- // let _ = Struct::PANIC;
318+ let _ = Struct::PANIC;
319+
320+ // Fine, ID is not 0
321+ let _ = GenericStruct::<1>::NON_ZERO;
322+
323+ // Compilation error from evaluating NON_ZERO with ID=0
324+ let _ = GenericStruct::<0>::NON_ZERO;
312325}
313326```
314327
@@ -381,5 +394,4 @@ fn main() {
381394[ regular function parameters ] : functions.md#attributes-on-function-parameters
382395[ generic parameters ] : generics.md
383396[ where clauses ] : generics.md#where-clauses
384- [ free ] : ../glossary.md#free-item
385397[ constant evaluation ] : ../const_eval.md
0 commit comments