From 73726271ceb075d42f5b6e9eeeed10171dd8c3e8 Mon Sep 17 00:00:00 2001 From: Nishant Bansal Date: Sun, 4 Feb 2024 10:09:49 +0530 Subject: [PATCH 1/3] fixes semantic error for int (Issue #1926) --- src/lpython/semantics/python_ast_to_asr.cpp | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/lpython/semantics/python_ast_to_asr.cpp b/src/lpython/semantics/python_ast_to_asr.cpp index 5eb253f8f0..8d2b3ab3c1 100644 --- a/src/lpython/semantics/python_ast_to_asr.cpp +++ b/src/lpython/semantics/python_ast_to_asr.cpp @@ -876,7 +876,12 @@ class CommonVisitor : public AST::BaseVisitor { } if( !type && raise_error ) { - throw SemanticError("Unsupported type annotation: " + var_annotation, loc); + if (var_annotation == "int") { + throw SemanticError(var_annotation + " type is not supported yet. " , loc); + } else + { + throw SemanticError("Unsupported type annotation: " + var_annotation, loc); + } } return type; From 942cffea2f0c718c123ec6cf34ddd96809ab4562 Mon Sep 17 00:00:00 2001 From: Nishant Bansal Date: Mon, 5 Feb 2024 10:42:44 +0530 Subject: [PATCH 2/3] Improved int semantic error message with hint (Issue #1926) --- src/lpython/semantics/python_ast_to_asr.cpp | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/lpython/semantics/python_ast_to_asr.cpp b/src/lpython/semantics/python_ast_to_asr.cpp index 8d2b3ab3c1..f3e24dd94e 100644 --- a/src/lpython/semantics/python_ast_to_asr.cpp +++ b/src/lpython/semantics/python_ast_to_asr.cpp @@ -877,9 +877,15 @@ class CommonVisitor : public AST::BaseVisitor { if( !type && raise_error ) { if (var_annotation == "int") { - throw SemanticError(var_annotation + " type is not supported yet. " , loc); - } else - { + 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); } } From a95716c8be1ab1195307b96da82bc48f6969e692 Mon Sep 17 00:00:00 2001 From: Nishant Bansal Date: Mon, 5 Feb 2024 22:12:09 +0530 Subject: [PATCH 3/3] Added Reference test (Issue #1926) --- tests/errors/test_int_semantic_error.py | 8 ++++++++ .../asr-test_int_semantic_error-44fe25e.json | 13 +++++++++++++ .../asr-test_int_semantic_error-44fe25e.stderr | 5 +++++ tests/tests.toml | 4 ++++ 4 files changed, 30 insertions(+) create mode 100644 tests/errors/test_int_semantic_error.py create mode 100644 tests/reference/asr-test_int_semantic_error-44fe25e.json create mode 100644 tests/reference/asr-test_int_semantic_error-44fe25e.stderr diff --git a/tests/errors/test_int_semantic_error.py b/tests/errors/test_int_semantic_error.py new file mode 100644 index 0000000000..6eda4a20aa --- /dev/null +++ b/tests/errors/test_int_semantic_error.py @@ -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() \ No newline at end of file diff --git a/tests/reference/asr-test_int_semantic_error-44fe25e.json b/tests/reference/asr-test_int_semantic_error-44fe25e.json new file mode 100644 index 0000000000..3747fc7b8b --- /dev/null +++ b/tests/reference/asr-test_int_semantic_error-44fe25e.json @@ -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 +} \ No newline at end of file diff --git a/tests/reference/asr-test_int_semantic_error-44fe25e.stderr b/tests/reference/asr-test_int_semantic_error-44fe25e.stderr new file mode 100644 index 0000000000..f75e0f05a6 --- /dev/null +++ b/tests/reference/asr-test_int_semantic_error-44fe25e.stderr @@ -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. diff --git a/tests/tests.toml b/tests/tests.toml index 5f6f56a9cf..57ed98bac4 100644 --- a/tests/tests.toml +++ b/tests/tests.toml @@ -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