Skip to content

Commit

Permalink
Merge pull request lcompilers#1966 from Smit-create/i-1952-error
Browse files Browse the repository at this point in the history
Fix error messages
  • Loading branch information
Smit-create authored Jun 21, 2023
2 parents 04a54ec + 11333e6 commit bcd882a
Show file tree
Hide file tree
Showing 16 changed files with 29 additions and 24 deletions.
6 changes: 3 additions & 3 deletions src/libasr/asr_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -1266,15 +1266,15 @@ static inline std::string type_to_str_python(const ASR::ttype_t *t,
}
case ASR::ttypeType::Struct: {
ASR::Struct_t* d = ASR::down_cast<ASR::Struct_t>(t);
return "struct " + std::string(symbol_name(d->m_derived_type));
return std::string(symbol_name(d->m_derived_type));
}
case ASR::ttypeType::Enum: {
ASR::Enum_t* d = ASR::down_cast<ASR::Enum_t>(t);
return "enum " + std::string(symbol_name(d->m_enum_type));
return std::string(symbol_name(d->m_enum_type));
}
case ASR::ttypeType::Union: {
ASR::Union_t* d = ASR::down_cast<ASR::Union_t>(t);
return "union " + std::string(symbol_name(d->m_union_type));
return std::string(symbol_name(d->m_union_type));
}
case ASR::ttypeType::Pointer: {
ASR::Pointer_t* p = ASR::down_cast<ASR::Pointer_t>(t);
Expand Down
19 changes: 12 additions & 7 deletions src/lpython/semantics/python_ast_to_asr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1296,8 +1296,9 @@ class CommonVisitor : public AST::BaseVisitor<Struct> {
args, StructType, loc);
}

if (args.size() > 0 && args.size() > StructType->n_members) {
throw SemanticError("StructConstructor arguments are greater the number of struct members", loc);
if (args.size() > 0 && args.size() > StructType->n_members) {
throw SemanticError("Struct constructor has more arguments than the number of struct members",
loc);
}

for( size_t i = 0; i < args.size(); i++ ) {
Expand All @@ -1322,7 +1323,7 @@ class CommonVisitor : public AST::BaseVisitor<Struct> {
}
args.p[i].m_value = arg_new_i;
}
for (size_t i=args.size(); i<StructType->n_members; i++) {
for (size_t i = args.size(); i < StructType->n_members; i++) {
std::string member_name = StructType->m_members[i];
ASR::Variable_t* member_var = ASR::down_cast<ASR::Variable_t>(
StructType->m_symtab->resolve_symbol(member_name));
Expand Down Expand Up @@ -2770,8 +2771,9 @@ class CommonVisitor : public AST::BaseVisitor<Struct> {
this->visit_expr(*x.m_value);
} else {
if (ASR::is_a<ASR::Struct_t>(*type)) {
throw SemanticError(ASRUtils::type_to_str_python(type) + " " + var_name
+ " must be initialized a value", x.base.base.loc);
//`s` must be initialized with an instance of S
throw SemanticError("`" + var_name + "` must be initialized with an instance of " +
ASRUtils::type_to_str_python(type), x.base.base.loc);
}
}
if( is_c_p_pointer_call ) {
Expand Down Expand Up @@ -4859,12 +4861,15 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
}
ASR::Variable_t* v = ASR::down_cast<ASR::Variable_t>(s);
if (v->m_intent == ASR::intentType::In) {
std::string msg = "Hint: create a new local variable with a different name";
if (ASRUtils::is_aggregate_type(v->m_type)) {
msg = "Use InOut[" + ASRUtils::type_to_str_python(v->m_type) + "] to allow assignment";
}
diag.add(diag::Diagnostic(
"Assignment to an input function parameter `"
+ std::string(v->m_name) + "` is not allowed",
diag::Level::Error, diag::Stage::Semantic, {
diag::Label("Use InOut[" + std::string(v->m_name) + "] to allow assignment",
{x->base.loc})
diag::Label(msg, {x->base.loc})
})
);
throw SemanticAbort();
Expand Down
2 changes: 1 addition & 1 deletion tests/reference/asr-func_02-b439474.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@
"stdout": null,
"stdout_hash": null,
"stderr": "asr-func_02-b439474.stderr",
"stderr_hash": "dbf45994153b013641156474184e8d5ae58a976e57eefefb0d2737dd",
"stderr_hash": "855967bc8c46a46b93c348de6e3004107861b10fad24496866c8a197",
"returncode": 2
}
2 changes: 1 addition & 1 deletion tests/reference/asr-func_02-b439474.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ semantic error: Assignment to an input function parameter `n` is not allowed
--> tests/errors/func_02.py:4:5
|
4 | n = 5
| ^ Use InOut[n] to allow assignment
| ^ Hint: create a new local variable with a different name
2 changes: 1 addition & 1 deletion tests/reference/asr-func_03-cd744a0.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@
"stdout": null,
"stdout_hash": null,
"stderr": "asr-func_03-cd744a0.stderr",
"stderr_hash": "1901c15b5939f22ae89d867dffd0528fd4059bc535d480aa335398a1",
"stderr_hash": "d1201fe81d9eeb213996a3f316f463ee4071c9192fb20d7bf327a898",
"returncode": 2
}
2 changes: 1 addition & 1 deletion tests/reference/asr-func_03-cd744a0.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ semantic error: Assignment to an input function parameter `l` is not allowed
--> tests/errors/func_03.py:4:5
|
4 | l[5] = 5
| ^ Use InOut[l] to allow assignment
| ^ Use InOut[list[i32]] to allow assignment
2 changes: 1 addition & 1 deletion tests/reference/asr-func_07-4a8c076.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@
"stdout": null,
"stdout_hash": null,
"stderr": "asr-func_07-4a8c076.stderr",
"stderr_hash": "3a25cc60174926a41548204a7db65b66897c7bbe0ac32001d5251625",
"stderr_hash": "f318e29b96f7730e9ed5fd89ae43e851f22bf50a5c3b2bde415e24e2",
"returncode": 2
}
2 changes: 1 addition & 1 deletion tests/reference/asr-func_07-4a8c076.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ semantic error: Assignment to an input function parameter `this` is not allowed
--> tests/errors/func_07.py:12:5
|
12 | this._len = len(this._buf)
| ^^^^ Use InOut[this] to allow assignment
| ^^^^ Use InOut[StringIO] to allow assignment
2 changes: 1 addition & 1 deletion tests/reference/asr-structs_02-f95782c.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@
"stdout": null,
"stdout_hash": null,
"stderr": "asr-structs_02-f95782c.stderr",
"stderr_hash": "feebf3045d755a862d604df8c8ab0e0cb346f7fbc285256b18e9d559",
"stderr_hash": "e77891a7762fcd30afe98c91c7ffeae4342b646a0d44eebd2cb08f1b",
"returncode": 2
}
2 changes: 1 addition & 1 deletion tests/reference/asr-structs_02-f95782c.stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
semantic error: struct S s must be initialized a value
semantic error: `s` must be initialized with an instance of S
--> tests/errors/structs_02.py:8:5
|
8 | s: S
Expand Down
2 changes: 1 addition & 1 deletion tests/reference/asr-structs_06-6e14537.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@
"stdout": null,
"stdout_hash": null,
"stderr": "asr-structs_06-6e14537.stderr",
"stderr_hash": "25bdf7fcdb115ed0080e2249bba374b0e7ba712af33f8ebfc5e15165",
"stderr_hash": "8e0da9c7e84854ce3d6ad79066a9fbb33d82c9fcde3af2a7baeccec8",
"returncode": 2
}
2 changes: 1 addition & 1 deletion tests/reference/asr-structs_06-6e14537.stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
semantic error: StructConstructor arguments are greater the number of struct members
semantic error: Struct constructor has more arguments than the number of struct members
--> tests/errors/structs_06.py:9:12
|
9 | s: S = S(2, 3, 4, 5)
Expand Down
2 changes: 1 addition & 1 deletion tests/reference/asr-structs_08-fa4dbf0.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@
"stdout": null,
"stdout_hash": null,
"stderr": "asr-structs_08-fa4dbf0.stderr",
"stderr_hash": "d21036687d451697c3ecbdedfe7319739db3949e8846c3264bce0788",
"stderr_hash": "cf488d893463c941c43080cebb56591bd17c5bed4cb7acd97353d59a",
"returncode": 2
}
2 changes: 1 addition & 1 deletion tests/reference/asr-structs_08-fa4dbf0.stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
semantic error: StructConstructor arguments are greater the number of struct members
semantic error: Struct constructor has more arguments than the number of struct members
--> tests/errors/structs_08.py:13:29
|
13 | test_dude3 : StringIO = StringIO(integer_asr, 3, 5, 2)
Expand Down
2 changes: 1 addition & 1 deletion tests/reference/asr-test_assign9-51278b8.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_assign9-51278b8.stderr",
"stderr_hash": "4d28235c9deb91408ca70a5c3dd21225d47057cc695e5fe0394019eb",
"stderr_hash": "602e4fa615e929bad9586849d2a5167741930e56db6373cdca3695d0",
"returncode": 2
}
2 changes: 1 addition & 1 deletion tests/reference/asr-test_assign9-51278b8.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ semantic error: Assignment to an input function parameter `x` is not allowed
--> tests/errors/test_assign9.py:2:5
|
2 | x = 2
| ^ Use InOut[x] to allow assignment
| ^ Hint: create a new local variable with a different name

0 comments on commit bcd882a

Please sign in to comment.