[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@pe31.hpc.amslabs.hpecorp.net>
This commit is contained in:
jay0x 2026-03-09 10:53:22 +05:30 committed by GitHub
parent e30f9c1946
commit 874ef0073a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 24 additions and 0 deletions

View File

@ -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);

View File

@ -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