Skip to content

Commit 973ca4e

Browse files
authored
[flang] Call finalization on empty type (llvm#66010)
According to 7.5.6.3 point 3, finalization occurs when > A nonpointer, nonallocatable object that is not a dummy argument or function result is finalized immediately before it would become undefined due to execution of a RETURN or END statement (19.6.6, item (3)). We were not calling the finalization on empty derived-type. There is no such restriction so this patch updates the code so the finalization is called for empty type as well.
1 parent e6e69f3 commit 973ca4e

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

flang/lib/Lower/ConvertVariable.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ static bool hasDefaultInitialization(const Fortran::semantics::Symbol &sym) {
8888

8989
// Does this variable have a finalization?
9090
static bool hasFinalization(const Fortran::semantics::Symbol &sym) {
91-
if (sym.has<Fortran::semantics::ObjectEntityDetails>() && sym.size())
91+
if (sym.has<Fortran::semantics::ObjectEntityDetails>())
9292
if (const Fortran::semantics::DeclTypeSpec *declTypeSpec = sym.GetType())
9393
if (const Fortran::semantics::DerivedTypeSpec *derivedTypeSpec =
9494
declTypeSpec->AsDerived())

flang/test/Lower/derived-type-finalization.f90

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ module derived_type_finalization
2323
type(t2) :: t
2424
end type
2525

26+
type t4
27+
contains
28+
final :: t4_final
29+
end type
30+
2631
contains
2732

2833
subroutine t1_final(this)
@@ -227,6 +232,17 @@ subroutine test_avoid_double_free()
227232
! CHECK: %[[RES_CONV:.*]] = fir.convert %[[RES]] : (!fir.ref<!fir.class<!fir.heap<!fir.array<?x!fir.type<_QMderived_type_finalizationTt1{a:i32}>>>>>) -> !fir.box<none>
228233
! CHECK: %{{.*}} = fir.call @_FortranADestroy(%[[RES_CONV]]) {{.*}} : (!fir.box<none>) -> none
229234

235+
subroutine t4_final(this)
236+
type(t4) :: this
237+
end subroutine
238+
239+
subroutine local_t4()
240+
type(t4) :: t
241+
end subroutine
242+
243+
! CHECK-LABEL: func.func @_QMderived_type_finalizationPlocal_t4()
244+
! CHECK: %{{.*}} = fir.call @_FortranADestroy(%2) fastmath<contract> : (!fir.box<none>) -> none
245+
230246
end module
231247

232248
program p

0 commit comments

Comments
 (0)