From a2f442c4f772a98ecf45284a026b3328c03b51c4 Mon Sep 17 00:00:00 2001 From: Peter Klausler Date: Fri, 9 May 2025 15:17:52 -0700 Subject: [PATCH] [flang] Catch deferred type parameters in ALLOCATE(type-spec::) The type-spec in ALLOCATE may not have any deferred type parameters. Fixes https://github.com/llvm/llvm-project/issues/138979. --- flang/lib/Semantics/check-allocate.cpp | 10 ++++++++-- flang/test/Semantics/allocate01.f90 | 4 ++++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/flang/lib/Semantics/check-allocate.cpp b/flang/lib/Semantics/check-allocate.cpp index b426dd81334bb..2c215f45bf516 100644 --- a/flang/lib/Semantics/check-allocate.cpp +++ b/flang/lib/Semantics/check-allocate.cpp @@ -116,13 +116,19 @@ static std::optional CheckAllocateOptions( // C937 if (auto it{FindCoarrayUltimateComponent(*derived)}) { context - .Say("Type-spec in ALLOCATE must not specify a type with a coarray" - " ultimate component"_err_en_US) + .Say( + "Type-spec in ALLOCATE must not specify a type with a coarray ultimate component"_err_en_US) .Attach(it->name(), "Type '%s' has coarray ultimate component '%s' declared here"_en_US, info.typeSpec->AsFortran(), it.BuildResultDesignatorName()); } } + if (auto dyType{evaluate::DynamicType::From(*info.typeSpec)}) { + if (dyType->HasDeferredTypeParameter()) { + context.Say( + "Type-spec in ALLOCATE must not have a deferred type parameter"_err_en_US); + } + } } const parser::Expr *parserSourceExpr{nullptr}; diff --git a/flang/test/Semantics/allocate01.f90 b/flang/test/Semantics/allocate01.f90 index a66e2467cbe4e..a10a7259ae94f 100644 --- a/flang/test/Semantics/allocate01.f90 +++ b/flang/test/Semantics/allocate01.f90 @@ -62,6 +62,7 @@ subroutine bar() real, pointer, save :: okp3 real, allocatable, save :: oka3, okac4[:,:] real, allocatable :: okacd5(:, :)[:] + character(:), allocatable :: chvar !ERROR: Name in ALLOCATE statement must be a variable name allocate(foo) @@ -102,6 +103,8 @@ subroutine bar() allocate(edc9%nok) !ERROR: Entity in ALLOCATE statement must have the ALLOCATABLE or POINTER attribute allocate(edc10) + !ERROR: Type-spec in ALLOCATE must not have a deferred type parameter + allocate(character(:) :: chvar) ! No errors expected below: allocate(a_var) @@ -117,4 +120,5 @@ subroutine bar() allocate(edc9%ok(4)) allocate(edc10%ok) allocate(rp) + allocate(character(123) :: chvar) end subroutine