Skip to content

Commit b9850da

Browse files
kabra1110czgdp1807
andauthored
Support bool typed keys in dict (#1771)
Co-authored-by: Gagandeep Singh <[email protected]>
1 parent bb8bfe7 commit b9850da

File tree

3 files changed

+51
-0
lines changed

3 files changed

+51
-0
lines changed

integration_tests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,7 @@ RUN(NAME test_dict_08 LABELS cpython llvm c)
374374
RUN(NAME test_dict_09 LABELS cpython llvm c)
375375
RUN(NAME test_dict_10 LABELS cpython llvm) # TODO: Add support of dict with string in C backend
376376
RUN(NAME test_dict_11 LABELS cpython llvm c)
377+
RUN(NAME test_dict_bool LABELS cpython llvm)
377378
RUN(NAME test_for_loop LABELS cpython llvm c)
378379
RUN(NAME modules_01 LABELS cpython llvm c wasm wasm_x86 wasm_x64)
379380
RUN(NAME modules_02 LABELS cpython llvm c wasm wasm_x86 wasm_x64)

integration_tests/test_dict_bool.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
from lpython import i32, f64
2+
3+
def test_dict_bool():
4+
d_int: dict[bool, i32] = {}
5+
d_float: dict[bool, f64] = {}
6+
d_str: dict[bool, str] = {}
7+
i: i32
8+
j: f64
9+
s: str = ""
10+
l_str: list[str] = ["a", "b", "c", "d"]
11+
12+
for i in range(10):
13+
d_int[True] = i
14+
assert d_int[True] == i
15+
16+
for i in range(10, 20):
17+
d_int[True] = i
18+
d_int[False] = i + 1
19+
assert d_int[True] == d_int[False] - 1
20+
assert d_int[True] == i
21+
22+
d_int[True] = 0
23+
d_int[False] = d_int[True]
24+
25+
for i in range(10, 99):
26+
d_int[i%2 == 0] = d_int[i%2 == 0] + 1
27+
assert d_int[True] == d_int[False] + 1
28+
assert d_int[True] == 45
29+
30+
j = 0.0
31+
while j < 1.0:
32+
d_float[False] = j + 1.0
33+
d_float[True] = d_float[False] * d_float[False]
34+
assert d_float[True] == (j + 1.0) * (j + 1.0)
35+
assert d_float[False] == j + 1.0
36+
j = j + 0.1
37+
38+
d_str[False] = s
39+
40+
for i in range(len(l_str)):
41+
d_str[True] = d_str[False]
42+
s += l_str[i]
43+
d_str[False] = s
44+
assert d_str[True] + l_str[i] == d_str[False]
45+
assert d_str[False] == s
46+
47+
test_dict_bool()

src/libasr/codegen/llvm_utils.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1962,6 +1962,9 @@ namespace LCompilers {
19621962
}
19631963
return tuple_hash;
19641964
}
1965+
case ASR::ttypeType::Logical: {
1966+
return builder->CreateZExt(key, llvm::Type::getInt32Ty(context));
1967+
}
19651968
default: {
19661969
throw LCompilersException("Hashing " + ASRUtils::type_to_str_python(key_asr_type) +
19671970
" isn't implemented yet.");

0 commit comments

Comments
 (0)