[flang-commits] [flang] [flang] Add warning and documentation for extension (PR #179708)

Peter Klausler via flang-commits flang-commits at lists.llvm.org
Wed Feb 4 08:39:14 PST 2026


https://github.com/klausler created https://github.com/llvm/llvm-project/pull/179708

When a pointer component has no default initialization in the derived type definition and does not appear explicitly as a component in a structure constructor, we assume that it is meant to be NULL() as an extension.  But there's no warning or documentation for this extension; add them.

Fixes https://github.com/llvm/llvm-project/issues/179580.

>From 412f685a0d2d3c589dacfc1a39639ecf544130f7 Mon Sep 17 00:00:00 2001
From: Peter Klausler <pklausler at nvidia.com>
Date: Wed, 4 Feb 2026 08:33:51 -0800
Subject: [PATCH] [flang] Add warning and documentation for extension

When a pointer component has no default initialization in the
derived type definition and does not appear explicitly as a
component in a structure constructor, we assume that it is
meant to be NULL() as an extension.  But there's no warning
or documentation for this extension; add them.

Fixes https://github.com/llvm/llvm-project/issues/179580.
---
 flang/docs/Extensions.md                       | 2 ++
 flang/include/flang/Support/Fortran-features.h | 3 ++-
 flang/lib/Semantics/expression.cpp             | 8 ++++++++
 flang/test/Semantics/bug179580                 | 9 +++++++++
 4 files changed, 21 insertions(+), 1 deletion(-)
 create mode 100644 flang/test/Semantics/bug179580

diff --git a/flang/docs/Extensions.md b/flang/docs/Extensions.md
index 493bb10af6f85..79e6213f7f3c9 100644
--- a/flang/docs/Extensions.md
+++ b/flang/docs/Extensions.md
@@ -481,6 +481,8 @@ end
 * A data object can be initialized multiple times by `DATA` statements
   and default component initialization, but only when all initializations
   are to the same value.  Distinct initializations remain errors.
+* A pointer component that has no default initialization or explicit value
+  in a structure constructor is defaulted to `NULL()`.
 
 ### Extensions supported when enabled by options
 
diff --git a/flang/include/flang/Support/Fortran-features.h b/flang/include/flang/Support/Fortran-features.h
index c705e031eff9d..fa300da48ffe1 100644
--- a/flang/include/flang/Support/Fortran-features.h
+++ b/flang/include/flang/Support/Fortran-features.h
@@ -56,7 +56,8 @@ ENUM_CLASS(LanguageFeature, BackslashEscapes, OldDebugLines,
     IgnoreIrrelevantAttributes, Unsigned, ContiguousOkForSeqAssociation,
     ForwardRefExplicitTypeDummy, InaccessibleDeferredOverride,
     CudaWarpMatchFunction, DoConcurrentOffload, TransferBOZ, Coarray,
-    PointerPassObject, MultipleIdenticalDATA)
+    PointerPassObject, MultipleIdenticalDATA,
+    DefaultStructConstructorNullPointer)
 
 // Portability and suspicious usage warnings
 ENUM_CLASS(UsageWarning, Portability, PointerToUndefinable,
diff --git a/flang/lib/Semantics/expression.cpp b/flang/lib/Semantics/expression.cpp
index fef6b4f0470dd..051b948ffb328 100644
--- a/flang/lib/Semantics/expression.cpp
+++ b/flang/lib/Semantics/expression.cpp
@@ -2431,6 +2431,14 @@ MaybeExpr ExpressionAnalyzer::CheckStructureConstructor(
         result.Add(symbol, Expr<SomeType>{ProcedureDesignator{**proc->init()}});
       } else if (IsAllocatableOrPointer(symbol)) {
         result.Add(symbol, Expr<SomeType>{NullPointer{}});
+        if (IsPointer(symbol)) {
+          AttachDeclaration(
+              Warn(common::LanguageFeature::DefaultStructConstructorNullPointer,
+                  typeName,
+                  "Structure constructor lacks a value for pointer component '%s', NULL() assumed"_warn_en_US,
+                  symbol.name()),
+              symbol);
+        }
       } else {
         AttachDeclaration(
             Say(typeName,
diff --git a/flang/test/Semantics/bug179580 b/flang/test/Semantics/bug179580
new file mode 100644
index 0000000000000..f3fcee8861cf5
--- /dev/null
+++ b/flang/test/Semantics/bug179580
@@ -0,0 +1,9 @@
+!RUN: %python %S/test_errors_py %s %flang_fc1 -pedantic -Werror
+type t
+  real v
+  integer, pointer :: p
+end type
+type(t), allocatable :: a
+!WARNING: Structure constructor lacks a value for pointer component 'p', NULL() assumed [-Wdefault-struct-constructor-null-pointer]
+allocate(a, source=t(v=3.3))
+end



More information about the flang-commits mailing list