diff --git a/src/lpython/semantics/python_ast_to_asr.cpp b/src/lpython/semantics/python_ast_to_asr.cpp index 4c736fd9c3..e8223b2f73 100644 --- a/src/lpython/semantics/python_ast_to_asr.cpp +++ b/src/lpython/semantics/python_ast_to_asr.cpp @@ -3868,6 +3868,12 @@ class SymbolTableVisitor : public CommonVisitor { AST::expr_t* arg_annotation_type = get_var_intent_and_annotation(x.m_args.m_args[i].m_annotation, s_intent); is_allocatable = false; ASR::ttype_t *arg_type = ast_expr_to_asr_type(x.base.base.loc, *arg_annotation_type, is_allocatable); + if ((s_intent == ASRUtils::intent_inout || s_intent == ASRUtils::intent_out) + && !ASRUtils::is_aggregate_type(arg_type)) { + throw SemanticError("Simple Type " + ASRUtils::type_to_str_python(arg_type) + + " cannot be intent InOut/Out", loc); + } + // Set the function as generic if an argument is typed with a type parameter if (ASRUtils::is_generic(*arg_type)) { ASR::ttype_t* arg_type_type = ASRUtils::get_type_parameter(arg_type); diff --git a/tests/errors/func_02.py b/tests/errors/func_02.py new file mode 100644 index 0000000000..75d71ec13b --- /dev/null +++ b/tests/errors/func_02.py @@ -0,0 +1,5 @@ +from lpython import i32, In + +def f(n: In[i32]): + n = 5 + print(n) diff --git a/tests/errors/func_03.py b/tests/errors/func_03.py new file mode 100644 index 0000000000..2ae09056b3 --- /dev/null +++ b/tests/errors/func_03.py @@ -0,0 +1,4 @@ +from lpython import i32 + +def f(l: list[i32]): + l[5] = 5 diff --git a/tests/errors/func_04.py b/tests/errors/func_04.py new file mode 100644 index 0000000000..2ae09056b3 --- /dev/null +++ b/tests/errors/func_04.py @@ -0,0 +1,4 @@ +from lpython import i32 + +def f(l: list[i32]): + l[5] = 5 diff --git a/tests/errors/func_05.py b/tests/errors/func_05.py new file mode 100644 index 0000000000..e5916cab60 --- /dev/null +++ b/tests/errors/func_05.py @@ -0,0 +1,5 @@ +from lpython import i32, InOut + +def f(n: InOut[i32]): + n = 5 + print(n) diff --git a/tests/reference/asr-func_02-b439474.json b/tests/reference/asr-func_02-b439474.json new file mode 100644 index 0000000000..9eb8dbc950 --- /dev/null +++ b/tests/reference/asr-func_02-b439474.json @@ -0,0 +1,13 @@ +{ + "basename": "asr-func_02-b439474", + "cmd": "lpython --show-asr --no-color {infile} -o {outfile}", + "infile": "tests/errors/func_02.py", + "infile_hash": "452658f9bae7ccab4d21b9b1c993adb7fbaf117c093dd82672755aa5", + "outfile": null, + "outfile_hash": null, + "stdout": null, + "stdout_hash": null, + "stderr": "asr-func_02-b439474.stderr", + "stderr_hash": "51b7e0552f91115242b7c38f82c258b3d8dda88c9596ddee5388e0a1", + "returncode": 2 +} \ No newline at end of file diff --git a/tests/reference/asr-func_02-b439474.stderr b/tests/reference/asr-func_02-b439474.stderr new file mode 100644 index 0000000000..d76b00137b --- /dev/null +++ b/tests/reference/asr-func_02-b439474.stderr @@ -0,0 +1,5 @@ +semantic error: Assignment to an input function parameter `n` is not allowed + --> tests/errors/func_02.py:4:5 + | +4 | n = 5 + | ^ diff --git a/tests/reference/asr-func_03-cd744a0.json b/tests/reference/asr-func_03-cd744a0.json new file mode 100644 index 0000000000..af9a233f99 --- /dev/null +++ b/tests/reference/asr-func_03-cd744a0.json @@ -0,0 +1,13 @@ +{ + "basename": "asr-func_03-cd744a0", + "cmd": "lpython --show-asr --no-color {infile} -o {outfile}", + "infile": "tests/errors/func_03.py", + "infile_hash": "763216ad3cb1090dc322e63706d4f92be4e806b1fc2df6f160d02fd0", + "outfile": null, + "outfile_hash": null, + "stdout": null, + "stdout_hash": null, + "stderr": "asr-func_03-cd744a0.stderr", + "stderr_hash": "cdd453e23646de3ff697ffe6a99908520edb195d28871de63db5a430", + "returncode": 2 +} \ No newline at end of file diff --git a/tests/reference/asr-func_03-cd744a0.stderr b/tests/reference/asr-func_03-cd744a0.stderr new file mode 100644 index 0000000000..b1947aa4b2 --- /dev/null +++ b/tests/reference/asr-func_03-cd744a0.stderr @@ -0,0 +1,5 @@ +semantic error: Assignment to an input function parameter `l` is not allowed + --> tests/errors/func_03.py:4:5 + | +4 | l[5] = 5 + | ^ diff --git a/tests/reference/asr-func_04-eef2656.json b/tests/reference/asr-func_04-eef2656.json new file mode 100644 index 0000000000..99983184d6 --- /dev/null +++ b/tests/reference/asr-func_04-eef2656.json @@ -0,0 +1,13 @@ +{ + "basename": "asr-func_04-eef2656", + "cmd": "lpython --show-asr --no-color {infile} -o {outfile}", + "infile": "tests/errors/func_04.py", + "infile_hash": "763216ad3cb1090dc322e63706d4f92be4e806b1fc2df6f160d02fd0", + "outfile": null, + "outfile_hash": null, + "stdout": null, + "stdout_hash": null, + "stderr": "asr-func_04-eef2656.stderr", + "stderr_hash": "c0ef482d68b30b03615927ecd02a30b14ff4d24b5673b7399671ee79", + "returncode": 2 +} \ No newline at end of file diff --git a/tests/reference/asr-func_04-eef2656.stderr b/tests/reference/asr-func_04-eef2656.stderr new file mode 100644 index 0000000000..d6507013f5 --- /dev/null +++ b/tests/reference/asr-func_04-eef2656.stderr @@ -0,0 +1,5 @@ +semantic error: Assignment to an input function parameter `l` is not allowed + --> tests/errors/func_04.py:4:5 + | +4 | l[5] = 5 + | ^ diff --git a/tests/reference/asr-func_05-c22b921.json b/tests/reference/asr-func_05-c22b921.json new file mode 100644 index 0000000000..46cec1c7b4 --- /dev/null +++ b/tests/reference/asr-func_05-c22b921.json @@ -0,0 +1,13 @@ +{ + "basename": "asr-func_05-c22b921", + "cmd": "lpython --show-asr --no-color {infile} -o {outfile}", + "infile": "tests/errors/func_05.py", + "infile_hash": "70a0b0ddad67252c7488d322ffc565a6669e1f79b96c3ef3014b0d0a", + "outfile": null, + "outfile_hash": null, + "stdout": null, + "stdout_hash": null, + "stderr": "asr-func_05-c22b921.stderr", + "stderr_hash": "9611beb75ae308f86e573592164c5aca50c4e1acec2e91e48687ba96", + "returncode": 2 +} \ No newline at end of file diff --git a/tests/reference/asr-func_05-c22b921.stderr b/tests/reference/asr-func_05-c22b921.stderr new file mode 100644 index 0000000000..fc959f032f --- /dev/null +++ b/tests/reference/asr-func_05-c22b921.stderr @@ -0,0 +1,5 @@ +semantic error: Simple Type i32 cannot be intent InOut/Out + --> tests/errors/func_05.py:3:7 + | +3 | def f(n: InOut[i32]): + | ^^^^^^^^^^^^^ diff --git a/tests/tests.toml b/tests/tests.toml index be62d907b1..9378df9697 100644 --- a/tests/tests.toml +++ b/tests/tests.toml @@ -1082,6 +1082,22 @@ asr = true filename = "errors/func_01.py" asr = true +[[test]] +filename = "errors/func_02.py" +asr = true + +[[test]] +filename = "errors/func_03.py" +asr = true + +[[test]] +filename = "errors/func_04.py" +asr = true + +[[test]] +filename = "errors/func_05.py" +asr = true + # tests/runtime_errors [[test]] filename = "runtime_errors/test_list_01.py"