Skip to content

Confusing diagnostics when using associated const as an array size #68695

Closed
@Disasm

Description

@Disasm
trait Adapter {
    const LINKS: usize;
}

struct Foo<A: Adapter> {
    adapter: A,
    links: [u32; A::LINKS],
}

Playground

error[E0599]: no associated item named `LINKS` found for type `A` in the current scope
 --> src/lib.rs:7:21
  |
7 |     links: [u32; A::LINKS],
  |                     ^^^^^ associated item not found in `A`
  |
  = help: items from traits can only be used if the type parameter is bounded by the trait
help: the following trait defines an item `LINKS`, perhaps you need to restrict type parameter `A` with it:
  |
5 | struct Foo<A: Adapter + Adapter> {
  |            ^^^^^^^^^^^^

At the same time on nightly with #![feature(const_generics)] error description is much better:

#![feature(const_generics)]
trait Adapter {
    const LINKS: usize;
}

struct Foo<A: Adapter> {
    adapter: A,
    links: [u32; A::LINKS],
}

Playground

error: constant expression depends on a generic parameter
 --> src/lib.rs:8:5
  |
8 |     links: [u32; A::LINKS],
  |     ^^^^^^^^^^^^^^^^^^^^^^
  |
  = note: this may fail depending on what value the parameter takes

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsC-enhancementCategory: An issue proposing an enhancement or a PR with one.T-compilerRelevant to the compiler 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