From 5c0b91e3c54f6528cb151ecf1b2776e6a04b6e7f Mon Sep 17 00:00:00 2001 From: Prince Roshan Date: Fri, 6 Oct 2023 15:31:37 +0530 Subject: [PATCH] Throw Exception for wrong arg type in join method (#2364) * Throw Exception for wrong argtype in join method * Remove SemanticError from test_str_01.py * Add reference for test handling join method * Remove .stdout file * Run reference test * Remove unnecessary files * Include update references * Update tests/errors/string_01.py Co-authored-by: Shaikh Ubaid * Update test reference * Remove .stdout file * Add test for corner cases * Remove unused import --------- Co-authored-by: Shaikh Ubaid --- integration_tests/test_str_01.py | 16 ++++++++++++++++ src/lpython/semantics/python_ast_to_asr.cpp | 15 ++++++++++----- tests/errors/string_02.py | 9 +++++++++ tests/reference/asr-string_02-499c9ff.json | 13 +++++++++++++ tests/reference/asr-string_02-499c9ff.stderr | 5 +++++ tests/tests.toml | 4 ++++ 6 files changed, 57 insertions(+), 5 deletions(-) create mode 100644 tests/errors/string_02.py create mode 100644 tests/reference/asr-string_02-499c9ff.json create mode 100644 tests/reference/asr-string_02-499c9ff.stderr diff --git a/integration_tests/test_str_01.py b/integration_tests/test_str_01.py index 2366c520c6..0df6f08bb1 100644 --- a/integration_tests/test_str_01.py +++ b/integration_tests/test_str_01.py @@ -59,6 +59,19 @@ def test_str_join2(): res:str = a.join(p) assert res == "a**b" +def test_str_join_empty_str(): + a: str + a = "" + p:list[str] = ["a","b"] + res:str = a.join(p) + assert res == "ab" + +def test_str_join_empty_list(): + a: str + a = "ab" + p:list[str] = [] + res:str = a.join(p) + assert res == "" def test_constant_str_subscript(): assert "abc"[2] == "c" @@ -71,6 +84,9 @@ def check(): test_str_slice() test_str_repeat() test_str_join() + test_str_join2() + test_str_join_empty_str() + test_str_join_empty_list() test_constant_str_subscript() check() diff --git a/src/lpython/semantics/python_ast_to_asr.cpp b/src/lpython/semantics/python_ast_to_asr.cpp index 50bb12d77f..32432069ce 100644 --- a/src/lpython/semantics/python_ast_to_asr.cpp +++ b/src/lpython/semantics/python_ast_to_asr.cpp @@ -34,7 +34,6 @@ #include #include - namespace LCompilers::LPython { namespace CastingUtil { @@ -6669,15 +6668,21 @@ class BodyVisitor : public CommonVisitor { throw SemanticError("str.join() takes one argument", loc); } + ASR::expr_t *arg_sub = args[0].m_value; + ASR::ttype_t *arg_sub_type = ASRUtils::expr_type(arg_sub); + if(!ASR::is_a(*arg_sub_type)){ + throw SemanticError("str.join() takes type list only", + loc); + } fn_call_name = "_lpython_str_join"; ASR::call_arg_t str_var; str_var.loc = loc; str_var.m_value = s_var; - ASR::call_arg_t passed_int; - passed_int.loc = loc; - passed_int.m_value = args[0].m_value; + ASR::call_arg_t list_of_str; + list_of_str.loc = loc; + list_of_str.m_value = args[0].m_value; fn_args.push_back(al, str_var); - fn_args.push_back(al, passed_int); + fn_args.push_back(al, list_of_str); } else if (attr_name == "find") { if (args.size() != 1) { throw SemanticError("str.find() takes one argument", diff --git a/tests/errors/string_02.py b/tests/errors/string_02.py new file mode 100644 index 0000000000..18c06077aa --- /dev/null +++ b/tests/errors/string_02.py @@ -0,0 +1,9 @@ +from lpython import i32 + +def test_wrong_argument_in_join(): + x: str = "ab" + p: i32 = 1 + res:str = x.join(p) + print(res) + +test_wrong_argument_in_join() \ No newline at end of file diff --git a/tests/reference/asr-string_02-499c9ff.json b/tests/reference/asr-string_02-499c9ff.json new file mode 100644 index 0000000000..ac33dbbf21 --- /dev/null +++ b/tests/reference/asr-string_02-499c9ff.json @@ -0,0 +1,13 @@ +{ + "basename": "asr-string_02-499c9ff", + "cmd": "lpython --show-asr --no-color {infile} -o {outfile}", + "infile": "tests/errors/string_02.py", + "infile_hash": "ed6511565e893791a4bd8ea0b4750817bab13cd6dc0731332127bf58", + "outfile": null, + "outfile_hash": null, + "stdout": null, + "stdout_hash": null, + "stderr": "asr-string_02-499c9ff.stderr", + "stderr_hash": "368ba74a1e0d6609f71e6f87f95bd0b6151420c81336e48a172cb613", + "returncode": 2 +} \ No newline at end of file diff --git a/tests/reference/asr-string_02-499c9ff.stderr b/tests/reference/asr-string_02-499c9ff.stderr new file mode 100644 index 0000000000..196515476b --- /dev/null +++ b/tests/reference/asr-string_02-499c9ff.stderr @@ -0,0 +1,5 @@ +semantic error: str.join() takes type list only + --> tests/errors/string_02.py:6:15 + | +6 | res:str = x.join(p) + | ^^^^^^^^^ diff --git a/tests/tests.toml b/tests/tests.toml index f94150597d..9ed98dd2f7 100644 --- a/tests/tests.toml +++ b/tests/tests.toml @@ -650,6 +650,10 @@ asr = true filename = "errors/string_01.py" asr = true +[[test]] +filename = "errors/string_02.py" +asr = true + [[test]] filename = "errors/structs_01.py" asr = true