Skip to content

Commit cb4165a

Browse files
authored
Fix missing allocation when returning an annotated array expression (pyccel#1831)
Fix missing allocation when returning an annotated array expression by ensuring a syntactic expression is passed to `_visit_Assign`. Fixes pyccel#1830.
1 parent dcfdc6e commit cb4165a

File tree

3 files changed

+20
-0
lines changed

3 files changed

+20
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ All notable changes to this project will be documented in this file.
2727
- #1732 : Fix multidimensional list indexing in Python.
2828
- #1785 : Add missing cast when creating an array of booleans from non-boolean values.
2929
- #1218 : Fix bug when assigning an array to a slice in Fortran.
30+
- #1830 : Fix missing allocation when returning an annotated array expression.
3031

3132
### Changed
3233
- #1720 : functions with the `@inline` decorator are no longer exposed to Python in the shared library.

pyccel/parser/semantic.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3855,6 +3855,9 @@ def _visit_Return(self, expr):
38553855
for o,r in zip(return_objs, results):
38563856
v = o.var
38573857
if not (isinstance(r, PyccelSymbol) and r == (v.name if isinstance(v, Variable) else v)):
3858+
# Create a syntactic object to visit
3859+
if isinstance(v, Variable):
3860+
v = PyccelSymbol(v.name)
38583861
a = self._visit(Assign(v, r, python_ast=expr.python_ast))
38593862
assigns.append(a)
38603863
if isinstance(a, ConstructorCall):

tests/epyccel/test_epyccel_return_arrays.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -889,3 +889,19 @@ def copy_f_to_default(b : 'T'):
889889
assert pyth_out.dtype is pycc_out.dtype
890890
assert pyth_out.flags.c_contiguous == pycc_out.flags.c_contiguous
891891
assert pyth_out.flags.f_contiguous == pycc_out.flags.f_contiguous
892+
893+
def test_annotated_return(language):
894+
def annotated_return(b : 'float[:,:]', c : 'float[:,:]') -> 'float[:,:]':
895+
return b + c
896+
897+
epyccel_func = epyccel(annotated_return, language=language)
898+
fl_b = np.array(uniform(min_float / 2, max_float / 2, (3,4)))
899+
fl_c = np.array(uniform(min_float / 2, max_float / 2, (3,4)))
900+
901+
pyth_out = annotated_return(fl_b, fl_c)
902+
pycc_out = epyccel_func(fl_b, fl_c)
903+
904+
assert np.array_equal(pyth_out, pycc_out)
905+
assert pyth_out.dtype is pycc_out.dtype
906+
assert pyth_out.flags.c_contiguous == pycc_out.flags.c_contiguous
907+
assert pyth_out.flags.f_contiguous == pycc_out.flags.f_contiguous

0 commit comments

Comments
 (0)