Skip to content

Commit 22a7b85

Browse files
committed
Fix assertion criteria
1 parent 652c959 commit 22a7b85

File tree

6 files changed

+87
-52
lines changed

6 files changed

+87
-52
lines changed

vlib/builtin/int_test.v

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ fn test_str_methods() {
1717
assert int(1).str() == '1'
1818
assert int(-1).str() == '-1'
1919
assert int(2147483647).str() == '2147483647'
20-
assert int(2147483648).str() == '-2147483648'
20+
assert int(u32(2147483648)).str() == '-2147483648'
2121
assert int(-2147483648).str() == '-2147483648'
2222
assert i64(1).str() == '1'
2323
assert i64(-1).str() == '-1'

vlib/builtin/string_int_test.v

+2-2
Original file line numberDiff line numberDiff line change
@@ -330,8 +330,8 @@ fn test_parse() {
330330
}
331331

332332
fn test_interpolate_binary_literals() {
333-
assert ' 1 ${i64(0b1000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000)}' == ' 1 -9223372036854775808'
334-
assert ' 2 ${i64(0b1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111)}' == ' 2 -1'
333+
assert ' 1 ${i64(u64(0b1000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000))}' == ' 1 -9223372036854775808'
334+
assert ' 2 ${i64(u64(0b1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111))}' == ' 2 -1'
335335
assert ' 3 ${i64(0b0111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111)}' == ' 3 9223372036854775807'
336336
assert ' 4 ${u64(0b1000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000)}' == ' 4 9223372036854775808'
337337
assert ' 5 ${u64(0b1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111)}' == ' 5 18446744073709551615'

vlib/strconv/format.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ hexadecimal
155155
import strconv
156156
157157
a1 := u8(0xff)
158-
b1 := i16(0xffff)
158+
b1 := i16(u16(0xffff))
159159
c1 := u32(0xffffffff)
160160
d1 := u64(-1)
161161
sc3 := '%hhx %hx %x %lx'

vlib/v/checker/checker.v

+24-10
Original file line numberDiff line numberDiff line change
@@ -3660,7 +3660,7 @@ fn (mut c Checker) cast_expr(mut node ast.CastExpr) ast.Type {
36603660
node.expr.val
36613661
}
36623662
}
3663-
value, e := strconv.common_parse_uint2(value_string, 0, bit_size)
3663+
v, e := strconv.common_parse_uint2(value_string, 0, bit_size)
36643664
match e {
36653665
0 {}
36663666
-3 {
@@ -3671,16 +3671,30 @@ fn (mut c Checker) cast_expr(mut node ast.CastExpr) ast.Type {
36713671
}
36723672
}
36733673

3674-
// checks if integer literal's most significant bit occupies sign bit when casting to
3675-
// signed integer, we determine the condition by checking most significant bit's index
3676-
if !signed && to_type.is_signed() {
3677-
mut v := value
3678-
mut ms_bit_index := 0
3679-
for v != 0 {
3680-
v >>= 1
3681-
ms_bit_index += 1
3674+
// checks if integer literal's most significant bit
3675+
// alters sign bit when casting to signed integer
3676+
if to_type.is_signed() {
3677+
signed_min := match to_type.idx() {
3678+
ast.i8_type_idx {
3679+
u64(0xff)
3680+
}
3681+
ast.i16_type_idx {
3682+
u64(0xffff)
3683+
}
3684+
ast.i32_type_idx {
3685+
u64(0xffffffff)
3686+
}
3687+
ast.i64_type_idx {
3688+
u64(0xffffffffffffffff)
3689+
}
3690+
else {
3691+
u64(0xffffffffffffffff)
3692+
}
36823693
}
3683-
if ms_bit_index == bit_size {
3694+
signed_max := signed_min ^ (1 << (bit_size - 1))
3695+
3696+
if (signed && (v - 2 == signed_max || v == signed_min))
3697+
|| (!signed && (v == signed_min || v == (1 << (bit_size - 1)))) {
36843698
c.error('value `${node.expr.val} overflows `${tt}`', node.pos)
36853699
}
36863700
}
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,76 @@
11
vlib/v/checker/tests/overflow_int_signed_err.vv:2:2: error: value `0xff overflows `i8`
22
1 | i8s := [
3-
2 | i8(0xff) // converted to -1
3+
2 | i8(0xff), // converted to -1
44
| ~~~~~~~~
5-
3 | i8(128) // converted to -128
6-
4 | i8(-129) // converted to +127
5+
3 | i8(128), // converted to -128
6+
4 | i8(-129), // converted to +127
77
vlib/v/checker/tests/overflow_int_signed_err.vv:3:2: error: value `128 overflows `i8`
88
1 | i8s := [
9-
2 | i8(0xff) // converted to -1
10-
3 | i8(128) // converted to -128
9+
2 | i8(0xff), // converted to -1
10+
3 | i8(128), // converted to -128
1111
| ~~~~~~~
12-
4 | i8(-129) // converted to +127
13-
5 | i8(-0xff) // converted to +1
12+
4 | i8(-129), // converted to +127
13+
5 | i8(-0xff), // converted to +1
14+
vlib/v/checker/tests/overflow_int_signed_err.vv:4:2: error: value `-129 overflows `i8`
15+
2 | i8(0xff), // converted to -1
16+
3 | i8(128), // converted to -128
17+
4 | i8(-129), // converted to +127
18+
| ~~~~~~~~
19+
5 | i8(-0xff), // converted to +1
20+
6 | ]
21+
vlib/v/checker/tests/overflow_int_signed_err.vv:5:2: error: value `-0xff overflows `i8`
22+
3 | i8(128), // converted to -128
23+
4 | i8(-129), // converted to +127
24+
5 | i8(-0xff), // converted to +1
25+
| ~~~~~~~~~
26+
6 | ]
27+
7 |
1428
vlib/v/checker/tests/overflow_int_signed_err.vv:9:2: error: value `0xffff overflows `i16`
1529
7 |
1630
8 | i16s := [
17-
9 | i16(0xffff) // converted to -1
31+
9 | i16(0xffff), // converted to -1
1832
| ~~~~~~~~~~~
19-
10 | i16(32768) // converted to -32768
20-
11 | i16(-32769) // converted to +32767
33+
10 | i16(32768), // converted to -32768
34+
11 | i16(-32769), // converted to +32767
2135
vlib/v/checker/tests/overflow_int_signed_err.vv:10:2: error: value `32768 overflows `i16`
2236
8 | i16s := [
23-
9 | i16(0xffff) // converted to -1
24-
10 | i16(32768) // converted to -32768
37+
9 | i16(0xffff), // converted to -1
38+
10 | i16(32768), // converted to -32768
2539
| ~~~~~~~~~~
26-
11 | i16(-32769) // converted to +32767
27-
12 | i16(-0xffff) // converted to +1
28-
vlib/v/checker/tests/overflow_int_signed_err.vv:16:2: error: value `0xffffffff overflows `int`
40+
11 | i16(-32769), // converted to +32767
41+
12 | i16(-0xffff), // converted to +1
42+
vlib/v/checker/tests/overflow_int_signed_err.vv:11:2: error: value `-32769 overflows `i16`
43+
9 | i16(0xffff), // converted to -1
44+
10 | i16(32768), // converted to -32768
45+
11 | i16(-32769), // converted to +32767
46+
| ~~~~~~~~~~~
47+
12 | i16(-0xffff), // converted to +1
48+
13 | ]
49+
vlib/v/checker/tests/overflow_int_signed_err.vv:12:2: error: value `-0xffff overflows `i16`
50+
10 | i16(32768), // converted to -32768
51+
11 | i16(-32769), // converted to +32767
52+
12 | i16(-0xffff), // converted to +1
53+
| ~~~~~~~~~~~~
54+
13 | ]
2955
14 |
30-
15 | ints := [
31-
16 | int(0xffffffff) // converted to -1
32-
| ~~~~~~~~~~~~~~~
33-
17 | int(2147483648) // converted to -2147483648
34-
18 | int(-2147483649) // converted to +2147483647
35-
vlib/v/checker/tests/overflow_int_signed_err.vv:17:2: error: value `2147483648 overflows `int`
36-
15 | ints := [
37-
16 | int(0xffffffff) // converted to -1
38-
17 | int(2147483648) // converted to -2147483648
39-
| ~~~~~~~~~~~~~~~
40-
18 | int(-2147483649) // converted to +2147483647
41-
19 | int(-0xffffffff) // converted to +1
56+
vlib/v/checker/tests/overflow_int_signed_err.vv:18:2: error: value `-2147483649 overflows `int`
57+
16 | int(0xffffffff), // converted to -1
58+
17 | int(2147483648), // converted to -2147483648
59+
18 | int(-2147483649), // converted to +2147483647
60+
| ~~~~~~~~~~~~~~~~
61+
19 | int(-0xffffffff), // converted to +1
62+
20 | ]
4263
vlib/v/checker/tests/overflow_int_signed_err.vv:23:2: error: value `0xffffffffffffffff overflows `i64`
4364
21 |
4465
22 | i64s := [
45-
23 | i64(0xffffffffffffffff) // converted to -1
66+
23 | i64(0xffffffffffffffff), // converted to -1
4667
| ~~~~~~~~~~~~~~~~~~~~~~~
47-
24 | i64(9223372036854775808) // converted to -9223372036854775808
48-
25 | i64(-9223372036854775809) // converted to +9223372036854775807
49-
vlib/v/checker/tests/overflow_int_signed_err.vv:24:2: error: value `9223372036854775808 overflows `i64`
50-
22 | i64s := [
51-
23 | i64(0xffffffffffffffff) // converted to -1
52-
24 | i64(9223372036854775808) // converted to -9223372036854775808
68+
24 | i64(9223372036854775808), // converted to -9223372036854775808
69+
25 | i64(-9223372036854775809), // converted to +9223372036854775807
70+
vlib/v/checker/tests/overflow_int_signed_err.vv:26:2: error: value `-0xffffffffffffffff overflows `i64`
71+
24 | i64(9223372036854775808), // converted to -9223372036854775808
72+
25 | i64(-9223372036854775809), // converted to +9223372036854775807
73+
26 | i64(-0xffffffffffffffff), // converted to +1
5374
| ~~~~~~~~~~~~~~~~~~~~~~~~
54-
25 | i64(-9223372036854775809) // converted to +9223372036854775807
55-
26 | i64(-0xffffffffffffffff) // converted to +1
75+
27 | ]
76+
28 |

vlib/v/gen/native/arm64.v

+2-2
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,9 @@ fn (mut c Arm64) sub_sp(v i32) {
102102
}
103103
// this is for 0x20 only
104104
if v < 0 {
105-
c.g.write32(i32(0x910083ff)) // add sp, X
105+
c.g.write32(i32(u32(0x910083ff))) // add sp, X
106106
} else {
107-
c.g.write32(i32(0xd10083ff)) // sub sp, X
107+
c.g.write32(i32(u32(0xd10083ff))) // sub sp, X
108108
}
109109
}
110110

0 commit comments

Comments
 (0)