Skip to content

Commit

Permalink
Raise error if loop start, increment and end values do not have same …
Browse files Browse the repository at this point in the history
…types
  • Loading branch information
czgdp1807 committed Jan 6, 2023
1 parent c9e229f commit 5dff5b0
Showing 1 changed file with 35 additions and 8 deletions.
43 changes: 35 additions & 8 deletions src/lpython/semantics/python_ast_to_asr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4259,18 +4259,44 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
a_kind, nullptr, 0));
ASR::expr_t *constant_one = ASR::down_cast<ASR::expr_t>(ASR::make_IntegerConstant_t(
al, loc, 1, a_type));
if (loop_start) {
head.m_start = loop_start;
} else {
head.m_start = ASR::down_cast<ASR::expr_t>(ASR::make_IntegerConstant_t(al, loc, 0, a_type));
if (!loop_start) {
loop_start = ASR::down_cast<ASR::expr_t>(ASR::make_IntegerConstant_t(al, loc, 0, a_type));
}
if (inc) {
head.m_increment = inc;
} else {
head.m_increment = constant_one;
if (!inc) {
inc = constant_one;
}

if( !ASRUtils::check_equal_type(ASRUtils::expr_type(loop_start), ASRUtils::expr_type(loop_end)) ) {
std::string loop_start_strtype = ASRUtils::type_to_str_python(ASRUtils::expr_type(loop_start));
std::string loop_end_strtype = ASRUtils::type_to_str_python(ASRUtils::expr_type(loop_end));
diag.add(diag::Diagnostic(
"Type mismatch in loop start and loop end values, the types must be compatible",
diag::Level::Error, diag::Stage::Semantic, {
diag::Label("type mismatch ('" + loop_start_strtype +
"' and '" + loop_end_strtype + "')",
{loop_start->base.loc, loop_end->base.loc})
})
);
throw SemanticAbort();
}

if( !ASRUtils::check_equal_type(ASRUtils::expr_type(loop_start), ASRUtils::expr_type(inc)) ) {
std::string loop_start_strtype = ASRUtils::type_to_str_python(ASRUtils::expr_type(loop_start));
std::string inc_strtype = ASRUtils::type_to_str_python(ASRUtils::expr_type(inc));
diag.add(diag::Diagnostic(
"Type mismatch in loop start and increment values, the types must be compatible",
diag::Level::Error, diag::Stage::Semantic, {
diag::Label("type mismatch ('" + loop_start_strtype +
"' and '" + inc_strtype + "')",
{loop_start->base.loc, inc->base.loc})
})
);
throw SemanticAbort();
}

head.m_start = loop_start;
head.m_increment = inc;

if( !ASR::is_a<ASR::Integer_t>(*ASRUtils::expr_type(inc)) ) {
throw SemanticError("For loop increment type should be Integer.", loc);
}
Expand All @@ -4293,6 +4319,7 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
offset_op, loc, false);
head.m_end = ASRUtils::EXPR(tmp);


return head;
}

Expand Down

0 comments on commit 5dff5b0

Please sign in to comment.