Skip to content

Raise error for a type mismatch in annassign #370

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/lpython/semantics/python_ast_to_asr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1737,6 +1737,18 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
this->visit_expr(*x.m_value);
value = ASRUtils::EXPR(tmp);
value = cast_helper(type, value, true);
if (!ASRUtils::check_equal_type(type, ASRUtils::expr_type(value))) {
std::string ltype = ASRUtils::type_to_str(type);
std::string rtype = ASRUtils::type_to_str(ASRUtils::expr_type(value));
diag.add(diag::Diagnostic(
"Type mismatch in annotation-assignment, the types must be compatible",
diag::Level::Error, diag::Stage::Semantic, {
diag::Label("type mismatch (" + ltype + " and " + rtype + ")",
{x.m_target->base.loc, value->base.loc})
})
);
throw SemanticAbort();
}
}
ASR::expr_t *init_expr = nullptr;
ASR::intentType s_intent = ASRUtils::intent_local;
Expand Down
7 changes: 7 additions & 0 deletions tests/errors/test_annassign_type_mismatch.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from ltypes import i32

def f():
x: i32[4] = [1, 2, 3, 4]
print(a)

f()
13 changes: 13 additions & 0 deletions tests/reference/asr-test_annassign_type_mismatch-7dac7be.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"basename": "asr-test_annassign_type_mismatch-7dac7be",
"cmd": "lpython --show-asr --no-color {infile} -o {outfile}",
"infile": "tests/errors/test_annassign_type_mismatch.py",
"infile_hash": "5dacc11ffb3bc47fb14e508f454836ce4b196598d1943237cba9c86c",
"outfile": null,
"outfile_hash": null,
"stdout": null,
"stdout_hash": null,
"stderr": "asr-test_annassign_type_mismatch-7dac7be.stderr",
"stderr_hash": "26b687e0b4a187c139b077ec970580df391288fce1d13bde3770ce3b",
"returncode": 2
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
semantic error: Type mismatch in annotation-assignment, the types must be compatible
--> tests/errors/test_annassign_type_mismatch.py:4:5
|
4 | x: i32[4] = [1, 2, 3, 4]
| ^ ^^^^^^^^^^^^ type mismatch (integer and list)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So the type on the left is not just an integer, but an array of integers. So we should improve the error message to say it is an array.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An idea what this can print is at #398 (comment), but that's longer term. As part of this PR or a next PR, we change just print it as:


4 |     x: i32[4] = [1, 2, 3, 4]
  |     ^           ^^^^^^^^^^^^ type mismatch (i32[4] and list[i32])

We can do this by changing ASRUtils::type_to_str to ASRUtils::type_to_str_python and this function would print the type as i32[4].

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll open a new PR for that soon. Thanks. We definitely need excellent and self-explanatory error messages.

4 changes: 4 additions & 0 deletions tests/tests.toml
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,10 @@ asr = true
filename = "errors/test_str_slicing.py"
asr = true

[[test]]
filename = "errors/test_annassign_type_mismatch.py"
asr = true

[[test]]
filename = "errors/test_append_type_mismatch.py"
asr = true