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

Add diagnostic for ++ and -- usages #83502

Closed
dbofmmbt opened this issue Mar 26, 2021 · 4 comments · Fixed by #88672
Closed

Add diagnostic for ++ and -- usages #83502

dbofmmbt opened this issue Mar 26, 2021 · 4 comments · Fixed by #88672
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-parser Area: The parsing of Rust source code to an AST. C-enhancement Category: An issue proposing an enhancement or a PR with one. D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@dbofmmbt
Copy link

dbofmmbt commented Mar 26, 2021

Given the following code in the playground:

fn main() {
    let i = 0;
    i++;
}

The current output is:

   Compiling playground v0.0.1 (/playground)
error: expected expression, found `+`
 --> src/main.rs:3:7
  |
3 |     i++;
  |       ^ expected expression

error: aborting due to previous error

error: could not compile `playground`

To learn more, run the command again with --verbose.

I think the message for errors involving ++ or -- should understand that the user is trying to use an operator which doesn't exist in Rust and suggest an idiomatic way to do the increment/decrement operation.

Explanation

Languages like C, Java or Javascript use ++ and -- to increment or decrement integer variables, respectively. As Rust does not support them, it may be good to add an specific diagnostic for those cases, suggesting the user to use something like i += 1 instead.

@dbofmmbt dbofmmbt added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Mar 26, 2021
@camelid camelid added D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. C-enhancement Category: An issue proposing an enhancement or a PR with one. labels Mar 26, 2021
@camelid camelid self-assigned this Mar 26, 2021
@camelid
Copy link
Member

camelid commented Mar 26, 2021

-- is trickier because i--2 is a valid expression, so by the time we realize there's a problem (e.g. when we reach a semicolon), we've lost the context that it's two -s in a row.

@leonardo-m
Copy link

See also #82987

@estebank estebank added the A-parser Area: The parsing of Rust source code to an AST. label Mar 26, 2021
@estebank
Copy link
Contributor

-- is trickier because i--2 is a valid expression, so by the time we realize there's a problem (e.g. when we reach a semicolon), we've lost the context that it's two -s in a row.

This is not uncommon. Small typos end up with errors in the parser or name resolution or the type checker depending on small variations 🙂

I think #82987 covers the "it was parsed as neg (neg (val) )" case.

@camelid
Copy link
Member

camelid commented Mar 26, 2021

I think #82987 covers the "it was parsed as neg (neg (val) )" case.

Though I don't think it will handle the i--; case, since that's a syntax error.

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-enhancement Category: An issue proposing an enhancement or a PR with one. D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
4 participants