Skip to content

Using Foo::<Bar> in some contexts where a type is expected gives wrong error message #36116

Closed
@mcarton

Description

@mcarton

The following example gives a wrong error message:

fn main() {
    let f = foo.map(|a| a as Foo::<Bar>);
}
error: expected identifier, found `<`
 --> b.rs:2:35
  |
2 |     let f = foo.map(|a| a as Foo::<Bar>);
  |                                   ^

error: chained comparison operators require parentheses
 --> b.rs:2:35
  |
2 |     let f = foo.map(|a| a as Foo::<Bar>);
  |                                   ^^^^^^
  |
  = help: use `::<...>` instead of `<...>` if you meant to specify type arguments

error: expected expression, found `)`
 --> b.rs:2:40
  |
2 |     let f = foo.map(|a| a as Foo::<Bar>);
  |                                        ^

error: aborting due to 4 previous errors

The help: use ::<...>instead of<...>`` bit is wrong and suggests the opposite of the fix.


Note that none of the following give that error message:

fn main() {
    let g = a as Foo::<Bar>;
}

gives

error: expected identifier, found `<`
 --> b.rs:2:23
  |
2 |     let g = a as Foo::<Bar>;
  |                       ^

error: aborting due to previous error

and

fn main() {
    let g: Foo::<Bar>;
}

gives

error: expected identifier, found `<`
 --> b.rs:2:17
  |
2 |     let g: Foo::<Bar>;
  |                 ^

error: aborting due to previous error

which are correct but could be more explicit about the problem.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lints

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions