Skip to content

Diagnostic points at entire path instead of path argument if the latter does not satisfy a trait bound #105306

@fmease

Description

@fmease

Edit: outstanding case:

struct S<T: Copy>(T);

fn main() {
    let _: S<String>;
}

Generic Arguments

fn main() {
    let _ = Option::<[u8]>::None;
}

Stderr:

error[E0277]: the size for values of type `[u8]` cannot be known at compilation time
 --> src/main.rs:2:13
  |
2 |     let _ = Option::<[u8]>::None;
  |             ^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
  |
  = help: the trait `Sized` is not implemented for `[u8]`
note: required by a bound in `None`

Ideally, it should be:

error[E0277]: the size for values of type `[u8]` cannot be known at compilation time
 --> src/main.rs:2:13
  |
2 |     let _ = Option::<[u8]>::None;
  |                      ^^^^ doesn't have a size known at compile-time
  |
  = help: the trait `Sized` is not implemented for `[u8]`
note: required by a bound in `None`

GAT Arguments Specifically

Given the following code:

struct S;
trait D {
    type P<T: Copy>;
}
impl D for S {
    type P<T: Copy> = ();
}
fn main() {
    let _: <S as D>::P<String>;
}

The current output is:

error[E0277]: the trait bound `String: Copy` is not satisfied
 --> src/main.rs:9:12
  |
9 |     let _: <S as D>::P<String>;
  |            ^^^^^^^^^^^^^^^^^^^ the trait `Copy` is not implemented for `String`

Ideally the output should look like:

error[E0277]: the trait bound `String: Copy` is not satisfied
 --> src/main.rs:9:12
  |
9 |     let _: <S as D>::P<String>;
  |                        ^^^^^^ the trait `Copy` is not implemented for `String`

The span is misleading because at first sight one might think that the compiler claimed that the projected type () ddid not implement Copy. In this simplified case, it's quite easy to see what's going on (especially after reading the message and the label) but I bet in more complex scenarios involving several type parameters and more complicated bounds, it's much more difficult to decipher the diagnostic.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsD-papercutDiagnostics: An error or lint that needs small tweaks.D-verboseDiagnostics: Too much output caused by a single piece of incorrect code.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