Skip to content

Document restrictions on use of generics in const contexts in type position #1581

Closed
@traviscross

Description

@traviscross

As raised here:

...related to the language:

Const contexts that are used as parts of types (array type and repeat length expressions as well as const generic arguments) can only make restricted use of surrounding generic type and lifetime parameters.

We should document what these restrictions are precisely.

Here's an example of what doesn't work:

fn foo<const C: usize>() {
    let _ = [(); const { C + 1 }];
    //~^ ERROR generic parameters may not be used in const operations
}

Similarly:

trait Tr {
    const C: usize;
}

impl Tr for () {
    const C: usize = 0;
}

fn foo<T: Tr>() {
    let _ = [(); const { T::C }];
    //~^ ERROR constant expression depends on a generic parameter
}

cc @RalfJung @ehuss @BoxyUwU

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions