Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e920140

Browse files
authored
Merge pull request #1852 from Shaikh-Ubaid/error_on_simple_type_with_output_intent
Error on simple type with output intent
2 parents 0c8fbe0 + f520779 commit e920140

14 files changed

+112
-0
lines changed

src/lpython/semantics/python_ast_to_asr.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3868,6 +3868,12 @@ class SymbolTableVisitor : public CommonVisitor<SymbolTableVisitor> {
38683868
AST::expr_t* arg_annotation_type = get_var_intent_and_annotation(x.m_args.m_args[i].m_annotation, s_intent);
38693869
is_allocatable = false;
38703870
ASR::ttype_t *arg_type = ast_expr_to_asr_type(x.base.base.loc, *arg_annotation_type, is_allocatable);
3871+
if ((s_intent == ASRUtils::intent_inout || s_intent == ASRUtils::intent_out)
3872+
&& !ASRUtils::is_aggregate_type(arg_type)) {
3873+
throw SemanticError("Simple Type " + ASRUtils::type_to_str_python(arg_type)
3874+
+ " cannot be intent InOut/Out", loc);
3875+
}
3876+
38713877
// Set the function as generic if an argument is typed with a type parameter
38723878
if (ASRUtils::is_generic(*arg_type)) {
38733879
ASR::ttype_t* arg_type_type = ASRUtils::get_type_parameter(arg_type);

tests/errors/func_02.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from lpython import i32, In
2+
3+
def f(n: In[i32]):
4+
n = 5
5+
print(n)

tests/errors/func_03.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
from lpython import i32
2+
3+
def f(l: list[i32]):
4+
l[5] = 5

tests/errors/func_04.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
from lpython import i32
2+
3+
def f(l: list[i32]):
4+
l[5] = 5

tests/errors/func_05.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from lpython import i32, InOut
2+
3+
def f(n: InOut[i32]):
4+
n = 5
5+
print(n)
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"basename": "asr-func_02-b439474",
3+
"cmd": "lpython --show-asr --no-color {infile} -o {outfile}",
4+
"infile": "tests/errors/func_02.py",
5+
"infile_hash": "452658f9bae7ccab4d21b9b1c993adb7fbaf117c093dd82672755aa5",
6+
"outfile": null,
7+
"outfile_hash": null,
8+
"stdout": null,
9+
"stdout_hash": null,
10+
"stderr": "asr-func_02-b439474.stderr",
11+
"stderr_hash": "51b7e0552f91115242b7c38f82c258b3d8dda88c9596ddee5388e0a1",
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: Assignment to an input function parameter `n` is not allowed
2+
--> tests/errors/func_02.py:4:5
3+
|
4+
4 | n = 5
5+
| ^
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"basename": "asr-func_03-cd744a0",
3+
"cmd": "lpython --show-asr --no-color {infile} -o {outfile}",
4+
"infile": "tests/errors/func_03.py",
5+
"infile_hash": "763216ad3cb1090dc322e63706d4f92be4e806b1fc2df6f160d02fd0",
6+
"outfile": null,
7+
"outfile_hash": null,
8+
"stdout": null,
9+
"stdout_hash": null,
10+
"stderr": "asr-func_03-cd744a0.stderr",
11+
"stderr_hash": "cdd453e23646de3ff697ffe6a99908520edb195d28871de63db5a430",
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: Assignment to an input function parameter `l` is not allowed
2+
--> tests/errors/func_03.py:4:5
3+
|
4+
4 | l[5] = 5
5+
| ^
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"basename": "asr-func_04-eef2656",
3+
"cmd": "lpython --show-asr --no-color {infile} -o {outfile}",
4+
"infile": "tests/errors/func_04.py",
5+
"infile_hash": "763216ad3cb1090dc322e63706d4f92be4e806b1fc2df6f160d02fd0",
6+
"outfile": null,
7+
"outfile_hash": null,
8+
"stdout": null,
9+
"stdout_hash": null,
10+
"stderr": "asr-func_04-eef2656.stderr",
11+
"stderr_hash": "c0ef482d68b30b03615927ecd02a30b14ff4d24b5673b7399671ee79",
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: Assignment to an input function parameter `l` is not allowed
2+
--> tests/errors/func_04.py:4:5
3+
|
4+
4 | l[5] = 5
5+
| ^
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"basename": "asr-func_05-c22b921",
3+
"cmd": "lpython --show-asr --no-color {infile} -o {outfile}",
4+
"infile": "tests/errors/func_05.py",
5+
"infile_hash": "70a0b0ddad67252c7488d322ffc565a6669e1f79b96c3ef3014b0d0a",
6+
"outfile": null,
7+
"outfile_hash": null,
8+
"stdout": null,
9+
"stdout_hash": null,
10+
"stderr": "asr-func_05-c22b921.stderr",
11+
"stderr_hash": "9611beb75ae308f86e573592164c5aca50c4e1acec2e91e48687ba96",
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: Simple Type i32 cannot be intent InOut/Out
2+
--> tests/errors/func_05.py:3:7
3+
|
4+
3 | def f(n: InOut[i32]):
5+
| ^^^^^^^^^^^^^

tests/tests.toml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1082,6 +1082,22 @@ asr = true
10821082
filename = "errors/func_01.py"
10831083
asr = true
10841084

1085+
[[test]]
1086+
filename = "errors/func_02.py"
1087+
asr = true
1088+
1089+
[[test]]
1090+
filename = "errors/func_03.py"
1091+
asr = true
1092+
1093+
[[test]]
1094+
filename = "errors/func_04.py"
1095+
asr = true
1096+
1097+
[[test]]
1098+
filename = "errors/func_05.py"
1099+
asr = true
1100+
10851101
# tests/runtime_errors
10861102
[[test]]
10871103
filename = "runtime_errors/test_list_01.py"

0 commit comments

Comments
 (0)