From 5d40049c4cf62c4f273088247b56df8c2039b67a Mon Sep 17 00:00:00 2001 From: Saurabh Kumar Date: Thu, 7 Mar 2024 12:01:58 +0530 Subject: [PATCH 01/26] Add support for accessing items from data-structures within `Const` --- src/libasr/codegen/asr_to_llvm.cpp | 11 +++-- src/lpython/semantics/python_ast_to_asr.cpp | 49 ++++++++++++++++++++- 2 files changed, 55 insertions(+), 5 deletions(-) diff --git a/src/libasr/codegen/asr_to_llvm.cpp b/src/libasr/codegen/asr_to_llvm.cpp index d5af2946f7..e7b4053ca2 100644 --- a/src/libasr/codegen/asr_to_llvm.cpp +++ b/src/libasr/codegen/asr_to_llvm.cpp @@ -1523,8 +1523,9 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor } void visit_ListItem(const ASR::ListItem_t& x) { - ASR::ttype_t* el_type = ASRUtils::get_contained_type( - ASRUtils::expr_type(x.m_a)); + ASR::ttype_t *el_type = ASR::is_a(*ASRUtils::expr_type(x.m_a)) + ? ASRUtils::get_contained_type(ASRUtils::type_get_past_const(ASRUtils::expr_type(x.m_a))) + : ASRUtils::get_contained_type(ASRUtils::expr_type(x.m_a)); int64_t ptr_loads_copy = ptr_loads; ptr_loads = 0; this->visit_expr(*x.m_a); @@ -1540,8 +1541,10 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor } void visit_DictItem(const ASR::DictItem_t& x) { - ASR::Dict_t* dict_type = ASR::down_cast( - ASRUtils::expr_type(x.m_a)); + ASR::Dict_t *dict_type = ASR::is_a(*ASRUtils::expr_type(x.m_a)) + ? ASR::down_cast(ASRUtils::type_get_past_const(ASRUtils::expr_type(x.m_a))) + : ASR::down_cast(ASRUtils::expr_type(x.m_a)); + int64_t ptr_loads_copy = ptr_loads; ptr_loads = 0; this->visit_expr(*x.m_a); diff --git a/src/lpython/semantics/python_ast_to_asr.cpp b/src/lpython/semantics/python_ast_to_asr.cpp index 4e11fe01ed..c460b5cc84 100644 --- a/src/lpython/semantics/python_ast_to_asr.cpp +++ b/src/lpython/semantics/python_ast_to_asr.cpp @@ -1750,6 +1750,12 @@ class CommonVisitor : public AST::BaseVisitor { } else if (var_annotation == "Const") { ASR::ttype_t *type = ast_expr_to_asr_type(loc, *s->m_slice, is_allocatable, raise_error, abi, is_argument); + if (ASR::is_a(*type)) { + throw SemanticError("'Const' not required as tuples are already immutable", loc); + } + else if (ASR::is_a(*type)) { + throw SemanticError("'Const' not required as strings are already immutable", loc); + } return ASRUtils::TYPE(ASR::make_Const_t(al, loc, type)); } else { AST::expr_t* dim_info = s->m_slice; @@ -3730,6 +3736,12 @@ class CommonVisitor : public AST::BaseVisitor { } else if (ASR::is_a(*type)) { throw SemanticError("unhashable type in dict: 'slice'", loc); } + if (ASR::is_a(*type)) + { + if (ASR::is_a(*ASRUtils::type_get_past_const(type))) { + throw SemanticError("slicing on a const list is not implemented till now", loc); + } + } } else if(AST::is_a(*m_slice) && ASRUtils::is_array(type)) { bool final_result = true; @@ -3740,7 +3752,42 @@ class CommonVisitor : public AST::BaseVisitor { } return final_result; } else { + ASR::expr_t *index = nullptr; this->visit_expr(*m_slice); + if (ASR::is_a(*type)) + { + ASR::ttype_t *contained_type = ASRUtils::type_get_past_const(type); + if (ASR::is_a(*contained_type)) + { + index = ASRUtils::EXPR(tmp); + ASR::ttype_t *key_type = ASR::down_cast(contained_type)->m_key_type; + if (!ASRUtils::check_equal_type(ASRUtils::expr_type(index), key_type)) + { + throw SemanticError("Key type should be '" + ASRUtils::type_to_str_python(key_type) + + "' instead of '" + + ASRUtils::type_to_str_python(ASRUtils::expr_type(index)) + "'", + index->base.loc); + } + tmp = ASR::make_DictItem_t(al, loc, value, index, nullptr, ASR::down_cast(contained_type)->m_value_type, nullptr); + return false; + } + else if (ASR::is_a(*contained_type)) + { + this->visit_expr(*m_slice); + index = ASRUtils::EXPR(tmp); + tmp = make_ListItem_t(al, loc, value, index, + ASR::down_cast(contained_type)->m_type, nullptr); + return false; + } + else if (ASR::is_a(*contained_type)) + { + throw SemanticError("'Const' not required as strings are already immutable", loc); + } + else if (ASR::is_a(*contained_type)) + { + throw SemanticError("'Const' not required as tuples are already immutable", loc); + } + } if (!ASR::is_a(*type) && !ASRUtils::is_integer(*ASRUtils::expr_type(ASRUtils::EXPR(tmp)))) { std::string fnd = ASRUtils::type_to_str_python(ASRUtils::expr_type(ASRUtils::EXPR(tmp))); @@ -3753,8 +3800,8 @@ class CommonVisitor : public AST::BaseVisitor { ); throw SemanticAbort(); } - ASR::expr_t *index = nullptr; if (ASR::is_a(*type)) { + this->visit_expr(*m_slice); index = ASRUtils::EXPR(tmp); ASR::ttype_t *key_type = ASR::down_cast(type)->m_key_type; if (!ASRUtils::check_equal_type(ASRUtils::expr_type(index), key_type)) { From 2fb304a7d8c3ce6612048f185188c5427c21edda Mon Sep 17 00:00:00 2001 From: Saurabh Kumar <151380951+kmr-srbh@users.noreply.github.com> Date: Thu, 7 Mar 2024 13:16:45 +0530 Subject: [PATCH 02/26] Update type checking logic for `Const dict` Co-authored-by: Thirumalai Shaktivel <74826228+Thirumalai-Shaktivel@users.noreply.github.com> --- src/libasr/codegen/asr_to_llvm.cpp | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/libasr/codegen/asr_to_llvm.cpp b/src/libasr/codegen/asr_to_llvm.cpp index e7b4053ca2..f4b3714ee7 100644 --- a/src/libasr/codegen/asr_to_llvm.cpp +++ b/src/libasr/codegen/asr_to_llvm.cpp @@ -1541,9 +1541,7 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor } void visit_DictItem(const ASR::DictItem_t& x) { - ASR::Dict_t *dict_type = ASR::is_a(*ASRUtils::expr_type(x.m_a)) - ? ASR::down_cast(ASRUtils::type_get_past_const(ASRUtils::expr_type(x.m_a))) - : ASR::down_cast(ASRUtils::expr_type(x.m_a)); + ASR::Dict_t *dict_type = ASR::down_cast(ASRUtils::type_get_past_const(ASRUtils::expr_type(x.m_a))); int64_t ptr_loads_copy = ptr_loads; ptr_loads = 0; From 416d1c32c1fed93049c3c1fb7da5a79e625aaa1f Mon Sep 17 00:00:00 2001 From: Saurabh Kumar <151380951+kmr-srbh@users.noreply.github.com> Date: Thu, 7 Mar 2024 13:23:33 +0530 Subject: [PATCH 03/26] Minor formatting change Co-authored-by: Thirumalai Shaktivel <74826228+Thirumalai-Shaktivel@users.noreply.github.com> --- src/lpython/semantics/python_ast_to_asr.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/lpython/semantics/python_ast_to_asr.cpp b/src/lpython/semantics/python_ast_to_asr.cpp index c460b5cc84..8acff7ff42 100644 --- a/src/lpython/semantics/python_ast_to_asr.cpp +++ b/src/lpython/semantics/python_ast_to_asr.cpp @@ -3754,8 +3754,7 @@ class CommonVisitor : public AST::BaseVisitor { } else { ASR::expr_t *index = nullptr; this->visit_expr(*m_slice); - if (ASR::is_a(*type)) - { + if (ASR::is_a(*type)) { ASR::ttype_t *contained_type = ASRUtils::type_get_past_const(type); if (ASR::is_a(*contained_type)) { From e622a9d04f718a5b5ee70f9e264784d26b757d9a Mon Sep 17 00:00:00 2001 From: Saurabh Kumar Date: Sun, 10 Mar 2024 14:07:29 +0530 Subject: [PATCH 04/26] Handle negative indices for `const list` and minor formatting changes --- src/lpython/semantics/python_ast_to_asr.cpp | 33 +++++++++++---------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/src/lpython/semantics/python_ast_to_asr.cpp b/src/lpython/semantics/python_ast_to_asr.cpp index d76ce7fe69..517b50d7c7 100644 --- a/src/lpython/semantics/python_ast_to_asr.cpp +++ b/src/lpython/semantics/python_ast_to_asr.cpp @@ -3756,36 +3756,39 @@ class CommonVisitor : public AST::BaseVisitor { this->visit_expr(*m_slice); if (ASR::is_a(*type)) { ASR::ttype_t *contained_type = ASRUtils::type_get_past_const(type); - if (ASR::is_a(*contained_type)) - { + if (ASR::is_a(*contained_type)) { index = ASRUtils::EXPR(tmp); ASR::ttype_t *key_type = ASR::down_cast(contained_type)->m_key_type; - if (!ASRUtils::check_equal_type(ASRUtils::expr_type(index), key_type)) - { + if (!ASRUtils::check_equal_type(ASRUtils::expr_type(index), key_type)) { throw SemanticError("Key type should be '" + ASRUtils::type_to_str_python(key_type) + "' instead of '" + ASRUtils::type_to_str_python(ASRUtils::expr_type(index)) + "'", index->base.loc); } - tmp = ASR::make_DictItem_t(al, loc, value, index, nullptr, ASR::down_cast(contained_type)->m_value_type, nullptr); + tmp = ASR::make_DictItem_t(al, loc, value, index, nullptr, + ASR::down_cast(contained_type)->m_value_type, nullptr); return false; } - else if (ASR::is_a(*contained_type)) - { + else if (ASR::is_a(*contained_type)) { this->visit_expr(*m_slice); index = ASRUtils::EXPR(tmp); + ASR::expr_t* val = ASRUtils::expr_value(index); + if (val && ASR::is_a(*val)) { + if (ASR::down_cast(val)->m_n < 0) { + // Replace `x[-1]` to `x[len(x)+(-1)]` + ASR::ttype_t *int_type = ASRUtils::TYPE(ASR::make_Integer_t( + al, loc, 4)); + ASR::expr_t *list_len = ASRUtils::EXPR(ASR::make_ListLen_t( + al, loc, value, int_type, nullptr)); + ASR::expr_t *neg_idx = ASRUtils::expr_value(index); + index = ASRUtils::EXPR(ASR::make_IntegerBinOp_t(al, loc, + list_len, ASR::binopType::Add, neg_idx, int_type, nullptr)); + } + } tmp = make_ListItem_t(al, loc, value, index, ASR::down_cast(contained_type)->m_type, nullptr); return false; } - else if (ASR::is_a(*contained_type)) - { - throw SemanticError("'Const' not required as strings are already immutable", loc); - } - else if (ASR::is_a(*contained_type)) - { - throw SemanticError("'Const' not required as tuples are already immutable", loc); - } } if (!ASR::is_a(*type) && !ASRUtils::is_integer(*ASRUtils::expr_type(ASRUtils::EXPR(tmp)))) { From 1a3a5d519bd1542f3747b37dd51b3ba41a167000 Mon Sep 17 00:00:00 2001 From: Saurabh Kumar Date: Sun, 10 Mar 2024 14:09:14 +0530 Subject: [PATCH 05/26] Add tests --- integration_tests/test_const_access.py | 14 ++++++++++++++ tests/errors/test_const_str_access.py | 8 ++++++++ tests/errors/test_const_tuple_access.py | 9 +++++++++ tests/tests.toml | 12 ++++++++++++ 4 files changed, 43 insertions(+) create mode 100644 integration_tests/test_const_access.py create mode 100644 tests/errors/test_const_str_access.py create mode 100644 tests/errors/test_const_tuple_access.py diff --git a/integration_tests/test_const_access.py b/integration_tests/test_const_access.py new file mode 100644 index 0000000000..253f4bc3a3 --- /dev/null +++ b/integration_tests/test_const_access.py @@ -0,0 +1,14 @@ +from lpython import i32, str, list, dict, Const + + +def test_const_access(): + CONST_LIST: Const[list[i32]] = [1, 2, 3, 4, 5] + CONST_DICTIONARY: Const[dict[str, i32]] = {"a": 1, "b": 2, "c": 3} + + assert CONST_LIST[0] == 1 + assert CONST_LIST[-2] == 4 + + assert CONST_DICTIONARY["a"] == 1 + + +test_const_access() diff --git a/tests/errors/test_const_str_access.py b/tests/errors/test_const_str_access.py new file mode 100644 index 0000000000..bc25387af8 --- /dev/null +++ b/tests/errors/test_const_str_access.py @@ -0,0 +1,8 @@ +from lpython import str, Const + + +def test_const_str_access(): + CONST_STRING: Const[str] = "Hello, LPython!" + + +test_const_str_access() diff --git a/tests/errors/test_const_tuple_access.py b/tests/errors/test_const_tuple_access.py new file mode 100644 index 0000000000..e8ea8b427d --- /dev/null +++ b/tests/errors/test_const_tuple_access.py @@ -0,0 +1,9 @@ +from lpython import str, tuple, Const + + +def test_const_tuple_access(): + CONST_TUPLE: Const[tuple[str, str, str]] = ("hello", "LPython", "!") + + +test_const_tuple_access() + diff --git a/tests/tests.toml b/tests/tests.toml index 972c72bf91..3e3d3c05ff 100644 --- a/tests/tests.toml +++ b/tests/tests.toml @@ -151,6 +151,10 @@ asr = true llvm = true llvm_dbg = true +[[test]] +filename = "../integration_tests/test_const_access.py" +asr = true + [[test]] filename = "../integration_tests/array_01_decl.py" asr = true @@ -1167,6 +1171,14 @@ asr = true filename = "errors/test_type_mismatch_01.py" asr = true +[[test]] +filename = "errors/test_const_str_access.py" +asr = true + +[[test]] +filename = "errors/test_const_tuple_access.py" +asr = true + # tests/tokens/errors [[test]] From 1f74255d0aa79e816317206c0ecdc9463002434f Mon Sep 17 00:00:00 2001 From: Saurabh Kumar Date: Sun, 10 Mar 2024 14:09:58 +0530 Subject: [PATCH 06/26] Update test references --- .../asr-test_const_access-82a9a24.json | 13 + .../asr-test_const_access-82a9a24.stdout | 266 ++++++++++++++++++ .../asr-test_const_str_access-59ff543.json | 13 + .../asr-test_const_str_access-59ff543.stderr | 5 + .../asr-test_const_tuple_access-0d4c6df.json | 13 + ...asr-test_const_tuple_access-0d4c6df.stderr | 5 + 6 files changed, 315 insertions(+) create mode 100644 tests/reference/asr-test_const_access-82a9a24.json create mode 100644 tests/reference/asr-test_const_access-82a9a24.stdout create mode 100644 tests/reference/asr-test_const_str_access-59ff543.json create mode 100644 tests/reference/asr-test_const_str_access-59ff543.stderr create mode 100644 tests/reference/asr-test_const_tuple_access-0d4c6df.json create mode 100644 tests/reference/asr-test_const_tuple_access-0d4c6df.stderr diff --git a/tests/reference/asr-test_const_access-82a9a24.json b/tests/reference/asr-test_const_access-82a9a24.json new file mode 100644 index 0000000000..9a5d516eca --- /dev/null +++ b/tests/reference/asr-test_const_access-82a9a24.json @@ -0,0 +1,13 @@ +{ + "basename": "asr-test_const_access-82a9a24", + "cmd": "lpython --show-asr --no-color {infile} -o {outfile}", + "infile": "tests/../integration_tests/test_const_access.py", + "infile_hash": "c25c7c1c19db38e066b47dc2891abaa5ddd7379b4df07892cebb94e3", + "outfile": null, + "outfile_hash": null, + "stdout": "asr-test_const_access-82a9a24.stdout", + "stdout_hash": "a98657aca0fefd42749ce13d47c09daf48deed01ec24170d242f5ee3", + "stderr": null, + "stderr_hash": null, + "returncode": 0 +} \ No newline at end of file diff --git a/tests/reference/asr-test_const_access-82a9a24.stdout b/tests/reference/asr-test_const_access-82a9a24.stdout new file mode 100644 index 0000000000..8cc7ff33d7 --- /dev/null +++ b/tests/reference/asr-test_const_access-82a9a24.stdout @@ -0,0 +1,266 @@ +(TranslationUnit + (SymbolTable + 1 + { + __main__: + (Module + (SymbolTable + 2 + { + __main__global_stmts: + (Function + (SymbolTable + 4 + { + + }) + __main__global_stmts + (FunctionType + [] + () + Source + Implementation + () + .false. + .false. + .false. + .false. + .false. + [] + .false. + ) + [test_const_access] + [] + [(SubroutineCall + 2 test_const_access + () + [] + () + )] + () + Public + .false. + .false. + () + ), + test_const_access: + (Function + (SymbolTable + 3 + { + CONST_DICTIONARY: + (Variable + 3 + CONST_DICTIONARY + [] + Local + (DictConstant + [(StringConstant + "a" + (Character 1 1 ()) + ) + (StringConstant + "b" + (Character 1 1 ()) + ) + (StringConstant + "c" + (Character 1 1 ()) + )] + [(IntegerConstant 1 (Integer 4)) + (IntegerConstant 2 (Integer 4)) + (IntegerConstant 3 (Integer 4))] + (Dict + (Character 1 1 ()) + (Integer 4) + ) + ) + (DictConstant + [(StringConstant + "a" + (Character 1 1 ()) + ) + (StringConstant + "b" + (Character 1 1 ()) + ) + (StringConstant + "c" + (Character 1 1 ()) + )] + [(IntegerConstant 1 (Integer 4)) + (IntegerConstant 2 (Integer 4)) + (IntegerConstant 3 (Integer 4))] + (Dict + (Character 1 1 ()) + (Integer 4) + ) + ) + Parameter + (Const + (Dict + (Character 1 -2 ()) + (Integer 4) + ) + ) + () + Source + Public + Required + .false. + ), + CONST_LIST: + (Variable + 3 + CONST_LIST + [] + Local + (ListConstant + [(IntegerConstant 1 (Integer 4)) + (IntegerConstant 2 (Integer 4)) + (IntegerConstant 3 (Integer 4)) + (IntegerConstant 4 (Integer 4)) + (IntegerConstant 5 (Integer 4))] + (List + (Integer 4) + ) + ) + (ListConstant + [(IntegerConstant 1 (Integer 4)) + (IntegerConstant 2 (Integer 4)) + (IntegerConstant 3 (Integer 4)) + (IntegerConstant 4 (Integer 4)) + (IntegerConstant 5 (Integer 4))] + (List + (Integer 4) + ) + ) + Parameter + (Const + (List + (Integer 4) + ) + ) + () + Source + Public + Required + .false. + ) + }) + test_const_access + (FunctionType + [] + () + Source + Implementation + () + .false. + .false. + .false. + .false. + .false. + [] + .false. + ) + [] + [] + [(Assert + (IntegerCompare + (ListItem + (Var 3 CONST_LIST) + (IntegerConstant 0 (Integer 4)) + (Integer 4) + () + ) + Eq + (IntegerConstant 1 (Integer 4)) + (Logical 4) + () + ) + () + ) + (Assert + (IntegerCompare + (ListItem + (Var 3 CONST_LIST) + (IntegerBinOp + (ListLen + (Var 3 CONST_LIST) + (Integer 4) + () + ) + Add + (IntegerConstant -2 (Integer 4)) + (Integer 4) + () + ) + (Integer 4) + () + ) + Eq + (IntegerConstant 4 (Integer 4)) + (Logical 4) + () + ) + () + ) + (Assert + (IntegerCompare + (DictItem + (Var 3 CONST_DICTIONARY) + (StringConstant + "a" + (Character 1 1 ()) + ) + () + (Integer 4) + () + ) + Eq + (IntegerConstant 1 (Integer 4)) + (Logical 4) + () + ) + () + )] + () + Public + .false. + .false. + () + ) + }) + __main__ + [] + .false. + .false. + ), + main_program: + (Program + (SymbolTable + 5 + { + __main__global_stmts: + (ExternalSymbol + 5 + __main__global_stmts + 2 __main__global_stmts + __main__ + [] + __main__global_stmts + Public + ) + }) + main_program + [__main__] + [(SubroutineCall + 5 __main__global_stmts + 2 __main__global_stmts + [] + () + )] + ) + }) + [] +) diff --git a/tests/reference/asr-test_const_str_access-59ff543.json b/tests/reference/asr-test_const_str_access-59ff543.json new file mode 100644 index 0000000000..bbe2263327 --- /dev/null +++ b/tests/reference/asr-test_const_str_access-59ff543.json @@ -0,0 +1,13 @@ +{ + "basename": "asr-test_const_str_access-59ff543", + "cmd": "lpython --show-asr --no-color {infile} -o {outfile}", + "infile": "tests/errors/test_const_str_access.py", + "infile_hash": "356d24d72537679b048256fe36e06defe1525ea6abbdef9d9a05bce7", + "outfile": null, + "outfile_hash": null, + "stdout": null, + "stdout_hash": null, + "stderr": "asr-test_const_str_access-59ff543.stderr", + "stderr_hash": "22aab31426a34d40549295ecdbdac10ecdf67689aa464522f42b0f52", + "returncode": 2 +} \ No newline at end of file diff --git a/tests/reference/asr-test_const_str_access-59ff543.stderr b/tests/reference/asr-test_const_str_access-59ff543.stderr new file mode 100644 index 0000000000..d73d860df0 --- /dev/null +++ b/tests/reference/asr-test_const_str_access-59ff543.stderr @@ -0,0 +1,5 @@ +semantic error: 'Const' not required as strings are already immutable + --> tests/errors/test_const_str_access.py:5:19 + | +5 | CONST_STRING: Const[str] = "Hello, LPython!" + | ^^^^^^^^^^ diff --git a/tests/reference/asr-test_const_tuple_access-0d4c6df.json b/tests/reference/asr-test_const_tuple_access-0d4c6df.json new file mode 100644 index 0000000000..82758eb648 --- /dev/null +++ b/tests/reference/asr-test_const_tuple_access-0d4c6df.json @@ -0,0 +1,13 @@ +{ + "basename": "asr-test_const_tuple_access-0d4c6df", + "cmd": "lpython --show-asr --no-color {infile} -o {outfile}", + "infile": "tests/errors/test_const_tuple_access.py", + "infile_hash": "7812a4013cd5e558c05323cb3a1d85287359e9f1f98bc22e46c6152e", + "outfile": null, + "outfile_hash": null, + "stdout": null, + "stdout_hash": null, + "stderr": "asr-test_const_tuple_access-0d4c6df.stderr", + "stderr_hash": "540d1fcd3312219511607039b3410b25cb58f2bbd00196441b880c44", + "returncode": 2 +} \ No newline at end of file diff --git a/tests/reference/asr-test_const_tuple_access-0d4c6df.stderr b/tests/reference/asr-test_const_tuple_access-0d4c6df.stderr new file mode 100644 index 0000000000..d8b02005a5 --- /dev/null +++ b/tests/reference/asr-test_const_tuple_access-0d4c6df.stderr @@ -0,0 +1,5 @@ +semantic error: 'Const' not required as tuples are already immutable + --> tests/errors/test_const_tuple_access.py:5:18 + | +5 | CONST_TUPLE: Const[tuple[str, str, str]] = ("hello", "LPython", "!") + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ From 20aa660573103c45ddb62b30ec2ec60c765f7a35 Mon Sep 17 00:00:00 2001 From: Saurabh Kumar Date: Sun, 10 Mar 2024 17:29:26 +0530 Subject: [PATCH 07/26] Heavily simplify handling `const` --- src/libasr/codegen/asr_to_llvm.cpp | 4 +- src/lpython/semantics/python_ast_to_asr.cpp | 43 +-------------------- 2 files changed, 2 insertions(+), 45 deletions(-) diff --git a/src/libasr/codegen/asr_to_llvm.cpp b/src/libasr/codegen/asr_to_llvm.cpp index f4b3714ee7..bc522ee733 100644 --- a/src/libasr/codegen/asr_to_llvm.cpp +++ b/src/libasr/codegen/asr_to_llvm.cpp @@ -1523,9 +1523,7 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor } void visit_ListItem(const ASR::ListItem_t& x) { - ASR::ttype_t *el_type = ASR::is_a(*ASRUtils::expr_type(x.m_a)) - ? ASRUtils::get_contained_type(ASRUtils::type_get_past_const(ASRUtils::expr_type(x.m_a))) - : ASRUtils::get_contained_type(ASRUtils::expr_type(x.m_a)); + ASR::ttype_t *el_type = ASRUtils::get_contained_type(ASRUtils::type_get_past_const(ASRUtils::expr_type(x.m_a))); int64_t ptr_loads_copy = ptr_loads; ptr_loads = 0; this->visit_expr(*x.m_a); diff --git a/src/lpython/semantics/python_ast_to_asr.cpp b/src/lpython/semantics/python_ast_to_asr.cpp index 517b50d7c7..5e53177de8 100644 --- a/src/lpython/semantics/python_ast_to_asr.cpp +++ b/src/lpython/semantics/python_ast_to_asr.cpp @@ -3686,6 +3686,7 @@ class CommonVisitor : public AST::BaseVisitor { ai.m_left = nullptr; ai.m_right = nullptr; ai.m_step = nullptr; + type = ASRUtils::type_get_past_const(type); if (AST::is_a(*m_slice)) { AST::Slice_t *sl = AST::down_cast(m_slice); if (sl->m_lower != nullptr) { @@ -3736,12 +3737,6 @@ class CommonVisitor : public AST::BaseVisitor { } else if (ASR::is_a(*type)) { throw SemanticError("unhashable type in dict: 'slice'", loc); } - if (ASR::is_a(*type)) - { - if (ASR::is_a(*ASRUtils::type_get_past_const(type))) { - throw SemanticError("slicing on a const list is not implemented till now", loc); - } - } } else if(AST::is_a(*m_slice) && ASRUtils::is_array(type)) { bool final_result = true; @@ -3754,42 +3749,6 @@ class CommonVisitor : public AST::BaseVisitor { } else { ASR::expr_t *index = nullptr; this->visit_expr(*m_slice); - if (ASR::is_a(*type)) { - ASR::ttype_t *contained_type = ASRUtils::type_get_past_const(type); - if (ASR::is_a(*contained_type)) { - index = ASRUtils::EXPR(tmp); - ASR::ttype_t *key_type = ASR::down_cast(contained_type)->m_key_type; - if (!ASRUtils::check_equal_type(ASRUtils::expr_type(index), key_type)) { - throw SemanticError("Key type should be '" + ASRUtils::type_to_str_python(key_type) + - "' instead of '" + - ASRUtils::type_to_str_python(ASRUtils::expr_type(index)) + "'", - index->base.loc); - } - tmp = ASR::make_DictItem_t(al, loc, value, index, nullptr, - ASR::down_cast(contained_type)->m_value_type, nullptr); - return false; - } - else if (ASR::is_a(*contained_type)) { - this->visit_expr(*m_slice); - index = ASRUtils::EXPR(tmp); - ASR::expr_t* val = ASRUtils::expr_value(index); - if (val && ASR::is_a(*val)) { - if (ASR::down_cast(val)->m_n < 0) { - // Replace `x[-1]` to `x[len(x)+(-1)]` - ASR::ttype_t *int_type = ASRUtils::TYPE(ASR::make_Integer_t( - al, loc, 4)); - ASR::expr_t *list_len = ASRUtils::EXPR(ASR::make_ListLen_t( - al, loc, value, int_type, nullptr)); - ASR::expr_t *neg_idx = ASRUtils::expr_value(index); - index = ASRUtils::EXPR(ASR::make_IntegerBinOp_t(al, loc, - list_len, ASR::binopType::Add, neg_idx, int_type, nullptr)); - } - } - tmp = make_ListItem_t(al, loc, value, index, - ASR::down_cast(contained_type)->m_type, nullptr); - return false; - } - } if (!ASR::is_a(*type) && !ASRUtils::is_integer(*ASRUtils::expr_type(ASRUtils::EXPR(tmp)))) { std::string fnd = ASRUtils::type_to_str_python(ASRUtils::expr_type(ASRUtils::EXPR(tmp))); From 32b691e1e76b5e45b29243ff6ce538d60a5230a0 Mon Sep 17 00:00:00 2001 From: Saurabh Kumar Date: Tue, 12 Mar 2024 16:52:59 +0530 Subject: [PATCH 08/26] Tests: Add compile time test --- integration_tests/CMakeLists.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/integration_tests/CMakeLists.txt b/integration_tests/CMakeLists.txt index a396205a23..0a86b9b4dd 100644 --- a/integration_tests/CMakeLists.txt +++ b/integration_tests/CMakeLists.txt @@ -552,6 +552,7 @@ RUN(NAME test_list_compare LABELS cpython llvm) RUN(NAME test_list_concat LABELS cpython llvm c NOFAST) RUN(NAME test_list_reserve LABELS cpython llvm) RUN(NAME test_const_list LABELS cpython llvm) +RUN(NAME test_const_access LABELS cpython llvm) RUN(NAME test_tuple_01 LABELS cpython llvm c) RUN(NAME test_tuple_02 LABELS cpython llvm c NOFAST) RUN(NAME test_tuple_03 LABELS cpython llvm c) From cb8a0f2c72aa598cefa60835b7596929679c28b4 Mon Sep 17 00:00:00 2001 From: Saurabh Kumar Date: Wed, 1 May 2024 11:04:31 +0530 Subject: [PATCH 09/26] Remove calls to `type_get_past_const()` --- src/libasr/codegen/asr_to_llvm.cpp | 4 ++-- src/lpython/semantics/python_ast_to_asr.cpp | 3 +-- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/src/libasr/codegen/asr_to_llvm.cpp b/src/libasr/codegen/asr_to_llvm.cpp index 70298087c0..18a731c954 100644 --- a/src/libasr/codegen/asr_to_llvm.cpp +++ b/src/libasr/codegen/asr_to_llvm.cpp @@ -1524,7 +1524,7 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor } void visit_ListItem(const ASR::ListItem_t& x) { - ASR::ttype_t *el_type = ASRUtils::get_contained_type(ASRUtils::type_get_past_const(ASRUtils::expr_type(x.m_a))); + ASR::ttype_t *el_type = ASRUtils::get_contained_type(ASRUtils::expr_type(x.m_a)); int64_t ptr_loads_copy = ptr_loads; ptr_loads = 0; this->visit_expr(*x.m_a); @@ -1540,7 +1540,7 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor } void visit_DictItem(const ASR::DictItem_t& x) { - ASR::Dict_t *dict_type = ASR::down_cast(ASRUtils::type_get_past_const(ASRUtils::expr_type(x.m_a))); + ASR::Dict_t *dict_type = ASR::down_cast(ASRUtils::expr_type(x.m_a)); int64_t ptr_loads_copy = ptr_loads; ptr_loads = 0; diff --git a/src/lpython/semantics/python_ast_to_asr.cpp b/src/lpython/semantics/python_ast_to_asr.cpp index a4239520f9..5ba13006be 100644 --- a/src/lpython/semantics/python_ast_to_asr.cpp +++ b/src/lpython/semantics/python_ast_to_asr.cpp @@ -1781,7 +1781,7 @@ class CommonVisitor : public AST::BaseVisitor { return ASRUtils::TYPE(ASR::make_Pointer_t(al, loc, type)); } else if (var_annotation == "Const") { ASR::ttype_t *type = ast_expr_to_asr_type(loc, *s->m_slice, - is_allocatable, raise_error, abi, is_argument); + is_allocatable, is_const, raise_error, abi, is_argument); if (ASR::is_a(*type)) { throw SemanticError("'Const' not required as tuples are already immutable", loc); } @@ -3770,7 +3770,6 @@ class CommonVisitor : public AST::BaseVisitor { ai.m_left = nullptr; ai.m_right = nullptr; ai.m_step = nullptr; - type = ASRUtils::type_get_past_const(type); if (AST::is_a(*m_slice)) { AST::Slice_t *sl = AST::down_cast(m_slice); if (sl->m_lower != nullptr) { From 9803138a5d5dc3e1bb68f31a6802e1b502e9b061 Mon Sep 17 00:00:00 2001 From: Saurabh Kumar Date: Wed, 1 May 2024 11:05:18 +0530 Subject: [PATCH 10/26] Tests: Update test references --- tests/reference/asr-test_const_access-82a9a24.json | 2 +- .../reference/asr-test_const_access-82a9a24.stdout | 14 +++++--------- 2 files changed, 6 insertions(+), 10 deletions(-) diff --git a/tests/reference/asr-test_const_access-82a9a24.json b/tests/reference/asr-test_const_access-82a9a24.json index 9a5d516eca..9ba4bdf3b4 100644 --- a/tests/reference/asr-test_const_access-82a9a24.json +++ b/tests/reference/asr-test_const_access-82a9a24.json @@ -6,7 +6,7 @@ "outfile": null, "outfile_hash": null, "stdout": "asr-test_const_access-82a9a24.stdout", - "stdout_hash": "a98657aca0fefd42749ce13d47c09daf48deed01ec24170d242f5ee3", + "stdout_hash": "b524387188bc417588767959fdd79bfe6d5360ab6fb08d5e5e0d9452", "stderr": null, "stderr_hash": null, "returncode": 0 diff --git a/tests/reference/asr-test_const_access-82a9a24.stdout b/tests/reference/asr-test_const_access-82a9a24.stdout index 8cc7ff33d7..e71d1803dd 100644 --- a/tests/reference/asr-test_const_access-82a9a24.stdout +++ b/tests/reference/asr-test_const_access-82a9a24.stdout @@ -97,11 +97,9 @@ ) ) Parameter - (Const - (Dict - (Character 1 -2 ()) - (Integer 4) - ) + (Dict + (Character 1 -2 ()) + (Integer 4) ) () Source @@ -136,10 +134,8 @@ ) ) Parameter - (Const - (List - (Integer 4) - ) + (List + (Integer 4) ) () Source From d8d82ea72b32d8748d27c53ab579764787ca4ff7 Mon Sep 17 00:00:00 2001 From: Saurabh Kumar <151380951+kmr-srbh@users.noreply.github.com> Date: Wed, 1 May 2024 18:19:05 +0530 Subject: [PATCH 11/26] Remove extra newline Co-authored-by: Shaikh Ubaid --- integration_tests/CMakeLists.txt | 1 - 1 file changed, 1 deletion(-) diff --git a/integration_tests/CMakeLists.txt b/integration_tests/CMakeLists.txt index 8eab7d0837..93da87d7ef 100644 --- a/integration_tests/CMakeLists.txt +++ b/integration_tests/CMakeLists.txt @@ -634,7 +634,6 @@ RUN(NAME test_math_02 LABELS cpython llvm llvm_jit NOFAST) RUN(NAME test_math_03 LABELS llvm llvm_jit) #1595: TODO: Test using CPython (3.11 recommended) RUN(NAME test_pass_compare LABELS cpython llvm llvm_jit c) RUN(NAME test_c_interop_01 LABELS cpython llvm llvm_jit c) - RUN(NAME test_c_interop_02 LABELS cpython llvm c EXTRAFILES test_c_interop_02b.c) RUN(NAME test_c_interop_03 LABELS cpython llvm c From 55ba905325bd8fe29146294cdf2edc8bf725d3cb Mon Sep 17 00:00:00 2001 From: Saurabh Kumar <151380951+kmr-srbh@users.noreply.github.com> Date: Wed, 1 May 2024 19:22:11 +0530 Subject: [PATCH 12/26] Delete tests/reference/asr-test_const_access-82a9a24.json --- tests/reference/asr-test_const_access-82a9a24.json | 13 ------------- 1 file changed, 13 deletions(-) delete mode 100644 tests/reference/asr-test_const_access-82a9a24.json diff --git a/tests/reference/asr-test_const_access-82a9a24.json b/tests/reference/asr-test_const_access-82a9a24.json deleted file mode 100644 index 9ba4bdf3b4..0000000000 --- a/tests/reference/asr-test_const_access-82a9a24.json +++ /dev/null @@ -1,13 +0,0 @@ -{ - "basename": "asr-test_const_access-82a9a24", - "cmd": "lpython --show-asr --no-color {infile} -o {outfile}", - "infile": "tests/../integration_tests/test_const_access.py", - "infile_hash": "c25c7c1c19db38e066b47dc2891abaa5ddd7379b4df07892cebb94e3", - "outfile": null, - "outfile_hash": null, - "stdout": "asr-test_const_access-82a9a24.stdout", - "stdout_hash": "b524387188bc417588767959fdd79bfe6d5360ab6fb08d5e5e0d9452", - "stderr": null, - "stderr_hash": null, - "returncode": 0 -} \ No newline at end of file From 89ad7e35ed5e56577ba7cc43455b2b822828ab1c Mon Sep 17 00:00:00 2001 From: Saurabh Kumar <151380951+kmr-srbh@users.noreply.github.com> Date: Wed, 1 May 2024 19:22:26 +0530 Subject: [PATCH 13/26] Delete tests/reference/asr-test_const_access-82a9a24.stdout --- .../asr-test_const_access-82a9a24.stdout | 262 ------------------ 1 file changed, 262 deletions(-) delete mode 100644 tests/reference/asr-test_const_access-82a9a24.stdout diff --git a/tests/reference/asr-test_const_access-82a9a24.stdout b/tests/reference/asr-test_const_access-82a9a24.stdout deleted file mode 100644 index e71d1803dd..0000000000 --- a/tests/reference/asr-test_const_access-82a9a24.stdout +++ /dev/null @@ -1,262 +0,0 @@ -(TranslationUnit - (SymbolTable - 1 - { - __main__: - (Module - (SymbolTable - 2 - { - __main__global_stmts: - (Function - (SymbolTable - 4 - { - - }) - __main__global_stmts - (FunctionType - [] - () - Source - Implementation - () - .false. - .false. - .false. - .false. - .false. - [] - .false. - ) - [test_const_access] - [] - [(SubroutineCall - 2 test_const_access - () - [] - () - )] - () - Public - .false. - .false. - () - ), - test_const_access: - (Function - (SymbolTable - 3 - { - CONST_DICTIONARY: - (Variable - 3 - CONST_DICTIONARY - [] - Local - (DictConstant - [(StringConstant - "a" - (Character 1 1 ()) - ) - (StringConstant - "b" - (Character 1 1 ()) - ) - (StringConstant - "c" - (Character 1 1 ()) - )] - [(IntegerConstant 1 (Integer 4)) - (IntegerConstant 2 (Integer 4)) - (IntegerConstant 3 (Integer 4))] - (Dict - (Character 1 1 ()) - (Integer 4) - ) - ) - (DictConstant - [(StringConstant - "a" - (Character 1 1 ()) - ) - (StringConstant - "b" - (Character 1 1 ()) - ) - (StringConstant - "c" - (Character 1 1 ()) - )] - [(IntegerConstant 1 (Integer 4)) - (IntegerConstant 2 (Integer 4)) - (IntegerConstant 3 (Integer 4))] - (Dict - (Character 1 1 ()) - (Integer 4) - ) - ) - Parameter - (Dict - (Character 1 -2 ()) - (Integer 4) - ) - () - Source - Public - Required - .false. - ), - CONST_LIST: - (Variable - 3 - CONST_LIST - [] - Local - (ListConstant - [(IntegerConstant 1 (Integer 4)) - (IntegerConstant 2 (Integer 4)) - (IntegerConstant 3 (Integer 4)) - (IntegerConstant 4 (Integer 4)) - (IntegerConstant 5 (Integer 4))] - (List - (Integer 4) - ) - ) - (ListConstant - [(IntegerConstant 1 (Integer 4)) - (IntegerConstant 2 (Integer 4)) - (IntegerConstant 3 (Integer 4)) - (IntegerConstant 4 (Integer 4)) - (IntegerConstant 5 (Integer 4))] - (List - (Integer 4) - ) - ) - Parameter - (List - (Integer 4) - ) - () - Source - Public - Required - .false. - ) - }) - test_const_access - (FunctionType - [] - () - Source - Implementation - () - .false. - .false. - .false. - .false. - .false. - [] - .false. - ) - [] - [] - [(Assert - (IntegerCompare - (ListItem - (Var 3 CONST_LIST) - (IntegerConstant 0 (Integer 4)) - (Integer 4) - () - ) - Eq - (IntegerConstant 1 (Integer 4)) - (Logical 4) - () - ) - () - ) - (Assert - (IntegerCompare - (ListItem - (Var 3 CONST_LIST) - (IntegerBinOp - (ListLen - (Var 3 CONST_LIST) - (Integer 4) - () - ) - Add - (IntegerConstant -2 (Integer 4)) - (Integer 4) - () - ) - (Integer 4) - () - ) - Eq - (IntegerConstant 4 (Integer 4)) - (Logical 4) - () - ) - () - ) - (Assert - (IntegerCompare - (DictItem - (Var 3 CONST_DICTIONARY) - (StringConstant - "a" - (Character 1 1 ()) - ) - () - (Integer 4) - () - ) - Eq - (IntegerConstant 1 (Integer 4)) - (Logical 4) - () - ) - () - )] - () - Public - .false. - .false. - () - ) - }) - __main__ - [] - .false. - .false. - ), - main_program: - (Program - (SymbolTable - 5 - { - __main__global_stmts: - (ExternalSymbol - 5 - __main__global_stmts - 2 __main__global_stmts - __main__ - [] - __main__global_stmts - Public - ) - }) - main_program - [__main__] - [(SubroutineCall - 5 __main__global_stmts - 2 __main__global_stmts - [] - () - )] - ) - }) - [] -) From 36a1716d5b369de65cc952942d2d6d547f035946 Mon Sep 17 00:00:00 2001 From: Saurabh Kumar <151380951+kmr-srbh@users.noreply.github.com> Date: Wed, 1 May 2024 19:22:46 +0530 Subject: [PATCH 14/26] Delete tests/reference/asr-test_const_str_access-59ff543.stderr --- tests/reference/asr-test_const_str_access-59ff543.stderr | 5 ----- 1 file changed, 5 deletions(-) delete mode 100644 tests/reference/asr-test_const_str_access-59ff543.stderr diff --git a/tests/reference/asr-test_const_str_access-59ff543.stderr b/tests/reference/asr-test_const_str_access-59ff543.stderr deleted file mode 100644 index d73d860df0..0000000000 --- a/tests/reference/asr-test_const_str_access-59ff543.stderr +++ /dev/null @@ -1,5 +0,0 @@ -semantic error: 'Const' not required as strings are already immutable - --> tests/errors/test_const_str_access.py:5:19 - | -5 | CONST_STRING: Const[str] = "Hello, LPython!" - | ^^^^^^^^^^ From b3bbe78c9f4536608efb30785f032b196691aaae Mon Sep 17 00:00:00 2001 From: Saurabh Kumar <151380951+kmr-srbh@users.noreply.github.com> Date: Wed, 1 May 2024 19:23:00 +0530 Subject: [PATCH 15/26] Delete tests/reference/asr-test_const_tuple_access-0d4c6df.json --- .../asr-test_const_tuple_access-0d4c6df.json | 13 ------------- 1 file changed, 13 deletions(-) delete mode 100644 tests/reference/asr-test_const_tuple_access-0d4c6df.json diff --git a/tests/reference/asr-test_const_tuple_access-0d4c6df.json b/tests/reference/asr-test_const_tuple_access-0d4c6df.json deleted file mode 100644 index 82758eb648..0000000000 --- a/tests/reference/asr-test_const_tuple_access-0d4c6df.json +++ /dev/null @@ -1,13 +0,0 @@ -{ - "basename": "asr-test_const_tuple_access-0d4c6df", - "cmd": "lpython --show-asr --no-color {infile} -o {outfile}", - "infile": "tests/errors/test_const_tuple_access.py", - "infile_hash": "7812a4013cd5e558c05323cb3a1d85287359e9f1f98bc22e46c6152e", - "outfile": null, - "outfile_hash": null, - "stdout": null, - "stdout_hash": null, - "stderr": "asr-test_const_tuple_access-0d4c6df.stderr", - "stderr_hash": "540d1fcd3312219511607039b3410b25cb58f2bbd00196441b880c44", - "returncode": 2 -} \ No newline at end of file From 3da9626528ca0a533b81c38393878860b3feed93 Mon Sep 17 00:00:00 2001 From: Saurabh Kumar <151380951+kmr-srbh@users.noreply.github.com> Date: Wed, 1 May 2024 19:23:15 +0530 Subject: [PATCH 16/26] Delete tests/reference/asr-test_const_tuple_access-0d4c6df.stderr --- tests/reference/asr-test_const_tuple_access-0d4c6df.stderr | 5 ----- 1 file changed, 5 deletions(-) delete mode 100644 tests/reference/asr-test_const_tuple_access-0d4c6df.stderr diff --git a/tests/reference/asr-test_const_tuple_access-0d4c6df.stderr b/tests/reference/asr-test_const_tuple_access-0d4c6df.stderr deleted file mode 100644 index d8b02005a5..0000000000 --- a/tests/reference/asr-test_const_tuple_access-0d4c6df.stderr +++ /dev/null @@ -1,5 +0,0 @@ -semantic error: 'Const' not required as tuples are already immutable - --> tests/errors/test_const_tuple_access.py:5:18 - | -5 | CONST_TUPLE: Const[tuple[str, str, str]] = ("hello", "LPython", "!") - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ From 733541e0f494e663d05abf589f82fd6427a01195 Mon Sep 17 00:00:00 2001 From: Saurabh Kumar <151380951+kmr-srbh@users.noreply.github.com> Date: Wed, 1 May 2024 19:23:31 +0530 Subject: [PATCH 17/26] Delete tests/reference/asr-test_const_str_access-59ff543.json --- .../asr-test_const_str_access-59ff543.json | 13 ------------- 1 file changed, 13 deletions(-) delete mode 100644 tests/reference/asr-test_const_str_access-59ff543.json diff --git a/tests/reference/asr-test_const_str_access-59ff543.json b/tests/reference/asr-test_const_str_access-59ff543.json deleted file mode 100644 index bbe2263327..0000000000 --- a/tests/reference/asr-test_const_str_access-59ff543.json +++ /dev/null @@ -1,13 +0,0 @@ -{ - "basename": "asr-test_const_str_access-59ff543", - "cmd": "lpython --show-asr --no-color {infile} -o {outfile}", - "infile": "tests/errors/test_const_str_access.py", - "infile_hash": "356d24d72537679b048256fe36e06defe1525ea6abbdef9d9a05bce7", - "outfile": null, - "outfile_hash": null, - "stdout": null, - "stdout_hash": null, - "stderr": "asr-test_const_str_access-59ff543.stderr", - "stderr_hash": "22aab31426a34d40549295ecdbdac10ecdf67689aa464522f42b0f52", - "returncode": 2 -} \ No newline at end of file From 41514a25e158db36b4e5a59f50dee9423a8e40a7 Mon Sep 17 00:00:00 2001 From: Saurabh Kumar Date: Wed, 1 May 2024 19:32:40 +0530 Subject: [PATCH 18/26] Formatting changes --- src/libasr/codegen/asr_to_llvm.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/libasr/codegen/asr_to_llvm.cpp b/src/libasr/codegen/asr_to_llvm.cpp index 18a731c954..29f0f1cbcc 100644 --- a/src/libasr/codegen/asr_to_llvm.cpp +++ b/src/libasr/codegen/asr_to_llvm.cpp @@ -1524,7 +1524,8 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor } void visit_ListItem(const ASR::ListItem_t& x) { - ASR::ttype_t *el_type = ASRUtils::get_contained_type(ASRUtils::expr_type(x.m_a)); + ASR::ttype_t *el_type = ASRUtils::get_contained_type( + ASRUtils::expr_type(x.m_a)); int64_t ptr_loads_copy = ptr_loads; ptr_loads = 0; this->visit_expr(*x.m_a); @@ -1540,7 +1541,8 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor } void visit_DictItem(const ASR::DictItem_t& x) { - ASR::Dict_t *dict_type = ASR::down_cast(ASRUtils::expr_type(x.m_a)); + ASR::Dict_t *dict_type = ASR::down_cast( + ASRUtils::expr_type(x.m_a)); int64_t ptr_loads_copy = ptr_loads; ptr_loads = 0; From ed8662d9e87dedf1bcdbd1d791c93fdb51f600c5 Mon Sep 17 00:00:00 2001 From: Saurabh Kumar Date: Wed, 1 May 2024 19:33:20 +0530 Subject: [PATCH 19/26] Tests: Add test to CMakeLists and update error references --- integration_tests/CMakeLists.txt | 1 + integration_tests/test_const_access.py | 17 ++++++----------- .../asr-test_const_str_access-59ff543.json | 13 +++++++++++++ .../asr-test_const_str_access-59ff543.stderr | 5 +++++ .../asr-test_const_tuple_access-0d4c6df.json | 13 +++++++++++++ .../asr-test_const_tuple_access-0d4c6df.stderr | 5 +++++ tests/tests.toml | 4 ---- 7 files changed, 43 insertions(+), 15 deletions(-) create mode 100644 tests/reference/asr-test_const_str_access-59ff543.json create mode 100644 tests/reference/asr-test_const_str_access-59ff543.stderr create mode 100644 tests/reference/asr-test_const_tuple_access-0d4c6df.json create mode 100644 tests/reference/asr-test_const_tuple_access-0d4c6df.stderr diff --git a/integration_tests/CMakeLists.txt b/integration_tests/CMakeLists.txt index 93da87d7ef..232fce6dcf 100644 --- a/integration_tests/CMakeLists.txt +++ b/integration_tests/CMakeLists.txt @@ -555,6 +555,7 @@ RUN(NAME test_list_compare LABELS cpython llvm llvm_jit) RUN(NAME test_list_concat LABELS cpython llvm llvm_jit c NOFAST) RUN(NAME test_list_reserve LABELS cpython llvm llvm_jit) RUN(NAME test_const_list LABELS cpython llvm llvm_jit) +RUN(NAME test_const_access LABELS cpython llvm llvm_jit) RUN(NAME test_tuple_01 LABELS cpython llvm llvm_jit c) RUN(NAME test_tuple_02 LABELS cpython llvm llvm_jit c NOFAST) RUN(NAME test_tuple_03 LABELS cpython llvm llvm_jit c) diff --git a/integration_tests/test_const_access.py b/integration_tests/test_const_access.py index 253f4bc3a3..4368e3ed0c 100644 --- a/integration_tests/test_const_access.py +++ b/integration_tests/test_const_access.py @@ -1,14 +1,9 @@ -from lpython import i32, str, list, dict, Const +from lpython import i32, Const +CONST_LIST: Const[list[i32]] = [1, 2, 3, 4, 5] +CONST_DICTIONARY: Const[dict[str, i32]] = {"a": 1, "b": 2, "c": 3} -def test_const_access(): - CONST_LIST: Const[list[i32]] = [1, 2, 3, 4, 5] - CONST_DICTIONARY: Const[dict[str, i32]] = {"a": 1, "b": 2, "c": 3} +assert CONST_LIST[0] == 1 +assert CONST_LIST[-2] == 4 - assert CONST_LIST[0] == 1 - assert CONST_LIST[-2] == 4 - - assert CONST_DICTIONARY["a"] == 1 - - -test_const_access() +assert CONST_DICTIONARY["a"] == 1 \ No newline at end of file diff --git a/tests/reference/asr-test_const_str_access-59ff543.json b/tests/reference/asr-test_const_str_access-59ff543.json new file mode 100644 index 0000000000..bbe2263327 --- /dev/null +++ b/tests/reference/asr-test_const_str_access-59ff543.json @@ -0,0 +1,13 @@ +{ + "basename": "asr-test_const_str_access-59ff543", + "cmd": "lpython --show-asr --no-color {infile} -o {outfile}", + "infile": "tests/errors/test_const_str_access.py", + "infile_hash": "356d24d72537679b048256fe36e06defe1525ea6abbdef9d9a05bce7", + "outfile": null, + "outfile_hash": null, + "stdout": null, + "stdout_hash": null, + "stderr": "asr-test_const_str_access-59ff543.stderr", + "stderr_hash": "22aab31426a34d40549295ecdbdac10ecdf67689aa464522f42b0f52", + "returncode": 2 +} \ No newline at end of file diff --git a/tests/reference/asr-test_const_str_access-59ff543.stderr b/tests/reference/asr-test_const_str_access-59ff543.stderr new file mode 100644 index 0000000000..d73d860df0 --- /dev/null +++ b/tests/reference/asr-test_const_str_access-59ff543.stderr @@ -0,0 +1,5 @@ +semantic error: 'Const' not required as strings are already immutable + --> tests/errors/test_const_str_access.py:5:19 + | +5 | CONST_STRING: Const[str] = "Hello, LPython!" + | ^^^^^^^^^^ diff --git a/tests/reference/asr-test_const_tuple_access-0d4c6df.json b/tests/reference/asr-test_const_tuple_access-0d4c6df.json new file mode 100644 index 0000000000..82758eb648 --- /dev/null +++ b/tests/reference/asr-test_const_tuple_access-0d4c6df.json @@ -0,0 +1,13 @@ +{ + "basename": "asr-test_const_tuple_access-0d4c6df", + "cmd": "lpython --show-asr --no-color {infile} -o {outfile}", + "infile": "tests/errors/test_const_tuple_access.py", + "infile_hash": "7812a4013cd5e558c05323cb3a1d85287359e9f1f98bc22e46c6152e", + "outfile": null, + "outfile_hash": null, + "stdout": null, + "stdout_hash": null, + "stderr": "asr-test_const_tuple_access-0d4c6df.stderr", + "stderr_hash": "540d1fcd3312219511607039b3410b25cb58f2bbd00196441b880c44", + "returncode": 2 +} \ No newline at end of file diff --git a/tests/reference/asr-test_const_tuple_access-0d4c6df.stderr b/tests/reference/asr-test_const_tuple_access-0d4c6df.stderr new file mode 100644 index 0000000000..d8b02005a5 --- /dev/null +++ b/tests/reference/asr-test_const_tuple_access-0d4c6df.stderr @@ -0,0 +1,5 @@ +semantic error: 'Const' not required as tuples are already immutable + --> tests/errors/test_const_tuple_access.py:5:18 + | +5 | CONST_TUPLE: Const[tuple[str, str, str]] = ("hello", "LPython", "!") + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/tests/tests.toml b/tests/tests.toml index a56e796f18..977d9c9244 100644 --- a/tests/tests.toml +++ b/tests/tests.toml @@ -152,10 +152,6 @@ asr = true llvm = true llvm_dbg = true -[[test]] -filename = "../integration_tests/test_const_access.py" -asr = true - [[test]] filename = "../integration_tests/array_01_decl.py" asr = true From 334f4299176bbdbd0eba64a43749e7401fa69127 Mon Sep 17 00:00:00 2001 From: Saurabh Kumar <151380951+kmr-srbh@users.noreply.github.com> Date: Wed, 1 May 2024 21:22:57 +0530 Subject: [PATCH 20/26] Update asr_to_llvm.cpp --- src/libasr/codegen/asr_to_llvm.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/libasr/codegen/asr_to_llvm.cpp b/src/libasr/codegen/asr_to_llvm.cpp index 29f0f1cbcc..10e656a12b 100644 --- a/src/libasr/codegen/asr_to_llvm.cpp +++ b/src/libasr/codegen/asr_to_llvm.cpp @@ -1524,7 +1524,7 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor } void visit_ListItem(const ASR::ListItem_t& x) { - ASR::ttype_t *el_type = ASRUtils::get_contained_type( + ASR::ttype_t* el_type = ASRUtils::get_contained_type( ASRUtils::expr_type(x.m_a)); int64_t ptr_loads_copy = ptr_loads; ptr_loads = 0; @@ -1542,7 +1542,7 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor void visit_DictItem(const ASR::DictItem_t& x) { ASR::Dict_t *dict_type = ASR::down_cast( - ASRUtils::expr_type(x.m_a)); + ASRUtils::expr_type(x.m_a)); int64_t ptr_loads_copy = ptr_loads; ptr_loads = 0; From 251b5d4c1fbf8c6dc7f754c295b5dcfeabd3641f Mon Sep 17 00:00:00 2001 From: Saurabh Kumar <151380951+kmr-srbh@users.noreply.github.com> Date: Wed, 1 May 2024 21:23:57 +0530 Subject: [PATCH 21/26] Undo formatting changes --- src/libasr/codegen/asr_to_llvm.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libasr/codegen/asr_to_llvm.cpp b/src/libasr/codegen/asr_to_llvm.cpp index 10e656a12b..a6a540b0a0 100644 --- a/src/libasr/codegen/asr_to_llvm.cpp +++ b/src/libasr/codegen/asr_to_llvm.cpp @@ -1541,7 +1541,7 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor } void visit_DictItem(const ASR::DictItem_t& x) { - ASR::Dict_t *dict_type = ASR::down_cast( + ASR::Dict_t* dict_type = ASR::down_cast( ASRUtils::expr_type(x.m_a)); int64_t ptr_loads_copy = ptr_loads; From be48acf64e1bd94e2040b87669889d28d7a099af Mon Sep 17 00:00:00 2001 From: Saurabh Kumar <151380951+kmr-srbh@users.noreply.github.com> Date: Wed, 1 May 2024 21:26:46 +0530 Subject: [PATCH 22/26] Undo formatting changes --- src/libasr/codegen/asr_to_llvm.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/src/libasr/codegen/asr_to_llvm.cpp b/src/libasr/codegen/asr_to_llvm.cpp index a6a540b0a0..f8b9d7e407 100644 --- a/src/libasr/codegen/asr_to_llvm.cpp +++ b/src/libasr/codegen/asr_to_llvm.cpp @@ -1543,7 +1543,6 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor void visit_DictItem(const ASR::DictItem_t& x) { ASR::Dict_t* dict_type = ASR::down_cast( ASRUtils::expr_type(x.m_a)); - int64_t ptr_loads_copy = ptr_loads; ptr_loads = 0; this->visit_expr(*x.m_a); From 2620956db683b13c0d862caeb47947a3dbe270dd Mon Sep 17 00:00:00 2001 From: Saurabh Kumar Date: Wed, 1 May 2024 21:35:26 +0530 Subject: [PATCH 23/26] Revert throwing error for `Const` annotated tuples and strings --- src/lpython/semantics/python_ast_to_asr.cpp | 8 -------- 1 file changed, 8 deletions(-) diff --git a/src/lpython/semantics/python_ast_to_asr.cpp b/src/lpython/semantics/python_ast_to_asr.cpp index 91d3564f50..9c8591e673 100644 --- a/src/lpython/semantics/python_ast_to_asr.cpp +++ b/src/lpython/semantics/python_ast_to_asr.cpp @@ -1780,14 +1780,6 @@ class CommonVisitor : public AST::BaseVisitor { is_allocatable, is_const, raise_error, abi, is_argument); return ASRUtils::TYPE(ASR::make_Pointer_t(al, loc, type)); } else if (var_annotation == "Const") { - ASR::ttype_t *type = ast_expr_to_asr_type(loc, *s->m_slice, - is_allocatable, is_const, raise_error, abi, is_argument); - if (ASR::is_a(*type)) { - throw SemanticError("'Const' not required as tuples are already immutable", loc); - } - else if (ASR::is_a(*type)) { - throw SemanticError("'Const' not required as strings are already immutable", loc); - } is_const = true; return ast_expr_to_asr_type(loc, *s->m_slice, is_allocatable, is_const, raise_error, abi, is_argument); From c0407b545f5033eb54440cb946257ae8c77b1f84 Mon Sep 17 00:00:00 2001 From: Saurabh Kumar Date: Wed, 1 May 2024 21:45:15 +0530 Subject: [PATCH 24/26] Tests: Remove error references --- tests/errors/test_const_str_access.py | 8 -------- tests/errors/test_const_tuple_access.py | 9 --------- .../asr-test_const_str_access-59ff543.json | 13 ------------- .../asr-test_const_str_access-59ff543.stderr | 5 ----- .../asr-test_const_tuple_access-0d4c6df.json | 13 ------------- .../asr-test_const_tuple_access-0d4c6df.stderr | 5 ----- tests/tests.toml | 8 -------- 7 files changed, 61 deletions(-) delete mode 100644 tests/errors/test_const_str_access.py delete mode 100644 tests/errors/test_const_tuple_access.py delete mode 100644 tests/reference/asr-test_const_str_access-59ff543.json delete mode 100644 tests/reference/asr-test_const_str_access-59ff543.stderr delete mode 100644 tests/reference/asr-test_const_tuple_access-0d4c6df.json delete mode 100644 tests/reference/asr-test_const_tuple_access-0d4c6df.stderr diff --git a/tests/errors/test_const_str_access.py b/tests/errors/test_const_str_access.py deleted file mode 100644 index bc25387af8..0000000000 --- a/tests/errors/test_const_str_access.py +++ /dev/null @@ -1,8 +0,0 @@ -from lpython import str, Const - - -def test_const_str_access(): - CONST_STRING: Const[str] = "Hello, LPython!" - - -test_const_str_access() diff --git a/tests/errors/test_const_tuple_access.py b/tests/errors/test_const_tuple_access.py deleted file mode 100644 index e8ea8b427d..0000000000 --- a/tests/errors/test_const_tuple_access.py +++ /dev/null @@ -1,9 +0,0 @@ -from lpython import str, tuple, Const - - -def test_const_tuple_access(): - CONST_TUPLE: Const[tuple[str, str, str]] = ("hello", "LPython", "!") - - -test_const_tuple_access() - diff --git a/tests/reference/asr-test_const_str_access-59ff543.json b/tests/reference/asr-test_const_str_access-59ff543.json deleted file mode 100644 index bbe2263327..0000000000 --- a/tests/reference/asr-test_const_str_access-59ff543.json +++ /dev/null @@ -1,13 +0,0 @@ -{ - "basename": "asr-test_const_str_access-59ff543", - "cmd": "lpython --show-asr --no-color {infile} -o {outfile}", - "infile": "tests/errors/test_const_str_access.py", - "infile_hash": "356d24d72537679b048256fe36e06defe1525ea6abbdef9d9a05bce7", - "outfile": null, - "outfile_hash": null, - "stdout": null, - "stdout_hash": null, - "stderr": "asr-test_const_str_access-59ff543.stderr", - "stderr_hash": "22aab31426a34d40549295ecdbdac10ecdf67689aa464522f42b0f52", - "returncode": 2 -} \ No newline at end of file diff --git a/tests/reference/asr-test_const_str_access-59ff543.stderr b/tests/reference/asr-test_const_str_access-59ff543.stderr deleted file mode 100644 index d73d860df0..0000000000 --- a/tests/reference/asr-test_const_str_access-59ff543.stderr +++ /dev/null @@ -1,5 +0,0 @@ -semantic error: 'Const' not required as strings are already immutable - --> tests/errors/test_const_str_access.py:5:19 - | -5 | CONST_STRING: Const[str] = "Hello, LPython!" - | ^^^^^^^^^^ diff --git a/tests/reference/asr-test_const_tuple_access-0d4c6df.json b/tests/reference/asr-test_const_tuple_access-0d4c6df.json deleted file mode 100644 index 82758eb648..0000000000 --- a/tests/reference/asr-test_const_tuple_access-0d4c6df.json +++ /dev/null @@ -1,13 +0,0 @@ -{ - "basename": "asr-test_const_tuple_access-0d4c6df", - "cmd": "lpython --show-asr --no-color {infile} -o {outfile}", - "infile": "tests/errors/test_const_tuple_access.py", - "infile_hash": "7812a4013cd5e558c05323cb3a1d85287359e9f1f98bc22e46c6152e", - "outfile": null, - "outfile_hash": null, - "stdout": null, - "stdout_hash": null, - "stderr": "asr-test_const_tuple_access-0d4c6df.stderr", - "stderr_hash": "540d1fcd3312219511607039b3410b25cb58f2bbd00196441b880c44", - "returncode": 2 -} \ No newline at end of file diff --git a/tests/reference/asr-test_const_tuple_access-0d4c6df.stderr b/tests/reference/asr-test_const_tuple_access-0d4c6df.stderr deleted file mode 100644 index d8b02005a5..0000000000 --- a/tests/reference/asr-test_const_tuple_access-0d4c6df.stderr +++ /dev/null @@ -1,5 +0,0 @@ -semantic error: 'Const' not required as tuples are already immutable - --> tests/errors/test_const_tuple_access.py:5:18 - | -5 | CONST_TUPLE: Const[tuple[str, str, str]] = ("hello", "LPython", "!") - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/tests/tests.toml b/tests/tests.toml index 977d9c9244..3e6589aef1 100644 --- a/tests/tests.toml +++ b/tests/tests.toml @@ -1249,14 +1249,6 @@ asr = true filename = "errors/test_type_mismatch_01.py" asr = true -[[test]] -filename = "errors/test_const_str_access.py" -asr = true - -[[test]] -filename = "errors/test_const_tuple_access.py" -asr = true - # tests/tokens/errors [[test]] From f48972012eba730daf7ff6e87d38aa654d299fad Mon Sep 17 00:00:00 2001 From: Saurabh Kumar <151380951+kmr-srbh@users.noreply.github.com> Date: Wed, 1 May 2024 22:19:12 +0530 Subject: [PATCH 25/26] Remove redundant visitor --- src/lpython/semantics/python_ast_to_asr.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/src/lpython/semantics/python_ast_to_asr.cpp b/src/lpython/semantics/python_ast_to_asr.cpp index 9c8591e673..4022581750 100644 --- a/src/lpython/semantics/python_ast_to_asr.cpp +++ b/src/lpython/semantics/python_ast_to_asr.cpp @@ -3840,7 +3840,6 @@ class CommonVisitor : public AST::BaseVisitor { throw SemanticAbort(); } if (ASR::is_a(*type)) { - this->visit_expr(*m_slice); index = ASRUtils::EXPR(tmp); ASR::ttype_t *key_type = ASR::down_cast(type)->m_key_type; if (!ASRUtils::check_equal_type(ASRUtils::expr_type(index), key_type)) { From 970ce8c229a36bd5d793d08819eaeae59ad48fa1 Mon Sep 17 00:00:00 2001 From: Saurabh Kumar <151380951+kmr-srbh@users.noreply.github.com> Date: Wed, 1 May 2024 22:21:24 +0530 Subject: [PATCH 26/26] Undo moving `index` --- src/lpython/semantics/python_ast_to_asr.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lpython/semantics/python_ast_to_asr.cpp b/src/lpython/semantics/python_ast_to_asr.cpp index 4022581750..afdb3086c3 100644 --- a/src/lpython/semantics/python_ast_to_asr.cpp +++ b/src/lpython/semantics/python_ast_to_asr.cpp @@ -3825,7 +3825,6 @@ class CommonVisitor : public AST::BaseVisitor { } return final_result; } else { - ASR::expr_t *index = nullptr; this->visit_expr(*m_slice); if (!ASR::is_a(*type) && !ASRUtils::is_integer(*ASRUtils::expr_type(ASRUtils::EXPR(tmp)))) { @@ -3839,6 +3838,7 @@ class CommonVisitor : public AST::BaseVisitor { ); throw SemanticAbort(); } + ASR::expr_t *index = nullptr; if (ASR::is_a(*type)) { index = ASRUtils::EXPR(tmp); ASR::ttype_t *key_type = ASR::down_cast(type)->m_key_type;