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() diff --git a/src/lpython/semantics/python_ast_to_asr.cpp b/src/lpython/semantics/python_ast_to_asr.cpp index b5b4ed7794..e710234f4b 100644 --- a/src/lpython/semantics/python_ast_to_asr.cpp +++ b/src/lpython/semantics/python_ast_to_asr.cpp @@ -2874,20 +2874,22 @@ 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); + } 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;