[flang-commits] [flang] 874ef00 - [Flang] Fix crash in structure constructor lowering for PDT (#183543)
via flang-commits
flang-commits at lists.llvm.org
Sun Mar 8 22:23:27 PDT 2026
Author: jay0x
Date: 2026-03-09T10:53:22+05:30
New Revision: 874ef0073aba2498516b06c8f2ec91a14e4d4336
URL: https://github.com/llvm/llvm-project/commit/874ef0073aba2498516b06c8f2ec91a14e4d4336
DIFF: https://github.com/llvm/llvm-project/commit/874ef0073aba2498516b06c8f2ec91a14e4d4336.diff
LOG: [Flang] Fix crash in structure constructor lowering for PDT (#183543)
Fixes - [#181278](https://github.com/llvm/llvm-project/issues/181278)
This patch fixes a crash in Flang when parsing array constructors like:
`[ty0(2)(4)]`
The current implementation parses this as a type constructor ty0(2),
followed by what appears to be another call (4), instead of rejecting it
as invalid syntax. The lowering of` StructureConstructor` attempts to
retrieve the parent derived type using `sym->owner().derivedTypeSpec()`,
which return `nullptr` for PDT cases and lead to a crash.
In` flang/lib/Lower/ConvertConstant.cpp`, a safeguard is being added
which ensures that we fall back to the constructor’s derived type
specification when the parent type cannot be obtained, preventing the
null dereference and eliminating the crash. This change addresses only
the immediate crash, proper diagnostic handling for this invalid syntax
is still pending and remains as TODO.
---------
Co-authored-by: Jay Satish Kumar Patel <kumarpat at pe31.hpc.amslabs.hpecorp.net>
Added:
flang/test/Lower/pdt-struct-constructor-init.f90
Modified:
flang/lib/Lower/ConvertConstant.cpp
Removed:
################################################################################
diff --git a/flang/lib/Lower/ConvertConstant.cpp b/flang/lib/Lower/ConvertConstant.cpp
index c44b9e8a3796b..f786673c606fc 100644
--- a/flang/lib/Lower/ConvertConstant.cpp
+++ b/flang/lib/Lower/ConvertConstant.cpp
@@ -532,6 +532,12 @@ static mlir::Value genInlinedStructureCtorLitImpl(
for (const auto &[sym, expr] : ctor.values()) {
const Fortran::semantics::DerivedTypeSpec *componentParentType =
sym->owner().derivedTypeSpec();
+ // TODO: This is not a complete fix. For some parameterized derived type
+ // component initializations, the component symbol owner does not have a
+ // derived type spec. Falling back to ctor.derivedTypeSpec() avoids the
+ // crash, but may not always represent the correct parent type.
+ if (!componentParentType)
+ TODO(loc, "parameterized derived types");
assert(componentParentType && "failed to retrieve component parent type");
if (!res) {
mlir::Type parentType = converter.genType(*componentParentType);
diff --git a/flang/test/Lower/pdt-struct-constructor-init.f90 b/flang/test/Lower/pdt-struct-constructor-init.f90
new file mode 100644
index 0000000000000..cffbc03d46a0f
--- /dev/null
+++ b/flang/test/Lower/pdt-struct-constructor-init.f90
@@ -0,0 +1,18 @@
+! RUN: not bbc -emit-hlfir %s 2>&1 | FileCheck %s
+
+program main
+ type ty0(k)
+ integer,kind::k
+ integer :: ii
+ end type
+
+ type ty(k,l)
+ integer,kind::k
+ integer,len ::l
+ type(ty0(2)) :: cmp(1) = [ty0(2)(4)]
+ end type
+
+ type(ty(2,4)) :: obj
+end program
+
+! CHECK: not yet implemented: parameterized derived types
More information about the flang-commits
mailing list