Skip to content

Projecting to an associated constant fails but with wrong reason #61815

Open
@Centril

Description

@Centril

When you have:

trait Tr {
    const C: usize = 0;
    fn fun(x: [u8; Self::C]) -> [u8; 0] { x }
}

you get the usual problem:

error[E0599]: no associated item named `C` found for type `Self` in the current scope
 --> src/lib.rs:3:26
  |
3 |     fn fun(x: [u8; Self::C]) -> [u8; 0] { x }
  |                          ^ associated item not found in `Self`
  |
  = help: items from traits can only be used if the trait is implemented and in scope
  = note: the following trait defines an item `C`, perhaps you need to implement it:
          candidate #1: `Tr`

That this errors is right. However, it fails for the wrong reason.

What should happen here is that Self::C is seen as an opaque constant the value of which you don't get to assume in fun's body. Therefore you should get a type error:


error[E0308]: mismatched types
 --> src/lib.rs:L:C
  |
L |     fn fun(x: [u8; Self::C]) -> [u8; 0] { x }
  |                                           ^ expected [u8; 0], found [u8; Self::C]
  |
  = note: expected type `[u8; 0]`
             found type `[u8; Self::C]`

cc #61812
cc #29661

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-associated-itemsArea: Associated items (types, constants & functions)A-lazy-normalizationArea: Lazy normalization (tracking issue: #60471)A-type-systemArea: Type systemC-bugCategory: This is a bug.E-needs-testCall for participation: An issue has been fixed and does not reproduce, but no test has been added.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.T-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