Skip to content

Commit 6577aa5

Browse files
authored
[flang] Fix proc ptr default initializers in structure constructors (llvm#178897)
The default initializers for procedure pointer components are not being used for unspecified components in structure constructors. Fixes llvm#178813.
1 parent 55ee00e commit 6577aa5

2 files changed

Lines changed: 22 additions & 14 deletions

File tree

flang/lib/Semantics/expression.cpp

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2421,22 +2421,22 @@ MaybeExpr ExpressionAnalyzer::CheckStructureConstructor(
24212421
for (const Symbol &symbol : components) {
24222422
if (!symbol.test(Symbol::Flag::ParentComp) &&
24232423
unavailable.find(symbol.name()) == unavailable.cend()) {
2424-
if (IsAllocatable(symbol)) {
2425-
// Set all remaining allocatables to explicit NULL().
2424+
if (const auto *object{
2425+
symbol.detailsIf<semantics::ObjectEntityDetails>()};
2426+
object && object->init()) {
2427+
result.Add(symbol, common::Clone(*object->init()));
2428+
} else if (const auto *proc{
2429+
symbol.detailsIf<semantics::ProcEntityDetails>()};
2430+
proc && proc->init() && *proc->init()) {
2431+
result.Add(symbol, Expr<SomeType>{ProcedureDesignator{**proc->init()}});
2432+
} else if (IsAllocatableOrPointer(symbol)) {
24262433
result.Add(symbol, Expr<SomeType>{NullPointer{}});
24272434
} else {
2428-
const auto *object{symbol.detailsIf<semantics::ObjectEntityDetails>()};
2429-
if (object && object->init()) {
2430-
result.Add(symbol, common::Clone(*object->init()));
2431-
} else if (IsPointer(symbol)) {
2432-
result.Add(symbol, Expr<SomeType>{NullPointer{}});
2433-
} else if (object) { // C799
2434-
AttachDeclaration(
2435-
Say(typeName,
2436-
"Structure constructor lacks a value for component '%s'"_err_en_US,
2437-
symbol.name()),
2438-
symbol);
2439-
}
2435+
AttachDeclaration(
2436+
Say(typeName,
2437+
"Structure constructor lacks a value for component '%s'"_err_en_US,
2438+
symbol.name()),
2439+
symbol);
24402440
}
24412441
}
24422442
}

flang/test/Semantics/bug178813.f90

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
!RUN: %flang_fc1 -fdebug-unparse %s 2>&1 | FileCheck %s
2+
external s
3+
type t
4+
procedure(), nopass, pointer :: p => s
5+
end type
6+
!CHECK: TYPE(t) :: x = t(p=s)
7+
type(t) :: x = t()
8+
end

0 commit comments

Comments
 (0)