-
Notifications
You must be signed in to change notification settings - Fork 13.3k
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
Comments
Similar issue happens with i16 as well where the below code dosen't compile
|
This looks intended to me. The binary literal |
A similar code compiles in c++ and hence I assumed it was a bug.
|
The C++ code works because C++ has implicit integer conversion. On a typical int = 32bit platform, fn main() {
let x: i32 = 0b1000_0000_0000_0000_0000_0000_0000_0000u32 as i32;
println!("{}", x);
} |
Ohh okay. Thanks for this information. Now I too feel that this behavior was intentional. |
So it sounds like the problem here is that the diagnostic doesn't teach all this. |
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 |
I can try and give this a shot. @rustbot claim |
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. |
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. |
Sorry, I haven't had the time to work on this. Releasing in hopes someone can pick it up. @rustbot release-assignment |
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 Facing a blocker now. How could one emit multiple suggestions (that share the same span) or "help"s? Reference: #[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 :) |
Thank you all for closing this |
I tried this code:
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
:Backtrace
The text was updated successfully, but these errors were encountered: