Skip to content

Commit

Permalink
[flang] Do not initialize intent(out) polymorphic pointer or allocatable
Browse files Browse the repository at this point in the history
Calling the runtime on disassociated pointer or unallocated
allocatable will trigger a segfault.

Reviewed By: PeteSteinfeld

Differential Revision: https://reviews.llvm.org/D144752
  • Loading branch information
clementval committed Feb 26, 2023
1 parent e168964 commit 287e9e9
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
5 changes: 4 additions & 1 deletion flang/lib/Lower/ConvertVariable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -600,7 +600,10 @@ mustBeDefaultInitializedAtRuntime(const Fortran::lower::pft::Variable &var) {
// Polymorphic intent(out) dummy might need default initialization
// at runtime.
if (Fortran::semantics::IsPolymorphic(sym) &&
Fortran::semantics::IsDummy(sym) && Fortran::semantics::IsIntentOut(sym))
Fortran::semantics::IsDummy(sym) &&
Fortran::semantics::IsIntentOut(sym) &&
!Fortran::semantics::IsAllocatable(sym) &&
!Fortran::semantics::IsPointer(sym))
return true;
// Local variables (including function results), and intent(out) dummies must
// be default initialized at runtime if their type has default initialization.
Expand Down
13 changes: 12 additions & 1 deletion flang/test/Lower/default-initialization.f90
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
! Test default initialization of local and dummy variables (dynamic initialization)
! RUN: bbc -emit-fir %s -o - | FileCheck %s
! RUN: bbc -emit-fir -polymorphic-type %s -o - | FileCheck %s

module test_dinit
type t
Expand Down Expand Up @@ -159,8 +159,19 @@ subroutine noinit_intentinout_dummy(x)
! CHECK: return
end subroutine


subroutine test_pointer_intentout(a, b)
type(t), pointer, intent(out) :: a
class(t), pointer, intent(out) :: b
end subroutine

! CHECK-LABEL: func.func @_QMtest_dinitPtest_pointer_intentout(
! CHECK-NOT: fir.call @_FortranAInitialize

end module

! CHECK-LABEL: func.func @_QQmain

! End-to-end test for debug pruposes.
use test_dinit
type(t) :: at
Expand Down

0 comments on commit 287e9e9

Please sign in to comment.