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
Member

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.

Activity

added
A-diagnosticsArea: Messages for errors, warnings, and lints
T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
on Dec 5, 2022
added
D-incorrectDiagnostics: A diagnostic that is giving misleading or incorrect information.
D-papercutDiagnostics: An error or lint that needs small tweaks.
on Dec 5, 2022
changed the title [-]Diagnostic points at whole path instead of type argument if the latter does not satisfy a trait bound[/-] [+]Diagnostic points at whole path instead of type argument of a GAT if the latter does not satisfy a trait bound[/+] on Dec 5, 2022
changed the title [-]Diagnostic points at whole path instead of type argument of a GAT if the latter does not satisfy a trait bound[/-] [+]Diagnostic points at whole path instead of a GAT argument if the latter does not satisfy a trait bound[/+] on Dec 5, 2022
changed the title [-]Diagnostic points at whole path instead of a GAT argument if the latter does not satisfy a trait bound[/-] [+]Diagnostic points at whole path instead of GAT argument if the latter does not satisfy a trait bound[/+] on Dec 5, 2022
added a commit that references this issue on Dec 6, 2022

Rollup merge of rust-lang#105324 - compiler-errors:gat-where-clause-b…

e09c71e
changed the title [-]Diagnostic points at whole path instead of GAT argument if the latter does not satisfy a trait bound[/-] [+]Diagnostic points at entire path instead of path argument if the latter does not satisfy a trait bound[/+] on Mar 22, 2023
fmease

fmease commented on Apr 21, 2023

@fmease
MemberAuthor

This also leads to some diagnostics getting deduplicated when they shouldn't. E.g. for T::P<String, String> where type P<A: Copy, B: Copy>, we only emit a single error when it's actually two. -Zdeduplicate-diagnostics=true|false does not have an effect on it leading me to assume the deduplication happens much earlier. Maybe when the obligations are registered since they have the same cause span.

estebank

estebank commented on Aug 25, 2023

@estebank
Contributor

@fmease could you provide a test for the last case you mentioned?

13 remaining items

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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

      Participants

      @estebank@fmease@rustbot

      Issue actions

        Diagnostic points at entire path instead of path argument if the latter does not satisfy a trait bound · Issue #105306 · rust-lang/rust