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

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

Closed
kornelski opened this issue Jun 13, 2015 · 3 comments · Fixed by #64676
Closed

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

kornelski opened this issue Jun 13, 2015 · 3 comments · Fixed by #64676
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-parser Area: The parsing of Rust source code to an AST C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@kornelski
Copy link
Contributor

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".
@arielb1 arielb1 added the A-diagnostics Area: Messages for errors, warnings, and lints label Jun 13, 2015
@Anachron
Copy link

I would give 👍 to the last one.

@steveklabnik
Copy link
Member

Triage: same error as today.

@steveklabnik steveklabnik added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Mar 9, 2017
@Mark-Simulacrum Mark-Simulacrum added the A-parser Area: The parsing of Rust source code to an AST label May 31, 2017
@Mark-Simulacrum Mark-Simulacrum added the C-bug Category: This is a bug. label Jul 22, 2017
@estebank
Copy link
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.

Centril added a commit to Centril/rust that referenced this issue Sep 22, 2019
…c, r=petrochenkov

Parse assoc type bounds in generic params and provide custom diagnostic

Fix rust-lang#26271.
@bors bors closed this as completed in cb449d2 Sep 23, 2019
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 A-parser Area: The parsing of Rust source code to an AST C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
6 participants