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

Peter Klausler via flang-commits flang-commits at lists.llvm.org
Thu Oct 28 15:20:50 PDT 2021


Author: peter klausler
Date: 2021-10-28T15:20:41-07:00
New Revision: a94b721d2607ee2eda9942c892160e89bd284cb2

URL: https://github.com/llvm/llvm-project/commit/a94b721d2607ee2eda9942c892160e89bd284cb2
DIFF: https://github.com/llvm/llvm-project/commit/a94b721d2607ee2eda9942c892160e89bd284cb2.diff

LOG: [flang] Improve error message for misuse of NULL(mold) as data statement constant

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.)

Differential Revision: https://reviews.llvm.org/D112740

Added: 
    

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

Removed: 
    


################################################################################
diff  --git a/flang/include/flang/Semantics/expression.h b/flang/include/flang/Semantics/expression.h
index 74e1b9614aeb5..203a88937728a 100644
--- a/flang/include/flang/Semantics/expression.h
+++ b/flang/include/flang/Semantics/expression.h
@@ -372,6 +372,7 @@ class ExpressionAnalyzer {
   bool isWholeAssumedSizeArrayOk_{false};
   bool useSavedTypedExprs_{true};
   bool inWhereBody_{false};
+  bool inDataStmtConstant_{false};
   friend class ArgumentAnalyzer;
 };
 

diff  --git a/flang/lib/Semantics/expression.cpp b/flang/lib/Semantics/expression.cpp
index b217c81f31a37..331b9b2cf5bc3 100644
--- a/flang/lib/Semantics/expression.cpp
+++ b/flang/lib/Semantics/expression.cpp
@@ -937,7 +937,13 @@ MaybeExpr ExpressionAnalyzer::Analyze(const parser::ArrayElement &ae) {
     } 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::Selector &selector) {
 }
 
 MaybeExpr ExpressionAnalyzer::Analyze(const parser::DataStmtConstant &x) {
+  auto restorer{common::ScopedSet(inDataStmtConstant_, true)};
   return ExprOrVariable(x, x.source);
 }
 

diff  --git a/flang/test/Semantics/data01.f90 b/flang/test/Semantics/data01.f90
index f8c8d379ec981..0bc50056ece81 100644
--- a/flang/test/Semantics/data01.f90
+++ b/flang/test/Semantics/data01.f90
@@ -47,7 +47,7 @@ subroutine CheckValue
   !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

diff  --git a/flang/test/Semantics/null-init.f90 b/flang/test/Semantics/null-init.f90
index a1ac29ed39974..53c1b0f95f54d 100644
--- a/flang/test/Semantics/null-init.f90
+++ b/flang/test/Semantics/null-init.f90
@@ -73,3 +73,25 @@ module m9b
   !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


        


More information about the flang-commits mailing list