Skip to content

Assinging to an i32 variable a binary representation of i32::MIN fails to compile #107896

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

Closed
psvri opened this issue Feb 10, 2023 · 13 comments · Fixed by #111212
Closed

Assinging to an i32 variable a binary representation of i32::MIN fails to compile #107896

psvri opened this issue Feb 10, 2023 · 13 comments · Fixed by #111212
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` C-enhancement Category: An issue proposing an enhancement or a PR with one. E-help-wanted Call for participation: Help is requested to fix this issue. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@psvri
Copy link

psvri commented Feb 10, 2023

I tried this code:

fn main() {
    let x: i32 = 0b1000_0000_0000_0000_0000_0000_0000_0000i32;
    println!("{}", x);
}

playground link

I expected the code to compile since its i am assigning into x binary representation of i32::MIN, instead the compiler throws an error.

Meta

rustc --version --verbose:

1.67.1
Backtrace

   Compiling playground v0.0.1 (/playground)
error: literal out of range for `i32`
 --> src/main.rs:2:18
  |
2 |     let x: i32 = 0b1000_0000_0000_0000_0000_0000_0000_0000i32;
  |                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using the type `u32` instead: `0b1000_0000_0000_0000_0000_0000_0000_0000u32`
  |
  = note: the literal `0b1000_0000_0000_0000_0000_0000_0000_0000i32` (decimal `2147483648`) does not fit into the type `i32` and will become `-2147483648i32`
  = note: `#[deny(overflowing_literals)]` on by default

error: could not compile `playground` due to previous error

@psvri psvri added the C-bug Category: This is a bug. label Feb 10, 2023
@psvri
Copy link
Author

psvri commented Feb 10, 2023

Similar issue happens with i16 as well where the below code dosen't compile

fn main() {
    let x: i16 = 0b1000_0000_0000_0000i16;
    println!("{}", x);
}

@wwylele
Copy link
Contributor

wwylele commented Feb 10, 2023

This looks intended to me. The binary literal 0b..., along with all other integer literals, represents the number in a more mathematical sense, instead of in a memory layout sense, so 0b1000_0000_0000_0000_0000_0000_0000_0000 really means the integer 2147483648, not -2147483648 (=i32::MIN)

@psvri
Copy link
Author

psvri commented Feb 10, 2023

A similar code compiles in c++ and hence I assumed it was a bug.

#include <iostream>
using namespace std;
int main()
{
    int32_t x = 0b10000000000000000000000000000000;
    cout<<x;
    return 0;
}

@wwylele
Copy link
Contributor

wwylele commented Feb 10, 2023

The C++ code works because C++ has implicit integer conversion. On a typical int = 32bit platform, 0b10000000000000000000000000000000 means 2147483648 (in both rust and C++), and is typed unsigned int in C++ (because it doesn't fit in int. "The type of the integer literal is the first type in which the value can fit"), and then when you assign it back to int32_t, the result is {implementation-defined (C++17 or earlier)} / {reinterpreted in terms of 2's complement(C++20 or later)}, which gives you back i32::MIN. Its rust equivalent code should be

fn main() {
    let x: i32 = 0b1000_0000_0000_0000_0000_0000_0000_0000u32 as i32;
    println!("{}", x);
}

@psvri
Copy link
Author

psvri commented Feb 10, 2023

Ohh okay. Thanks for this information. Now I too feel that this behavior was intentional.

@saethlin
Copy link
Member

So it sounds like the problem here is that the diagnostic doesn't teach all this.

@wwylele
Copy link
Contributor

wwylele commented Feb 10, 2023

A nice diagnostic would be that, if an overflowing literal has "sign bit" set, guess the user wants to express a negative number in 2's complement, and suggest using the negative number literal

@workingjubilee workingjubilee added C-enhancement Category: An issue proposing an enhancement or a PR with one. A-diagnostics Area: Messages for errors, warnings, and lints E-help-wanted Call for participation: Help is requested to fix this issue. A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` and removed C-bug Category: This is a bug. labels Feb 11, 2023
@edward-shen
Copy link
Contributor

I can try and give this a shot.

@rustbot claim

@Stargateur
Copy link
Contributor

fn main() {
    let x: i32 = -0b1000_0000_0000_0000_0000_0000_0000_0000i32;
    println!("{}", x);
}

Thus that very weird and probably a bad practice in most case. Doing binary operation on signed number is never a good thing in my book.

@workingjubilee
Copy link
Member

In that case, the compiler emits an error if you do it at compile-time since it's a case of the operation inducing wraparound (which lands on the same value). That's not a satisfying answer as to why the mere literal fails to compile.

@Noratrieb Noratrieb added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Feb 24, 2023
@edward-shen
Copy link
Contributor

Sorry, I haven't had the time to work on this. Releasing in hopes someone can pick it up.

@rustbot release-assignment

@nicklimmm
Copy link
Contributor

nicklimmm commented May 3, 2023

I'd like to give this a try, but I need some assistance as I have no experience working on the compiler.

@rustbot claim

My idea is to add another note and help when the literal fits in the unsigned variant, but not the signed variant (in the provided example, u32 and i32 respectively)

Facing a blocker now. How could one emit multiple suggestions (that share the same span) or "help"s?

Reference: OverflowingBinHexSub definition

#[derive(Subdiagnostic)]
pub enum OverflowingBinHexSub<'a> {
    #[suggestion(
        lint_suggestion,
        code = "{sans_suffix}{suggestion_ty}",
        applicability = "machine-applicable"
    )]
    Suggestion {
        #[primary_span]
        span: Span,
        suggestion_ty: &'a str,
        sans_suffix: &'a str,
    },
    #[help(lint_help)]
    Help { suggestion_ty: &'a str },
}

Would love to hear suggestions or guidance :)

@psvri
Copy link
Author

psvri commented Jun 20, 2023

Thank you all for closing this

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` C-enhancement Category: An issue proposing an enhancement or a PR with one. E-help-wanted Call for participation: Help is requested to fix this issue. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
8 participants