Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content

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

Closed

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

  1. added
    A-diagnostics
    on Jun 13, 2015
  2. Anachron commented on Jun 14, 2015

    @Anachron

    I would give 👍 to the last one.

  3. steveklabnik commented on Jan 3, 2017

    @steveklabnik
    Contributor

    Triage: same error as today.

  4. added
    T-compiler
    on Mar 9, 2017
  5. added
    A-parser
    on May 31, 2017
  6. added
    C-bug
    on Jul 22, 2017
  7. estebank commented on Sep 19, 2018

    @estebank
    Contributor

    Funnily enough, this code now works in the latest beta.


    Do not close: we still want to change the parser to accept struct Bar<'a, Item: Tr, <Item as Tr>::TrSubtype: 'a> { and emit a tailored diagnostic.

  8. added 2 commits that reference this issue on Sep 22, 2019
    7c830ec
    cb449d2
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-diagnosticsA-parserC-bugT-compiler

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions