Skip to content

Commit 1c1028d

Browse files
committed
Raise error for a type mismatch in annassign
1 parent 1677c91 commit 1c1028d

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

src/lpython/semantics/python_ast_to_asr.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1737,6 +1737,18 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
17371737
this->visit_expr(*x.m_value);
17381738
value = ASRUtils::EXPR(tmp);
17391739
value = cast_helper(type, value, true);
1740+
if (!ASRUtils::check_equal_type(type, ASRUtils::expr_type(value))) {
1741+
std::string ltype = ASRUtils::type_to_str(type);
1742+
std::string rtype = ASRUtils::type_to_str(ASRUtils::expr_type(value));
1743+
diag.add(diag::Diagnostic(
1744+
"Type mismatch for an annotation-assignment. In LPython, the types must be compatible.",
1745+
diag::Level::Error, diag::Stage::Semantic, {
1746+
diag::Label("type mismatch (" + ltype + " and " + rtype + ")",
1747+
{x.m_target->base.loc, value->base.loc})
1748+
})
1749+
);
1750+
throw SemanticAbort();
1751+
}
17401752
}
17411753
ASR::expr_t *init_expr = nullptr;
17421754
ASR::intentType s_intent = ASRUtils::intent_local;

0 commit comments

Comments
 (0)