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

Error message on casting negative numbers to usize is quite unhelpful #76413

Closed
Disasm opened this issue Sep 6, 2020 · 3 comments · Fixed by #77578
Closed

Error message on casting negative numbers to usize is quite unhelpful #76413

Disasm opened this issue Sep 6, 2020 · 3 comments · Fixed by #77578
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` D-papercut Diagnostics: An error or lint that needs small tweaks. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@Disasm
Copy link
Contributor

Disasm commented Sep 6, 2020

Code:

fn syscall(nr: usize) -> usize {
    match nr {
        1 => (-1) as usize,
        _ => -1 as usize,
    }
}

fn syscall_i(nr: usize) -> isize {
    match nr {
        1 => (-1) as isize,
        _ => -1 as isize,
    }
}

fn main() {
    let rc = syscall(42);
    println!("result: {}", rc);
    
    let rc = syscall_i(42);
    println!("result: {}", rc);
}

Playground: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=04afbf8753fe268ea47b7b3fae6f897c

Error message:

error[E0600]: cannot apply unary operator `-` to type `usize`
 --> src/main.rs:3:14
  |
3 |         1 => (-1) as usize,
  |              ^^^^ cannot apply unary operator `-`
  |
  = note: unsigned values cannot be negated

error[E0600]: cannot apply unary operator `-` to type `usize`
 --> src/main.rs:4:14
  |
4 |         _ => -1 as usize,
  |              ^^ cannot apply unary operator `-`
  |
  = note: unsigned values cannot be negated

Somehow, in one case this 1 in the -1 expression is unsigned, but in the other case it is signed and everything is fine.

@leonardo-m
Copy link

For (-1) as usize the compiler has to suggest to replace it with usize::MAX.

The compiler should also suggest to replace -1 as usize with -1_usize, to remove some hard "as" casts from the code.

@estebank estebank added A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` D-papercut Diagnostics: An error or lint that needs small tweaks. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Sep 6, 2020
@euclio
Copy link
Contributor

euclio commented Oct 2, 2020

@rustbot claim

@euclio
Copy link
Contributor

euclio commented Oct 5, 2020

The compiler should also suggest to replace -1 as usize with -1_usize, to remove some hard "as" casts from the code.

I think this is more appropriate as a clippy lint, opened rust-lang/rust-clippy#6116.

Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this issue Oct 8, 2020
suggest `MAX` constant if -1 is assigned to unsigned type

Fixes rust-lang#76413.
Fixes rust-lang#77416.
@bors bors closed this as completed in 2359ecc Oct 9, 2020
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-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` D-papercut Diagnostics: An error or lint that needs small tweaks. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants