Diagnostic points at entire path instead of path argument if the latter does not satisfy a trait bound
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 claims that the projected type () does 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.
@rustbot label F-generic_associated_types D-papercut D-incorrect
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 implementCopy. 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.León Orell Valerian Liehr at 2023-03-22 01:52:05
This also leads to some diagnostics getting deduplicated when they shouldn't. E.g. for
T::P<String, String>wheretype P<A: Copy, B: Copy>, we only emit a single error when it's actually two.-Zdeduplicate-diagnostics=true|falsedoes 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.León Orell Valerian Liehr at 2023-04-21 17:30:13
@fmease could you provide a test for the last case you mentioned?
Esteban Kuber at 2023-08-25 16:26:40
@estebank
trait Trait { type P<T: Copy, U: Copy>; } impl Trait for () { type P<T: Copy, U: Copy> = (); } fn main() { let _: <() as Trait>::P<String, String>; }Current output (only 1 error when it should be 2):
error[E0277]: the trait bound `String: Copy` is not satisfied --> src/main.rs:8:12 | 8 | let _: <() as Trait>::P<String, String>; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `Copy` is not implemented for `String` | note: required by a bound in `Trait::P` --> src/main.rs:2:15 | 2 | type P<T: Copy, U: Copy>; | ^^^^ required by this bound in `Trait::P`Expected output (2 errors with more precise spans):
error[E0277]: the trait bound `String: Copy` is not satisfied --> src/main.rs:8:12 | 8 | let _: <() as Trait>::P<String, String>; | ^^^^^^ the trait `Copy` is not implemented for `String` | note: required by a bound in `Trait::P` --> src/main.rs:2:15 | 2 | type P<T: Copy, U: Copy>; | ^^^^ required by this bound in `Trait::P` error[E0277]: the trait bound `String: Copy` is not satisfied --> src/main.rs:8:12 | 8 | let _: <() as Trait>::P<String, String>; | ^^^^^^ the trait `Copy` is not implemented for `String` | note: required by a bound in `Trait::P` --> src/main.rs:2:24 | 2 | type P<T: Copy, U: Copy>; | ^^^^ required by this bound in `Trait::P`León Orell Valerian Liehr at 2023-08-25 16:44:33
Clearly the tracking on my PR is not correct:
error[E0277]: the trait bound `String: Copy` is not satisfied --> f72.rs:8:37 | 8 | let _: <() as Trait>::P<String, String>; | ^^^^^^ the trait `Copy` is not implemented for `String` | note: required by a bound in `Trait::P` --> f72.rs:2:15 | 2 | type P<T: Copy, U: Copy>; | ^^^^ required by this bound in `Trait::P`Esteban Kuber at 2023-08-25 17:06:49
Unfortunately, your PR didn't fix this issue for the following case:
struct S<T: Copy>(T); fn main() { let _: S<String>; }error[E0277]: the trait bound `String: Copy` is not satisfied --> simple.rs:4:12 | 4 | let _: S<String>; | ^^^^^^^^^ the trait `Copy` is not implemented for `String` | note: required by a bound in `S` --> simple.rs:1:13 | 1 | struct S<T: Copy>(T); | ^^^^ required by this bound in `S`It should only highlight
Stringbut it highlights the entire pathS<String>. We probably need to adjust the spans of fulfillment errors for any sort of path (with generic args). I don't to have the perms to reopen this issue.León Orell Valerian Liehr at 2023-08-27 20:12:52
@fmease thanks for filing the follow up ticket. I think that having multiple smaller (but interlinked) tickets makes it easier to keep track of the conversation and to write smaller more targeted PRs.
Esteban Kuber at 2023-08-28 02:33:54
Reopening for the last case.
Esteban Kuber at 2023-08-28 17:08:26