From 6b71af7d0efc8b9a118ea721b740faaea95bb53f Mon Sep 17 00:00:00 2001 From: faze-geek Date: Mon, 12 Feb 2024 11:37:43 +0530 Subject: [PATCH 1/3] Add semantic error for float declaration --- 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..9ac77f3ab0 --- /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 + 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..5e2b8ef9dd --- /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": "17c10a2ded670f826ae6e14179387d85d6276b419ee416537813f4cf", + "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 From b782e7522576ae1014d087659007d5d01daaf545 Mon Sep 17 00:00:00 2001 From: faze-geek Date: Mon, 12 Feb 2024 11:40:47 +0530 Subject: [PATCH 2/3] Use a floating number for radius --- tests/errors/test_float_semantic_error.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/errors/test_float_semantic_error.py b/tests/errors/test_float_semantic_error.py index 9ac77f3ab0..d0e88ccbc5 100644 --- a/tests/errors/test_float_semantic_error.py +++ b/tests/errors/test_float_semantic_error.py @@ -2,7 +2,7 @@ def main0(): pi: float = 3.14 - radi: f64 = 2 + radi: f64 = 2.0 print("Area of Circle is", pi*radi**2) main0() \ No newline at end of file From b90cd1244d14aba5a3481ff95921a1e873e5b58a Mon Sep 17 00:00:00 2001 From: faze-geek Date: Mon, 12 Feb 2024 11:54:02 +0530 Subject: [PATCH 3/3] Update references for latest main --- tests/reference/asr-test_float_semantic_error-58c0c90.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/reference/asr-test_float_semantic_error-58c0c90.json b/tests/reference/asr-test_float_semantic_error-58c0c90.json index 5e2b8ef9dd..e451309c05 100644 --- a/tests/reference/asr-test_float_semantic_error-58c0c90.json +++ b/tests/reference/asr-test_float_semantic_error-58c0c90.json @@ -2,7 +2,7 @@ "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": "17c10a2ded670f826ae6e14179387d85d6276b419ee416537813f4cf", + "infile_hash": "10929e3991a4aee1a2de473fc5f8caa48b6dcf4a35e2329a15ea5f1f", "outfile": null, "outfile_hash": null, "stdout": null,