[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 15:20:54 PDT 2021


This revision was automatically updated to reflect the committed changes.
Closed by commit rGa94b721d2607: [flang] Improve error message for misuse of NULL(mold) as data statement… (authored by klausler).
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D112740/new/

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.383176.patch
Type: text/x-patch
Size: 2955 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/flang-commits/attachments/20211028/8523f0fb/attachment-0001.bin>


More information about the flang-commits mailing list