Skip to content

Commit a9eff5c

Browse files
faze-geekAgent-Hellboy
authored andcommitted
Added semantic error for float declaration. (lcompilers#2529)
* Add semantic error for float declaration & added tests for the same.
1 parent 6a11d34 commit a9eff5c

5 files changed

Lines changed: 40 additions & 1 deletion

File tree

src/lpython/semantics/python_ast_to_asr.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -885,7 +885,16 @@ class CommonVisitor : public AST::BaseVisitor<Struct> {
885885
})
886886
);
887887
throw SemanticAbort();
888-
} else {
888+
} else if (var_annotation == "float") {
889+
std::string msg = "Hint: Use f32 or f64 for now. ";
890+
diag.add(diag::Diagnostic(
891+
var_annotation + " type is not supported yet. ",
892+
diag::Level::Error, diag::Stage::Semantic, {
893+
diag::Label(msg, {loc})
894+
})
895+
);
896+
throw SemanticAbort();
897+
} else {
889898
throw SemanticError("The type '" + var_annotation+"' is undeclared.", loc);
890899
}
891900
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from lpython import f64
2+
3+
def main0():
4+
pi: float = 3.14
5+
radi: f64 = 2.0
6+
print("Area of Circle is", pi*radi**2)
7+
8+
main0()
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"basename": "asr-test_float_semantic_error-58c0c90",
3+
"cmd": "lpython --show-asr --no-color {infile} -o {outfile}",
4+
"infile": "tests/errors/test_float_semantic_error.py",
5+
"infile_hash": "10929e3991a4aee1a2de473fc5f8caa48b6dcf4a35e2329a15ea5f1f",
6+
"outfile": null,
7+
"outfile_hash": null,
8+
"stdout": null,
9+
"stdout_hash": null,
10+
"stderr": "asr-test_float_semantic_error-58c0c90.stderr",
11+
"stderr_hash": "c11166fb86be513e74796a51f5c9e84653c2eb894ed95d502ed8af57",
12+
"returncode": 2
13+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
semantic error: float type is not supported yet.
2+
--> tests/errors/test_float_semantic_error.py:4:9
3+
|
4+
4 | pi: float = 3.14
5+
| ^^^^^ Hint: Use f32 or f64 for now.

tests/tests.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1104,6 +1104,10 @@ asr = true
11041104
filename = "errors/test_int_semantic_error.py"
11051105
asr = true
11061106

1107+
[[test]]
1108+
filename = "errors/test_float_semantic_error.py"
1109+
asr = true
1110+
11071111
[[test]]
11081112
filename = "errors/generics_error_01.py"
11091113
asr = true

0 commit comments

Comments
 (0)