Merge pull request #2719 from xonx4l/const_generics_docs

docs:improve const generics
This commit is contained in:
Boxy 2026-01-08 03:39:29 +00:00 committed by GitHub
commit 3c77e23b41
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -111,14 +111,15 @@ The third point is also somewhat subtle, by not inheriting any of the where clau
This also makes it much more likely that the compiler will ICE or atleast incidentally emit some kind of error if we *do* accidentally allow generic parameters in an anon const, as the anon const will have none of the necessary information in its environment to properly handle the generic parameters.
#### Array repeat expressions
The one exception to all of the above is repeat counts of array expressions. As a *backwards compatibility hack* we allow the repeat count const argument to use generic parameters.
```rust
fn foo<T: Sized>() {
let a = [1_u8; size_of::<*mut T>()];
let a = [1_u8; size_of::<T>()];
}
```
The one exception to all of the above is repeat counts of array expressions. As a *backwards compatibility hack* we allow the repeat count const argument to use generic parameters.
However, to avoid most of the problems involved in allowing generic parameters in anon const const arguments we require that the constant be evaluated before monomorphization (e.g. during type checking). In some sense we only allow generic parameters here when they are semantically unused.
In the previous example the anon const can be evaluated for any type parameter `T` because raw pointers to sized types always have the same size (e.g. `8` on 64bit platforms).