@@ -445,7 +445,7 @@ class CommonVisitor : public AST::BaseVisitor<Derived> {
445
445
std::map<int , ASR::symbol_t *> &ast_overload;
446
446
std::string parent_dir;
447
447
Vec<ASR::stmt_t *> *current_body;
448
- ASR::ttype_t *ann_assign_target_type, *assign_target_type;
448
+ ASR::ttype_t *ann_assign_target_type, *assign_target_type, *subscript_value_type ;
449
449
450
450
std::map<std::string, int > generic_func_nums;
451
451
std::map<std::string, std::map<std::string, ASR::ttype_t *>> generic_func_subs;
@@ -456,7 +456,7 @@ class CommonVisitor : public AST::BaseVisitor<Derived> {
456
456
: diag{diagnostics}, al{al}, current_scope{symbol_table}, main_module{main_module},
457
457
ast_overload{ast_overload}, parent_dir{parent_dir},
458
458
current_body{nullptr }, ann_assign_target_type{nullptr },
459
- assign_target_type{nullptr } {
459
+ assign_target_type{nullptr }, subscript_value_type{ nullptr } {
460
460
current_module_dependencies.reserve (al, 4 );
461
461
}
462
462
@@ -2388,6 +2388,7 @@ class CommonVisitor : public AST::BaseVisitor<Derived> {
2388
2388
2389
2389
if ( !visit_SubscriptIndices (x.m_slice , args, value, type,
2390
2390
is_item, x.base .base .loc ) ) {
2391
+ subscript_value_type = type;
2391
2392
return ;
2392
2393
}
2393
2394
@@ -2408,6 +2409,7 @@ class CommonVisitor : public AST::BaseVisitor<Derived> {
2408
2409
tmp = ASR::make_ArraySection_t (al, x.base .base .loc , v_Var, args.p ,
2409
2410
args.size (), type, nullptr );
2410
2411
}
2412
+ subscript_value_type = ASRUtils::expr_type (value);
2411
2413
}
2412
2414
2413
2415
};
@@ -3088,65 +3090,59 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
3088
3090
for (size_t i=0 ; i<x.n_targets ; i++) {
3089
3091
if (AST::is_a<AST::Subscript_t>(*x.m_targets [i])) {
3090
3092
AST::Subscript_t *sb = AST::down_cast<AST::Subscript_t>(x.m_targets [i]);
3091
- if (AST::is_a<AST::Name_t>(*sb->m_value )) {
3092
- std::string name = AST::down_cast<AST::Name_t>(sb->m_value )->m_id ;
3093
- ASR::symbol_t *s = current_scope->resolve_symbol (name);
3094
- if (!s) {
3095
- throw SemanticError (" Variable: '" + name + " ' is not declared" ,
3096
- x.base .base .loc );
3093
+ subscript_value_type = nullptr ;
3094
+ visit_Subscript (*sb);
3095
+ ASR::ttype_t * type = subscript_value_type;
3096
+ ASR::expr_t * subscript_expr = ASRUtils::EXPR (tmp);
3097
+ if (ASR::is_a<ASR::Dict_t>(*type)) {
3098
+ // dict insert case;
3099
+ this ->visit_expr (*sb->m_slice );
3100
+ ASR::expr_t *key = ASRUtils::EXPR (tmp);
3101
+ ASR::ttype_t *key_type = ASR::down_cast<ASR::Dict_t>(type)->m_key_type ;
3102
+ ASR::ttype_t *value_type = ASR::down_cast<ASR::Dict_t>(type)->m_value_type ;
3103
+ if (!ASRUtils::check_equal_type (ASRUtils::expr_type (key), key_type)) {
3104
+ std::string ktype = ASRUtils::type_to_str_python (ASRUtils::expr_type (key));
3105
+ std::string totype = ASRUtils::type_to_str_python (key_type);
3106
+ diag.add (diag::Diagnostic (
3107
+ " Type mismatch in dictionary key, the types must be compatible" ,
3108
+ diag::Level::Error, diag::Stage::Semantic, {
3109
+ diag::Label (" type mismatch (found: '" + ktype + " ', expected: '" + totype + " ')" ,
3110
+ {key->base .loc })
3111
+ })
3112
+ );
3113
+ throw SemanticAbort ();
3097
3114
}
3098
- ASR::Variable_t *v = ASR::down_cast<ASR::Variable_t>(s);
3099
- ASR::ttype_t *type = v->m_type ;
3100
- if (ASR::is_a<ASR::Dict_t>(*type)) {
3101
- // dict insert case;
3102
- this ->visit_expr (*sb->m_slice );
3103
- ASR::expr_t *key = ASRUtils::EXPR (tmp);
3104
- ASR::ttype_t *key_type = ASR::down_cast<ASR::Dict_t>(type)->m_key_type ;
3105
- ASR::ttype_t *value_type = ASR::down_cast<ASR::Dict_t>(type)->m_value_type ;
3106
- if (!ASRUtils::check_equal_type (ASRUtils::expr_type (key), key_type)) {
3107
- std::string ktype = ASRUtils::type_to_str_python (ASRUtils::expr_type (key));
3108
- std::string totype = ASRUtils::type_to_str_python (key_type);
3109
- diag.add (diag::Diagnostic (
3110
- " Type mismatch in dictionary key, the types must be compatible" ,
3111
- diag::Level::Error, diag::Stage::Semantic, {
3112
- diag::Label (" type mismatch (found: '" + ktype + " ', expected: '" + totype + " ')" ,
3113
- {key->base .loc })
3114
- })
3115
- );
3116
- throw SemanticAbort ();
3117
- }
3118
- assign_target_type = value_type;
3119
- this ->visit_expr (*x.m_value );
3120
- if (tmp) {
3121
- // This happens if `m.m_value` is `empty`, such as in:
3122
- // a = empty(16)
3123
- // We skip this statement for now, the array is declared
3124
- // by the annotation.
3125
- // TODO: enforce that empty(), ones(), zeros() is called
3126
- // for every declaration.
3127
- tmp_value = ASRUtils::EXPR (tmp);
3128
- }
3129
- if (!ASRUtils::check_equal_type (ASRUtils::expr_type (tmp_value), value_type)) {
3130
- std::string vtype = ASRUtils::type_to_str_python (ASRUtils::expr_type (tmp_value));
3131
- std::string totype = ASRUtils::type_to_str_python (value_type);
3132
- diag.add (diag::Diagnostic (
3133
- " Type mismatch in dictionary value, the types must be compatible" ,
3134
- diag::Level::Error, diag::Stage::Semantic, {
3135
- diag::Label (" type mismatch (found: '" + vtype + " ', expected: '" + totype + " ')" ,
3136
- {tmp_value->base .loc })
3137
- })
3138
- );
3139
- throw SemanticAbort ();
3140
- }
3141
- ASR::expr_t * se = ASR::down_cast<ASR::expr_t >(
3142
- ASR::make_Var_t (al, x.base .base .loc , s));
3143
- tmp = nullptr ;
3144
- tmp_vec.push_back (make_DictInsert_t (al, x.base .base .loc , se, key, tmp_value));
3145
- continue ;
3146
- } else if (ASRUtils::is_immutable (type)) {
3147
- throw SemanticError (" '" + ASRUtils::type_to_str_python (type) + " ' object does not support"
3148
- " item assignment" , x.base .base .loc );
3115
+ assign_target_type = value_type;
3116
+ this ->visit_expr (*x.m_value );
3117
+ if (tmp) {
3118
+ // This happens if `m.m_value` is `empty`, such as in:
3119
+ // a = empty(16)
3120
+ // We skip this statement for now, the array is declared
3121
+ // by the annotation.
3122
+ // TODO: enforce that empty(), ones(), zeros() is called
3123
+ // for every declaration.
3124
+ tmp_value = ASRUtils::EXPR (tmp);
3149
3125
}
3126
+ if (!ASRUtils::check_equal_type (ASRUtils::expr_type (tmp_value), value_type)) {
3127
+ std::string vtype = ASRUtils::type_to_str_python (ASRUtils::expr_type (tmp_value));
3128
+ std::string totype = ASRUtils::type_to_str_python (value_type);
3129
+ diag.add (diag::Diagnostic (
3130
+ " Type mismatch in dictionary value, the types must be compatible" ,
3131
+ diag::Level::Error, diag::Stage::Semantic, {
3132
+ diag::Label (" type mismatch (found: '" + vtype + " ', expected: '" + totype + " ')" ,
3133
+ {tmp_value->base .loc })
3134
+ })
3135
+ );
3136
+ throw SemanticAbort ();
3137
+ }
3138
+ LFORTRAN_ASSERT (ASR::is_a<ASR::DictItem_t>(*subscript_expr));
3139
+ ASR::DictItem_t* dict_item = ASR::down_cast<ASR::DictItem_t>(subscript_expr);
3140
+ tmp = nullptr ;
3141
+ tmp_vec.push_back (make_DictInsert_t (al, x.base .base .loc , dict_item->m_a , key, tmp_value));
3142
+ continue ;
3143
+ } else if (ASRUtils::is_immutable (type)) {
3144
+ throw SemanticError (" '" + ASRUtils::type_to_str_python (type) + " ' object does not support"
3145
+ " item assignment" , x.base .base .loc );
3150
3146
}
3151
3147
} else if (AST::is_a<AST::Attribute_t>(*x.m_targets [i])) {
3152
3148
AST::Attribute_t *attr = AST::down_cast<AST::Attribute_t>(x.m_targets [i]);
@@ -3175,7 +3171,7 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
3175
3171
}
3176
3172
}
3177
3173
}
3178
- if (!tmp_value) continue ;
3174
+
3179
3175
this ->visit_expr (*x.m_targets [i]);
3180
3176
target = ASRUtils::EXPR (tmp);
3181
3177
assign_target_type = ASRUtils::expr_type (target);
@@ -3189,6 +3185,7 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
3189
3185
// for every declaration.
3190
3186
tmp_value = ASRUtils::EXPR (tmp);
3191
3187
}
3188
+ if (!tmp_value) continue ;
3192
3189
ASR::ttype_t *target_type = ASRUtils::expr_type (target);
3193
3190
ASR::ttype_t *value_type = ASRUtils::expr_type (tmp_value);
3194
3191
// Check if the target parameter type can be assigned with zero
0 commit comments