Skip to content

Checking the type of const parameters #44

Open
@lcnr

Description

@lcnr

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-param-typesConst parameter typesC-design-docsCategory: This is part of our design documentationK-implDocument Kind: regarding implementationP-optionalPriority: not strictly requiredS-active

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions