Skip to content

Return type that doesn't satisfy impl Trait messages should point out the reason #44530

Closed
@estebank

Description

@estebank

Currently, for the following code where a function's signature returns an impl Trait that isn't implemented by the actual return type:

#![feature(conservative_impl_trait)]

trait T {}

struct S;

fn foo() -> impl T {
    return 1;
}

provides the following output:

error[E0277]: the trait bound `{integer}: T` is not satisfied
 --> src/main.rs:7:13
  |
7 | fn foo() -> impl T {
  |             ^^^^^^ the trait `T` is not implemented for `{integer}`
  |
  = note: the return type of a function must have a statically known size

An appropriate output would at the very least need to be:

error[E0277]: the trait bound `{integer}: T` is not satisfied
 --> src/main.rs:7:13
  |
7 | fn foo() -> impl T {
  |             ^^^^^^ the trait `T` is not implemented for `{integer}`
8 |     return 1;
  |     ^^^^^^^^ `{integer}` is returned here

On cases where the returned type is local to the project, the declaration of the type should be pointed out

#![feature(conservative_impl_trait)]

trait T {}

struct S;

fn foo() -> impl T {
    S
}
error[E0277]: the trait bound `S: T` is not satisfied
 --> src/main.rs:7:13
  |
7 | fn foo() -> impl T {
  |             ^^^^^^ the trait `T` is not implemented for `S`
8 |     S
  |     ^ `S` is returned here
note: `S`'s definition:
  |
5 | struct S;
  | ^^^^^^^^
note: `T`'s definition:
  |
3 | trait T {}
  | ^^^^^^^^^^

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.C-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