Skip to content

Commit

Permalink
Merge pull request #2476 from NishantBansal2003/semantic_error_update
Browse files Browse the repository at this point in the history
fixes semantic error for int (Issue #1926)
  • Loading branch information
certik authored Feb 7, 2024
2 parents 8b1988d + a95716c commit 6285062
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/lpython/semantics/python_ast_to_asr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -876,7 +876,18 @@ class CommonVisitor : public AST::BaseVisitor<Struct> {
}

if( !type && raise_error ) {
throw SemanticError("Unsupported type annotation: " + var_annotation, loc);
if (var_annotation == "int") {
std::string msg = "Hint: Use i8, i16, i32 or i64 for now. ";
diag.add(diag::Diagnostic(
var_annotation + " type is not supported yet. ",
diag::Level::Error, diag::Stage::Semantic, {
diag::Label(msg, {loc})
})
);
throw SemanticAbort();
} else {
throw SemanticError("Unsupported type annotation: " + var_annotation, loc);
}
}

return type;
Expand Down
8 changes: 8 additions & 0 deletions tests/errors/test_int_semantic_error.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from lpython import i32

def variable_function():
x: int = 1
y: i32 = 2
print("x + y is", x + y)

variable_function()
13 changes: 13 additions & 0 deletions tests/reference/asr-test_int_semantic_error-44fe25e.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"basename": "asr-test_int_semantic_error-44fe25e",
"cmd": "lpython --show-asr --no-color {infile} -o {outfile}",
"infile": "tests/errors/test_int_semantic_error.py",
"infile_hash": "79ca7d3f440b2538aa0819f910bea5ef24820d245b2179e1bf4cce6d",
"outfile": null,
"outfile_hash": null,
"stdout": null,
"stdout_hash": null,
"stderr": "asr-test_int_semantic_error-44fe25e.stderr",
"stderr_hash": "a1cd1ec0fee194e3c1651668753a1666ca46c925fa91335c6230e026",
"returncode": 2
}
5 changes: 5 additions & 0 deletions tests/reference/asr-test_int_semantic_error-44fe25e.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
semantic error: int type is not supported yet.
--> tests/errors/test_int_semantic_error.py:4:8
|
4 | x: int = 1
| ^^^ Hint: Use i8, i16, i32 or i64 for now.
4 changes: 4 additions & 0 deletions tests/tests.toml
Original file line number Diff line number Diff line change
Expand Up @@ -1100,6 +1100,10 @@ asr = true
filename = "errors/test_unsupported_type.py"
asr = true

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

[[test]]
filename = "errors/generics_error_01.py"
asr = true
Expand Down

0 comments on commit 6285062

Please sign in to comment.