Skip to content

Incorrect span for GAT equality bound with missing type parameter in where clause #84009

Closed
@PatchMixolydic

Description

@PatchMixolydic

Given the following code (taken from #79636 (comment), playground):

#![allow(incomplete_features)]
#![feature(generic_associated_types)]

pub trait SomeTrait {
    type Wrapped<A>: SomeTrait;

    fn f() -> ();
}

fn program<W>() -> ()
where
    // Missing type parameter for `Wrapped`
    W: SomeTrait<Wrapped = W>,
{
    return W::f();
}

fn main() {}

The current output is:

error[E0107]: missing generics for associated type `SomeTrait::Wrapped`
 --> src/main.rs:5:10
  |
5 |     type Wrapped<A>: SomeTrait;
  |          ^^^^^^^ expected 1 type argument
  |
note: associated type defined here, with 1 type parameter: `A`
 --> src/main.rs:5:10
  |
5 |     type Wrapped<A>: SomeTrait;
  |          ^^^^^^^ -
help: use angle brackets to add missing type argument
  |
5 |     type Wrapped<A><A>: SomeTrait;
  |                 ^^^

Note that the suggestion is syntactically invalid:

pub trait SomeTrait {
    type Wrapped<A><A>: SomeTrait;

    fn f() -> ();
}
error: expected one of `:`, `;`, `=`, or `where`, found `<`
 --> src/main.rs:5:20
  |
4 | pub trait SomeTrait {
  |                     - while parsing this item list starting here
5 |     type Wrapped<A><A>: SomeTrait;
  |                    ^ expected one of `:`, `;`, `=`, or `where`
...
8 | }
  | - the item list ends here

The problem is that the span is pointing at the associated type, not the where clause bound.

Ideally the output should look like:

error[E0107]: missing generics for associated type `SomeTrait::Wrapped`
  --> src/main.rs:12:18
   |
12 |     W: SomeTrait<Wrapped = W>,
   |                  ^^^^^^^ expected 1 type argument
   |
note: associated type defined here, with 1 type parameter: `A`
 --> src/main.rs:5:10
  |
5 |     type Wrapped<A>: SomeTrait;
  |          ^^^^^^^ -
help: use angle brackets to add missing type argument
   |
12 |     W: SomeTrait<Wrapped<A> = W>,
   |                         ^^^

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-GATsArea: Generic associated types (GATs)A-associated-itemsArea: Associated items (types, constants & functions)A-diagnosticsArea: Messages for errors, warnings, and lintsF-generic_associated_types`#![feature(generic_associated_types)]` a.k.a. GATsT-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

    Issue actions