← 输入框|github.com|刷新
Skip to content

Disallow parameter (argument) redefinition #1784

Closed

Description

@certik
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"

Activity

  1. certik commented on May 11, 2023

    @certik
    ContributorAuthor

    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
      | ^ 
  2. ubaidsk commented on May 13, 2023

    @ubaidsk
    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.

  3. ubaidsk commented on May 13, 2023

    @ubaidsk
    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions