Skip to content

Commit 7020f3a

Browse files
committed
fixed
1 parent 76efa21 commit 7020f3a

File tree

3 files changed

+68
-75
lines changed

3 files changed

+68
-75
lines changed

integration_tests/test_dict_06.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
11
from ltypes import i32, f64
22

33
def test_dict():
4-
graph: dict[i32, dict[i32, f64]] = {0: {2: 1.0/2.0}, 1: {3: 1.0/3.0}}
4+
graph: dict[i32, dict[i32, f64]] = {0: {2: 1.0/2.0}, 1: {3: 1.0/4.0}}
55
i: i32; j: i32; nodes: i32; eps: f64 = 1e-12
66
nodes = 100
77

8-
for i in range(nodes):
8+
assert abs(graph[0][2] - 0.5) <= eps
9+
assert abs(graph[1][3] - 0.25) <= eps
10+
11+
for i in range(1, nodes):
912
graph[i] = {}
10-
for j in range(nodes):
11-
graph[i][j] = float(abs(j - i))
13+
for j in range(1, nodes):
14+
graph[i][j] = 1.0/float(j + i)
1215

13-
for i in range(nodes):
14-
for j in range(nodes):
15-
print(graph[i][j], float(abs(j - i)))
16-
# assert abs( graph[i][j] - float(abs(j - i)) ) <= eps
16+
for i in range(1, nodes):
17+
for j in range(1, nodes):
18+
assert abs( graph[i][j] - 1.0/float(j + i) ) <= eps
1719

1820
test_dict()

src/libasr/codegen/asr_to_llvm.cpp

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1304,7 +1304,7 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
13041304
set_dict_api(dict_type);
13051305
tmp = llvm_utils->dict_api->read_item(pdict, key, *module, dict_type,
13061306
LLVM::is_llvm_struct(dict_type->m_value_type) ||
1307-
ptr_loads == 0);
1307+
is_assignment_target);
13081308
}
13091309

13101310
void visit_DictPop(const ASR::DictPop_t& x) {
@@ -3318,12 +3318,6 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
33183318
this->visit_expr_wrapper(asr_target0->m_pos, true);
33193319
llvm::Value* pos = tmp;
33203320
target = list_api->read_item(list, pos, true);
3321-
} else if( ASR::is_a<ASR::DictItem_t>(*x.m_target) ) {
3322-
uint64_t ptr_loads_copy = ptr_loads;
3323-
ptr_loads = 0;
3324-
visit_expr(*x.m_target);
3325-
ptr_loads = ptr_loads_copy;
3326-
target = tmp;
33273321
}
33283322
} else {
33293323
ASR::Variable_t *asr_target = EXPR2VAR(x.m_target);

src/lpython/semantics/python_ast_to_asr.cpp

Lines changed: 57 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,7 @@ class CommonVisitor : public AST::BaseVisitor<Derived> {
445445
std::map<int, ASR::symbol_t*> &ast_overload;
446446
std::string parent_dir;
447447
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;
449449

450450
std::map<std::string, int> generic_func_nums;
451451
std::map<std::string, std::map<std::string, ASR::ttype_t*>> generic_func_subs;
@@ -456,7 +456,7 @@ class CommonVisitor : public AST::BaseVisitor<Derived> {
456456
: diag{diagnostics}, al{al}, current_scope{symbol_table}, main_module{main_module},
457457
ast_overload{ast_overload}, parent_dir{parent_dir},
458458
current_body{nullptr}, ann_assign_target_type{nullptr},
459-
assign_target_type{nullptr} {
459+
assign_target_type{nullptr}, subscript_value_type{nullptr} {
460460
current_module_dependencies.reserve(al, 4);
461461
}
462462

@@ -2388,6 +2388,7 @@ class CommonVisitor : public AST::BaseVisitor<Derived> {
23882388

23892389
if( !visit_SubscriptIndices(x.m_slice, args, value, type,
23902390
is_item, x.base.base.loc) ) {
2391+
subscript_value_type = type;
23912392
return ;
23922393
}
23932394

@@ -2408,6 +2409,7 @@ class CommonVisitor : public AST::BaseVisitor<Derived> {
24082409
tmp = ASR::make_ArraySection_t(al, x.base.base.loc, v_Var, args.p,
24092410
args.size(), type, nullptr);
24102411
}
2412+
subscript_value_type = ASRUtils::expr_type(value);
24112413
}
24122414

24132415
};
@@ -3088,65 +3090,59 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
30883090
for (size_t i=0; i<x.n_targets; i++) {
30893091
if (AST::is_a<AST::Subscript_t>(*x.m_targets[i])) {
30903092
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();
30973114
}
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);
31493125
}
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);
31503146
}
31513147
} else if (AST::is_a<AST::Attribute_t>(*x.m_targets[i])) {
31523148
AST::Attribute_t *attr = AST::down_cast<AST::Attribute_t>(x.m_targets[i]);
@@ -3175,7 +3171,7 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
31753171
}
31763172
}
31773173
}
3178-
if (!tmp_value) continue;
3174+
31793175
this->visit_expr(*x.m_targets[i]);
31803176
target = ASRUtils::EXPR(tmp);
31813177
assign_target_type = ASRUtils::expr_type(target);
@@ -3189,6 +3185,7 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
31893185
// for every declaration.
31903186
tmp_value = ASRUtils::EXPR(tmp);
31913187
}
3188+
if (!tmp_value) continue;
31923189
ASR::ttype_t *target_type = ASRUtils::expr_type(target);
31933190
ASR::ttype_t *value_type = ASRUtils::expr_type(tmp_value);
31943191
// Check if the target parameter type can be assigned with zero

0 commit comments

Comments
 (0)