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

Less accurate span is used when projection occurs in associated type position #83794

Closed
Aaron1011 opened this issue Apr 2, 2021 · 1 comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-bug Category: This is a bug. D-inconsistent Diagnostics: Inconsistency in formatting, grammar or style between diagnostic messages. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@Aaron1011
Copy link
Member

The following code:

trait MyTrait {
    type Item;
}

trait OtherTrait {
    type OtherItem;
}

/*impl OtherTrait for bool {
    type OtherItem = (<u8 as MyTrait>::Item, bool);
}*/

fn main() {
    let a: (<u8 as MyTrait>::Item, bool);
}

produces the following error:

error[E0277]: the trait bound `u8: MyTrait` is not satisfied
  --> src/main.rs:14:13
   |
14 |     let a: (<u8 as MyTrait>::Item, bool);
   |             ^^^^^^^^^^^^^^^^^^^^^ the trait `MyTrait` is not implemented for `u8`

error: aborting due to previous error

Note that the span points directly at <u8 as MyTrait>::Item

However, if the same projection <u8 as MyTrait>::Item is used in associated type position:

trait MyTrait {
    type Item;
}

trait OtherTrait {
    type OtherItem;
}

impl OtherTrait for bool {
    type OtherItem = (<u8 as MyTrait>::Item, bool);
}

/*fn main() {
    let a: (<u8 as MyTrait>::Item, bool);
}*/

produces this error:

error[E0277]: the trait bound `u8: MyTrait` is not satisfied
  --> src/lib.rs:10:5
   |
10 |     type OtherItem = (<u8 as MyTrait>::Item, bool);
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `MyTrait` is not implemented for `u8`

error: aborting due to previous error

Here, the span points to the entire associated type, instead of just the projection.
This can lead to unhelpful errors in macro-generated code, since the span of the associated type may be completely different from the span of the projection.

@Aaron1011 Aaron1011 added A-diagnostics Area: Messages for errors, warnings, and lints C-bug Category: This is a bug. D-inconsistent Diagnostics: Inconsistency in formatting, grammar or style between diagnostic messages. labels Apr 2, 2021
@JohnTitor JohnTitor added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Apr 3, 2021
@Aaron1011
Copy link
Member Author

As of the latest nightly, this now produces:

error[E0277]: the trait bound `u8: MyTrait` is not satisfied
  --> src/lib.rs:10:23
   |
1  | trait MyTrait {
   | ------------- required by this bound in `MyTrait`
...
10 |     type OtherItem = (<u8 as MyTrait>::Item, bool);
   |                       ^^^^^^^^^^^^^^^^^^^^^ the trait `MyTrait` is not implemented for `u8`

pointing directly to the affected projection.

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 C-bug Category: This is a bug. D-inconsistent Diagnostics: Inconsistency in formatting, grammar or style between diagnostic messages. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

2 participants