Skip to content

Commit 9c0b45e

Browse files
authored
Fix missing loop in slice assignment (pyccel#2027)
Fix missing loop in slice assignment by creating missing `LoopCollection` object. Fixes pyccel#2026 . Modify the existing test so it would fail on the `devel` branch.
1 parent 2381034 commit 9c0b45e

File tree

3 files changed

+11
-4
lines changed

3 files changed

+11
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ All notable changes to this project will be documented in this file.
6767
- #1930 : Preserve ordering of import targets.
6868
- #1892 : Fix implementation of list function when an iterable is passed as parameter.
6969
- #1972 : Simplified `printf` statement for Literal String.
70+
- #2026 : Fix missing loop in slice assignment.
7071

7172
### Changed
7273

pyccel/ast/utilities.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -571,13 +571,19 @@ def collect_loops(block, indices, new_index, language_has_vectors = False, resul
571571
lhs = line.lhs
572572
rhs = line.rhs
573573
if lhs.rank > rhs.rank:
574-
for index_depth in range(lhs.rank-rhs.rank):
574+
loop_len = []
575+
n_new_loops = lhs.rank-rhs.rank
576+
for index_depth in range(n_new_loops):
577+
loop_len.append(lhs.shape[0])
575578
# If an index exists at the same depth, reuse it if not create one
576579
if index_depth >= len(indices):
577580
indices.append(new_index(PythonNativeInt(), 'i'))
578581
index = indices[index_depth]
579582
lhs = insert_index(lhs, index_depth, index)
580-
collect_loops([Assign(lhs, rhs)], indices, new_index, language_has_vectors, result = result)
583+
block = collect_loops([Assign(lhs, rhs)], indices, new_index, language_has_vectors)
584+
for s in loop_len:
585+
block = LoopCollection(block, s, set([lhs]))
586+
result.append(block)
581587

582588
elif not language_has_vectors:
583589
if isinstance(rhs, NumpyArray):

tests/epyccel/modules/arrays.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1592,8 +1592,8 @@ def array_2d_C_slice_stride_23(a : 'int[:,:]'):
15921592

15931593
def copy_to_slice_issue_1218(n : int):
15941594
from numpy import zeros, array
1595-
x = 1
1596-
arr = zeros((2, n))
1595+
x = 2
1596+
arr = zeros((3, n))
15971597
arr[0:x, 0:6:2] = array([2, 5, 6])
15981598
return arr
15991599

0 commit comments

Comments
 (0)