Skip to content

Associated things not found even though they do exist when used as array length #61730

Closed
@djc

Description

@djc
struct Foo;

impl Bar for Foo {
    const BAR: usize = 1;
}

trait Bar {
    const BAR: usize;
}

struct Array<S> where S: Bar {
    data: [u8; S::BAR],
    s: S,
}

This errors out with:

error[E0599]: no associated item named `BAR` found for type `S` in the current scope
  --> src/lib.rs:12:19
   |
12 |     data: [u8; S::BAR],
   |                   ^^^ associated item not found in `S`
   |
   = help: items from traits can only be used if the trait is implemented and in scope
   = note: the following trait defines an item `BAR`, perhaps you need to implement it:
           candidate #1: `Bar`

Even though S::BAR clearly does exist. The same thing happens for constants within associated types (so T::Bar::BAR), with E0220, and this confused me a whole bunch. Instead apparently the problem is that array sizes currently require a literal or some other more consty consts (is that part of #133)?

cc @estebank.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-associated-itemsArea: Associated items (types, constants & functions)A-const-evalArea: Constant evaluation, covers all const contexts (static, const fn, ...)A-const-genericsArea: const generics (parameters and arguments)A-diagnosticsArea: Messages for errors, warnings, and lintsD-confusingDiagnostics: Confusing error or lint that should be reworked.D-invalid-suggestionDiagnostics: A structured suggestion resulting in incorrect code.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