diff --git a/src/lpython/semantics/python_ast_to_asr.cpp b/src/lpython/semantics/python_ast_to_asr.cpp index afdb3086c3..e70fcbcf71 100644 --- a/src/lpython/semantics/python_ast_to_asr.cpp +++ b/src/lpython/semantics/python_ast_to_asr.cpp @@ -5167,6 +5167,39 @@ 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[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); + 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) { @@ -5303,6 +5336,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); } @@ -6149,6 +6186,17 @@ class BodyVisitor : public CommonVisitor { } } 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; + 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); + 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); } @@ -6165,6 +6213,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); } @@ -6629,6 +6682,9 @@ class BodyVisitor : public CommonVisitor { } } else { if (!ASRUtils::check_equal_type(ASRUtils::expr_type(value), type)) { + //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", x.base.base.loc); }