[flang-commits] [flang] [flang] Catch usage of : and * lengths in array c'tors (PR #128974)
Peter Klausler via flang-commits
flang-commits at lists.llvm.org
Wed Feb 26 16:18:34 PST 2025
https://github.com/klausler created https://github.com/llvm/llvm-project/pull/128974
The definition of an array constructor doesn't prelude the use of [character(:)::] or [character(*)::] directly, but there is language elsewhere in the standard that restricts their use to specific contexts, neither of which include explicitly typed array constructors.
Fixes https://github.com/llvm/llvm-project/issues/128755.
>From 39b0f5be36f2a016a64206e4d0c5df18b4f8b6d3 Mon Sep 17 00:00:00 2001
From: Peter Klausler <pklausler at nvidia.com>
Date: Wed, 26 Feb 2025 16:14:33 -0800
Subject: [PATCH] [flang] Catch usage of : and * lengths in array c'tors
The definition of an array constructor doesn't prelude the
use of [character(:)::] or [character(*)::] directly, but
there is language elsewhere in the standard that restricts
their use to specific contexts, neither of which include
explicitly typed array constructors.
Fixes https://github.com/llvm/llvm-project/issues/128755.
---
flang/lib/Semantics/expression.cpp | 6 ++++--
flang/test/Semantics/array-constr-len.f90 | 4 ++++
2 files changed, 8 insertions(+), 2 deletions(-)
diff --git a/flang/lib/Semantics/expression.cpp b/flang/lib/Semantics/expression.cpp
index 6949e5693d08f..2aeb8aabf5ddb 100644
--- a/flang/lib/Semantics/expression.cpp
+++ b/flang/lib/Semantics/expression.cpp
@@ -78,8 +78,10 @@ static std::optional<DynamicTypeWithLength> AnalyzeTypeSpec(
const semantics::CharacterTypeSpec &cts{
typeSpec->characterTypeSpec()};
const semantics::ParamValue &len{cts.length()};
- // N.B. CHARACTER(LEN=*) is allowed in type-specs in ALLOCATE() &
- // type guards, but not in array constructors.
+ if (len.isAssumed() || len.isDeferred()) {
+ context.messages().Say(
+ "A length specifier of '*' or ':' may not appear in the type of an array constructor"_err_en_US);
+ }
DynamicTypeWithLength type{DynamicType{kind, len}};
if (auto lenExpr{type.LEN()}) {
type.length = Fold(context,
diff --git a/flang/test/Semantics/array-constr-len.f90 b/flang/test/Semantics/array-constr-len.f90
index 4de9c76c7041c..9b23026a16012 100644
--- a/flang/test/Semantics/array-constr-len.f90
+++ b/flang/test/Semantics/array-constr-len.f90
@@ -11,4 +11,8 @@ subroutine subr(s,n)
print *, [(s(1:1),j=1,0)] ! ok
print *, [character(2)::(s(1:n),j=1,0)] ! ok
print *, [character(n)::(s(1:n),j=1,0)]
+ !ERROR: A length specifier of '*' or ':' may not appear in the type of an array constructor
+ print *, [ character(:) :: ]
+ !ERROR: A length specifier of '*' or ':' may not appear in the type of an array constructor
+ print *, [ character(*) :: ]
end
More information about the flang-commits
mailing list