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

            Bug ID: 121425
           Summary: Invalid derived type constructor using polymorphic
                    variable
           Product: gcc
           Version: 16.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: fortran
          Assignee: unassigned at gcc dot gnu.org
          Reporter: federico.perini at gmail dot com
  Target Milestone: ---

A default DT constructor called with a variable of the correct type, but
upstream polymorphic, returns wrong data. 

Fails with all versions of gfortran. I'm not sure about the standard
conformance of this, I defer to your comments... but it seems like it works
with other compilers (https://godbolt.org/z/PYcs5sGhP). 


```
module types
   use iso_fortran_env, only: rp => real64
   type :: vec
      real(rp) :: xyz(3) = 0
   end type vec
   type :: ax
      type(vec) :: p
      type(vec) :: n
   end type ax

   contains

   subroutine use_class(v)
      class(vec), intent(in) :: v ! no error if type(vec)

      type(ax) :: aa

      aa = ax(p=v,n=vec([1,0,0]))

      if (any(nint(aa%p%xyz)/=[1,2,3])) then 
         print *, 'n=',aa%n%xyz
         print *, 'p=',aa%p%xyz
         error stop 'invalid default constructor'
      endif  

   end subroutine use_class
end module types

program t
   use types
   type(vec) :: x
   x = vec([1,2,3])
   call use_class(x)
end program t   

```

It's important in use cases where overriding the default constructor with a
function would void defining `parameter`s on the DT.

Thank you, 
Federico
  • [Bug fortran/121425] New: In... federico.perini at gmail dot com via Gcc-bugs

Reply via email to