Skip to content

Commit d18baaf

Browse files
committed
Fixed issue #2042 - dispatched the type of list and dict
1 parent e311c63 commit d18baaf

File tree

1 file changed

+23
-3
lines changed

1 file changed

+23
-3
lines changed

src/lpython/semantics/python_ast_to_asr.cpp

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5071,8 +5071,9 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
50715071
assign_asr_target = ASRUtils::EXPR(tmp);
50725072
//Construction-While-Assigning Problem
50735073
AST::exprType value_type_ast = (x.m_value)->type;
5074-
AST::exprType objects_to_check[4] {AST::exprType::Set,AST::exprType::Dict, // just check these 4 for now.
5075-
AST::exprType::Tuple,AST::exprType::List};
5074+
AST::exprType objects_to_check[3] { AST::exprType::Set, // just check these 3 for now.
5075+
AST::exprType::Dict,
5076+
AST::exprType::List };
50765077
for(AST::exprType &exp : objects_to_check){
50775078
if (exp == value_type_ast){
50785079
ASR::ttype_t *target_type = ASRUtils::expr_type(assign_asr_target);
@@ -5237,6 +5238,10 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
52375238
this->visit_expr(*x.m_elts[i]);
52385239
expr = ASRUtils::EXPR(tmp);
52395240
if (!ASRUtils::check_equal_type(ASRUtils::expr_type(expr), type)) {
5241+
//set the tmp to use it in the error message.(copied from the end of this function)
5242+
ASR::ttype_t* list_type = ASRUtils::TYPE(ASR::make_List_t(al, x.base.base.loc, type));
5243+
tmp = ASR::make_ListConstant_t(al, x.base.base.loc, list.p,
5244+
list.size(), list_type);
52405245
throw SemanticError("All List elements must be of the same type for now",
52415246
x.base.base.loc);
52425247
}
@@ -6063,6 +6068,16 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
60636068
key_type = ASRUtils::expr_type(key);
60646069
} else {
60656070
if (!ASRUtils::check_equal_type(ASRUtils::expr_type(key), key_type)) {
6071+
//set the tmp to use it in the error message.(copied from the end of this function + creating values_type)
6072+
Vec<ASR::expr_t*> values;
6073+
ASR::ttype_t* value_type = nullptr;
6074+
visit_expr(*x.m_values[0]);
6075+
ASR::expr_t *value = ASRUtils::EXPR(tmp);
6076+
value_type = ASRUtils::expr_type(value);
6077+
ASR::ttype_t* type = ASRUtils::TYPE(ASR::make_Dict_t(al, x.base.base.loc,
6078+
key_type, value_type));
6079+
tmp = ASR::make_DictConstant_t(al, x.base.base.loc, keys.p, keys.size(),
6080+
values.p, values.size(), type);
60666081
throw SemanticError("All dictionary keys must be of the same type",
60676082
x.base.base.loc);
60686083
}
@@ -6079,6 +6094,11 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
60796094
value_type = ASRUtils::expr_type(value);
60806095
} else {
60816096
if (!ASRUtils::check_equal_type(ASRUtils::expr_type(value), value_type)) {
6097+
//set the tmp to use it in the error message.(copied from the end of this function)
6098+
ASR::ttype_t* type = ASRUtils::TYPE(ASR::make_Dict_t(al, x.base.base.loc,
6099+
key_type, value_type));
6100+
tmp = ASR::make_DictConstant_t(al, x.base.base.loc, keys.p, keys.size(),
6101+
values.p, values.size(), type);
60826102
throw SemanticError("All dictionary values must be of the same type",
60836103
x.base.base.loc);
60846104
}
@@ -6541,7 +6561,7 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
65416561
type = ASRUtils::expr_type(value);
65426562
} else {
65436563
if (!ASRUtils::check_equal_type(ASRUtils::expr_type(value), type)) {
6544-
//set the tmp to use it in the error message.
6564+
//set the tmp to use it in the error message.(copied from the end of this function)
65456565
ASR::ttype_t* set_type = ASRUtils::TYPE(ASR::make_Set_t(al, x.base.base.loc, type));
65466566
tmp = ASR::make_SetConstant_t(al, x.base.base.loc, elements.p, elements.size(), set_type);
65476567
throw SemanticError("All Set values must be of the same type for now",

0 commit comments

Comments
 (0)