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; }; 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/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/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