Skip to content

Commit fff611a

Browse files
authored
Merge pull request #418 from namannimmo10/err-msg
Improve the error messages
2 parents bff0ebc + 521b549 commit fff611a

12 files changed

+35
-17
lines changed

src/libasr/asr_utils.h

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -162,13 +162,30 @@ static inline std::string type_to_str_python(const ASR::ttype_t *t)
162162
{
163163
switch (t->type) {
164164
case ASR::ttypeType::Integer: {
165-
return "int";
165+
ASR::Integer_t *i = (ASR::Integer_t*)t;
166+
switch (i->m_kind) {
167+
case 1: { return "i8"; }
168+
case 2: { return "i16"; }
169+
case 4: { return "i32"; }
170+
case 8: { return "i64"; }
171+
default: { throw LFortranException("Integer kind not supported"); }
172+
}
166173
}
167174
case ASR::ttypeType::Real: {
168-
return "float";
175+
ASR::Real_t *r = (ASR::Real_t*)t;
176+
switch (r->m_kind) {
177+
case 4: { return "f32"; }
178+
case 8: { return "f64"; }
179+
default: { throw LFortranException("Float kind not supported"); }
180+
}
169181
}
170182
case ASR::ttypeType::Complex: {
171-
return "complex";
183+
ASR::Complex_t *c = (ASR::Complex_t*)t;
184+
switch (c->m_kind) {
185+
case 4: { return "c32"; }
186+
case 8: { return "c64"; }
187+
default: { throw LFortranException("Complex kind not supported"); }
188+
}
172189
}
173190
case ASR::ttypeType::Logical: {
174191
return "bool";
@@ -186,7 +203,8 @@ static inline std::string type_to_str_python(const ASR::ttype_t *t)
186203
return "dict";
187204
}
188205
case ASR::ttypeType::List: {
189-
return "list";
206+
ASR::List_t *l = (ASR::List_t *)t;
207+
return "list[" + type_to_str_python(l->m_type) + "]";
190208
}
191209
default : throw LFortranException("Not implemented");
192210
}

src/lpython/semantics/python_ast_to_asr.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -817,15 +817,15 @@ class CommonVisitor : public AST::BaseVisitor<Derived> {
817817
} else if (ASR::is_a<ASR::List_t>(*left_type) && ASR::is_a<ASR::List_t>(*right_type)
818818
&& op == ASR::binopType::Add) {
819819
dest_type = left_type;
820+
std::string ltype = ASRUtils::type_to_str_python(left_type);
821+
std::string rtype = ASRUtils::type_to_str_python(right_type);
820822
ASR::ttype_t *left_type2 = ASR::down_cast<ASR::List_t>(left_type)->m_type;
821823
ASR::ttype_t *right_type2 = ASR::down_cast<ASR::List_t>(right_type)->m_type;
822-
std::string ltype = ASRUtils::type_to_str_python(left_type2);
823-
std::string rtype = ASRUtils::type_to_str_python(right_type2);
824824
if (!ASRUtils::check_equal_type(left_type2, right_type2)) {
825825
diag.add(diag::Diagnostic(
826826
"Both the lists should be of the same type for concatenation.",
827827
diag::Level::Error, diag::Stage::Semantic, {
828-
diag::Label("type mismatch (list[" + ltype + "] and list[" + rtype + "])",
828+
diag::Label("type mismatch ('" + ltype + "' and '" + rtype + "')",
829829
{left->base.loc, right->base.loc})
830830
})
831831
);

tests/reference/asr-test_annassign_type_mismatch-7dac7be.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@
88
"stdout": null,
99
"stdout_hash": null,
1010
"stderr": "asr-test_annassign_type_mismatch-7dac7be.stderr",
11-
"stderr_hash": "568f25869fd6c0d11300ad6095d4994eedbb4e2b538f23c8e34e020c",
11+
"stderr_hash": "2e2230047083bb3431865114def04970e6c69c261d97bc7b05ecf365",
1212
"returncode": 2
1313
}

tests/reference/asr-test_annassign_type_mismatch-7dac7be.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ semantic error: Type mismatch in annotation-assignment, the types must be compat
22
--> tests/errors/test_annassign_type_mismatch.py:4:5
33
|
44
4 | x: i32[4] = [1, 2, 3, 4]
5-
| ^ ^^^^^^^^^^^^ type mismatch ('int' and 'list')
5+
| ^ ^^^^^^^^^^^^ type mismatch ('i32' and 'list[i32]')

tests/reference/asr-test_append_type_mismatch-030e9c7.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@
88
"stdout": null,
99
"stdout_hash": null,
1010
"stderr": "asr-test_append_type_mismatch-030e9c7.stderr",
11-
"stderr_hash": "6a14ee224ad1fd27e0d28e1f33d4a96b858663aae7d07bca8987575d",
11+
"stderr_hash": "a59a2722e83e531fd9a27bdc31ad61af475a178dcce48c75a1d62595",
1212
"returncode": 2
1313
}

tests/reference/asr-test_append_type_mismatch-030e9c7.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
semantic error: Type mismatch while appending to a list, found ('str' and 'int').
1+
semantic error: Type mismatch while appending to a list, found ('str' and 'i32').
22
--> tests/errors/test_append_type_mismatch.py:6:5
33
|
44
6 | l.append('a')

tests/reference/asr-test_list1-73fd538.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@
88
"stdout": null,
99
"stdout_hash": null,
1010
"stderr": "asr-test_list1-73fd538.stderr",
11-
"stderr_hash": "4bda0c0451782d4d487da271e484718ac1efc9fbe001e4d8e122bfc0",
11+
"stderr_hash": "7143db86381df6ff92cb7432deb228b51302b10edb703cf57e8aaec1",
1212
"returncode": 2
1313
}

tests/reference/asr-test_list1-73fd538.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
semantic error: Type mismatch while removing from a list, found ('str' and 'int').
1+
semantic error: Type mismatch while removing from a list, found ('str' and 'i32').
22
--> tests/errors/test_list1.py:6:5
33
|
44
6 | a.remove('error')

tests/reference/asr-test_list_concat-41d186f.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@
88
"stdout": null,
99
"stdout_hash": null,
1010
"stderr": "asr-test_list_concat-41d186f.stderr",
11-
"stderr_hash": "c5c416e09e0aee961b0734b7a491c03e59299c0bca6c6afbbbb0f626",
11+
"stderr_hash": "40b83e988e6e02beadaeeafbed4b29e65c985dfd596dd1d0f4a7bd3f",
1212
"returncode": 2
1313
}

tests/reference/asr-test_list_concat-41d186f.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ semantic error: Both the lists should be of the same type for concatenation.
22
--> tests/errors/test_list_concat.py:8:3
33
|
44
8 | a += b
5-
| ^ ^ type mismatch (list[int] and list[float])
5+
| ^ ^ type mismatch ('list[i32]' and 'list[f32]')

tests/reference/asr-test_set1-11379c7.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@
88
"stdout": null,
99
"stdout_hash": null,
1010
"stderr": "asr-test_set1-11379c7.stderr",
11-
"stderr_hash": "c3c7502dd2515b6cbde6697c886e3d428029e46e4ea5cb8029908cf6",
11+
"stderr_hash": "b876c33be46bf8ce85f551ce68d7edc446e41bd00b65b76d5490e5b5",
1212
"returncode": 2
1313
}

tests/reference/asr-test_set1-11379c7.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
semantic error: Found type mismatch in 'add' ('str' and 'int').
1+
semantic error: Found type mismatch in 'add' ('str' and 'i32').
22
--> tests/errors/test_set1.py:6:5
33
|
44
6 | a.add('err')

0 commit comments

Comments
 (0)