From a1f20e1f3a003c17cb2b9701326c316c497f980a Mon Sep 17 00:00:00 2001 From: Prince Roshan Date: Fri, 6 Oct 2023 07:10:54 +0530 Subject: [PATCH 01/12] Throw Exception for wrong argtype in join method --- integration_tests/test_str_01.py | 19 +++++++++++++++++++ src/lpython/semantics/python_ast_to_asr.cpp | 7 ++++++- 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/integration_tests/test_str_01.py b/integration_tests/test_str_01.py index 2366c520c6..c748375748 100644 --- a/integration_tests/test_str_01.py +++ b/integration_tests/test_str_01.py @@ -1,3 +1,5 @@ +from lpython import i32, SemanticError + def f(): x: str x = "ok" @@ -59,6 +61,21 @@ def test_str_join2(): res:str = a.join(p) assert res == "a**b" +def test_str_join3(): + a: str + a = "**-" + p:list[str] = ["a","b"] + res:str = a.join(p) + assert res == "a**-b" + +# def test_str_join4(): +# a: str +# p:i32 = 8 +# try: +# res:str = a.join(p) +# except Exception as e: +# assert isinstance(e, SemanticError), "Expected a SemanticError exception" + def test_constant_str_subscript(): assert "abc"[2] == "c" @@ -71,6 +88,8 @@ def check(): test_str_slice() test_str_repeat() test_str_join() + test_str_join2() + test_str_join3() 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..49df909c8e 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,6 +6668,12 @@ 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; From 8cdf7cd267329d5df10a2d1cb84660f5e51a431d Mon Sep 17 00:00:00 2001 From: Prince Roshan Date: Fri, 6 Oct 2023 07:18:02 +0530 Subject: [PATCH 02/12] Remove SemanticError from test_str_01.py --- integration_tests/test_str_01.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/integration_tests/test_str_01.py b/integration_tests/test_str_01.py index c748375748..c0834e8403 100644 --- a/integration_tests/test_str_01.py +++ b/integration_tests/test_str_01.py @@ -1,4 +1,4 @@ -from lpython import i32, SemanticError +from lpython import i32 def f(): x: str From aba112800b9206e92e20c2502ae60d2061f1e815 Mon Sep 17 00:00:00 2001 From: Prince Roshan Date: Fri, 6 Oct 2023 13:07:57 +0530 Subject: [PATCH 03/12] Add reference for test handling join method --- integration_tests/test_str_01.py | 9 --------- src/lpython/semantics/python_ast_to_asr.cpp | 8 ++++---- tests/errors/string_01.py | 1 + tests/errors/string_02.py | 9 +++++++++ tests/reference/asr-string_01-78629c4.json | 2 +- tests/reference/asr-string_02-499c9ff.json | 13 +++++++++++++ tests/reference/asr-string_02-499c9ff.stderr | 5 +++++ tests/reference/asr-string_02-499c9ff.stdout | 1 + tests/tests.toml | 4 ++++ 9 files changed, 38 insertions(+), 14 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 create mode 100644 tests/reference/asr-string_02-499c9ff.stdout diff --git a/integration_tests/test_str_01.py b/integration_tests/test_str_01.py index c0834e8403..fc8b6816ea 100644 --- a/integration_tests/test_str_01.py +++ b/integration_tests/test_str_01.py @@ -68,15 +68,6 @@ def test_str_join3(): res:str = a.join(p) assert res == "a**-b" -# def test_str_join4(): -# a: str -# p:i32 = 8 -# try: -# res:str = a.join(p) -# except Exception as e: -# assert isinstance(e, SemanticError), "Expected a SemanticError exception" - - def test_constant_str_subscript(): assert "abc"[2] == "c" assert "abc"[:2] == "ab" diff --git a/src/lpython/semantics/python_ast_to_asr.cpp b/src/lpython/semantics/python_ast_to_asr.cpp index 49df909c8e..32432069ce 100644 --- a/src/lpython/semantics/python_ast_to_asr.cpp +++ b/src/lpython/semantics/python_ast_to_asr.cpp @@ -6678,11 +6678,11 @@ class BodyVisitor : public CommonVisitor { 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_01.py b/tests/errors/string_01.py index fcfc9eb1bf..e5d2ee960c 100644 --- a/tests/errors/string_01.py +++ b/tests/errors/string_01.py @@ -2,4 +2,5 @@ def test_tuple_indicing_in_str(): x: str = "abcdefgh" print(x[2, 5]) + test_tuple_indicing_in_str() diff --git a/tests/errors/string_02.py b/tests/errors/string_02.py new file mode 100644 index 0000000000..670cc979cd --- /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:list[i32] = [1,2] + 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_01-78629c4.json b/tests/reference/asr-string_01-78629c4.json index 8cf78f4a79..1529bbbba8 100644 --- a/tests/reference/asr-string_01-78629c4.json +++ b/tests/reference/asr-string_01-78629c4.json @@ -2,7 +2,7 @@ "basename": "asr-string_01-78629c4", "cmd": "lpython --show-asr --no-color {infile} -o {outfile}", "infile": "tests/errors/string_01.py", - "infile_hash": "92496679f4475e3351686f6556f0e68411437beed22a447f9a65bfdf", + "infile_hash": "6463d5b0d140abf583e4b2fd9218d720be61615c7d0e2ccd85f4c032", "outfile": null, "outfile_hash": null, "stdout": null, diff --git a/tests/reference/asr-string_02-499c9ff.json b/tests/reference/asr-string_02-499c9ff.json new file mode 100644 index 0000000000..06e061a9df --- /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": "8e3af4f58ab3182695994f853e5f037f05c9d6dc9a0b8b0ad510351d", + "outfile": null, + "outfile_hash": null, + "stdout": "asr-string_02-499c9ff.stdout", + "stdout_hash": "dd9544bf3a59b5af013ee61749651abcdabc6053cd3971f0bc5409d7", + "stderr": "asr-string_02-499c9ff.stderr", + "stderr_hash": "ba0ba2e577ca9a8d3b1bb89eea25f52c96f4a525d2cad46fa6e27f58", + "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..e93cb9ee67 --- /dev/null +++ b/tests/reference/asr-string_02-499c9ff.stderr @@ -0,0 +1,5 @@ +semantic error: Arguments do not match for any generic procedure, _lpython_str_join + --> tests/errors/string_02.py:6:15 + | +6 | res:str = x.join(p) + | ^^^^^^^^^ diff --git a/tests/reference/asr-string_02-499c9ff.stdout b/tests/reference/asr-string_02-499c9ff.stdout new file mode 100644 index 0000000000..09760b0cd2 --- /dev/null +++ b/tests/reference/asr-string_02-499c9ff.stdout @@ -0,0 +1 @@ +(Var 3 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 From 74e08e56494f1519632df5ff43e7433fc3f6d9a4 Mon Sep 17 00:00:00 2001 From: Prince Roshan Date: Fri, 6 Oct 2023 13:15:55 +0530 Subject: [PATCH 04/12] Remove .stdout file --- tests/reference/asr-string_02-499c9ff.stdout | 1 - 1 file changed, 1 deletion(-) delete mode 100644 tests/reference/asr-string_02-499c9ff.stdout diff --git a/tests/reference/asr-string_02-499c9ff.stdout b/tests/reference/asr-string_02-499c9ff.stdout deleted file mode 100644 index 09760b0cd2..0000000000 --- a/tests/reference/asr-string_02-499c9ff.stdout +++ /dev/null @@ -1 +0,0 @@ -(Var 3 p) From 4a4e40cd7763e10eaf2ed4f3e8f2dcbd46fcb1a5 Mon Sep 17 00:00:00 2001 From: Prince Roshan Date: Fri, 6 Oct 2023 13:56:18 +0530 Subject: [PATCH 05/12] Run reference test --- integration_tests/test_str_01.py | 1 + tests/errors/string_02.py | 2 +- tests/reference/asr-string_02-499c9ff.json | 4 ++-- tests/reference/asr-string_02-499c9ff.stderr | 2 +- 4 files changed, 5 insertions(+), 4 deletions(-) diff --git a/integration_tests/test_str_01.py b/integration_tests/test_str_01.py index fc8b6816ea..5af2b36350 100644 --- a/integration_tests/test_str_01.py +++ b/integration_tests/test_str_01.py @@ -68,6 +68,7 @@ def test_str_join3(): res:str = a.join(p) assert res == "a**-b" + def test_constant_str_subscript(): assert "abc"[2] == "c" assert "abc"[:2] == "ab" diff --git a/tests/errors/string_02.py b/tests/errors/string_02.py index 670cc979cd..18c06077aa 100644 --- a/tests/errors/string_02.py +++ b/tests/errors/string_02.py @@ -2,7 +2,7 @@ def test_wrong_argument_in_join(): x: str = "ab" - p:list[i32] = [1,2] + p: i32 = 1 res:str = x.join(p) print(res) diff --git a/tests/reference/asr-string_02-499c9ff.json b/tests/reference/asr-string_02-499c9ff.json index 06e061a9df..129a7e53d4 100644 --- a/tests/reference/asr-string_02-499c9ff.json +++ b/tests/reference/asr-string_02-499c9ff.json @@ -2,12 +2,12 @@ "basename": "asr-string_02-499c9ff", "cmd": "lpython --show-asr --no-color {infile} -o {outfile}", "infile": "tests/errors/string_02.py", - "infile_hash": "8e3af4f58ab3182695994f853e5f037f05c9d6dc9a0b8b0ad510351d", + "infile_hash": "ed6511565e893791a4bd8ea0b4750817bab13cd6dc0731332127bf58", "outfile": null, "outfile_hash": null, "stdout": "asr-string_02-499c9ff.stdout", "stdout_hash": "dd9544bf3a59b5af013ee61749651abcdabc6053cd3971f0bc5409d7", "stderr": "asr-string_02-499c9ff.stderr", - "stderr_hash": "ba0ba2e577ca9a8d3b1bb89eea25f52c96f4a525d2cad46fa6e27f58", + "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 index e93cb9ee67..196515476b 100644 --- a/tests/reference/asr-string_02-499c9ff.stderr +++ b/tests/reference/asr-string_02-499c9ff.stderr @@ -1,4 +1,4 @@ -semantic error: Arguments do not match for any generic procedure, _lpython_str_join +semantic error: str.join() takes type list only --> tests/errors/string_02.py:6:15 | 6 | res:str = x.join(p) From 5a22c5989c66ced02c1308b9d708a7fa1cd4169f Mon Sep 17 00:00:00 2001 From: Prince Roshan Date: Fri, 6 Oct 2023 14:06:12 +0530 Subject: [PATCH 06/12] Remove unnecessary files --- tests/reference/asr-string_01-78629c4.json | 13 ------------- tests/reference/asr-string_02-499c9ff.json | 13 ------------- tests/reference/asr-string_02-499c9ff.stderr | 5 ----- 3 files changed, 31 deletions(-) delete mode 100644 tests/reference/asr-string_01-78629c4.json delete mode 100644 tests/reference/asr-string_02-499c9ff.json delete mode 100644 tests/reference/asr-string_02-499c9ff.stderr diff --git a/tests/reference/asr-string_01-78629c4.json b/tests/reference/asr-string_01-78629c4.json deleted file mode 100644 index 1529bbbba8..0000000000 --- a/tests/reference/asr-string_01-78629c4.json +++ /dev/null @@ -1,13 +0,0 @@ -{ - "basename": "asr-string_01-78629c4", - "cmd": "lpython --show-asr --no-color {infile} -o {outfile}", - "infile": "tests/errors/string_01.py", - "infile_hash": "6463d5b0d140abf583e4b2fd9218d720be61615c7d0e2ccd85f4c032", - "outfile": null, - "outfile_hash": null, - "stdout": null, - "stdout_hash": null, - "stderr": "asr-string_01-78629c4.stderr", - "stderr_hash": "29aa01d7370a20ada91bdb13c569434f36a0c18309700d946d89fbf0", - "returncode": 2 -} \ No newline at end of file diff --git a/tests/reference/asr-string_02-499c9ff.json b/tests/reference/asr-string_02-499c9ff.json deleted file mode 100644 index 129a7e53d4..0000000000 --- a/tests/reference/asr-string_02-499c9ff.json +++ /dev/null @@ -1,13 +0,0 @@ -{ - "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": "asr-string_02-499c9ff.stdout", - "stdout_hash": "dd9544bf3a59b5af013ee61749651abcdabc6053cd3971f0bc5409d7", - "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 deleted file mode 100644 index 196515476b..0000000000 --- a/tests/reference/asr-string_02-499c9ff.stderr +++ /dev/null @@ -1,5 +0,0 @@ -semantic error: str.join() takes type list only - --> tests/errors/string_02.py:6:15 - | -6 | res:str = x.join(p) - | ^^^^^^^^^ From 931f19d4a3c7c5feff73ae601d43da49d81d3e8f Mon Sep 17 00:00:00 2001 From: Prince Roshan Date: Fri, 6 Oct 2023 14:11:22 +0530 Subject: [PATCH 07/12] Include update references --- tests/reference/asr-string_01-78629c4.json | 13 +++++++++++++ tests/reference/asr-string_02-499c9ff.json | 13 +++++++++++++ tests/reference/asr-string_02-499c9ff.stderr | 5 +++++ tests/reference/asr-string_02-499c9ff.stdout | 1 + 4 files changed, 32 insertions(+) create mode 100644 tests/reference/asr-string_01-78629c4.json create mode 100644 tests/reference/asr-string_02-499c9ff.json create mode 100644 tests/reference/asr-string_02-499c9ff.stderr create mode 100644 tests/reference/asr-string_02-499c9ff.stdout diff --git a/tests/reference/asr-string_01-78629c4.json b/tests/reference/asr-string_01-78629c4.json new file mode 100644 index 0000000000..1529bbbba8 --- /dev/null +++ b/tests/reference/asr-string_01-78629c4.json @@ -0,0 +1,13 @@ +{ + "basename": "asr-string_01-78629c4", + "cmd": "lpython --show-asr --no-color {infile} -o {outfile}", + "infile": "tests/errors/string_01.py", + "infile_hash": "6463d5b0d140abf583e4b2fd9218d720be61615c7d0e2ccd85f4c032", + "outfile": null, + "outfile_hash": null, + "stdout": null, + "stdout_hash": null, + "stderr": "asr-string_01-78629c4.stderr", + "stderr_hash": "29aa01d7370a20ada91bdb13c569434f36a0c18309700d946d89fbf0", + "returncode": 2 +} \ 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/reference/asr-string_02-499c9ff.stdout b/tests/reference/asr-string_02-499c9ff.stdout new file mode 100644 index 0000000000..09760b0cd2 --- /dev/null +++ b/tests/reference/asr-string_02-499c9ff.stdout @@ -0,0 +1 @@ +(Var 3 p) From 2872a652e499929152214c52ec68831382d48fc4 Mon Sep 17 00:00:00 2001 From: Prince Roshan Date: Fri, 6 Oct 2023 14:13:23 +0530 Subject: [PATCH 08/12] Update tests/errors/string_01.py Co-authored-by: Shaikh Ubaid --- tests/errors/string_01.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/errors/string_01.py b/tests/errors/string_01.py index e5d2ee960c..fcfc9eb1bf 100644 --- a/tests/errors/string_01.py +++ b/tests/errors/string_01.py @@ -2,5 +2,4 @@ def test_tuple_indicing_in_str(): x: str = "abcdefgh" print(x[2, 5]) - test_tuple_indicing_in_str() From da38f2f530bcce54e74abeb5bbccc1d79b76a9d9 Mon Sep 17 00:00:00 2001 From: Prince Roshan Date: Fri, 6 Oct 2023 14:18:07 +0530 Subject: [PATCH 09/12] Update test reference --- tests/reference/asr-string_01-78629c4.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/reference/asr-string_01-78629c4.json b/tests/reference/asr-string_01-78629c4.json index 1529bbbba8..8cf78f4a79 100644 --- a/tests/reference/asr-string_01-78629c4.json +++ b/tests/reference/asr-string_01-78629c4.json @@ -2,7 +2,7 @@ "basename": "asr-string_01-78629c4", "cmd": "lpython --show-asr --no-color {infile} -o {outfile}", "infile": "tests/errors/string_01.py", - "infile_hash": "6463d5b0d140abf583e4b2fd9218d720be61615c7d0e2ccd85f4c032", + "infile_hash": "92496679f4475e3351686f6556f0e68411437beed22a447f9a65bfdf", "outfile": null, "outfile_hash": null, "stdout": null, From 863fabad447de62ba057df88469616cd72f03799 Mon Sep 17 00:00:00 2001 From: Prince Roshan Date: Fri, 6 Oct 2023 14:42:03 +0530 Subject: [PATCH 10/12] Remove .stdout file --- tests/reference/asr-string_02-499c9ff.stdout | 1 - 1 file changed, 1 deletion(-) delete mode 100644 tests/reference/asr-string_02-499c9ff.stdout diff --git a/tests/reference/asr-string_02-499c9ff.stdout b/tests/reference/asr-string_02-499c9ff.stdout deleted file mode 100644 index 09760b0cd2..0000000000 --- a/tests/reference/asr-string_02-499c9ff.stdout +++ /dev/null @@ -1 +0,0 @@ -(Var 3 p) From 9cbc86279f2ebbdbc321b1dd42559087629505a3 Mon Sep 17 00:00:00 2001 From: Prince Roshan Date: Fri, 6 Oct 2023 15:03:43 +0530 Subject: [PATCH 11/12] Add test for corner cases --- integration_tests/test_str_01.py | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/integration_tests/test_str_01.py b/integration_tests/test_str_01.py index 5af2b36350..a1ff651776 100644 --- a/integration_tests/test_str_01.py +++ b/integration_tests/test_str_01.py @@ -61,13 +61,19 @@ def test_str_join2(): res:str = a.join(p) assert res == "a**b" -def test_str_join3(): +def test_str_join_empty_str(): a: str - a = "**-" + a = "" p:list[str] = ["a","b"] res:str = a.join(p) - assert res == "a**-b" + 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" @@ -81,7 +87,8 @@ def check(): test_str_repeat() test_str_join() test_str_join2() - test_str_join3() + test_str_join_empty_str() + test_str_join_empty_list() test_constant_str_subscript() check() From dcb18bd826fd0f6d2a432c8a32c10a19efd7f529 Mon Sep 17 00:00:00 2001 From: Prince Roshan Date: Fri, 6 Oct 2023 15:05:58 +0530 Subject: [PATCH 12/12] Remove unused import --- integration_tests/test_str_01.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/integration_tests/test_str_01.py b/integration_tests/test_str_01.py index a1ff651776..0df6f08bb1 100644 --- a/integration_tests/test_str_01.py +++ b/integration_tests/test_str_01.py @@ -1,5 +1,3 @@ -from lpython import i32 - def f(): x: str x = "ok"