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 list.pop() for negative arguments #2456

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
5 changes: 5 additions & 0 deletions integration_tests/test_list_pop.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ def test_list_pop():
l1: list[i32]
l2: list[tuple[i32, f64]]
l3: list[list[str]]
l4: list[i32]
i: i32
j: i32
total: i32
Expand Down Expand Up @@ -45,6 +46,10 @@ def test_list_pop():
assert l1.pop(len(l1) - 1) == 4
assert l1 == [1, 2]

l4 = [1, 2, 3, 4, 5]
assert l4.pop(-5) == 1
assert l4.pop(-1) == 5

total = 10
l1 = []
for i in range(total):
Expand Down
18 changes: 17 additions & 1 deletion src/libasr/codegen/llvm_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4659,10 +4659,26 @@ namespace LCompilers {

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please update the equivalent C++ code above, to make it clear how the logic works?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, if we are going ahead with the change I will update the logic as well.
For understanding the logic is below -

if(pos<0){
      if(abs(pos)<= end_point){
            adjusted_pos = endpoint + pos
    }
}
else{
   adjusted_pos = pos
}
if(adjusted_pos <0 || adjusted_pos>=end_point) return IndexError

llvm::Value* end_point_ptr = get_pointer_to_current_end_point(list);
llvm::Value* end_point = LLVM::CreateLoad(*builder, end_point_ptr);
llvm::Value* zero = llvm::ConstantInt::get(llvm::Type::getInt32Ty(context),
llvm::APInt(32, 0));
llvm::Value* adjusted_pos = pos;
llvm::Value* is_negative = builder->CreateICmpSLT(pos, zero);
llvm::Value* abs_pos = builder->CreateSelect(is_negative,
builder->CreateNeg(pos),
pos
);
llvm::Value* is_negative_and_in_bounds = builder->CreateAnd(is_negative,
builder->CreateICmpULE(abs_pos, end_point)
);

// If the index is negative, convert it to the corresponding positive index
adjusted_pos = builder->CreateSelect(is_negative_and_in_bounds,
builder->CreateAdd(end_point, pos),
adjusted_pos
);
llvm::AllocaInst *pos_ptr = builder0.CreateAlloca(
llvm::Type::getInt32Ty(context), nullptr);
LLVM::CreateStore(*builder, pos, pos_ptr);
LLVM::CreateStore(*builder, adjusted_pos, pos_ptr);
llvm::Value* tmp = nullptr;

// Get element to return
Expand Down
Loading