From 9aee6660bf35b2f3176dbab6d5b84c2e17180744 Mon Sep 17 00:00:00 2001 From: Shaikh Ubaid Date: Thu, 22 Jun 2023 10:29:53 +0530 Subject: [PATCH 1/4] ASR: Skip pass inside struct --- src/lpython/semantics/python_ast_to_asr.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/lpython/semantics/python_ast_to_asr.cpp b/src/lpython/semantics/python_ast_to_asr.cpp index b5b4ed7794..a728ed9c81 100644 --- a/src/lpython/semantics/python_ast_to_asr.cpp +++ b/src/lpython/semantics/python_ast_to_asr.cpp @@ -2885,6 +2885,10 @@ class CommonVisitor : public AST::BaseVisitor { if ( AST::is_a(*x.m_body[i]) ) { throw SemanticError("Struct member functions are not supported", x.m_body[i]->base.loc); } + if (AST::is_a(*x.m_body[i])) { + continue; + } + LCOMPILERS_ASSERT(AST::is_a(*x.m_body[i])); AST::AnnAssign_t* ann_assign = AST::down_cast(x.m_body[i]); LCOMPILERS_ASSERT(AST::is_a(*ann_assign->m_target)); From b1e04a0ee7d559919ed4da6766b129b3dd5a48f0 Mon Sep 17 00:00:00 2001 From: Shaikh Ubaid Date: Thu, 22 Jun 2023 10:32:23 +0530 Subject: [PATCH 2/4] Refactor: Replace if with if-else-if ladder --- src/lpython/semantics/python_ast_to_asr.cpp | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/src/lpython/semantics/python_ast_to_asr.cpp b/src/lpython/semantics/python_ast_to_asr.cpp index a728ed9c81..3c050cab4c 100644 --- a/src/lpython/semantics/python_ast_to_asr.cpp +++ b/src/lpython/semantics/python_ast_to_asr.cpp @@ -2874,18 +2874,14 @@ class CommonVisitor : public AST::BaseVisitor { if (AST::is_a(*expr->m_value)) { // It is a doc string. Skip doc strings for now. continue; - } else { - throw SemanticError("Only doc strings allowed as expressions inside class", expr->base.base.loc); } - } - if( AST::is_a(*x.m_body[i]) ) { + throw SemanticError("Only doc strings allowed as expressions inside class", expr->base.base.loc); + } else if( AST::is_a(*x.m_body[i]) ) { visit_ClassDef(*AST::down_cast(x.m_body[i])); continue; - } - if ( AST::is_a(*x.m_body[i]) ) { + } else if ( AST::is_a(*x.m_body[i]) ) { throw SemanticError("Struct member functions are not supported", x.m_body[i]->base.loc); - } - if (AST::is_a(*x.m_body[i])) { + } else if (AST::is_a(*x.m_body[i])) { continue; } From a1b378a7704df45ea770252b2cbc556aff62509a Mon Sep 17 00:00:00 2001 From: Shaikh Ubaid Date: Thu, 22 Jun 2023 10:36:38 +0530 Subject: [PATCH 3/4] ASR: Replace LCOMPILERS_ASSERT with SemanticError() --- src/lpython/semantics/python_ast_to_asr.cpp | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/lpython/semantics/python_ast_to_asr.cpp b/src/lpython/semantics/python_ast_to_asr.cpp index 3c050cab4c..e710234f4b 100644 --- a/src/lpython/semantics/python_ast_to_asr.cpp +++ b/src/lpython/semantics/python_ast_to_asr.cpp @@ -2883,11 +2883,13 @@ class CommonVisitor : public AST::BaseVisitor { throw SemanticError("Struct member functions are not supported", x.m_body[i]->base.loc); } else if (AST::is_a(*x.m_body[i])) { continue; + } else if (!AST::is_a(*x.m_body[i])) { + throw SemanticError("AnnAssign expected inside struct", x.m_body[i]->base.loc); } - - LCOMPILERS_ASSERT(AST::is_a(*x.m_body[i])); AST::AnnAssign_t* ann_assign = AST::down_cast(x.m_body[i]); - LCOMPILERS_ASSERT(AST::is_a(*ann_assign->m_target)); + if (!AST::is_a(*ann_assign->m_target)) { + throw SemanticError("Only Name supported as target in AnnAssign inside struct", x.m_body[i]->base.loc); + } AST::Name_t *n = AST::down_cast(ann_assign->m_target); std::string var_name = n->m_id; ASR::expr_t* init_expr = nullptr; From 304107fe7dce2e0882559d08a6ad2b6ee2ae710e Mon Sep 17 00:00:00 2001 From: Shaikh Ubaid Date: Thu, 22 Jun 2023 10:46:27 +0530 Subject: [PATCH 4/4] TEST: Add test for pass inside struct --- integration_tests/CMakeLists.txt | 1 + integration_tests/structs_28.py | 15 +++++++++++++++ 2 files changed, 16 insertions(+) create mode 100644 integration_tests/structs_28.py diff --git a/integration_tests/CMakeLists.txt b/integration_tests/CMakeLists.txt index a18c06df40..e5b8c6f5cb 100644 --- a/integration_tests/CMakeLists.txt +++ b/integration_tests/CMakeLists.txt @@ -578,6 +578,7 @@ RUN(NAME structs_24 LABELS cpython llvm c) RUN(NAME structs_25 LABELS cpython llvm c) RUN(NAME structs_26 LABELS cpython llvm c) RUN(NAME structs_27 LABELS cpython llvm c) +RUN(NAME structs_28 LABELS cpython llvm c) RUN(NAME symbolics_01 LABELS cpython_sym c_sym) RUN(NAME symbolics_02 LABELS cpython_sym c_sym) diff --git a/integration_tests/structs_28.py b/integration_tests/structs_28.py new file mode 100644 index 0000000000..9e0f3bf502 --- /dev/null +++ b/integration_tests/structs_28.py @@ -0,0 +1,15 @@ +from lpython import dataclass, i32 + +@dataclass +class Pattern: + _foo : str + pass + _n: i32 + +def main0(): + p: Pattern = Pattern("some string", 5) + assert p._foo == "some string" + assert p._n == 5 + print(p) + +main0()