Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
traviscross opened this issue Aug 20, 2024 · 1 comment · Fixed by #1566
Closed

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

traviscross opened this issue Aug 20, 2024 · 1 comment · Fixed by #1566
Labels
A-const-eval Area: Const evaluation

Comments

@traviscross
Copy link
Contributor

traviscross commented Aug 20, 2024

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

@ehuss ehuss added the A-const-eval Area: Const evaluation label Aug 20, 2024
@RalfJung
Copy link
Member

We should probably also emit more specific errors -- it is not wrong for "constant expression [to depend] on a generic parameter", it's just wrong in this specific case where that expression occurs in a type.

Also, strange that the two examples above lead to different errors that say the same thing in different words -- they must be caught by different checks, somehow?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-const-eval Area: Const evaluation
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants