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

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

Closed
mcarton opened this issue Aug 29, 2016 · 1 comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints

Comments

@mcarton
Copy link
Member

mcarton commented Aug 29, 2016

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.

@GuillaumeGomez GuillaumeGomez added the A-diagnostics Area: Messages for errors, warnings, and lints label Aug 31, 2016
@GuillaumeGomez
Copy link
Member

cc @jonathandturner

GuillaumeGomez added a commit to GuillaumeGomez/rust that referenced this issue Oct 28, 2016
Fix bad error message with `::<` in types

Fix rust-lang#36116.

Before:
```rust
error: expected identifier, found `<`
  --> src/test/compile-fail/issue-36116.rs:16:52
   |
16 |     let f = Some(Foo { _a: 42 }).map(|a| a as Foo::<i32>);
   |                                                    ^

error: chained comparison operators require parentheses
  --> src/test/compile-fail/issue-36116.rs:16:52
   |
16 |     let f = Some(Foo { _a: 42 }).map(|a| a as Foo::<i32>);
   |                                                    ^^^^^^
   |
   = help: use `::<...>` instead of `<...>` if you meant to specify type arguments

error: expected expression, found `)`
  --> src/test/compile-fail/issue-36116.rs:16:57
   |
16 |     let f = Some(Foo { _a: 42 }).map(|a| a as Foo::<i32>);
   |                                                         ^

error: expected identifier, found `<`
  --> src/test/compile-fail/issue-36116.rs:20:17
   |
20 |     let g: Foo::<i32> = Foo { _a: 42 };
   |                 ^

error: aborting due to 5 previous errors
```

After:
```rust
error: unexpected token: `::`
  --> src/test/compile-fail/issue-36116.rs:16:50
   |
16 |     let f = Some(Foo { _a: 42 }).map(|a| a as Foo::<i32>);
   |                                                  ^^
   |
   = help: use `<...>` instead of `::<...>` if you meant to specify type arguments

error: unexpected token: `::`
  --> src/test/compile-fail/issue-36116.rs:20:15
   |
20 |     let g: Foo::<i32> = Foo { _a: 42 };
   |               ^^
   |
   = help: use `<...>` instead of `::<...>` if you meant to specify type arguments

error: aborting due to 2 previous errors
```
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
Projects
None yet
Development

No branches or pull requests

2 participants