Skip to content

Commit bac8685

Browse files
authored
Merge pull request #1855 from Smit-create/i-1839
ASR: Fix a bug in dict len
2 parents 3a6d92b + 425bde0 commit bac8685

File tree

4 files changed

+19
-15
lines changed

4 files changed

+19
-15
lines changed

integration_tests/test_dict_01.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,12 @@ def test_dict():
1616
assert abs(rollnumber2cpi[0] - 1.1) <= 1e-12
1717
assert len(rollnumber2cpi) == 1001
1818

19+
def test_issue_1839():
20+
assert len({1: 2, 1: 3, 4: 5}) == 2
21+
x: dict[i32, i32] = {}
22+
x = {1: 1, 1: 2, 1: 3}
23+
assert len(x) == 1
24+
25+
1926
test_dict()
27+
test_issue_1839()

src/lpython/semantics/python_intrinsic_eval.h

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -349,12 +349,10 @@ struct IntrinsicNodeHandler {
349349
return (ASR::asr_t *)ASR::down_cast<ASR::expr_t>(
350350
ASR::make_StringLen_t(al, loc, arg, to_type, value));
351351
} else if (ASR::is_a<ASR::Set_t>(*type)) {
352-
if (ASRUtils::expr_value(arg) != nullptr) {
353-
int64_t ival = (int64_t)ASR::down_cast<ASR::SetConstant_t>(
354-
ASRUtils::expr_value(arg))->n_elements;
355-
value = ASR::down_cast<ASR::expr_t>(make_IntegerConstant_t(al,
356-
loc, ival, to_type));
357-
}
352+
// Skip for set as they can have multiple same values
353+
// which shouldn't account for len.
354+
// Example: {1, 1, 1, 3} and the length should be 2.
355+
// So it should be handled in the backend.
358356
return (ASR::asr_t *)ASR::down_cast<ASR::expr_t>(
359357
ASR::make_SetLen_t(al, loc, arg, to_type, value));
360358
} else if (ASR::is_a<ASR::Tuple_t>(*type)) {
@@ -371,12 +369,10 @@ struct IntrinsicNodeHandler {
371369
return (ASR::asr_t *)ASR::down_cast<ASR::expr_t>(
372370
ASR::make_ListLen_t(al, loc, arg, to_type, value));
373371
} else if (ASR::is_a<ASR::Dict_t>(*type)) {
374-
if (ASRUtils::expr_value(arg) != nullptr) {
375-
int64_t ival = (int64_t)ASR::down_cast<ASR::DictConstant_t>(
376-
ASRUtils::expr_value(arg))->n_keys;
377-
value = ASR::down_cast<ASR::expr_t>(make_IntegerConstant_t(al,
378-
loc, ival, to_type));
379-
}
372+
// Skip for dictionary as they can have multiple same keys
373+
// which shouldn't account for len.
374+
// Example: {1: 2, 1: 3, 1: 4} and the length should be 1.
375+
// So it should be handled in the backend.
380376
return (ASR::asr_t *)ASR::down_cast<ASR::expr_t>(
381377
ASR::make_DictLen_t(al, loc, arg, to_type, value));
382378
}

tests/reference/asr-constants1-5828e8a.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"outfile": null,
77
"outfile_hash": null,
88
"stdout": "asr-constants1-5828e8a.stdout",
9-
"stdout_hash": "db1b5652c2fca471c43f0523c3ec810113caa1fd5f2be545084cb7e5",
9+
"stdout_hash": "85c3149c95930b691c248030d7907a6649078d38d3509ad54f78d891",
1010
"stderr": null,
1111
"stderr_hash": null,
1212
"returncode": 0

tests/reference/asr-constants1-5828e8a.stdout

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1479,7 +1479,7 @@
14791479
)
14801480
)
14811481
(Integer 4 [])
1482-
(IntegerConstant 3 (Integer 4 []))
1482+
()
14831483
)
14841484
()
14851485
)
@@ -1508,7 +1508,7 @@
15081508
)
15091509
)
15101510
(Integer 4 [])
1511-
(IntegerConstant 3 (Integer 4 []))
1511+
()
15121512
)
15131513
()
15141514
)

0 commit comments

Comments
 (0)