@@ -293,22 +293,35 @@ type that the definition has to implement.
293
293
An * associated constant definition* defines a constant associated with a
294
294
type. It is written the same as a [ constant item] .
295
295
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.
298
299
299
- ``` rust
300
+ ``` rust,compile_fail
300
301
struct Struct;
302
+ struct GenericStruct<const ID: i32>;
301
303
302
304
impl Struct {
303
- const ID : i32 = 1 ;
304
305
// Definition not immediately evaluated
305
306
const PANIC: () = panic!("compile-time panic");
306
307
}
307
308
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
+
308
316
fn main() {
309
- assert_eq! (1 , Struct :: ID );
310
317
// 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;
312
325
}
313
326
```
314
327
@@ -381,5 +394,4 @@ fn main() {
381
394
[ regular function parameters ] : functions.md#attributes-on-function-parameters
382
395
[ generic parameters ] : generics.md
383
396
[ where clauses ] : generics.md#where-clauses
384
- [ free ] : ../glossary.md#free-item
385
397
[ constant evaluation ] : ../const_eval.md
0 commit comments