stfc / PSyclone

Domain-specific compiler and code transformation system for Finite Difference/Volume/Element Earth-system models in Fortran
BSD 3-Clause "New" or "Revised" License
104 stars 27 forks source link

Copying a tree containing shadowed symbols may change semantics #2666

Open arporter opened 2 months ago

arporter commented 2 months ago

ScopingNode._refine_copy() uses replace_symbols_using to ensure that Symbols referred to in a sub-tree are updated. However, since replace_symbols_using only uses symbol names, this won't get the correct symbol if the PSyIR happens to have symbols shadowed in nested scopes. In practise, this isn't often a problem but it is easy to imagine a situation where it will break, e.g.:

  subroutine test
    integer :: a
    integer :: b = 1
    if condition then
      ! PSyIR declares a shadowed, locally-scoped a'
      a' = 1
      if condition2 then
        ! PSyIR declares a shadowed, locally-scoped b'
        b' = 2
        a = a' + b'

Here, the final assignment will end up being a' = a' + b' and thus the semantics of the code are changed. (Note that Fortran code is only used to illustrate the problem - it's not possible to create such a situation directly from actual Fortran because it doesn't support nested scopes*.)