Skip to content

Misleading error message when using associated const in where clause for fixed size array #44774

Closed
@JDemler

Description

@JDemler

When using an associated const in a where clause to describe a fixed size array, the error message does not state that fixed size arrays cannot be described by associated consts.

Alas, it says, that the trait bound is not satisfied and suggests adding the trait bound.

Example rust code: Playground

struct Seq([u8; 8]);

impl From<[u8; 8]> for Seq {
    fn from(data: [u8; 8]) -> Seq {
        Seq(data)
    }
}

trait Sequence {
    const LEN: usize;
}

impl Sequence for Seq {
    const LEN: usize = 8;
}

trait ByteSequence {
    fn check();
}

impl<S> ByteSequence for S
where
    S: Sequence + From<[u8; <S as Sequence>::LEN]>,
{
    fn check() {}
}

Resulting Error message:

error[E0277]: the trait bound `S: Sequence` is not satisfied
  --> src/main.rs:23:29
   |
23 |     S: Sequence + From<[u8; <S as Sequence>::LEN]>,
   |                             ^^^^^^^^^^^^^^^^^^^^ the trait `Sequence` is not implemented for `S`
   |
   = help: consider adding a `where S: Sequence` bound
   = note: required by `Sequence::LEN`

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.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions