Skip to content

Commit

Permalink
Raise error when slice step is 0 (lcompilers#1879)
Browse files Browse the repository at this point in the history
  • Loading branch information
kabra1110 authored Jun 12, 2023
1 parent 2db6c0f commit 13a386b
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/lpython/semantics/python_ast_to_asr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3464,9 +3464,15 @@ class CommonVisitor : public AST::BaseVisitor<Struct> {
}
if (sl->m_step != nullptr) {
this->visit_expr(*sl->m_step);
ASR::asr_t *tmp_step = tmp;
if (!ASRUtils::is_integer(*ASRUtils::expr_type(ASRUtils::EXPR(tmp)))) {
throw SemanticError("slice indices must be integers or None", tmp->loc);
}
ASR::expr_t* val = ASRUtils::expr_value(ASRUtils::EXPR(tmp_step));
int64_t const_value = 1;
if (ASRUtils::is_value_constant(val, const_value) && const_value == 0) {
throw SemanticError("slice step cannot be zero", tmp_step->loc);
}
ai.m_step = ASRUtils::EXPR(tmp);
}
if( ai.m_left != nullptr &&
Expand Down
8 changes: 8 additions & 0 deletions tests/errors/test_list_slicing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from lpython import i32

def test_slice_step_list():
l: list[i32]
l = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
print(l[0:10:0])

test_slice_step_list()
6 changes: 6 additions & 0 deletions tests/errors/test_str_slicing4.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
def test_slice_step_str():
s: str
s = "abcd"
print(s[0:1:0])

test_slice_step_str()
13 changes: 13 additions & 0 deletions tests/reference/asr-test_list_slicing-984fbf0.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"basename": "asr-test_list_slicing-984fbf0",
"cmd": "lpython --show-asr --no-color {infile} -o {outfile}",
"infile": "tests/errors/test_list_slicing.py",
"infile_hash": "7dfe8cfa4697e4ce8bf18e24c5b11b969e8329febc711d83d2fba746",
"outfile": null,
"outfile_hash": null,
"stdout": null,
"stdout_hash": null,
"stderr": "asr-test_list_slicing-984fbf0.stderr",
"stderr_hash": "6763533f9c1730429d50f58b653595bfeef48c4d19943c36d037d023",
"returncode": 2
}
5 changes: 5 additions & 0 deletions tests/reference/asr-test_list_slicing-984fbf0.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
semantic error: slice step cannot be zero
--> tests/errors/test_list_slicing.py:6:18
|
6 | print(l[0:10:0])
| ^
13 changes: 13 additions & 0 deletions tests/reference/asr-test_str_slicing4-a0c7a69.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"basename": "asr-test_str_slicing4-a0c7a69",
"cmd": "lpython --show-asr --no-color {infile} -o {outfile}",
"infile": "tests/errors/test_str_slicing4.py",
"infile_hash": "7bd3e8d21550f50244ee88e383dbd42cde33f03c516cfcaaeaaadd71",
"outfile": null,
"outfile_hash": null,
"stdout": null,
"stdout_hash": null,
"stderr": "asr-test_str_slicing4-a0c7a69.stderr",
"stderr_hash": "a5dd047df86649936606a0b134d10e76c6aacb224319be4aefd64bfe",
"returncode": 2
}
5 changes: 5 additions & 0 deletions tests/reference/asr-test_str_slicing4-a0c7a69.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
semantic error: slice step cannot be zero
--> tests/errors/test_str_slicing4.py:4:17
|
4 | print(s[0:1:0])
| ^
8 changes: 8 additions & 0 deletions tests/tests.toml
Original file line number Diff line number Diff line change
Expand Up @@ -713,6 +713,14 @@ asr = true
filename = "errors/test_str_slicing3.py"
asr = true

[[test]]
filename = "errors/test_str_slicing4.py"
asr = true

[[test]]
filename = "errors/test_list_slicing.py"
asr = true

[[test]]
filename = "errors/test_annassign_type_mismatch.py"
asr = true
Expand Down

0 comments on commit 13a386b

Please sign in to comment.