Skip to content

Commit 7f22dab

Browse files
authored
Merge pull request #615 from faze-geek/slicing_error
Uniform semantic errors in string slicing .
2 parents 70fef49 + 5002fbf commit 7f22dab

8 files changed

+62
-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
@@ -1776,6 +1776,9 @@ class CommonVisitor : public AST::BaseVisitor<Derived> {
17761776
AST::Slice_t *sl = AST::down_cast<AST::Slice_t>(x.m_slice);
17771777
if (sl->m_lower != nullptr) {
17781778
this->visit_expr(*sl->m_lower);
1779+
if (!ASRUtils::is_integer(*ASRUtils::expr_type(ASRUtils::EXPR(tmp)))) {
1780+
throw SemanticError("slice indices must be integers or None", tmp->loc);
1781+
}
17791782
ai.m_left = index_add_one(x.base.base.loc, ASRUtils::EXPR(tmp));
17801783
}
17811784
if (sl->m_upper != nullptr) {
@@ -1787,6 +1790,9 @@ class CommonVisitor : public AST::BaseVisitor<Derived> {
17871790
}
17881791
if (sl->m_step != nullptr) {
17891792
this->visit_expr(*sl->m_step);
1793+
if (!ASRUtils::is_integer(*ASRUtils::expr_type(ASRUtils::EXPR(tmp)))) {
1794+
throw SemanticError("slice indices must be integers or None", tmp->loc);
1795+
}
17901796
ai.m_step = index_add_one(x.base.base.loc, ASRUtils::EXPR(tmp));
17911797
}
17921798
if (ASR::is_a<ASR::List_t>(*type)) {

tests/errors/test_str_slicing2.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
def f():
2+
s: str
3+
s = "abcd"
4+
print(s[1.5:3])
5+
6+
f()

tests/errors/test_str_slicing3.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
def f():
2+
s: str
3+
s = "abcd"
4+
print(s[1:3:0.5])
5+
6+
f()
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"basename": "asr-test_str_slicing2-2f07e9a",
3+
"cmd": "lpython --show-asr --no-color {infile} -o {outfile}",
4+
"infile": "tests/errors/test_str_slicing2.py",
5+
"infile_hash": "eb9027d2b6ec0aba9cc78aeedc1b0b36972ff36b076fecf547eeb014",
6+
"outfile": null,
7+
"outfile_hash": null,
8+
"stdout": null,
9+
"stdout_hash": null,
10+
"stderr": "asr-test_str_slicing2-2f07e9a.stderr",
11+
"stderr_hash": "48a9286126c2333bdf5237358bd4ad27acac4a16a78069c9bd36d089",
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 indices must be integers or None
2+
--> tests/errors/test_str_slicing2.py:4:13
3+
|
4+
4 | print(s[1.5:3])
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_slicing3-fe6a03c",
3+
"cmd": "lpython --show-asr --no-color {infile} -o {outfile}",
4+
"infile": "tests/errors/test_str_slicing3.py",
5+
"infile_hash": "b223abc3ec96fe91876fa188259f151d61383687776e8975df45b139",
6+
"outfile": null,
7+
"outfile_hash": null,
8+
"stdout": null,
9+
"stdout_hash": null,
10+
"stderr": "asr-test_str_slicing3-fe6a03c.stderr",
11+
"stderr_hash": "5f7553d1509bed25d5137abc4fc2cb1d2cb983a1fab81d8d178ed197",
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 indices must be integers or None
2+
--> tests/errors/test_str_slicing3.py:4:17
3+
|
4+
4 | print(s[1:3:0.5])
5+
| ^^^

tests/tests.toml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,14 @@ asr = true
398398
filename = "errors/test_str_slicing.py"
399399
asr = true
400400

401+
[[test]]
402+
filename = "errors/test_str_slicing2.py"
403+
asr = true
404+
405+
[[test]]
406+
filename = "errors/test_str_slicing3.py"
407+
asr = true
408+
401409
[[test]]
402410
filename = "errors/test_annassign_type_mismatch.py"
403411
asr = true

0 commit comments

Comments
 (0)