Skip to content

Hint for a lifetime binding of a type in a trait suggests invalid syntax [E0309] #26271

Description

@kornelski

Hint in the error message for this code appears to suggest a syntax that doesn't compile (playpen):

trait Tr {
    type TrSubtype;
}

struct Bar<'a, Item: Tr>  {
    item: Item,
    item_sub: &'a <Item as Tr>::TrSubtype,
}

error: the associated type <Item as Tr>::TrSubtype may not live long enough
[…] consider adding an explicit lifetime bound <Item as Tr>::TrSubtype: 'a...

When I did what (I thought was) suggested (since the same syntax worked in simpler cases):

struct Bar<'a, Item: Tr, <Item as Tr>::TrSubtype: 'a>  {

it failed with a very unfriendly syntax error:

error: expected ident, found <

After a lot of head scratching I've discovered that it meant:

struct Bar<'a, Item: Tr> where <Item as Tr>::TrSubtype: 'a  {

So I suggest either:

  • Make the error hint suggest syntax with the where keyword when necessary
  • Allow the syntax without where: <'a, Item: Tr, <Item as Tr>::TrSubtype: 'a>>
  • or detect when < is found instead of an ident and emit error along lines of "Move that to the where clause".

Activity

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 lintsA-parserArea: The lexing & parsing of Rust source code to an ASTC-bugCategory: This is a bug.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

      Issue actions