https://gcc.gnu.org/bugzilla/show_bug.cgi?id=118730
Bug ID: 118730
Summary: Final subroutine of derived type variable that is
declared but not used is not called
Product: gcc
Version: 14.2.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: fortran
Assignee: unassigned at gcc dot gnu.org
Reporter: z00823823 at outlook dot com
Target Milestone: ---
Dear maintainer:
I found the final procedure is not called when exit block if the derived type
variable is declared but not used. An example is:
```fortran
module poc
implicit none
type :: test_type
integer::test
contains
final :: finalize
end type test_type
contains
subroutine finalize(this)
type(test_type), intent(inout) :: this
print *, 'finalize'
end subroutine finalize
end module poc
program poc_test
use::poc
implicit none
block
type(test_type) :: test
print *, 'start'
end block
end program poc_test
```
the expected behaviour is the finalize subroutine is called, but with gfortran
14.2 the final string is not printed. By adding a `test%test = 1` (i.e. use the
variable) the finalize string is printed.
I'm not quite familiar with fortran grammar, but flang and ifx all print the
finalize string. Sorry in advance if this is some implementation
dependent/undefined behaviour. I tried to compile and run it with
`-fsanitize=undefined` in godbolt but it does not produce any error.
a online version with output result can be found at
https://godbolt.org/z/7n1Wan9hE