Skip to content

Commit 5f95175

Browse files
authored
Merge pull request #3 from lcnr/generic-const-param-types
generic-const-param-types design doc
2 parents 4f6eb57 + 12a91f6 commit 5f95175

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Generic const param types
2+
3+
We want to support the types of const parameters
4+
to depend on other generic parameters.
5+
```rust
6+
fn foo<const LEN: usize, const ARR: [u8; LEN]>() -> [u8; LEN] {
7+
ARR
8+
}
9+
```
10+
11+
This is currently forbidden during name resolution.
12+
13+
Probably the biggest blocker is type-checking const arguments
14+
for generic parameters. This currently uses [a hack][WithOptConstParam]
15+
to supply the `DefId` of the corresponding const parameter.
16+
17+
Now, let's look at the following:
18+
```rust
19+
fn foo<const N: usize, const M: [u8; N]>() {}
20+
21+
fn bar() {
22+
foo::<3, { [0; 3] }>();
23+
}
24+
```
25+
Here the expected type of `{ [0; 3] }` should be `[u8; 3]`. With the
26+
current approach it is `[u8; N]` instead. To fix this we would have to
27+
supply the affected queries the expected type of the const argument itself
28+
or use a `(DefId, SubstsRef<'tcx>)` pair instead.
29+
30+
Doing so isn't trivial because we have to worry about accidentially
31+
ending up with different types for the same const argument which would
32+
break stuff.
33+
34+
Potential dangers include partially resolved types and projections.
35+
36+
[WithOptConstParam]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/struct.WithOptConstParam.html

0 commit comments

Comments
 (0)