diff --git a/src/lpython/semantics/python_ast_to_asr.cpp b/src/lpython/semantics/python_ast_to_asr.cpp index 8efedb4cd4..91d726602a 100644 --- a/src/lpython/semantics/python_ast_to_asr.cpp +++ b/src/lpython/semantics/python_ast_to_asr.cpp @@ -1636,6 +1636,9 @@ class CommonVisitor : public AST::BaseVisitor { AST::Slice_t *sl = AST::down_cast(x.m_slice); if (sl->m_lower != nullptr) { this->visit_expr(*sl->m_lower); + if (!ASRUtils::is_integer(*ASRUtils::expr_type(ASRUtils::EXPR(tmp)))) { + throw SemanticError("slice indices must be integers or None", tmp->loc); + } ai.m_left = index_add_one(x.base.base.loc, ASRUtils::EXPR(tmp)); } if (sl->m_upper != nullptr) { @@ -1647,6 +1650,9 @@ class CommonVisitor : public AST::BaseVisitor { } if (sl->m_step != nullptr) { this->visit_expr(*sl->m_step); + if (!ASRUtils::is_integer(*ASRUtils::expr_type(ASRUtils::EXPR(tmp)))) { + throw SemanticError("slice indices must be integers or None", tmp->loc); + } ai.m_step = index_add_one(x.base.base.loc, ASRUtils::EXPR(tmp)); } if (ASR::is_a(*type)) { diff --git a/tests/errors/test_str_slicing2.py b/tests/errors/test_str_slicing2.py new file mode 100644 index 0000000000..78ceccdedb --- /dev/null +++ b/tests/errors/test_str_slicing2.py @@ -0,0 +1,6 @@ +def f(): + s: str + s = "abcd" + print(s[1.5:3]) + +f() \ No newline at end of file diff --git a/tests/errors/test_str_slicing3.py b/tests/errors/test_str_slicing3.py new file mode 100644 index 0000000000..070225f930 --- /dev/null +++ b/tests/errors/test_str_slicing3.py @@ -0,0 +1,6 @@ +def f(): + s: str + s = "abcd" + print(s[1:3:0.5]) + +f() \ No newline at end of file diff --git a/tests/reference/asr-test_str_slicing2-2f07e9a.json b/tests/reference/asr-test_str_slicing2-2f07e9a.json new file mode 100644 index 0000000000..dc63e9a7b0 --- /dev/null +++ b/tests/reference/asr-test_str_slicing2-2f07e9a.json @@ -0,0 +1,13 @@ +{ + "basename": "asr-test_str_slicing2-2f07e9a", + "cmd": "lpython --show-asr --no-color {infile} -o {outfile}", + "infile": "tests/errors/test_str_slicing2.py", + "infile_hash": "eb9027d2b6ec0aba9cc78aeedc1b0b36972ff36b076fecf547eeb014", + "outfile": null, + "outfile_hash": null, + "stdout": null, + "stdout_hash": null, + "stderr": "asr-test_str_slicing2-2f07e9a.stderr", + "stderr_hash": "48a9286126c2333bdf5237358bd4ad27acac4a16a78069c9bd36d089", + "returncode": 2 +} \ No newline at end of file diff --git a/tests/reference/asr-test_str_slicing2-2f07e9a.stderr b/tests/reference/asr-test_str_slicing2-2f07e9a.stderr new file mode 100644 index 0000000000..94a5f03259 --- /dev/null +++ b/tests/reference/asr-test_str_slicing2-2f07e9a.stderr @@ -0,0 +1,5 @@ +semantic error: slice indices must be integers or None + --> tests/errors/test_str_slicing2.py:4:13 + | +4 | print(s[1.5:3]) + | ^^^ diff --git a/tests/reference/asr-test_str_slicing3-fe6a03c.json b/tests/reference/asr-test_str_slicing3-fe6a03c.json new file mode 100644 index 0000000000..d1fe49fdfe --- /dev/null +++ b/tests/reference/asr-test_str_slicing3-fe6a03c.json @@ -0,0 +1,13 @@ +{ + "basename": "asr-test_str_slicing3-fe6a03c", + "cmd": "lpython --show-asr --no-color {infile} -o {outfile}", + "infile": "tests/errors/test_str_slicing3.py", + "infile_hash": "b223abc3ec96fe91876fa188259f151d61383687776e8975df45b139", + "outfile": null, + "outfile_hash": null, + "stdout": null, + "stdout_hash": null, + "stderr": "asr-test_str_slicing3-fe6a03c.stderr", + "stderr_hash": "5f7553d1509bed25d5137abc4fc2cb1d2cb983a1fab81d8d178ed197", + "returncode": 2 +} \ No newline at end of file diff --git a/tests/reference/asr-test_str_slicing3-fe6a03c.stderr b/tests/reference/asr-test_str_slicing3-fe6a03c.stderr new file mode 100644 index 0000000000..b1180c30ae --- /dev/null +++ b/tests/reference/asr-test_str_slicing3-fe6a03c.stderr @@ -0,0 +1,5 @@ +semantic error: slice indices must be integers or None + --> tests/errors/test_str_slicing3.py:4:17 + | +4 | print(s[1:3:0.5]) + | ^^^ diff --git a/tests/tests.toml b/tests/tests.toml index 82ce491f02..767669b103 100644 --- a/tests/tests.toml +++ b/tests/tests.toml @@ -373,6 +373,14 @@ asr = true filename = "errors/test_str_slicing.py" asr = true +[[test]] +filename = "errors/test_str_slicing2.py" +asr = true + +[[test]] +filename = "errors/test_str_slicing3.py" +asr = true + [[test]] filename = "errors/test_annassign_type_mismatch.py" asr = true