|
| 1 | +# 📚 Design |
| 2 | + |
| 3 | +For an explanation of how const generics currently works in the compiler, |
| 4 | +also check out the [rustc-dev-guide](https://rustc-dev-guide.rust-lang.org/constants.html). |
| 5 | + |
| 6 | + |
| 7 | +Const generics has quite a few deep rooted issues and design challenges. |
| 8 | +In this document we try to highlight some specific topics which are fairly |
| 9 | +pervasive while working on this feature. |
| 10 | + |
| 11 | +### 🔙 Backwards compatability |
| 12 | + |
| 13 | +Rust was not design with const generics in mind, and we've also made |
| 14 | +some unfortunate decisions even after work on const generics started. |
| 15 | + |
| 16 | +Future extensions of const generics must not cause substantial breakage |
| 17 | +to existing code, or if they do, this breakage has to be contained somehow. |
| 18 | +A possible solution to many such changes are editions. |
| 19 | + |
| 20 | +### ⚖️ No perfect solution |
| 21 | + |
| 22 | +There isn't just one *perfect* version of const generics we are working towards. |
| 23 | +Instead there are a lot of major and minor tradeoffs between learnability, expressiveness, |
| 24 | +implementation complexity and many other factors. As we can't perfectly tell how |
| 25 | +const generics will be used in the future, making these decisions can be especially |
| 26 | +hard. For any such issues we should try to reach out to the wider community before |
| 27 | +coming to a final conclusion. |
| 28 | + |
| 29 | +### 🔄 The query system |
| 30 | + |
| 31 | +Const generics mixes evaluation with typechecking which very quickly reaches the edge |
| 32 | +of what is possible with the query system. It might make sense to look at these issues in |
| 33 | +unison and consider whether there are some more fundamental changes to the compiler which |
| 34 | +provide a better solution to them instead of [adding more hacks](https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/struct.WithOptConstParam.html). |
| 35 | + |
| 36 | +### ❔ Optional extensions |
| 37 | + |
| 38 | +There are a lot of things which are nice to have but not strictly necessary or maybe not even |
| 39 | +desirable. This means that decisions which prevents them may still be the correct ones, |
| 40 | +i.e. these are not strictly a blocking concern. |
| 41 | +We should still try to find alternatives before blocking these though. |
| 42 | + |
| 43 | +## Const parameter types ([`adt_const_params`](https://github.com/rust-lang/rust/issues/95174)) |
| 44 | + |
| 45 | +On stable, only integers, `bool`, and `char` is allowed as the type of const parameters. |
| 46 | +We want to extend this to more types in the future. |
| 47 | +```rust |
| 48 | +struct NamedType<const NAME: &'static str> { ... } |
| 49 | +``` |
| 50 | + |
| 51 | +Additionally, we want to support generic const parameter types and must not stabilize anything |
| 52 | +which prevents the addition of that in the future. |
| 53 | +```rust |
| 54 | +struct WithGenericArray<T, const N: usize, const ARRAY: [T; N]> { ... } |
| 55 | +``` |
| 56 | + |
| 57 | +This feature interacts with the following topics: |
| 58 | + |
| 59 | +- [❗ Constraining generic parameters](./design/constraining-generic-parameters.html) |
| 60 | +- [❗🔙 ⚖️ Structural equality](./design/structural-equality.html) |
| 61 | +- [❗⚖️ Valid const parameter types](./design/valid-const-parameter-types.html) |
| 62 | +- [❗ Valtrees](./design/valtrees.html) |
| 63 | +- [❗⚖️🔄 Generic const parameter types](./design/generic-const-param-types.html) |
| 64 | +- [❔ Exhaustiveness](./design/exhaustiveness.html) |
| 65 | +- [❔🔙 Functions as const parameters](./design/functions-as-const-parameters.html) |
| 66 | + |
| 67 | +## Generic constants in the type system ([`generic_const_exprs`](https://github.com/rust-lang/rust/issues/76560)) |
| 68 | + |
| 69 | +- ❗🔙 🔄 Unused substs |
| 70 | +- ❗🔄 Self referential where clauses |
| 71 | +- ❗🔄 Evaluation without first checking where-clauses |
| 72 | +### Unifying generic constants |
| 73 | + |
| 74 | +- [❗🔙 Restrictions on const evaluation](./design/const-eval-requirements.html) |
| 75 | +- ❗ Do not leak implementation details |
| 76 | +- ❗ Splitting constants during unification |
| 77 | +- [❗ Constraining generic parameters](./design/constraining-generic-parameters.html) |
| 78 | +- ❔⚖️ Extending unification logic |
| 79 | + |
| 80 | +### Const evaluatable bounds |
| 81 | + |
| 82 | +ALL OF THIS |
| 83 | + |
| 84 | +## Repeat length backcompatability lint ([`const_evaluatable_unchecked`](https://github.com/rust-lang/rust/issues/76200)) |
| 85 | + |
| 86 | +close this or wait until it's possible on stable. |
0 commit comments