Skip to content

Misleading diagnostic on missing comma #93867

Closed
@notriddle

Description

@notriddle

Given the following code: https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=4f89aeb79b3d5b09db11201a04b0786e

pub struct Entry<'a, K, V> {
    k: &'a mut K,
    v: V,
}

pub fn entry<'a, K, V>() -> Entry<'a K, V> {
//                                  ^ missing comma
    unimplemented!()
}

The current output is:

error: expected one of `,`, `:`, `=`, or `>`, found `K`
 --> src/lib.rs:6:28
  |
6 | pub fn entry() -> Entry<'a K, V> {
  |                            ^ expected one of `,`, `:`, `=`, or `>`

error: could not compile `playground` due to previous error

The : and = suggestions are misleading, because this is a generic invocation, not a generic declaration. You cannot, in fact, write a : or an = here:

When you write a :

This code:

pub struct Entry<'a, K, V> {
    k: &'a mut K,
    v: V,
}

pub fn entry<'a, K, V>() -> Entry<'a: 'static, K, V> {
    unimplemented!()
}

Produces this output: https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=fe71e2beefbe699e32f4d2c91dfb9b4e

error: expected one of `,` or `>`, found `:`
 --> src/lib.rs:6:37
  |
6 | pub fn entry<'a, K, V>() -> Entry<'a: 'static, K, V> {
  |                                     ^ expected one of `,` or `>`
  |
help: expressions must be enclosed in braces to be used as const generic arguments
  |
6 | pub fn entry<'a, K, V>() -> Entry<{ 'a: 'static }, K, V> {
  |                                   +             +

[E0106]: missing lifetime specifier
 --> src/lib.rs:6:35
  |
6 | pub fn entry<'a, K, V>() -> Entry<'a: 'static, K, V> {
  |                                   ^ expected named lifetime parameter
  |
  = help: this function's return type contains a borrowed value, but there is no value for it to be borrowed from
help: consider using the `'a` lifetime
  |
6 | pub fn entry<'a, K, V>() -> Entry<'a, 'a: 'static, K, V> {
  |                                   +++

[E0107]: this struct takes 2 generic arguments but 3 generic arguments were supplied
 --> src/lib.rs:6:29
  |
6 | pub fn entry<'a, K, V>() -> Entry<'a: 'static, K, V> {
  |                             ^^^^^                 - help: remove this generic argument
  |                             |
  |                             expected 2 generic arguments
  |
note: struct defined here, with 2 generic parameters: `K`, `V`
 --> src/lib.rs:1:12
  |
1 | pub struct Entry<'a, K, V> {
  |            ^^^^^     -  -

Some errors have detailed explanations: E0106, E0107.
For more information about an error, try `rustc --explain E0106`.
When you write an =

This code:

pub struct Entry<'a, K, V> {
    k: &'a mut K,
    v: V,
}

pub fn entry<'a, K, V>() -> Entry<'a = 'static, K, V> {
    unimplemented!()
}

Produces this output: https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=4eb1f410e20dd4e36879637261f0d859

error: expected one of `,`, `:`, or `>`, found `=`
 --> src/lib.rs:6:38
  |
6 | pub fn entry<'a, K, V>() -> Entry<'a = 'static, K, V> {
  |                                   -- ^ expected one of `,`, `:`, or `>`
  |                                   |
  |                                   maybe try to close unmatched angle bracket

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsT-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