Skip to content

Disallow parameter (argument) redefinition #1784

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

Closed
Tracked by #1802
certik opened this issue May 11, 2023 · 3 comments · Fixed by #1837
Closed
Tracked by #1802

Disallow parameter (argument) redefinition #1784

certik opened this issue May 11, 2023 · 3 comments · Fixed by #1837

Comments

@certik
Copy link
Contributor

certik commented May 11, 2023

def f(x: i32) -> i32:
    x = 2
    return x * x

print(f(3)) # => 4

Currently this is allowed:

$ lpython a.py 
4

But we should give a compile time error message "input parameter x cannot be assigned to"

@certik
Copy link
Contributor Author

certik commented May 11, 2023

For a comparison, in Fortran this:

integer function f(x) result(y)
integer, intent(in) :: x
x = 2
y = x*x
end function

is not allowed:

$ gfortran a.f90 
a.f90:3:0:

    3 | x = 2
      | 
Error: Dummy argument 'x' with INTENT(IN) in variable definition context (assignment) at (1)
$ lfortran a.f90 
semantic error: Cannot assign to an intent(in) variable `x`
 --> a.f90:3:1
  |
3 | x = 2
  | ^ 

@certik certik mentioned this issue May 13, 2023
25 tasks
@Shaikh-Ubaid
Copy link
Collaborator

It seems that the example

from lpython import i32

def f(x: i32) -> i32:
    x = 2
    return x * x

print(f(3)) # => 4

works in cpython.

$ python examples/expr2.py
4

Also, I think assignment to function parameter is allowed in C/CPP, for example:

$ cat examples/expr2.cpp 
#include <iostream>

int f(int x) {
    x = 2;
    return x * x;
}

int main() {
    std::cout << f(3) << std::endl;
}
$ g++ examples/expr2.cpp -o expr2cpp
$ ./expr2cpp 
4

Thus, it seems assignment to function parameter could be allowed in lpython.

@Shaikh-Ubaid
Copy link
Collaborator

I shared a concern here #1803 (comment).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants