flang
flang copied to clipboard
Explicitly initialized pointers should acquire the SAVE attribute implicitly
Section 8.4 ("Initialization") of the Fortran 2018 standard states:
If
null-initappears, the initial association status of the object is disassociated. Ifinitial-data-targetappears, the object is initially associated with the target.Explicit initialization of a variable that is not in a common block implies the SAVE attribute, which may be confirmed by explicit specification.
Flang supports pointer initialization, but does not give such pointers the SAVE attribute, as shown by the example below:
program test_pointer_init
call test_sub() ! expected "F", got "F"
call test_sub() ! expected "T", got "F"
contains
subroutine test_sub()
implicit none
integer, pointer :: ptr => NULL()
logical :: initialized
initialized = associated(ptr)
if (.not. initialized) allocate(ptr)
print *, initialized
end subroutine
end program
The programmer can work around this by explicitly adding the SAVE attribute to the declaration of ptr.