Skip to content

Improve the error messages #418

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 4 commits into from
Apr 24, 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
26 changes: 22 additions & 4 deletions src/libasr/asr_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -162,13 +162,30 @@ static inline std::string type_to_str_python(const ASR::ttype_t *t)
{
switch (t->type) {
case ASR::ttypeType::Integer: {
return "int";
ASR::Integer_t *i = (ASR::Integer_t*)t;
switch (i->m_kind) {
case 1: { return "i8"; }
case 2: { return "i16"; }
case 4: { return "i32"; }
case 8: { return "i64"; }
default: { throw LFortranException("Integer kind not supported"); }
}
}
case ASR::ttypeType::Real: {
return "float";
ASR::Real_t *r = (ASR::Real_t*)t;
switch (r->m_kind) {
case 4: { return "f32"; }
case 8: { return "f64"; }
default: { throw LFortranException("Float kind not supported"); }
}
}
case ASR::ttypeType::Complex: {
return "complex";
ASR::Complex_t *c = (ASR::Complex_t*)t;
switch (c->m_kind) {
case 4: { return "c32"; }
case 8: { return "c64"; }
default: { throw LFortranException("Complex kind not supported"); }
}
}
case ASR::ttypeType::Logical: {
return "bool";
Expand All @@ -186,7 +203,8 @@ static inline std::string type_to_str_python(const ASR::ttype_t *t)
return "dict";
}
case ASR::ttypeType::List: {
return "list";
ASR::List_t *l = (ASR::List_t *)t;
return "list[" + type_to_str_python(l->m_type) + "]";
}
default : throw LFortranException("Not implemented");
}
Expand Down
6 changes: 3 additions & 3 deletions src/lpython/semantics/python_ast_to_asr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -817,15 +817,15 @@ class CommonVisitor : public AST::BaseVisitor<Derived> {
} else if (ASR::is_a<ASR::List_t>(*left_type) && ASR::is_a<ASR::List_t>(*right_type)
&& op == ASR::binopType::Add) {
dest_type = left_type;
std::string ltype = ASRUtils::type_to_str_python(left_type);
std::string rtype = ASRUtils::type_to_str_python(right_type);
ASR::ttype_t *left_type2 = ASR::down_cast<ASR::List_t>(left_type)->m_type;
ASR::ttype_t *right_type2 = ASR::down_cast<ASR::List_t>(right_type)->m_type;
std::string ltype = ASRUtils::type_to_str_python(left_type2);
std::string rtype = ASRUtils::type_to_str_python(right_type2);
if (!ASRUtils::check_equal_type(left_type2, right_type2)) {
diag.add(diag::Diagnostic(
"Both the lists should be of the same type for concatenation.",
diag::Level::Error, diag::Stage::Semantic, {
diag::Label("type mismatch (list[" + ltype + "] and list[" + rtype + "])",
diag::Label("type mismatch ('" + ltype + "' and '" + rtype + "')",
{left->base.loc, right->base.loc})
})
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@
"stdout": null,
"stdout_hash": null,
"stderr": "asr-test_annassign_type_mismatch-7dac7be.stderr",
"stderr_hash": "568f25869fd6c0d11300ad6095d4994eedbb4e2b538f23c8e34e020c",
"stderr_hash": "2e2230047083bb3431865114def04970e6c69c261d97bc7b05ecf365",
"returncode": 2
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ semantic error: Type mismatch in annotation-assignment, the types must be compat
--> tests/errors/test_annassign_type_mismatch.py:4:5
|
4 | x: i32[4] = [1, 2, 3, 4]
| ^ ^^^^^^^^^^^^ type mismatch ('int' and 'list')
| ^ ^^^^^^^^^^^^ type mismatch ('i32' and 'list[i32]')
2 changes: 1 addition & 1 deletion tests/reference/asr-test_append_type_mismatch-030e9c7.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@
"stdout": null,
"stdout_hash": null,
"stderr": "asr-test_append_type_mismatch-030e9c7.stderr",
"stderr_hash": "6a14ee224ad1fd27e0d28e1f33d4a96b858663aae7d07bca8987575d",
"stderr_hash": "a59a2722e83e531fd9a27bdc31ad61af475a178dcce48c75a1d62595",
"returncode": 2
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
semantic error: Type mismatch while appending to a list, found ('str' and 'int').
semantic error: Type mismatch while appending to a list, found ('str' and 'i32').
--> tests/errors/test_append_type_mismatch.py:6:5
|
6 | l.append('a')
Expand Down
2 changes: 1 addition & 1 deletion tests/reference/asr-test_list1-73fd538.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@
"stdout": null,
"stdout_hash": null,
"stderr": "asr-test_list1-73fd538.stderr",
"stderr_hash": "4bda0c0451782d4d487da271e484718ac1efc9fbe001e4d8e122bfc0",
"stderr_hash": "7143db86381df6ff92cb7432deb228b51302b10edb703cf57e8aaec1",
"returncode": 2
}
2 changes: 1 addition & 1 deletion tests/reference/asr-test_list1-73fd538.stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
semantic error: Type mismatch while removing from a list, found ('str' and 'int').
semantic error: Type mismatch while removing from a list, found ('str' and 'i32').
--> tests/errors/test_list1.py:6:5
|
6 | a.remove('error')
Expand Down
2 changes: 1 addition & 1 deletion tests/reference/asr-test_list_concat-41d186f.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@
"stdout": null,
"stdout_hash": null,
"stderr": "asr-test_list_concat-41d186f.stderr",
"stderr_hash": "c5c416e09e0aee961b0734b7a491c03e59299c0bca6c6afbbbb0f626",
"stderr_hash": "40b83e988e6e02beadaeeafbed4b29e65c985dfd596dd1d0f4a7bd3f",
"returncode": 2
}
2 changes: 1 addition & 1 deletion tests/reference/asr-test_list_concat-41d186f.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ semantic error: Both the lists should be of the same type for concatenation.
--> tests/errors/test_list_concat.py:8:3
|
8 | a += b
| ^ ^ type mismatch (list[int] and list[float])
| ^ ^ type mismatch ('list[i32]' and 'list[f32]')
2 changes: 1 addition & 1 deletion tests/reference/asr-test_set1-11379c7.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@
"stdout": null,
"stdout_hash": null,
"stderr": "asr-test_set1-11379c7.stderr",
"stderr_hash": "c3c7502dd2515b6cbde6697c886e3d428029e46e4ea5cb8029908cf6",
"stderr_hash": "b876c33be46bf8ce85f551ce68d7edc446e41bd00b65b76d5490e5b5",
"returncode": 2
}
2 changes: 1 addition & 1 deletion tests/reference/asr-test_set1-11379c7.stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
semantic error: Found type mismatch in 'add' ('str' and 'int').
semantic error: Found type mismatch in 'add' ('str' and 'i32').
--> tests/errors/test_set1.py:6:5
|
6 | a.add('err')
Expand Down