Skip to content

[flang] Process pointer component default initializers sooner #145601

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions flang/docs/Extensions.md
Original file line number Diff line number Diff line change
Expand Up @@ -875,6 +875,13 @@ print *, [(j,j=1,10)]
compilers) and don't use any defined unformatted WRITE that might have been
defined.

* Forward references to target objects are allowed to appear
in the initializers of data pointer declarationss.
Forward references to target objects are not accepted in the default
initializers of derived type component declarations, however,
since these default values need to be available to process incomplete
structure constructors.

## De Facto Standard Features

* `EXTENDS_TYPE_OF()` returns `.TRUE.` if both of its arguments have the
Expand Down
19 changes: 9 additions & 10 deletions flang/lib/Semantics/resolve-names.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5383,7 +5383,7 @@ void DeclarationVisitor::Post(const parser::EntityDecl &x) {
ConvertToObjectEntity(symbol) || ConvertToProcEntity(symbol);
symbol.set(
Symbol::Flag::EntryDummyArgument, false); // forestall excessive errors
Initialization(name, *init, false);
Initialization(name, *init, /*inComponentDecl=*/false);
} else if (attrs.test(Attr::PARAMETER)) { // C882, C883
Say(name, "Missing initialization for parameter '%s'"_err_en_US);
}
Expand Down Expand Up @@ -6398,7 +6398,7 @@ void DeclarationVisitor::Post(const parser::ComponentDecl &x) {
SetCUDADataAttr(name.source, symbol, cudaDataAttr());
if (symbol.has<ObjectEntityDetails>()) {
if (auto &init{std::get<std::optional<parser::Initialization>>(x.t)}) {
Initialization(name, *init, true);
Initialization(name, *init, /*inComponentDecl=*/true);
}
}
currScope().symbol()->get<DerivedTypeDetails>().add_component(symbol);
Expand Down Expand Up @@ -8933,9 +8933,13 @@ void DeclarationVisitor::Initialization(const parser::Name &name,
// Defer analysis to the end of the specification part
// so that forward references and attribute checks like SAVE
// work better.
auto restorer{common::ScopedSet(deferImplicitTyping_, true)};
Walk(target);
ultimate.set(Symbol::Flag::InDataStmt);
if (inComponentDecl) {
PointerInitialization(name, target);
} else {
auto restorer{common::ScopedSet(deferImplicitTyping_, true)};
Walk(target);
ultimate.set(Symbol::Flag::InDataStmt);
}
},
[&](const std::list<Indirection<parser::DataStmtValue>> &values) {
// Handled later in data-to-inits conversion
Expand Down Expand Up @@ -10355,11 +10359,6 @@ class DeferredCheckVisitor {
std::get<std::optional<parser::Initialization>>(decl.t));
return false;
}
bool Pre(const parser::ComponentDecl &decl) {
Init(std::get<parser::Name>(decl.t),
std::get<std::optional<parser::Initialization>>(decl.t));
return false;
}
bool Pre(const parser::ProcDecl &decl) {
if (const auto &init{
std::get<std::optional<parser::ProcPointerInit>>(decl.t)}) {
Expand Down
13 changes: 13 additions & 0 deletions flang/test/Semantics/bug1056.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
! RUN: %flang_fc1 -fdebug-unparse %s | FileCheck %s
program bug
integer, target :: ita(2) = [1,2], itb(2) = [3,4], itc(2) = [5,6]
type t1
integer, pointer :: p1(:) => ita, p2(:) => itb
end type
type t2
!CHECK: TYPE(t1) :: comp = t1(p1=itc,p2=itb)
type(t1) :: comp = t1(itc)
end type
integer, pointer :: p3(:) => itd
integer, target :: itd(2) = [7,8]
end
19 changes: 11 additions & 8 deletions flang/test/Semantics/symbol15.f90
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ subroutine iface
!DEF: /m/pp6 EXTERNAL, POINTER, PUBLIC (Subroutine) ProcEntity
!DEF: /m/modproc1 PUBLIC (Subroutine) Subprogram
procedure(iface), pointer :: pp6 => modproc1
!DEF: /m/xx PUBLIC, TARGET ObjectEntity REAL(4)
!DEF: /m/yy PUBLIC, TARGET ObjectEntity REAL(4)
real, target :: xx, yy(2)
!DEF: /m/t1 PUBLIC DerivedType
type :: t1
!DEF: /m/t1/opc1 POINTER ObjectEntity REAL(4)
Expand All @@ -51,11 +54,11 @@ subroutine iface
!REF: /m/null
real, pointer :: opc2 => null()
!DEF: /m/t1/opc3 POINTER ObjectEntity REAL(4)
!REF: /m/x
real, pointer :: opc3 => x
!REF: /m/xx
real, pointer :: opc3 => xx
!DEF: /m/t1/opc4 POINTER ObjectEntity REAL(4)
!REF: /m/y
real, pointer :: opc4 => y(1)
!REF: /m/yy
real, pointer :: opc4 => yy(1)
!REF: /m/iface
!DEF: /m/t1/ppc1 NOPASS, POINTER (Subroutine) ProcEntity
procedure(iface), nopass, pointer :: ppc1
Expand Down Expand Up @@ -101,12 +104,12 @@ subroutine iface
!REF: /m/null
real, pointer :: opc2 => null()
!DEF: /m/pdt1/opc3 POINTER ObjectEntity REAL(4)
!REF: /m/x
real, pointer :: opc3 => x
!REF: /m/xx
real, pointer :: opc3 => xx
!DEF: /m/pdt1/opc4 POINTER ObjectEntity REAL(4)
!REF: /m/y
!REF: /m/yy
!REF: /m/pdt1/k
real, pointer :: opc4 => y(k)
real, pointer :: opc4 => yy(k)
!REF: /m/iface
!DEF: /m/pdt1/ppc1 NOPASS, POINTER (Subroutine) ProcEntity
procedure(iface), nopass, pointer :: ppc1
Expand Down
11 changes: 11 additions & 0 deletions t.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
integer, target :: ita(2) = [1,2], itb(2) = [3,4], itc(2) = [5,6]
type t1
integer, pointer :: p1(:) => ita, p2(:) => itb
end type
type t2
type(t1) :: comp = t1(itc)
end type
type(t2) :: var
print *, var%comp%p2
var%comp = t1(itc)
end
Loading