From 4f37bb2cfab995464f89b0b7dac7938ac88e4261 Mon Sep 17 00:00:00 2001 From: Anurag Bhat <90216905+faze-geek@users.noreply.github.com> Date: Tue, 13 Feb 2024 12:46:46 +0530 Subject: [PATCH] Added semantic error for float declaration. (#2529) * Add semantic error for float declaration & added tests for the same. --- src/lpython/semantics/python_ast_to_asr.cpp | 11 ++++++++++- tests/errors/test_float_semantic_error.py | 8 ++++++++ .../asr-test_float_semantic_error-58c0c90.json | 13 +++++++++++++ .../asr-test_float_semantic_error-58c0c90.stderr | 5 +++++ tests/tests.toml | 4 ++++ 5 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 tests/errors/test_float_semantic_error.py create mode 100644 tests/reference/asr-test_float_semantic_error-58c0c90.json create mode 100644 tests/reference/asr-test_float_semantic_error-58c0c90.stderr diff --git a/src/lpython/semantics/python_ast_to_asr.cpp b/src/lpython/semantics/python_ast_to_asr.cpp index e75028c9e7..ff2ea35546 100644 --- a/src/lpython/semantics/python_ast_to_asr.cpp +++ b/src/lpython/semantics/python_ast_to_asr.cpp @@ -885,7 +885,16 @@ class CommonVisitor : public AST::BaseVisitor { }) ); throw SemanticAbort(); - } else { + } else if (var_annotation == "float") { + std::string msg = "Hint: Use f32 or f64 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("The type '" + var_annotation+"' is undeclared.", loc); } } diff --git a/tests/errors/test_float_semantic_error.py b/tests/errors/test_float_semantic_error.py new file mode 100644 index 0000000000..d0e88ccbc5 --- /dev/null +++ b/tests/errors/test_float_semantic_error.py @@ -0,0 +1,8 @@ +from lpython import f64 + +def main0(): + pi: float = 3.14 + radi: f64 = 2.0 + print("Area of Circle is", pi*radi**2) + +main0() \ No newline at end of file diff --git a/tests/reference/asr-test_float_semantic_error-58c0c90.json b/tests/reference/asr-test_float_semantic_error-58c0c90.json new file mode 100644 index 0000000000..e451309c05 --- /dev/null +++ b/tests/reference/asr-test_float_semantic_error-58c0c90.json @@ -0,0 +1,13 @@ +{ + "basename": "asr-test_float_semantic_error-58c0c90", + "cmd": "lpython --show-asr --no-color {infile} -o {outfile}", + "infile": "tests/errors/test_float_semantic_error.py", + "infile_hash": "10929e3991a4aee1a2de473fc5f8caa48b6dcf4a35e2329a15ea5f1f", + "outfile": null, + "outfile_hash": null, + "stdout": null, + "stdout_hash": null, + "stderr": "asr-test_float_semantic_error-58c0c90.stderr", + "stderr_hash": "c11166fb86be513e74796a51f5c9e84653c2eb894ed95d502ed8af57", + "returncode": 2 +} \ No newline at end of file diff --git a/tests/reference/asr-test_float_semantic_error-58c0c90.stderr b/tests/reference/asr-test_float_semantic_error-58c0c90.stderr new file mode 100644 index 0000000000..2698ced8c9 --- /dev/null +++ b/tests/reference/asr-test_float_semantic_error-58c0c90.stderr @@ -0,0 +1,5 @@ +semantic error: float type is not supported yet. + --> tests/errors/test_float_semantic_error.py:4:9 + | +4 | pi: float = 3.14 + | ^^^^^ Hint: Use f32 or f64 for now. diff --git a/tests/tests.toml b/tests/tests.toml index b033615665..4f048e910b 100644 --- a/tests/tests.toml +++ b/tests/tests.toml @@ -1104,6 +1104,10 @@ asr = true filename = "errors/test_int_semantic_error.py" asr = true +[[test]] +filename = "errors/test_float_semantic_error.py" +asr = true + [[test]] filename = "errors/generics_error_01.py" asr = true