Skip to content

Commit 37a717e

Browse files
advikkabraczgdp1807
authored andcommitted
Add support for in place sets and dictionaries in function calls
1 parent 4b06d70 commit 37a717e

File tree

3 files changed

+23
-1
lines changed

3 files changed

+23
-1
lines changed

integration_tests/test_dict_14.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
from lpython import i32
22

3+
def takes_dict(a: dict[i32, i32]) -> dict[i32, i32]:
4+
return {1:1, 2:2}
5+
36
def test_dict():
47
d_i32: dict[i32, i32] = {5: 1, 5: 2}
58
d_str: dict[str, i32] = {'a': 1, 'a': 2}
@@ -62,4 +65,7 @@ def test_dict():
6265
l_i32_2.append(i)
6366
assert l_i32_2 == [30]
6467

68+
w: dict[i32, i32] = takes_dict({1:1, 2:2})
69+
assert len(w) == 2
70+
6571
test_dict()
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
from lpython import i32
22

3+
def takes_set(a: set[i32]) -> set[i32]:
4+
return {1, 2, 3}
5+
36
s1: set[str] = {"a", "b", "c", "a"}
47
s2: set[i32] = {1, 2, 3, 1}
58
s3: set[tuple[i32, i32]] = {(1, 2), (2, 3), (4, 5)}
69

10+
s4: set[i32] = takes_set({1, 2})
11+
712
assert len(s1) == 3
813
assert len(s2) == 3
914
assert len(s3) == 3
15+
assert len(s4) == 3

src/libasr/codegen/asr_to_llvm.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8859,6 +8859,14 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
88598859
target_type = llvm_utils->get_type_from_ttype_t_util(arg_type_, module.get());
88608860
break;
88618861
}
8862+
case (ASR::ttypeType::Dict): {
8863+
target_type = llvm_utils->get_type_from_ttype_t_util(arg_type_, module.get());
8864+
break;
8865+
}
8866+
case (ASR::ttypeType::Set): {
8867+
target_type = llvm_utils->get_type_from_ttype_t_util(arg_type_, module.get());
8868+
break;
8869+
}
88628870
default :
88638871
throw CodeGenError("Type " + ASRUtils::type_to_str(arg_type) + " not implemented yet.");
88648872
}
@@ -8913,7 +8921,9 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
89138921
llvm::AllocaInst *target = builder0.CreateAlloca(
89148922
target_type, nullptr, "call_arg_value");
89158923
if( ASR::is_a<ASR::Tuple_t>(*arg_type) ||
8916-
ASR::is_a<ASR::List_t>(*arg_type) ) {
8924+
ASR::is_a<ASR::List_t>(*arg_type) ||
8925+
ASR::is_a<ASR::Set_t>(*arg_type) ||
8926+
ASR::is_a<ASR::Dict_t>(*arg_type)) {
89178927
llvm_utils->deepcopy(value, target, arg_type, module.get(), name2memidx);
89188928
} else {
89198929
builder->CreateStore(value, target);

0 commit comments

Comments
 (0)