Skip to content

Give a better error when an impl block implicitly requires Sized #98392

Open
@jyn514

Description

@jyn514

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

use core::fmt::Display;

struct S<T: ?Sized> {
    inner: Box<T>,
}

impl<T> S<T> {
    fn foo(&self) {}
}

fn main() {
    let x: S<dyn Display> = S { inner: Box::new(0) as _ };
    x.foo();
}

The current output is:

error[E0599]: the method `foo` exists for struct `S<dyn std::fmt::Display>`, but its trait bounds were not satisfied
   --> src/main.rs:12:7
    |
2   |   struct S<T: ?Sized> {
    |   ------------------- method `foo` not found for this
...
12  |       x.foo();
    |         ^^^ method cannot be called on `S<dyn std::fmt::Display>` due to unsatisfied trait bounds
    |
    = note: the following trait bounds were not satisfied:
            `dyn std::fmt::Display: Sized`

Ideally the output should look like:

error[E0599]: the method `foo` exists for struct `S<dyn std::fmt::Display>`, but its trait bounds were not satisfied
   --> src/main.rs:12:7
    |
2   |   struct S<T: ?Sized> {
    |   ------------------- method `foo` not found for this
...
12  |       x.foo();
    |         ^^^ method cannot be called on `S<dyn std::fmt::Display>` due to unsatisfied trait bounds
    |
    = note: the following trait bounds were not satisfied:
            `dyn std::fmt::Display: Sized`
note: implicitly required by the following `impl` block:
 7 | impl<T> S<T> {
    = help: consider relaxing the implicit `Sized` restriction
 7 | impl<T: ?Sized> S<T> {
           ++++++++

This already exists for free functions; not sure why it's broken above. https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=4c7a6a69f9678c4ea5b410431e201c8c

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-DSTsArea: Dynamically-sized types (DSTs)A-diagnosticsArea: Messages for errors, warnings, and lintsT-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