Skip to content

impl trait error does not explain its reasoning #93327

Description

@oli-obk

Given the following code: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=1497daa8a96b61decc37bed908eec352

use std::fmt::Display;

trait Foo {
    type Bar;
}

impl Foo for () {
    type Bar = Vec<i32>;
}

fn foo() -> impl Foo<Bar = impl Display> {
    ()
}

The current output is:

error[E0277]: `Vec<i32>` doesn't implement `std::fmt::Display`
  --> src/main.rs:11:13
   |
11 | fn foo() -> impl Foo<Bar = impl Display> {
   |             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `Vec<i32>` cannot be formatted with the default formatter
   |
   = help: the trait `std::fmt::Display` is not implemented for `Vec<i32>`
   = note: in format strings you may be able to use `{:?}` (or {:#?} for pretty-print) instead

Ideally the output should look like:

error[E0277]: `Vec<i32>` doesn't implement `std::fmt::Display`
  --> src/main.rs:11:13
   |
11 | fn foo() -> impl Foo<Bar = impl Display> {
   |             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `Vec<i32>` cannot be formatted with the default formatter
   |
   = help: the trait `std::fmt::Display` is not implemented for `Vec<i32>`
   = note: in format strings you may be able to use `{:?}` (or {:#?} for pretty-print) instead
note: required due to associated type of `impl Foo for ()`
   | 
 9 |     type Bar = Vec<i32>;
   |                ^^^^^^^^

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-impl-traitArea: `impl Trait`. Universally / existentially quantified anonymous types with static dispatch.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