Skip to content

Commit a0e483a

Browse files
authored
Merge pull request #2719 from xonx4l/const_generics_docs
docs:improve const generics
2 parents 7037e0d + 6c961df commit a0e483a

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

src/const-generics.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,14 +111,15 @@ The third point is also somewhat subtle, by not inheriting any of the where clau
111111

112112
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.
113113

114+
#### Array repeat expressions
115+
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.
116+
114117
```rust
115118
fn foo<T: Sized>() {
116-
let a = [1_u8; size_of::<*mut T>()];
119+
let a = [1_u8; size_of::<T>()];
117120
}
118121
```
119122

120-
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.
121-
122123
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.
123124

124125
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).

0 commit comments

Comments
 (0)