Skip to content
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

Fix unsigned to signed int cast #2178

Merged
merged 3 commits into from
Jul 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions integration_tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,7 @@ RUN(NAME test_unary_op_05 LABELS cpython llvm c) # unsigned unary minus, plus
RUN(NAME test_unary_op_06 LABELS cpython llvm c) # unsigned unary bitnot
RUN(NAME test_unsigned_01 LABELS cpython llvm c) # unsigned bitshift left, right
RUN(NAME test_unsigned_02 LABELS cpython llvm c)
RUN(NAME test_unsigned_03 LABELS cpython llvm c)
RUN(NAME test_bool_binop LABELS cpython llvm c)
RUN(NAME test_issue_518 LABELS cpython llvm c NOFAST)
RUN(NAME structs_01 LABELS cpython llvm c)
Expand Down
17 changes: 11 additions & 6 deletions integration_tests/test_unsigned_02.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
from lpython import u16, i32
from lpython import u16, i32, u8, u32, u64

# test issue 2170

i : i32
u : u16 = u16(32768)
x : i32
u_1 : u16 = u16(32768)
u_2 : u8 = u8(24)
u_3 : u32 = u32(32768)
u_4 : u64 = u64(32768)

for i in range(i32(u)):
x = i * 2
assert u_1 == u16(32768)
assert u_2 == u8(24)
assert u_3 == u32(32768)
assert u_4 == u64(32768)

print(u_1, u_2, u_3, u_4)
19 changes: 19 additions & 0 deletions integration_tests/test_unsigned_03.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from lpython import u16, i32, u8, u16, u64, i64, u32, i8

# test issue 2174

def f():
u: u16 = u16(32768)
assert i32(u) == 32768
u1: u8 = u8(23)
assert i8(u1) == i8(23)
assert u16(u1) == u16(23)
assert u32(u1) == u32(23)
assert u64(u1) == u64(23)
print(i8(u1), u16(u1), u32(u1), u64(u1))
assert i64(u1) == i64(23)
assert i64(u) == i64(32768)
assert i32(u1) == 23
print(i64(u), i32(u))

f()
2 changes: 1 addition & 1 deletion src/libasr/codegen/asr_to_llvm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6329,7 +6329,7 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
arg_kind != dest_kind )
{
if (dest_kind > arg_kind) {
tmp = builder->CreateSExt(tmp, llvm_utils->getIntType(dest_kind));
tmp = builder->CreateZExt(tmp, llvm_utils->getIntType(dest_kind));
} else {
tmp = builder->CreateTrunc(tmp, llvm_utils->getIntType(dest_kind));
}
Expand Down