Skip to content

Error on assignment to function parameter #1803

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
May 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions integration_tests/generics_02.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
from lpython import TypeVar
from lpython import TypeVar, InOut

T = TypeVar('T')

def swap(x: T, y: T):
def swap(x: InOut[T], y: InOut[T]):
temp: T
temp = x
x = y
y = temp
print(x)
print(y)

swap(1,2)
swap(1,2)
7 changes: 7 additions & 0 deletions src/lpython/semantics/python_ast_to_asr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4844,6 +4844,13 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
std::string var_name = std::string(v->m_name);
throw SemanticError("Assignment to loop variable `" + std::string(to_lower(var_name)) +"` is not allowed", target->base.loc);
}
if (sym->type == ASR::symbolType::Variable) {
ASR::Variable_t *v = ASR::down_cast<ASR::Variable_t>(sym);
if (v->m_intent == ASR::intentType::In) {
throw SemanticError("Assignment to an input function parameter `"
+ std::string(v->m_name) + "` is not allowed", target->base.loc);
}
}
}
tmp_vec.push_back(ASR::make_Assignment_t(al, x.base.base.loc, target, tmp_value,
overloaded));
Expand Down
56 changes: 31 additions & 25 deletions src/runtime/lpython_builtin.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,18 +198,20 @@ def bin(n: i32) -> str:
return '0b0'
prep: str
prep = '0b'
if n < 0:
n = -n
n_: i32
n_ = n
if n_ < 0:
n_ = -n_
prep = '-0b'
res: str
res = ''
if (n - _lpython_floordiv(n, 2)*2) == 0:
if (n_ - _lpython_floordiv(n_, 2)*2) == 0:
res += '0'
else:
res += '1'
while n > 1:
n = _lpython_floordiv(n, 2)
if (n - _lpython_floordiv(n, 2)*2) == 0:
while n_ > 1:
n_ = _lpython_floordiv(n_, 2)
if (n_ - _lpython_floordiv(n_, 2)*2) == 0:
res += '0'
else:
res += '1'
Expand All @@ -227,16 +229,18 @@ def hex(n: i32) -> str:
return '0x0'
prep: str
prep = '0x'
if n < 0:
n_: i32
n_ = n
if n_ < 0:
prep = '-0x'
n = -n
n_ = -n_
res: str
res = ""
remainder: i32
while n > 0:
remainder = n - _lpython_floordiv(n, 16)*16
n -= remainder
n = _lpython_floordiv(n, 16)
while n_ > 0:
remainder = n_ - _lpython_floordiv(n_, 16)*16
n_ -= remainder
n_ = _lpython_floordiv(n_, 16)
res += hex_values[remainder]
return prep + res[::-1]

Expand All @@ -252,16 +256,18 @@ def oct(n: i32) -> str:
return '0o0'
prep: str
prep = '0o'
if n < 0:
n_: i32
n_ = n
if n_ < 0:
prep = '-0o'
n = -n
n_ = -n_
res: str
res = ""
remainder: i32
while n > 0:
remainder = n - _lpython_floordiv(n, 8)*8
n -= remainder
n = _lpython_floordiv(n, 8)
while n_ > 0:
remainder = n_ - _lpython_floordiv(n_, 8)*8
n_ -= remainder
n_ = _lpython_floordiv(n_, 8)
res += _values[remainder]
return prep + res[::-1]

Expand Down Expand Up @@ -805,18 +811,18 @@ def _lpython_str_startswith(s: str ,sub: str) -> bool:
return res

@overload
def _lpython_str_endswith(s: str, suffix: str) -> bool:
def _lpython_str_endswith(s: str, suffix: str) -> bool:

if(len(suffix) > len(s)):
return False

i : i32
i = 0
while(i < len(suffix)):
if(suffix[len(suffix) - i - 1] != s[len(s) - i - 1]):
return False
i += 1

return True

@overload
Expand All @@ -833,13 +839,13 @@ def _lpython_str_partition(s:str, sep: str) -> tuple[str, str, str]:
ind = _lpython_str_find(s, sep)
if ind == -1:
res = (s, "", "")
else:
res = (s[0:ind], sep, s[ind+len(sep): len(s)])
else:
res = (s[0:ind], sep, s[ind+len(sep): len(s)])
return res

@overload
def _lpython_str_islower(s: str) -> bool:
is_cased_present: bool
is_cased_present: bool
is_cased_present = False
i:str
for i in s:
Expand All @@ -851,7 +857,7 @@ def _lpython_str_islower(s: str) -> bool:

@overload
def _lpython_str_isupper(s: str) -> bool:
is_cased_present: bool
is_cased_present: bool
is_cased_present = False
i:str
for i in s:
Expand Down
40 changes: 24 additions & 16 deletions src/runtime/math.py
Original file line number Diff line number Diff line change
Expand Up @@ -472,29 +472,37 @@ def gcd(a: i32, b: i32) -> i32:
Returns greatest common divisor of `a` and `b`
"""
temp: i32
if a < 0:
a = -a
if b < 0:
b = -b
while b != 0:
a = mod(a, b)
temp = a
a = b
b = temp
return a
a_: i32
b_: i32
a_ = a
b_ = b
if a_ < 0:
a_ = -a_
if b_ < 0:
b_ = -b_
while b_ != 0:
a_ = mod(a_, b_)
temp = a_
a_ = b_
b_ = temp
return a_


def lcm(a: i32, b: i32) -> i32:
"""
Returns least common multiple of `a` and `b`
"""
if a < 0:
a = -a
if b < 0:
b = -b
if a*b == 0:
a_: i32
b_: i32
a_ = a
b_ = b
if a_ < 0:
a_ = -a_
if b_ < 0:
b_ = -b_
if a_*b_ == 0:
return 0
return i32(floor((a*b)/gcd(a, b)))
return i32(floor((a_*b_)/gcd(a_, b_)))


def copysign(x: f64, y: f64) -> f64:
Expand Down
5 changes: 5 additions & 0 deletions tests/errors/test_assign9.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
def f(x: i32) -> i32:
x = 2
return x * x

print(f(3)) # => 4
6 changes: 0 additions & 6 deletions tests/expr9.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,14 @@ def test_return_2(a: i32) -> str:


def test_return_3(a: i32) -> i32:
a = 3
return a


def test_return_4(a: i32):
a = 1
return

def main0():
i: i32
i = test_return_1(4)
s: str
s = test_return_2(4)
i = test_return_3(4)
test_return_4(4)

main0()
4 changes: 2 additions & 2 deletions tests/reference/asr-expr9-814e4bc.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
"basename": "asr-expr9-814e4bc",
"cmd": "lpython --show-asr --no-color {infile} -o {outfile}",
"infile": "tests/expr9.py",
"infile_hash": "1f02a7486b298ae9e74a163875a76f8fa7cc25d7f50547133dbbdfab",
"infile_hash": "4819e0f20d2ed25647ab94f74cb7b5b61e3d4f43e159e46ad79c1c4c",
"outfile": null,
"outfile_hash": null,
"stdout": "asr-expr9-814e4bc.stdout",
"stdout_hash": "fd9a9323419a77d41f2b3b06c607be0aab4dc626a070dda593b2117c",
"stdout_hash": "b1daccda9413a8fc6ad88b8898e011d5f556e862f9b853b3564d7189",
"stderr": null,
"stderr_hash": null,
"returncode": 0
Expand Down
Loading