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

            Bug ID: 127731
           Summary: Wrong values from a pointer function result associated
                    with a complex part designator
           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: ---

Using the imaginary component of a complex array returned via a procedure
pointer, gives wrong results when the function is used as a value. 

This happens in an expression, an associate statement, as a procedure actual
argument, and when the result is assigned to an unlimited polymorphic pointer.
Pointer assignment works correctly. 

The error was found using Claude Fable. It works as expected with ifx and
flang. 

program p
  implicit none
  complex, target :: z(3) = [(1., 2.), (3., 4.), (5., 6.)]
  real,     pointer :: r(:)
  class(*), pointer :: q(:)

  print *, 'expression:      ', g()             ! 2. 4. 6.
  print *, 'sum:             ', sum(g())        ! 12.

  associate (a => g())
    print *, 'associate:       ', a             ! 2. 4. 6.
  end associate

  call show('actual argument: ', g())           ! 2. 4. 6.

  q => f()
  select type (q)
  type is (real)
    print *, 'CLASS(*) q => f():', q            ! 2. 4. 6.
  end select

  r => g()
  print *, 'REAL r => g():   ', r               ! 2. 4. 6. (works)

contains

  function g() result(res)
    real, pointer :: res(:)
    res => z%im
  end function

  function f() result(res)
    class(*), pointer :: res(:)
    res => z%im
  end function

  subroutine show(label, d)
    character(*), intent(in) :: label
    real, intent(in) :: d(:)
    print *, label, d
  end subroutine

end program

Reply via email to