File tree Expand file tree Collapse file tree 6 files changed +57
-5
lines changed
Expand file tree Collapse file tree 6 files changed +57
-5
lines changed Original file line number Diff line number Diff line change @@ -59,6 +59,19 @@ def test_str_join2():
5959 res :str = a .join (p )
6060 assert res == "a**b"
6161
62+ def test_str_join_empty_str ():
63+ a : str
64+ a = ""
65+ p :list [str ] = ["a" ,"b" ]
66+ res :str = a .join (p )
67+ assert res == "ab"
68+
69+ def test_str_join_empty_list ():
70+ a : str
71+ a = "ab"
72+ p :list [str ] = []
73+ res :str = a .join (p )
74+ assert res == ""
6275
6376def test_constant_str_subscript ():
6477 assert "abc" [2 ] == "c"
@@ -71,6 +84,9 @@ def check():
7184 test_str_slice ()
7285 test_str_repeat ()
7386 test_str_join ()
87+ test_str_join2 ()
88+ test_str_join_empty_str ()
89+ test_str_join_empty_list ()
7490 test_constant_str_subscript ()
7591
7692check ()
Original file line number Diff line number Diff line change 3434#include < lpython/parser/parser.h>
3535#include < libasr/serialization.h>
3636
37-
3837namespace LCompilers ::LPython {
3938
4039namespace CastingUtil {
@@ -6669,15 +6668,21 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
66696668 throw SemanticError (" str.join() takes one argument" ,
66706669 loc);
66716670 }
6671+ ASR::expr_t *arg_sub = args[0 ].m_value ;
6672+ ASR::ttype_t *arg_sub_type = ASRUtils::expr_type (arg_sub);
6673+ if (!ASR::is_a<ASR::List_t>(*arg_sub_type)){
6674+ throw SemanticError (" str.join() takes type list only" ,
6675+ loc);
6676+ }
66726677 fn_call_name = " _lpython_str_join" ;
66736678 ASR::call_arg_t str_var;
66746679 str_var.loc = loc;
66756680 str_var.m_value = s_var;
6676- ASR::call_arg_t passed_int ;
6677- passed_int .loc = loc;
6678- passed_int .m_value = args[0 ].m_value ;
6681+ ASR::call_arg_t list_of_str ;
6682+ list_of_str .loc = loc;
6683+ list_of_str .m_value = args[0 ].m_value ;
66796684 fn_args.push_back (al, str_var);
6680- fn_args.push_back (al, passed_int );
6685+ fn_args.push_back (al, list_of_str );
66816686 } else if (attr_name == " find" ) {
66826687 if (args.size () != 1 ) {
66836688 throw SemanticError (" str.find() takes one argument" ,
Original file line number Diff line number Diff line change 1+ from lpython import i32
2+
3+ def test_wrong_argument_in_join ():
4+ x : str = "ab"
5+ p : i32 = 1
6+ res :str = x .join (p )
7+ print (res )
8+
9+ test_wrong_argument_in_join ()
Original file line number Diff line number Diff line change 1+ {
2+ "basename" : " asr-string_02-499c9ff" ,
3+ "cmd" : " lpython --show-asr --no-color {infile} -o {outfile}" ,
4+ "infile" : " tests/errors/string_02.py" ,
5+ "infile_hash" : " ed6511565e893791a4bd8ea0b4750817bab13cd6dc0731332127bf58" ,
6+ "outfile" : null ,
7+ "outfile_hash" : null ,
8+ "stdout" : null ,
9+ "stdout_hash" : null ,
10+ "stderr" : " asr-string_02-499c9ff.stderr" ,
11+ "stderr_hash" : " 368ba74a1e0d6609f71e6f87f95bd0b6151420c81336e48a172cb613" ,
12+ "returncode" : 2
13+ }
Original file line number Diff line number Diff line change 1+ semantic error: str.join() takes type list only
2+ --> tests/errors/string_02.py:6:15
3+ |
4+ 6 | res:str = x.join(p)
5+ | ^^^^^^^^^
Original file line number Diff line number Diff line change @@ -650,6 +650,10 @@ asr = true
650650filename = " errors/string_01.py"
651651asr = true
652652
653+ [[test ]]
654+ filename = " errors/string_02.py"
655+ asr = true
656+
653657[[test ]]
654658filename = " errors/structs_01.py"
655659asr = true
You can’t perform that action at this time.
0 commit comments