return;
}
+ /* Ensure that variables of derived or class type having a finalizer are
+ marked used even when the variable is not used anything else in the scope.
+ This fixes PR118730. */
+ if (sym->attr.flavor == FL_VARIABLE && !sym->attr.referenced
+ && (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS)
+ && gfc_may_be_finalized (sym->ts))
+ gfc_set_sym_referenced (sym);
+
if (sym->attr.flavor == FL_DERIVED && !resolve_fl_derived (sym))
return;
implicit none
type (tn), target :: b
class(ncBh), pointer :: bh
- class(ncBh), allocatable, dimension(:) :: t
+
allocate(b%cBh(1),source=defaultBhC)
b%cBh(1)%hostNode => b
! #1 this worked
! { dg-final { scan-tree-dump-times "foo \\(struct __class_MAIN___T_1_1t & restrict x, void \\* restrict caf_token.., integer\\(kind=\[48\]\\) caf_offset..\\)" 1 "original" } }
! { dg-final { scan-tree-dump-times "bar \\(struct __class_MAIN___T_1_1t \\* restrict x, void \\* restrict caf_token.., integer\\(kind=\[48\]\\) caf_offset..\\)" 1 "original" } }
! { dg-final { scan-tree-dump-times "bar \\(0B, 0B, 0\\);" 1 "original" } }
-! { dg-final { scan-tree-dump-times "foo \\(&class.., y._data.token, \\(integer\\(kind=\[48\]\\)\\) class..._data.data - \\(integer\\(kind=\[48\]\\)\\) y._data.data\\);" 1 "original" } }
+! { dg-final { scan-tree-dump-times "foo \\(&class.\[0-9\]+, y._data.token, \\(integer\\(kind=\[48\]\\)\\) class.\[0-9\]+._data.data - \\(integer\\(kind=\[48\]\\)\\) y._data.data\\);" 1 "original" } }
! { dg-final { scan-tree-dump-times "foo \\(struct __class_MAIN___T_1_1t & restrict x, void \\* restrict caf_token.., integer\\(kind=\[48\]\\) caf_offset..\\)" 1 "original" } }
! { dg-final { scan-tree-dump-times "bar \\(struct __class_MAIN___T_1_1t \\* restrict x, void \\* restrict caf_token.., integer\\(kind=\[48\]\\) caf_offset..\\)" 1 "original" } }
! { dg-final { scan-tree-dump-times "bar \\(0B, 0B, 0\\);" 1 "original" } }
-! { dg-final { scan-tree-dump-times "foo \\(&class.., y._data.token, \\(integer\\(kind=\[48\]\\)\\) class..._data.data - \\(integer\\(kind=\[48\]\\)\\) y._data.data\\);" 1 "original" } }
+! { dg-final { scan-tree-dump-times "foo \\(&class.\[0-9\]+, y._data.token, \\(integer\\(kind=\[48\]\\)\\) class.\[0-9\]+._data.data - \\(integer\\(kind=\[48\]\\)\\) y._data.data\\);" 1 "original" } }
--- /dev/null
+!{ dg-do run }
+!
+! Check that the finalizer is called on unused variables too.
+! Contributed by LXYAN <z00823823@outlook.com>
+
+module pr118730_mod
+ implicit none
+
+ logical :: finished = .FALSE.
+
+ type :: test_type
+ integer::test
+ contains
+ final :: finalize
+ end type test_type
+
+contains
+ subroutine finalize(this)
+ type(test_type), intent(inout) :: this
+ finished = .TRUE.
+ end subroutine finalize
+end module pr118730_mod
+
+program pr118730
+ use :: pr118730_mod
+ implicit none
+
+ block
+ type(test_type) :: test
+ end block
+
+ if (.NOT. finished) error stop 1
+end program pr118730