Skip to content

Commit 31d3a29

Browse files
authored
Merge pull request #2178 from Smit-create/i-2174
Fix unsigned to signed int cast
2 parents 3d61ec0 + 6dc605a commit 31d3a29

File tree

4 files changed

+32
-7
lines changed

4 files changed

+32
-7
lines changed

integration_tests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -574,6 +574,7 @@ RUN(NAME test_unary_op_05 LABELS cpython llvm c) # unsigned unary minus, plus
574574
RUN(NAME test_unary_op_06 LABELS cpython llvm c) # unsigned unary bitnot
575575
RUN(NAME test_unsigned_01 LABELS cpython llvm c) # unsigned bitshift left, right
576576
RUN(NAME test_unsigned_02 LABELS cpython llvm c)
577+
RUN(NAME test_unsigned_03 LABELS cpython llvm c)
577578
RUN(NAME test_bool_binop LABELS cpython llvm c)
578579
RUN(NAME test_issue_518 LABELS cpython llvm c NOFAST)
579580
RUN(NAME structs_01 LABELS cpython llvm c)
Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
1-
from lpython import u16, i32
1+
from lpython import u16, i32, u8, u32, u64
22

33
# test issue 2170
44

5-
i : i32
6-
u : u16 = u16(32768)
7-
x : i32
5+
u_1 : u16 = u16(32768)
6+
u_2 : u8 = u8(24)
7+
u_3 : u32 = u32(32768)
8+
u_4 : u64 = u64(32768)
89

9-
for i in range(i32(u)):
10-
x = i * 2
10+
assert u_1 == u16(32768)
11+
assert u_2 == u8(24)
12+
assert u_3 == u32(32768)
13+
assert u_4 == u64(32768)
14+
15+
print(u_1, u_2, u_3, u_4)
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from lpython import u16, i32, u8, u16, u64, i64, u32, i8
2+
3+
# test issue 2174
4+
5+
def f():
6+
u: u16 = u16(32768)
7+
assert i32(u) == 32768
8+
u1: u8 = u8(23)
9+
assert i8(u1) == i8(23)
10+
assert u16(u1) == u16(23)
11+
assert u32(u1) == u32(23)
12+
assert u64(u1) == u64(23)
13+
print(i8(u1), u16(u1), u32(u1), u64(u1))
14+
assert i64(u1) == i64(23)
15+
assert i64(u) == i64(32768)
16+
assert i32(u1) == 23
17+
print(i64(u), i32(u))
18+
19+
f()

src/libasr/codegen/asr_to_llvm.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6329,7 +6329,7 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
63296329
arg_kind != dest_kind )
63306330
{
63316331
if (dest_kind > arg_kind) {
6332-
tmp = builder->CreateSExt(tmp, llvm_utils->getIntType(dest_kind));
6332+
tmp = builder->CreateZExt(tmp, llvm_utils->getIntType(dest_kind));
63336333
} else {
63346334
tmp = builder->CreateTrunc(tmp, llvm_utils->getIntType(dest_kind));
63356335
}

0 commit comments

Comments
 (0)