blob: 51247094a1ce831715eebc2380a76fc65e5e0cec [file] [edit]
! RUN: %python %S/test_errors.py %s %flang_fc1
! A component of derived type whose type has default initialization has
! default initialization (F'2023 7.5.4.6 p8), so a structure constructor
! need not provide a value for it (C7104, 7.5.10 p3). flang gives no default
! value to a component whose type has only partial default initialization, so
! a value must still be provided for such a component.
module m
type inner
integer :: x = 1
end type
type outer
type(inner) :: comp
end type
type outer2
type(inner) :: comps(3)
real :: r = 2.
end type
type nested
type(outer) :: o
end type
type partial
integer :: y = 3
integer :: z
end type
type haspartial
type(partial) :: p
end type
type, extends(inner) :: child
integer :: w = 4
end type
type haschild
type(child) :: c
end type
type noinit
integer :: n
end type
type hasnoinit
type(noinit) :: comp
end type
type onlyalloc
integer, allocatable :: a(:)
end type
type hasonlyalloc
type(onlyalloc) :: comp
end type
type onlyptr
integer, pointer :: p
end type
type hasonlyptr
type(onlyptr) :: comp
end type
type initptr
integer, pointer :: p => null()
end type
type hasinitptr
type(initptr) :: comp
end type
type nestedpartial
type(haspartial) :: hp
integer :: n = 1
end type
type(outer), parameter :: pc = outer() ! ok
!ERROR: Structure constructor lacks a value for component 'p'
type(haspartial), parameter :: pd = haspartial()
type holder
type(outer) :: ho = outer() ! ok
!ERROR: Structure constructor lacks a value for component 'p'
type(haspartial) :: hd = haspartial()
end type
contains
subroutine s
type(outer) :: a
type(outer2) :: b
type(nested) :: c
type(haspartial) :: d
type(haschild) :: e
type(hasnoinit) :: f
type(hasonlyalloc) :: g
type(hasonlyptr) :: h
type(hasinitptr) :: i
type(nestedpartial) :: j
a = outer() ! ok
b = outer2() ! ok
b = outer2(r=3.) ! ok
c = nested() ! ok
e = haschild() ! ok
i = hasinitptr() ! ok; a pointer initialization is default initialization
!ERROR: Structure constructor lacks a value for component 'p'
d = haspartial()
!ERROR: Structure constructor lacks a value for component 'hp'
j = nestedpartial()
!ERROR: Structure constructor lacks a value for component 'comp'
f = hasnoinit()
! An allocatable component, or a pointer component without
! initialization, is not default initialization
!ERROR: Structure constructor lacks a value for component 'comp'
g = hasonlyalloc()
!ERROR: Structure constructor lacks a value for component 'comp'
h = hasonlyptr()
end subroutine
end module