Skip to content

Allow referencing generic parameters inside array length expressions #140524

Closed as not planned
@tmandry

Description

@tmandry

Today this works:

fn bar<const N: usize>() {
    let x = [0u32; N];
}

but this does not:

trait Foo {
    const C: usize;
}

fn foo<T: Foo>() {
    let x = [0u32; T::C];
}

with the following error:

error: constant expression depends on a generic parameter
 --> src/lib.rs:7:20
  |
7 |     let y = [0u32; T::C];
  |                    ^^^^
  |
  = note: this may fail depending on what value the parameter takes

We talked about this in the lang meeting today, and the consensus of those present was that this is not a sufficient justification for disallowing this. In particular, we are happy to allow this and issue a post-monomorphization error in the cases where the value of the const makes the array too large.

Ideally I'd like us to FCP this issue to formalize the consensus, but first want to collect any context we might be missing.

cc @nikomatsakis who had some context and was going to post it here.
cc @rust-lang/types; is there another justification we're missing?
cc @rust-lang/lang

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-discussionCategory: Discussion or questions that doesn't represent real issues.T-langRelevant to the language teamT-typesRelevant to the types team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions