Description
What is this
This is a design document for const generics. Any discussions about its content should be on zulip. The conclusions of these discussions should then be edited back into this issue. Please do not post any comments directly in this issue.
Content
The way we currently check that the type of const parameters is the same for trait definition and impls is quite ad-hoc, resulting in bugs. This was an issue both for methods and for GATs:
trait Gat {
type Assoc<const N: usize>;
}
impl Gat for () {
type Assoc<const N: u8> = ();
}
trait Tr {
fn foo<const N: u8>(self) -> u8;
}
impl Tr for f32 {
fn foo<const N: bool>(self) -> u8 { 42 }
}
While talking about modeling const generics in a-mir-formality @nikomatsakis suggested to instead encode the expected parameter type using a new predicate. Something like this easily prevent these oversights which will still be meaningful if we add generic named constants in the future. It may also make it easier to add generic const parameter types (#28) in the future.
TODO: talk about the fact that we currently check that const arguments have the right type by it simply being the expected type while typechecking the anon const. That requires us to always typeck them which is somewhat surprising.