https://gcc.gnu.org/bugzilla/show_bug.cgi?id=113245

            Bug ID: 113245
           Summary: SIZE with optional DIM argument that has the
                    OPTIONAL+VALUE attributes
           Product: gcc
           Version: 14.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: fortran
          Assignee: unassigned at gcc dot gnu.org
          Reporter: anlauf at gcc dot gnu.org
  Target Milestone: ---

Related to pr30865, but with optional that has the VALUE attribute.
Testcase:

program p
  implicit none
  real :: a(2,3)
  call ref (a,2) ! works
  call val (a,2) ! works
  print *, "--"
  call ref (a)   ! works
  call val (a)   ! fails
contains
  subroutine ref (x, d)
    real,              intent(in) :: x(:,:)
    integer, optional, intent(in) :: d
    print *, "present (d) =", present (d)
    print *, "size (a, d) =", size (x, dim=d)
  end
  subroutine val (x, d)
    real,              intent(in) :: x(:,:)
    integer, optional, value      :: d
    print *, "present (d) =", present (d)
    print *, "size (a, d) =", size (x, dim=d)  ! <<< miscompiled
  end
end

The dump-tree shows that the presence test is miscompiled, leading to
always accessing 'd', which is 0 for an absent argument, and causing an
invalid access.

Reply via email to