[flang-commits] [PATCH] D112740: [flang] Improve error message for misuse of NULL(mold) as data statement constant

Peter Klausler via Phabricator via flang-commits flang-commits at lists.llvm.org
Thu Oct 28 11:14:08 PDT 2021


klausler created this revision.
klausler added a reviewer: schweitz.
klausler added a project: Flang.
Herald added a subscriber: jdoerfert.
klausler requested review of this revision.

While "null()" is accepted as a data statement constant when it
corresponds to a pointer object, "null(mold=p)" and "null(p)"
are not allowed.  The current error messages simply complain
that null is not an array.  This patch adds a context-sensitive
message to the effect that a data statement constant followed
by non-empty parentheses must be an array or structure constructor.

(Note that f18 can't simply special-case the name "null" when parsing
data statement constants, since programs are free to repurpose that
name as an array or derived type.)


https://reviews.llvm.org/D112740

Files:
  flang/include/flang/Semantics/expression.h
  flang/lib/Semantics/expression.cpp
  flang/test/Semantics/data01.f90
  flang/test/Semantics/null-init.f90


Index: flang/test/Semantics/null-init.f90
===================================================================
--- flang/test/Semantics/null-init.f90
+++ flang/test/Semantics/null-init.f90
@@ -73,3 +73,25 @@
   !ERROR: An initial data target must be a designator with constant subscripts
   data d2/null()/
 end module
+
+subroutine m10
+  real, pointer :: x, y
+  !ERROR: 'null' must be an array or structure constructor if used with non-empty parentheses as a DATA statement constant
+  data x/null(y)/
+end
+
+subroutine m11
+  type :: null
+    integer :: mold
+  end type
+  type(null) :: obj(2)
+  integer, parameter :: j = 0
+  data obj/null(mold=j), null(j)/ ! both fine
+end subroutine
+
+subroutine m12
+  integer, parameter :: j = 1
+  integer, target, save :: null(1)
+  integer, pointer :: p
+  data p/null(j)/ ! ok
+end subroutine
Index: flang/test/Semantics/data01.f90
===================================================================
--- flang/test/Semantics/data01.f90
+++ flang/test/Semantics/data01.f90
@@ -47,7 +47,7 @@
   !OK: constant structure constructor
   data myname(1) / person(1, 'Abcd Ijkl') /
   !C883
-  !ERROR: 'persn' is not an array
+  !ERROR: 'persn' must be an array or structure constructor if used with non-empty parentheses as a DATA statement constant
   data myname(2) / persn(2, 'Abcd Efgh') /
   !C884
   !ERROR: DATA statement value 'person(age=myage,name="Abcd Ijkl                ")' for 'myname(3_8)%age' is not a constant
Index: flang/lib/Semantics/expression.cpp
===================================================================
--- flang/lib/Semantics/expression.cpp
+++ flang/lib/Semantics/expression.cpp
@@ -937,7 +937,13 @@
     } else if (baseExpr->Rank() == 0) {
       if (const Symbol * symbol{GetLastSymbol(*baseExpr)}) {
         if (!context_.HasError(symbol)) {
-          Say("'%s' is not an array"_err_en_US, symbol->name());
+          if (inDataStmtConstant_) {
+            // Better error for NULL(X) with a MOLD= argument
+            Say("'%s' must be an array or structure constructor if used with non-empty parentheses as a DATA statement constant"_err_en_US,
+                symbol->name());
+          } else {
+            Say("'%s' is not an array"_err_en_US, symbol->name());
+          }
           context_.SetError(*symbol);
         }
       }
@@ -2947,6 +2953,7 @@
 }
 
 MaybeExpr ExpressionAnalyzer::Analyze(const parser::DataStmtConstant &x) {
+  auto restorer{common::ScopedSet(inDataStmtConstant_, true)};
   return ExprOrVariable(x, x.source);
 }
 
Index: flang/include/flang/Semantics/expression.h
===================================================================
--- flang/include/flang/Semantics/expression.h
+++ flang/include/flang/Semantics/expression.h
@@ -372,6 +372,7 @@
   bool isWholeAssumedSizeArrayOk_{false};
   bool useSavedTypedExprs_{true};
   bool inWhereBody_{false};
+  bool inDataStmtConstant_{false};
   friend class ArgumentAnalyzer;
 };
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D112740.383096.patch
Type: text/x-patch
Size: 2955 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/flang-commits/attachments/20211028/259f0f90/attachment.bin>


More information about the flang-commits mailing list