Skip to content

[flang] Catch usage of : and * lengths in array c'tors #128974

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 27, 2025

Conversation

klausler
Copy link
Contributor

@klausler klausler commented Feb 27, 2025

The definition of an array constructor doesn't preclude 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 #128755.

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 llvm#128755.
@llvmbot llvmbot added flang Flang issues not falling into any other category flang:semantics labels Feb 27, 2025
@llvmbot
Copy link
Member

llvmbot commented Feb 27, 2025

@llvm/pr-subscribers-flang-semantics

Author: Peter Klausler (klausler)

Changes

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 #128755.


Full diff: https://github.com/llvm/llvm-project/pull/128974.diff

2 Files Affected:

  • (modified) flang/lib/Semantics/expression.cpp (+4-2)
  • (modified) flang/test/Semantics/array-constr-len.f90 (+4)
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

Copy link
Contributor

@DanielCChen DanielCChen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.
Thanks for the fix!

@klausler klausler merged commit c6dd9f4 into llvm:main Feb 27, 2025
14 checks passed
@klausler klausler deleted the bug128755 branch February 27, 2025 22:32
cheezeburglar pushed a commit to cheezeburglar/llvm-project that referenced this pull request Feb 28, 2025
The definition of an array constructor doesn't preclude 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 llvm#128755.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
flang:semantics flang Flang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Flang] Missing diagnostic on type-spec with deferred length being used in array constructor
4 participants