Skip to content

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

@Aaron1011

Description

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

Activity

added
A-diagnosticsArea: Messages for errors, warnings, and lints
C-bugCategory: This is a bug.
D-inconsistentDiagnostics: Inconsistency in formatting, grammar or style between diagnostic messages.
on Apr 2, 2021
added
T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
on Apr 3, 2021
Aaron1011

Aaron1011 commented on Jul 24, 2021

@Aaron1011
MemberAuthor

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsC-bugCategory: This is a bug.D-inconsistentDiagnostics: Inconsistency in formatting, grammar or style between diagnostic messages.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

        Participants

        @Aaron1011@JohnTitor

        Issue actions

          Less accurate span is used when projection occurs in associated type position · Issue #83794 · rust-lang/rust