From e311c6327aa826c05d23df95f80a8a45c0c1f3fc Mon Sep 17 00:00:00 2001 From: Assem Date: Wed, 6 Mar 2024 19:31:48 +0200 Subject: [PATCH 1/3] Fixed Issue #2042 --- src/lpython/semantics/python_ast_to_asr.cpp | 35 +++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/src/lpython/semantics/python_ast_to_asr.cpp b/src/lpython/semantics/python_ast_to_asr.cpp index 4e11fe01ed..924c3e0760 100644 --- a/src/lpython/semantics/python_ast_to_asr.cpp +++ b/src/lpython/semantics/python_ast_to_asr.cpp @@ -5069,6 +5069,38 @@ class BodyVisitor : public CommonVisitor { ASR::expr_t* assign_asr_target_copy = assign_asr_target; this->visit_expr(*x.m_targets[0]); assign_asr_target = ASRUtils::EXPR(tmp); + //Construction-While-Assigning Problem + AST::exprType value_type_ast = (x.m_value)->type; + AST::exprType objects_to_check[4] {AST::exprType::Set,AST::exprType::Dict, // just check these 4 for now. + AST::exprType::Tuple,AST::exprType::List}; + for(AST::exprType &exp : objects_to_check){ + if (exp == value_type_ast){ + ASR::ttype_t *target_type = ASRUtils::expr_type(assign_asr_target); + try{ + this->visit_expr(*x.m_value); + } + catch (SemanticError &error){ + ASR::expr_t *value_ASR_after_down_cast = ASRUtils::EXPR(tmp); + ASR::ttype_t *value_type = ASRUtils::expr_type(value_ASR_after_down_cast); + if (!ASRUtils::check_equal_type(target_type, value_type)){ + std::string ltype = ASRUtils::type_to_str_python(target_type); + std::string rtype = ASRUtils::type_to_str_python(value_type); + diag.add(diag::Diagnostic( + "Type mismatch in assignment, the types must be compatible", + diag::Level::Error, diag::Stage::Semantic, { + diag::Label("type mismatch ('" + ltype + "' and '" + rtype + "')", + {assign_asr_target->base.loc, value_ASR_after_down_cast->base.loc}) + }) + ); + throw SemanticAbort(); + } + } + } + + } + + + this->visit_expr(*x.m_value); assign_asr_target = assign_asr_target_copy; if (tmp) { @@ -6509,6 +6541,9 @@ class BodyVisitor : public CommonVisitor { type = ASRUtils::expr_type(value); } else { if (!ASRUtils::check_equal_type(ASRUtils::expr_type(value), type)) { + //set the tmp to use it in the error message. + ASR::ttype_t* set_type = ASRUtils::TYPE(ASR::make_Set_t(al, x.base.base.loc, type)); + tmp = ASR::make_SetConstant_t(al, x.base.base.loc, elements.p, elements.size(), set_type); throw SemanticError("All Set values must be of the same type for now", x.base.base.loc); } From d18baaf54edcf592d94e50a0c17f50a8b70d7299 Mon Sep 17 00:00:00 2001 From: Assem Date: Wed, 6 Mar 2024 20:03:16 +0200 Subject: [PATCH 2/3] Fixed issue #2042 - dispatched the type of list and dict --- src/lpython/semantics/python_ast_to_asr.cpp | 26 ++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/src/lpython/semantics/python_ast_to_asr.cpp b/src/lpython/semantics/python_ast_to_asr.cpp index 924c3e0760..d6a8d2f89d 100644 --- a/src/lpython/semantics/python_ast_to_asr.cpp +++ b/src/lpython/semantics/python_ast_to_asr.cpp @@ -5071,8 +5071,9 @@ class BodyVisitor : public CommonVisitor { assign_asr_target = ASRUtils::EXPR(tmp); //Construction-While-Assigning Problem AST::exprType value_type_ast = (x.m_value)->type; - AST::exprType objects_to_check[4] {AST::exprType::Set,AST::exprType::Dict, // just check these 4 for now. - AST::exprType::Tuple,AST::exprType::List}; + AST::exprType objects_to_check[3] { AST::exprType::Set, // just check these 3 for now. + AST::exprType::Dict, + AST::exprType::List }; for(AST::exprType &exp : objects_to_check){ if (exp == value_type_ast){ ASR::ttype_t *target_type = ASRUtils::expr_type(assign_asr_target); @@ -5237,6 +5238,10 @@ class BodyVisitor : public CommonVisitor { this->visit_expr(*x.m_elts[i]); expr = ASRUtils::EXPR(tmp); if (!ASRUtils::check_equal_type(ASRUtils::expr_type(expr), type)) { + //set the tmp to use it in the error message.(copied from the end of this function) + ASR::ttype_t* list_type = ASRUtils::TYPE(ASR::make_List_t(al, x.base.base.loc, type)); + tmp = ASR::make_ListConstant_t(al, x.base.base.loc, list.p, + list.size(), list_type); throw SemanticError("All List elements must be of the same type for now", x.base.base.loc); } @@ -6063,6 +6068,16 @@ class BodyVisitor : public CommonVisitor { key_type = ASRUtils::expr_type(key); } else { if (!ASRUtils::check_equal_type(ASRUtils::expr_type(key), key_type)) { + //set the tmp to use it in the error message.(copied from the end of this function + creating values_type) + Vec values; + ASR::ttype_t* value_type = nullptr; + visit_expr(*x.m_values[0]); + ASR::expr_t *value = ASRUtils::EXPR(tmp); + value_type = ASRUtils::expr_type(value); + ASR::ttype_t* type = ASRUtils::TYPE(ASR::make_Dict_t(al, x.base.base.loc, + key_type, value_type)); + tmp = ASR::make_DictConstant_t(al, x.base.base.loc, keys.p, keys.size(), + values.p, values.size(), type); throw SemanticError("All dictionary keys must be of the same type", x.base.base.loc); } @@ -6079,6 +6094,11 @@ class BodyVisitor : public CommonVisitor { value_type = ASRUtils::expr_type(value); } else { if (!ASRUtils::check_equal_type(ASRUtils::expr_type(value), value_type)) { + //set the tmp to use it in the error message.(copied from the end of this function) + ASR::ttype_t* type = ASRUtils::TYPE(ASR::make_Dict_t(al, x.base.base.loc, + key_type, value_type)); + tmp = ASR::make_DictConstant_t(al, x.base.base.loc, keys.p, keys.size(), + values.p, values.size(), type); throw SemanticError("All dictionary values must be of the same type", x.base.base.loc); } @@ -6541,7 +6561,7 @@ class BodyVisitor : public CommonVisitor { type = ASRUtils::expr_type(value); } else { if (!ASRUtils::check_equal_type(ASRUtils::expr_type(value), type)) { - //set the tmp to use it in the error message. + //set the tmp to use it in the error message.(copied from the end of this function) ASR::ttype_t* set_type = ASRUtils::TYPE(ASR::make_Set_t(al, x.base.base.loc, type)); tmp = ASR::make_SetConstant_t(al, x.base.base.loc, elements.p, elements.size(), set_type); throw SemanticError("All Set values must be of the same type for now", From 505f53c55db00c26f01f547ca27952c6fb80c109 Mon Sep 17 00:00:00 2001 From: Assem Date: Wed, 6 Mar 2024 21:12:23 +0200 Subject: [PATCH 3/3] Fix Warning :UnInitialized Vec --- src/lpython/semantics/python_ast_to_asr.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/lpython/semantics/python_ast_to_asr.cpp b/src/lpython/semantics/python_ast_to_asr.cpp index d6a8d2f89d..ef2f9fa423 100644 --- a/src/lpython/semantics/python_ast_to_asr.cpp +++ b/src/lpython/semantics/python_ast_to_asr.cpp @@ -6070,6 +6070,7 @@ class BodyVisitor : public CommonVisitor { if (!ASRUtils::check_equal_type(ASRUtils::expr_type(key), key_type)) { //set the tmp to use it in the error message.(copied from the end of this function + creating values_type) Vec values; + values.reserve(al, x.n_values); ASR::ttype_t* value_type = nullptr; visit_expr(*x.m_values[0]); ASR::expr_t *value = ASRUtils::EXPR(tmp);