Skip to content

Wrong error message when attempting to use a trait method's result as array length #48701

@gnzlbg

Description

@gnzlbg

The following code came up on IRC. It cannot ever work, but the error message is completely wrong (playground):

pub trait Foo { fn foo() -> usize; }

struct Bar();
impl Foo for Bar { fn foo() -> usize { 3 } }

fn baz<F: Foo>() {
    let _x = [0_u32; <F as Foo>::foo()];
}

fn main() {
    baz::<Bar>();
}

outputs:

error[E0277]: the trait bound `F: Foo` is not satisfied
 --> src/main.rs:7:22
  |
7 |     let _x = [0_u32; <F as Foo>::foo()];
  |                      ^^^^^^^^^^^^^^^ the trait `Foo` is not implemented for `F`
  |
  = help: consider adding a `where F: Foo` bound
  = note: required by `Foo::foo`

This error messages is 100% wrong: F does implement the trait Foo since the function signature does require it.

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