Skip to content

Commit 13a386b

Browse files
authored
Raise error when slice step is 0 (#1879)
1 parent 2db6c0f commit 13a386b

8 files changed

+64
-0
lines changed

src/lpython/semantics/python_ast_to_asr.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3464,9 +3464,15 @@ class CommonVisitor : public AST::BaseVisitor<Struct> {
34643464
}
34653465
if (sl->m_step != nullptr) {
34663466
this->visit_expr(*sl->m_step);
3467+
ASR::asr_t *tmp_step = tmp;
34673468
if (!ASRUtils::is_integer(*ASRUtils::expr_type(ASRUtils::EXPR(tmp)))) {
34683469
throw SemanticError("slice indices must be integers or None", tmp->loc);
34693470
}
3471+
ASR::expr_t* val = ASRUtils::expr_value(ASRUtils::EXPR(tmp_step));
3472+
int64_t const_value = 1;
3473+
if (ASRUtils::is_value_constant(val, const_value) && const_value == 0) {
3474+
throw SemanticError("slice step cannot be zero", tmp_step->loc);
3475+
}
34703476
ai.m_step = ASRUtils::EXPR(tmp);
34713477
}
34723478
if( ai.m_left != nullptr &&

tests/errors/test_list_slicing.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from lpython import i32
2+
3+
def test_slice_step_list():
4+
l: list[i32]
5+
l = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
6+
print(l[0:10:0])
7+
8+
test_slice_step_list()

tests/errors/test_str_slicing4.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
def test_slice_step_str():
2+
s: str
3+
s = "abcd"
4+
print(s[0:1:0])
5+
6+
test_slice_step_str()
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"basename": "asr-test_list_slicing-984fbf0",
3+
"cmd": "lpython --show-asr --no-color {infile} -o {outfile}",
4+
"infile": "tests/errors/test_list_slicing.py",
5+
"infile_hash": "7dfe8cfa4697e4ce8bf18e24c5b11b969e8329febc711d83d2fba746",
6+
"outfile": null,
7+
"outfile_hash": null,
8+
"stdout": null,
9+
"stdout_hash": null,
10+
"stderr": "asr-test_list_slicing-984fbf0.stderr",
11+
"stderr_hash": "6763533f9c1730429d50f58b653595bfeef48c4d19943c36d037d023",
12+
"returncode": 2
13+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
semantic error: slice step cannot be zero
2+
--> tests/errors/test_list_slicing.py:6:18
3+
|
4+
6 | print(l[0:10:0])
5+
| ^
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"basename": "asr-test_str_slicing4-a0c7a69",
3+
"cmd": "lpython --show-asr --no-color {infile} -o {outfile}",
4+
"infile": "tests/errors/test_str_slicing4.py",
5+
"infile_hash": "7bd3e8d21550f50244ee88e383dbd42cde33f03c516cfcaaeaaadd71",
6+
"outfile": null,
7+
"outfile_hash": null,
8+
"stdout": null,
9+
"stdout_hash": null,
10+
"stderr": "asr-test_str_slicing4-a0c7a69.stderr",
11+
"stderr_hash": "a5dd047df86649936606a0b134d10e76c6aacb224319be4aefd64bfe",
12+
"returncode": 2
13+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
semantic error: slice step cannot be zero
2+
--> tests/errors/test_str_slicing4.py:4:17
3+
|
4+
4 | print(s[0:1:0])
5+
| ^

tests/tests.toml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -713,6 +713,14 @@ asr = true
713713
filename = "errors/test_str_slicing3.py"
714714
asr = true
715715

716+
[[test]]
717+
filename = "errors/test_str_slicing4.py"
718+
asr = true
719+
720+
[[test]]
721+
filename = "errors/test_list_slicing.py"
722+
asr = true
723+
716724
[[test]]
717725
filename = "errors/test_annassign_type_mismatch.py"
718726
asr = true

0 commit comments

Comments
 (0)