Index: flang/lib/Evaluate/intrinsics.cpp =================================================================== --- flang/lib/Evaluate/intrinsics.cpp +++ flang/lib/Evaluate/intrinsics.cpp @@ -1462,6 +1462,36 @@ return true; } +static void CheckMaxMinA1A2Argument(const ActualArguments &arguments, + std::set &set, + parser::ContextualMessages &messages) { + parser::CharBlock kwA1{"a1", 2}; + parser::CharBlock kwA2{"a2", 2}; + bool missingA1 = set.find(kwA1) == set.end(); + bool missingA2 = set.find(kwA2) == set.end(); + + if (arguments[0]->keyword()) { + // If the keyword is specified in the first argument, the following + // arguments must have the keywords. + if (missingA1 && missingA2) { + messages.Say("missing mandatory '%s=' and '%s=' arguments"_err_en_US, + kwA1.ToString(), kwA2.ToString()); + } else if (missingA1 && !missingA2) { + messages.Say("missing mandatory '%s=' argument"_err_en_US, + kwA1.ToString()); + } else if (!missingA1 && missingA2) { + messages.Say("missing mandatory '%s=' argument"_err_en_US, + kwA2.ToString()); + } + } else if (arguments[1]->keyword()) { + // No keyword is specified in the first argument. + if (missingA1 && missingA2) { + messages.Say("missing mandatory '%s=' argument"_err_en_US, + kwA2.ToString()); + } + } +} + static bool CheckAtomicKind(const ActualArgument &arg, const semantics::Scope *builtinsScope, parser::ContextualMessages &messages) { @@ -1523,6 +1553,11 @@ } else if (isMaxMin) { if (CheckMaxMinArgument(arg->keyword(), maxMinKeywords, name, messages)) { actualForDummy.push_back(&*arg); + + // max(x)/min(x) is invalid + if (arguments.size() < 2) { + actualForDummy.push_back(nullptr); + } } else { return std::nullopt; } @@ -1570,6 +1605,10 @@ } } + if (isMaxMin) { + CheckMaxMinA1A2Argument(arguments, maxMinKeywords, messages); + } + std::size_t dummies{actualForDummy.size()}; // Check types and kinds of the actual arguments against the intrinsic's @@ -1590,7 +1629,22 @@ const ActualArgument *arg{actualForDummy[j]}; if (!arg) { if (d.optionality == Optionality::required) { - messages.Say("missing mandatory '%s=' argument"_err_en_US, d.keyword); + std::string kw{d.keyword}; + if (isMaxMin && maxMinKeywords.size() == 1) { + // max(a1=x) or max(a2=x) + if (maxMinKeywords.begin()->ToString().compare("a1") == 0) { + messages.Say("missing mandatory '%s=' argument"_err_en_US, "a2"); + } else if (maxMinKeywords.begin()->ToString().compare("a2") == 0) { + messages.Say("missing mandatory '%s=' argument"_err_en_US, "a1"); + } else { + messages.Say( + "missing mandatory '%s=' and '%s=' arguments"_err_en_US, "a1", + "a2"); + } + } else { + messages.Say("missing mandatory '%s=' argument"_err_en_US, + kw.c_str()); + } return std::nullopt; // missing non-OPTIONAL argument } else { continue; Index: flang/test/Semantics/call23.f90 =================================================================== --- flang/test/Semantics/call23.f90 +++ flang/test/Semantics/call23.f90 @@ -1,6 +1,6 @@ ! RUN: %python %S/test_errors.py %s %flang_fc1 ! Check errors on MAX/MIN with keywords, a weird case in Fortran -real :: x = 0.0 ! prevent folding +real :: x = 0.0, y = 0.0 , y1 = 0.0 ! prevent folding !ERROR: Argument keyword 'a1=' was repeated in call to 'max' print *, max(a1=x,a1=1) !ERROR: Keyword argument 'a1=' has already been specified positionally (#1) in this procedure reference @@ -9,4 +9,22 @@ print *, max(x,0,a99=0) ! ok !ERROR: Argument keyword 'a06=' is not known in call to 'max' print *, max(a1=x,a2=0,a06=0) +!ERROR: missing mandatory 'a2=' argument +print *, max(a3=y, a1=x) +!ERROR: missing mandatory 'a1=' argument +print *, max(a3=y, a2=x) +!ERROR: missing mandatory 'a1=' and 'a2=' arguments +print *, max(a3=y, a4=x) +!ERROR: missing mandatory 'a2=' argument +print *, max(y1, a3=y) +!ERROR: missing mandatory 'a1=' and 'a2=' arguments +print *, max(a9=x, a5=y, a4=y1) +!ERROR: missing mandatory 'a2=' argument +print *, max(x) +!ERROR: missing mandatory 'a2=' argument +print *, max(a1=x) +!ERROR: missing mandatory 'a1=' argument +print *, max(a2=y) +!ERROR: missing mandatory 'a1=' and 'a2=' arguments +print *, max(a3=x) end