Skip to content

Commit

Permalink
Added semantic error for float declaration. (lcompilers#2529)
Browse files Browse the repository at this point in the history
* Add semantic error for float declaration & added tests for the same.
  • Loading branch information
faze-geek committed Feb 13, 2024
1 parent 4edbf38 commit 4f37bb2
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/lpython/semantics/python_ast_to_asr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -885,7 +885,16 @@ class CommonVisitor : public AST::BaseVisitor<Struct> {
})
);
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);
}
}
Expand Down
8 changes: 8 additions & 0 deletions tests/errors/test_float_semantic_error.py
Original file line number Diff line number Diff line change
@@ -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()
13 changes: 13 additions & 0 deletions tests/reference/asr-test_float_semantic_error-58c0c90.json
Original file line number Diff line number Diff line change
@@ -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
}
5 changes: 5 additions & 0 deletions tests/reference/asr-test_float_semantic_error-58c0c90.stderr
Original file line number Diff line number Diff line change
@@ -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.
4 changes: 4 additions & 0 deletions tests/tests.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 4f37bb2

Please sign in to comment.