Skip to content

[flang] static bounds checking of unreachable code #82684

@bcornille

Description

@bcornille

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:

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:

>> 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:

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

Metadata

Metadata

Assignees

Labels

flang:frontendquestionA question, not bug report. Check out https://llvm.org/docs/GettingInvolved.html instead!

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions