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

Misleading error message with precedence issue involving "as" #36367

Closed
joshtriplett opened this issue Sep 9, 2016 · 5 comments
Closed

Misleading error message with precedence issue involving "as" #36367

joshtriplett opened this issue Sep 9, 2016 · 5 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints

Comments

@joshtriplett
Copy link
Member

Consider the following code:

if mmap_size as usize < std::mem::size_of::<run>() {
    ...
}

This code has a precedence issue: as binds looser than <, so I think this attempts to parse what comes after the as as a type, usize<std::mem::size_of ..., parsing the < as a generic. That results in the following error message:

error: expected identifier, found `<`
   --> src/main.rs:202:48
    |
202 |     if mmap_size as usize < std::mem::size_of::<run>() {
    |                                                ^

This leads the user down the wrong path. Ideally, rustc should provide an error that hints at the precedence issue instead.

@Cobrand
Copy link
Contributor

Cobrand commented Sep 9, 2016

Ready-to-paste example :

fn main() {
    let i : i64 = 5 ;
    if i as usize < std::mem::size_of::<i64>() {
        println!("Case A");
    }
    if i as usize < 5 {
        println!("Case B");
    }
}

@Cobrand
Copy link
Contributor

Cobrand commented Sep 9, 2016

I just discovered that "usize", "i64", and stuff like this can actually be a variable name. This might why the error is so confusing.

@mcarton
Copy link
Member

mcarton commented Sep 10, 2016

See also #36206.

@Mark-Simulacrum
Copy link
Member

We spit out the garbage below today. Presumably, this means that usize<...> is what's being parsed.

error: unexpected token: `::`
 --> test.rs:3:38
  |
3 |     if i as usize < std::mem::size_of::<i64>() {
  |                                      ^^
  |
  = help: use `<...>` instead of `::<...>` if you meant to specify type arguments

error: expected one of `!`, `+`, `,`, `::`, or `>`, found `(`
 --> test.rs:3:45
  |
3 |     if i as usize < std::mem::size_of::<i64>() {
  |                                             ^ expected one of `!`, `+`, `,`, `::`, or `>` here

error: aborting due to 2 previous errors

@Mark-Simulacrum Mark-Simulacrum added the A-diagnostics Area: Messages for errors, warnings, and lints label May 13, 2017
@Mark-Simulacrum
Copy link
Member

Closing as fixed since we suggest what's happening today.

error: unexpected token: `::`
 --> test.rs:3:38
  |
3 |     if i as usize < std::mem::size_of::<i64>() {
  |                                      ^^
  |
  = help: use `<...>` instead of `::<...>` if you meant to specify type arguments

error: `<` is interpreted as a start of generic arguments for `usize`, not a comparison
 --> test.rs:3:45
  |
3 |     if i as usize < std::mem::size_of::<i64>() {
  |                   -                         ^ interpreted as generic argument
  |                   |
  |                   not interpreted as comparison
  |
help: if you want to compare the casted value then write:
  |     if (i as usize) < std::mem::size_of::<i64>() {

error: `<` is interpreted as a start of generic arguments for `usize`, not a comparison
 --> test.rs:6:21
  |
6 |     if i as usize < 5 {
  |                   - ^ interpreted as generic argument
  |                   |
  |                   not interpreted as comparison
  |
help: if you want to compare the casted value then write:
  |     if (i as usize) < 5 {

error: aborting due to previous error(s)

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

4 participants