[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
This commit is contained in:
peter klausler
2021-10-25 13:25:34 -07:00
committed by Peter Klausler
parent 6eb38e5171
commit a94b721d26
4 changed files with 32 additions and 2 deletions

View File

@@ -372,6 +372,7 @@ private:
bool isWholeAssumedSizeArrayOk_{false};
bool useSavedTypedExprs_{true};
bool inWhereBody_{false};
bool inDataStmtConstant_{false};
friend class ArgumentAnalyzer;
};

View File

@@ -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);
}

View File

@@ -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

View File

@@ -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