Skip to content

Commit 3665bcf

Browse files
authored
Merge pull request #1803 from Shaikh-Ubaid/input_param_redef
Error on assignment to function parameter
2 parents e487d07 + 41cb230 commit 3665bcf

19 files changed

+127
-225
lines changed

integration_tests/generics_02.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
from lpython import TypeVar
1+
from lpython import TypeVar, InOut
22

33
T = TypeVar('T')
44

5-
def swap(x: T, y: T):
5+
def swap(x: InOut[T], y: InOut[T]):
66
temp: T
77
temp = x
88
x = y
99
y = temp
1010
print(x)
1111
print(y)
1212

13-
swap(1,2)
13+
swap(1,2)

src/lpython/semantics/python_ast_to_asr.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4844,6 +4844,13 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
48444844
std::string var_name = std::string(v->m_name);
48454845
throw SemanticError("Assignment to loop variable `" + std::string(to_lower(var_name)) +"` is not allowed", target->base.loc);
48464846
}
4847+
if (sym->type == ASR::symbolType::Variable) {
4848+
ASR::Variable_t *v = ASR::down_cast<ASR::Variable_t>(sym);
4849+
if (v->m_intent == ASR::intentType::In) {
4850+
throw SemanticError("Assignment to an input function parameter `"
4851+
+ std::string(v->m_name) + "` is not allowed", target->base.loc);
4852+
}
4853+
}
48474854
}
48484855
tmp_vec.push_back(ASR::make_Assignment_t(al, x.base.base.loc, target, tmp_value,
48494856
overloaded));

src/runtime/lpython_builtin.py

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -198,18 +198,20 @@ def bin(n: i32) -> str:
198198
return '0b0'
199199
prep: str
200200
prep = '0b'
201-
if n < 0:
202-
n = -n
201+
n_: i32
202+
n_ = n
203+
if n_ < 0:
204+
n_ = -n_
203205
prep = '-0b'
204206
res: str
205207
res = ''
206-
if (n - _lpython_floordiv(n, 2)*2) == 0:
208+
if (n_ - _lpython_floordiv(n_, 2)*2) == 0:
207209
res += '0'
208210
else:
209211
res += '1'
210-
while n > 1:
211-
n = _lpython_floordiv(n, 2)
212-
if (n - _lpython_floordiv(n, 2)*2) == 0:
212+
while n_ > 1:
213+
n_ = _lpython_floordiv(n_, 2)
214+
if (n_ - _lpython_floordiv(n_, 2)*2) == 0:
213215
res += '0'
214216
else:
215217
res += '1'
@@ -227,16 +229,18 @@ def hex(n: i32) -> str:
227229
return '0x0'
228230
prep: str
229231
prep = '0x'
230-
if n < 0:
232+
n_: i32
233+
n_ = n
234+
if n_ < 0:
231235
prep = '-0x'
232-
n = -n
236+
n_ = -n_
233237
res: str
234238
res = ""
235239
remainder: i32
236-
while n > 0:
237-
remainder = n - _lpython_floordiv(n, 16)*16
238-
n -= remainder
239-
n = _lpython_floordiv(n, 16)
240+
while n_ > 0:
241+
remainder = n_ - _lpython_floordiv(n_, 16)*16
242+
n_ -= remainder
243+
n_ = _lpython_floordiv(n_, 16)
240244
res += hex_values[remainder]
241245
return prep + res[::-1]
242246

@@ -252,16 +256,18 @@ def oct(n: i32) -> str:
252256
return '0o0'
253257
prep: str
254258
prep = '0o'
255-
if n < 0:
259+
n_: i32
260+
n_ = n
261+
if n_ < 0:
256262
prep = '-0o'
257-
n = -n
263+
n_ = -n_
258264
res: str
259265
res = ""
260266
remainder: i32
261-
while n > 0:
262-
remainder = n - _lpython_floordiv(n, 8)*8
263-
n -= remainder
264-
n = _lpython_floordiv(n, 8)
267+
while n_ > 0:
268+
remainder = n_ - _lpython_floordiv(n_, 8)*8
269+
n_ -= remainder
270+
n_ = _lpython_floordiv(n_, 8)
265271
res += _values[remainder]
266272
return prep + res[::-1]
267273

@@ -805,18 +811,18 @@ def _lpython_str_startswith(s: str ,sub: str) -> bool:
805811
return res
806812

807813
@overload
808-
def _lpython_str_endswith(s: str, suffix: str) -> bool:
814+
def _lpython_str_endswith(s: str, suffix: str) -> bool:
809815

810816
if(len(suffix) > len(s)):
811817
return False
812-
818+
813819
i : i32
814820
i = 0
815821
while(i < len(suffix)):
816822
if(suffix[len(suffix) - i - 1] != s[len(s) - i - 1]):
817823
return False
818824
i += 1
819-
825+
820826
return True
821827

822828
@overload
@@ -833,13 +839,13 @@ def _lpython_str_partition(s:str, sep: str) -> tuple[str, str, str]:
833839
ind = _lpython_str_find(s, sep)
834840
if ind == -1:
835841
res = (s, "", "")
836-
else:
837-
res = (s[0:ind], sep, s[ind+len(sep): len(s)])
842+
else:
843+
res = (s[0:ind], sep, s[ind+len(sep): len(s)])
838844
return res
839845

840846
@overload
841847
def _lpython_str_islower(s: str) -> bool:
842-
is_cased_present: bool
848+
is_cased_present: bool
843849
is_cased_present = False
844850
i:str
845851
for i in s:
@@ -851,7 +857,7 @@ def _lpython_str_islower(s: str) -> bool:
851857

852858
@overload
853859
def _lpython_str_isupper(s: str) -> bool:
854-
is_cased_present: bool
860+
is_cased_present: bool
855861
is_cased_present = False
856862
i:str
857863
for i in s:

src/runtime/math.py

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -472,29 +472,37 @@ def gcd(a: i32, b: i32) -> i32:
472472
Returns greatest common divisor of `a` and `b`
473473
"""
474474
temp: i32
475-
if a < 0:
476-
a = -a
477-
if b < 0:
478-
b = -b
479-
while b != 0:
480-
a = mod(a, b)
481-
temp = a
482-
a = b
483-
b = temp
484-
return a
475+
a_: i32
476+
b_: i32
477+
a_ = a
478+
b_ = b
479+
if a_ < 0:
480+
a_ = -a_
481+
if b_ < 0:
482+
b_ = -b_
483+
while b_ != 0:
484+
a_ = mod(a_, b_)
485+
temp = a_
486+
a_ = b_
487+
b_ = temp
488+
return a_
485489

486490

487491
def lcm(a: i32, b: i32) -> i32:
488492
"""
489493
Returns least common multiple of `a` and `b`
490494
"""
491-
if a < 0:
492-
a = -a
493-
if b < 0:
494-
b = -b
495-
if a*b == 0:
495+
a_: i32
496+
b_: i32
497+
a_ = a
498+
b_ = b
499+
if a_ < 0:
500+
a_ = -a_
501+
if b_ < 0:
502+
b_ = -b_
503+
if a_*b_ == 0:
496504
return 0
497-
return i32(floor((a*b)/gcd(a, b)))
505+
return i32(floor((a_*b_)/gcd(a_, b_)))
498506

499507

500508
def copysign(x: f64, y: f64) -> f64:

tests/errors/test_assign9.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
def f(x: i32) -> i32:
2+
x = 2
3+
return x * x
4+
5+
print(f(3)) # => 4

tests/expr9.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,14 @@ def test_return_2(a: i32) -> str:
1111

1212

1313
def test_return_3(a: i32) -> i32:
14-
a = 3
1514
return a
1615

1716

18-
def test_return_4(a: i32):
19-
a = 1
20-
return
21-
2217
def main0():
2318
i: i32
2419
i = test_return_1(4)
2520
s: str
2621
s = test_return_2(4)
2722
i = test_return_3(4)
28-
test_return_4(4)
2923

3024
main0()

tests/reference/asr-expr9-814e4bc.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
"basename": "asr-expr9-814e4bc",
33
"cmd": "lpython --show-asr --no-color {infile} -o {outfile}",
44
"infile": "tests/expr9.py",
5-
"infile_hash": "1f02a7486b298ae9e74a163875a76f8fa7cc25d7f50547133dbbdfab",
5+
"infile_hash": "4819e0f20d2ed25647ab94f74cb7b5b61e3d4f43e159e46ad79c1c4c",
66
"outfile": null,
77
"outfile_hash": null,
88
"stdout": "asr-expr9-814e4bc.stdout",
9-
"stdout_hash": "fd9a9323419a77d41f2b3b06c607be0aab4dc626a070dda593b2117c",
9+
"stdout_hash": "b1daccda9413a8fc6ad88b8898e011d5f556e862f9b853b3564d7189",
1010
"stderr": null,
1111
"stderr_hash": null,
1212
"returncode": 0

0 commit comments

Comments
 (0)