Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 6c73f67

Browse files
committed
Auto merge of rust-lang#15105 - dfireBird:fix-15096, r=HKalbasi
Change comparsion for checking if number is negative to include 128 The last byte in Little-Endian representation of negative integers start at 128 (Ox80) till 255 (OxFF). The comparison before the fix didn't check for 128 which made is_negative variable as false. Potentially fixes rust-lang#15096
2 parents f0e00ed + 410ede9 commit 6c73f67

File tree

3 files changed

+85
-1
lines changed

3 files changed

+85
-1
lines changed

crates/hir-ty/src/consteval/tests.rs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2018,6 +2018,57 @@ fn consts() {
20182018
"#,
20192019
6,
20202020
);
2021+
2022+
check_number(
2023+
r#"
2024+
const F1: i32 = 2147483647;
2025+
const F2: i32 = F1 - 25;
2026+
const GOAL: i32 = F2;
2027+
"#,
2028+
2147483622,
2029+
);
2030+
2031+
check_number(
2032+
r#"
2033+
const F1: i32 = -2147483648;
2034+
const F2: i32 = F1 + 18;
2035+
const GOAL: i32 = F2;
2036+
"#,
2037+
-2147483630,
2038+
);
2039+
2040+
check_number(
2041+
r#"
2042+
const F1: i32 = 10;
2043+
const F2: i32 = F1 - 20;
2044+
const GOAL: i32 = F2;
2045+
"#,
2046+
-10,
2047+
);
2048+
2049+
check_number(
2050+
r#"
2051+
const F1: i32 = 25;
2052+
const F2: i32 = F1 - 25;
2053+
const GOAL: i32 = F2;
2054+
"#,
2055+
0,
2056+
);
2057+
2058+
check_number(
2059+
r#"
2060+
const A: i32 = -2147483648;
2061+
const GOAL: bool = A > 0;
2062+
"#,
2063+
0,
2064+
);
2065+
2066+
check_number(
2067+
r#"
2068+
const GOAL: i64 = (-2147483648_i32) as i64;
2069+
"#,
2070+
-2147483648,
2071+
);
20212072
}
20222073

20232074
#[test]

crates/hir-ty/src/mir/eval.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2122,7 +2122,7 @@ impl Evaluator<'_> {
21222122
}
21232123

21242124
pub fn pad16(x: &[u8], is_signed: bool) -> [u8; 16] {
2125-
let is_negative = is_signed && x.last().unwrap_or(&0) > &128;
2125+
let is_negative = is_signed && x.last().unwrap_or(&0) > &127;
21262126
let fill_with = if is_negative { 255 } else { 0 };
21272127
x.iter()
21282128
.copied()

crates/ide/src/hover/tests.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -779,6 +779,39 @@ const foo$0: u32 = {
779779
```
780780
"#]],
781781
);
782+
783+
check(
784+
r#"const FOO$0: i32 = -2147483648;"#,
785+
expect![[r#"
786+
*FOO*
787+
788+
```rust
789+
test
790+
```
791+
792+
```rust
793+
const FOO: i32 = -2147483648 (0x80000000)
794+
```
795+
"#]],
796+
);
797+
798+
check(
799+
r#"
800+
const FOO: i32 = -2147483648;
801+
const BAR$0: bool = FOO > 0;
802+
"#,
803+
expect![[r#"
804+
*BAR*
805+
806+
```rust
807+
test
808+
```
809+
810+
```rust
811+
const BAR: bool = false
812+
```
813+
"#]],
814+
);
782815
}
783816

784817
#[test]

0 commit comments

Comments
 (0)