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

            Bug ID: 127732
           Summary: Wrong LBOUND of SELECT TYPE associate name when the
                    polymorphic selector is not a whole array
           Product: gcc
           Version: 16.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: fortran
          Assignee: unassigned at gcc dot gnu.org
          Reporter: ivan.pribec at gmail dot com
  Target Milestone: ---

The cases below demonstrates some discrepancies when an associate name in a
SELECT TYPE statement is not a whole array:

! Check bounds in select type statement with polymorphic selector
!
! Each line prints  lbound  ubound  size.  Expected:
!   case 1 (control):  2  4  3
!   cases 2 to 5:      1  3  3
!
! Assisted by: Claude Fable 5.1
module m
  implicit none
  real, target :: rt(2:4) = [20., 30., 40.]
contains
  function fa() result(r)                  ! CLASS(*) allocatable, bounds 2:4
    class(*), allocatable :: r(:)
    allocate (r, source=rt)
  end function
end module

program p
  use m
  implicit none
  character(*), parameter :: fmt = '(a,t44,3i4)'
  class(*), pointer :: q(:)

  q(2:) => rt

  ! 1. Pointer, whole array (control)
  select type (x => q)
  type is (real)
    print fmt, '1 pointer, whole array (control)', lbound(x), ubound(x),
size(x)
  end select

  ! 2. Array section of a pointer
  select type (x => q(:))
  type is (real)
    print fmt, '2 array section q(:)', lbound(x), ubound(x), size(x)
  end select

  ! 3. Parenthesized pointer (an expression)
  select type (x => (q))
  type is (real)
    print fmt, '3 parenthesized pointer (q)', lbound(x), ubound(x), size(x)
  end select

  ! 4. Allocatable function result
  select type (x => fa())
  type is (real)
    print fmt, '4 allocatable function result fa()', lbound(x), ubound(x),
size(x)
  end select

  ! 5. ASSOCIATE name of an allocatable function result
  associate (a => fa())
    select type (a)
    type is (real)
      print fmt, '5 ASSOCIATE (a => fa()), SELECT TYPE (a)', lbound(a),
ubound(a), size(a)
    end select
  end associate
end program

Results from gfortran:

1 pointer, whole array (control)              2   4   3
2 array section q(:)                          2   4   3
3 parenthesized pointer (q)                   2   4   3
4 allocatable function result fa()            2   4   3
5 ASSOCIATE (a => fa()), SELECT TYPE (a)      2   4   3

Results from flang:

1 pointer, whole array (control)              2   4   3
2 array section q(:)                          1   3   3
3 parenthesized pointer (q)                   1   3   3
4 allocatable function result fa()            1   3   3
5 ASSOCIATE (a => fa()), SELECT TYPE (a)      1   3   3

Results from ifx:

1 pointer, whole array (control)              2   4   3
2 array section q(:)                          1   3   3
3 parenthesized pointer (q)                   1   3   3
4 allocatable function result fa()            2   4   3
5 ASSOCIATE (a => fa()), SELECT TYPE (a)      1   3   3

Reply via email to