<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/82684>82684</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            [flang] static bounds checking of unreachable code
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            flang
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          bcornille
      </td>
    </tr>
</table>

<pre>
    The below code encounters a semantic error check in flang-new. It may require some Fortran language lawyers to verify its correctness, but no runtime out of bounds access can occur in this program. Can someone please comment on whether this is truly a semantic error of the program or if flang is being too aggressive in its restriction? I encountered this pattern in a large application code and at first glance it seems like it would be a lot of work to use the preprocessor or other mechanism to generate code that flang would accept.

bounds_unreachable.f90:
```Fortran
program main
 implicit none
 
  integer, parameter :: N = ONE_OR_TWO
  integer :: array(N)
 
  array(1) = 1
  if (N > 1) then
    array(2) = 2
 endif
 
  write(*,*) array
 
end program main
```

Compilation error:
```shell
>> flang-new -cpp -DONE_OR_TWO=1 bounds_unreachable.f90
error: Semantic errors in bounds_unreachable.f90
./bounds_unreachable.f90:9:5: error: Subscript 2 is greater than upper bound 1 for dimension 1 of array
      array(2) = 2
 ^^^^^^^^
./bounds_unreachable.f90:5:14: Declaration of 'array'
 integer :: array(N)
               ^^^^^
```

gfortran behavior:
```shell
>> gfortran -cpp -DONE_OR_TWO=1 bounds_unreachable.f90
bounds_unreachable.f90:9:10:

     array(2) = 2
          1
Warning: Array reference at (1) is out of bounds (2 > 1) in dimension 1
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJyUVU1v4zgM_TXKhWjgyHYTH3zoNg0wlymwO8AcC1qmbW1lyUvJDfLvF7KdJtOv2Q0MG5FE8vE9UkTvdWuJSpH_IfL9CsfQOS4r5dhqY2hVufpU_ugIKjLuCMrVBGSVG20g9oDgqUcbtAJidgyqI_UM2kJj0LY3lo5r-Bagx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