Skip to content

Commit

Permalink
Fix passing temporary arrays to functions (#1499)
Browse files Browse the repository at this point in the history
Fix problems when passing temporary arrays to functions by widening the
applicability of the `if` block which results in temporary variables
being created for arrays. Fixes #1126.
  • Loading branch information
EmilyBourne committed Aug 23, 2023
1 parent 20797b4 commit 4371217
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 24 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ All notable changes to this project will be documented in this file.

- #682 : Wrong data layout when copying a slice of an array.
- #1453 : Fix error-level developer mode output.
- #1499 : Fix passing temporary arrays to functions.
- \[INTERNALS\] Fix string base class selection.

### Changed
Expand Down
2 changes: 1 addition & 1 deletion pyccel/parser/semantic.py
Original file line number Diff line number Diff line change
Expand Up @@ -1827,7 +1827,7 @@ def _visit_PythonList(self, expr):
def _visit_FunctionCallArgument(self, expr):
value = self._visit(expr.value)
a = FunctionCallArgument(value, expr.keyword)
if isinstance(a.value, PyccelArithmeticOperator) and a.value.rank:
if isinstance(value, (PyccelArithmeticOperator, PyccelInternalFunction)) and value.rank:
tmp_var = self.scope.get_new_name()
assign = self._visit(Assign(tmp_var, expr.value, fst = expr.value.fst))
self._additional_exprs[-1].append(assign)
Expand Down
38 changes: 16 additions & 22 deletions tests/pyccel/scripts/functions.py
Original file line number Diff line number Diff line change
@@ -1,57 +1,50 @@
# pylint: disable=missing-function-docstring, missing-module-docstring
import numpy as np

#$ header function incr_(int)
def incr_(x):
#$ header function decr_(int)
def decr_(y):
y = y-1
return y
x = x + 1
def incr_(x : int):
x = x + 2
return x

def helloworld():
print('hello world')

#$ header function incr(int)
def incr(x):
def incr(x : int):
x = x + 1
return x

#$ header function decr(int) results(int)
def decr(x):
def decr(x : int) -> int:
y = x - 1
return y

#$ header function incr_array(int [:])
def incr_array(x):
def incr_array(x : 'int[:]'):
x[:] = x + 1

y_=[1,2,3]
y_ = np.array([1,2,3])

# #$ header function decr_array([int]) results([int])
# def decr_array(x):
# def decr_array(x : int) -> int:
# y_[1] = 6
# z = y_
# t = y_+x
# return t

#$ header function decr_array(int [:])
def decr_array(x):
def decr_array(x : 'int[:]'):
x[:] = x - 1

#$ header function f1(int, int, int) results(int)
def f1(x, n=2, m=3):
def f1(x : int, n : int = 2, m : int = 3) -> int:
y = x - n*m
return y

#$ header function f2(int, int) results(int)
def f2(x, m=None):
def f2(x : int, m : int = None):
if m is None:
y = x + 1
else:
y = x - 1
return y

def my_print(a : 'int[:]'):
print(a)


y = decr(2)
z = f1(1)

Expand All @@ -69,3 +62,4 @@ def pass_fun():
print(z)
print(z1)
print(z2)
my_print(np.array([1,2,3]))
2 changes: 1 addition & 1 deletion tests/pyccel/test_pyccel.py
Original file line number Diff line number Diff line change
Expand Up @@ -1083,7 +1083,7 @@ def test_function_aliasing():

def test_function(language):
pyccel_test("scripts/functions.py",
language = language, output_dtype=[str]+[int]*7 )
language = language, output_dtype=[str]+[int]*8 )

#------------------------------------------------------------------------------
@pytest.mark.xdist_incompatible
Expand Down

0 comments on commit 4371217

Please sign in to comment.