Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
fmease opened this issue Dec 5, 2022 · 7 comments · Fixed by #115219
Labels
A-diagnostics Area: Messages for errors, warnings, and lints D-papercut Diagnostics: An error or lint that needs small tweaks. D-verbose Diagnostics: Too much output caused by a single piece of incorrect code. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@fmease
Copy link
Member

fmease commented Dec 5, 2022

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.

@fmease fmease added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Dec 5, 2022
@rustbot rustbot added D-incorrect Diagnostics: A diagnostic that is giving misleading or incorrect information. D-papercut Diagnostics: An error or lint that needs small tweaks. F-generic_associated_types `#![feature(generic_associated_types)]` a.k.a. GATs labels Dec 5, 2022
@fmease fmease 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 Dec 5, 2022
@fmease fmease 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 Dec 5, 2022
@fmease fmease 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 Dec 5, 2022
JohnTitor pushed a commit to JohnTitor/rust that referenced this issue Dec 6, 2022
…inding-obl, r=jackh726

Point at GAT `where` clause when an obligation is unsatisfied

Slightly helps with rust-lang#105306
@fmease fmease 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 Mar 22, 2023
@rustbot rustbot removed the F-generic_associated_types `#![feature(generic_associated_types)]` a.k.a. GATs label Mar 22, 2023
@fmease
Copy link
Member Author

fmease commented Apr 21, 2023

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
Copy link
Contributor

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

@fmease
Copy link
Member Author

fmease commented Aug 25, 2023

@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`

@estebank
Copy link
Contributor

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`

@bors bors closed this as completed in e877e2a Aug 27, 2023
@fmease
Copy link
Member Author

fmease commented Aug 27, 2023

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 String but it highlights the entire path S<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.

@estebank
Copy link
Contributor

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

@estebank
Copy link
Contributor

Reopening for the last case.

@estebank estebank reopened this Aug 28, 2023
@estebank estebank added D-verbose Diagnostics: Too much output caused by a single piece of incorrect code. and removed D-incorrect Diagnostics: A diagnostic that is giving misleading or incorrect information. labels Dec 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints D-papercut Diagnostics: An error or lint that needs small tweaks. D-verbose Diagnostics: Too much output caused by a single piece of incorrect code. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants