Skip to content

[flang] Correct checking of PRESENT() #78364

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 0 additions & 16 deletions flang/lib/Evaluate/intrinsics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2881,8 +2881,6 @@ static bool ApplySpecificChecks(SpecificCall &call, FoldingContext &context) {
arg ? arg->sourceLocation() : context.messages().at(),
"Argument of ALLOCATED() must be an ALLOCATABLE object or component"_err_en_US);
}
} else if (name == "associated" || name == "reduce") {
// Now handled in Semantics/check-call.cpp
} else if (name == "atomic_and" || name == "atomic_or" ||
name == "atomic_xor") {
return CheckForCoindexedObject(
Expand Down Expand Up @@ -2924,20 +2922,6 @@ static bool ApplySpecificChecks(SpecificCall &call, FoldingContext &context) {
arg ? arg->sourceLocation() : context.messages().at(),
"Argument of LOC() must be an object or procedure"_err_en_US);
}
} else if (name == "present") {
const auto &arg{call.arguments[0]};
if (arg) {
if (const auto *expr{arg->UnwrapExpr()}) {
if (const Symbol *symbol{UnwrapWholeSymbolDataRef(*expr)}) {
ok = symbol->attrs().test(semantics::Attr::OPTIONAL);
}
}
}
if (!ok) {
context.messages().Say(
arg ? arg->sourceLocation() : context.messages().at(),
"Argument of PRESENT() must be the name of an OPTIONAL dummy argument"_err_en_US);
}
} else if (name == "ucobound") {
return CheckDimAgainstCorank(call, context);
}
Expand Down
26 changes: 26 additions & 0 deletions flang/lib/Semantics/check-call.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1468,6 +1468,30 @@ static void CheckMove_Alloc(evaluate::ActualArguments &arguments,
}
}

// PRESENT (F'2023 16.9.163)
static void CheckPresent(evaluate::ActualArguments &arguments,
parser::ContextualMessages &messages) {
if (arguments.size() == 1) {
if (const auto &arg{arguments[0]}; arg) {
const Symbol *symbol{nullptr};
if (const auto *expr{arg->UnwrapExpr()}) {
if (const auto *proc{
std::get_if<evaluate::ProcedureDesignator>(&expr->u)}) {
symbol = proc->GetSymbol();
} else {
symbol = evaluate::UnwrapWholeSymbolDataRef(*expr);
}
} else {
symbol = arg->GetAssumedTypeDummy();
}
if (!symbol || !symbol->attrs().test(semantics::Attr::OPTIONAL)) {
messages.Say(arg ? arg->sourceLocation() : messages.at(),
"Argument of PRESENT() must be the name of a whole OPTIONAL dummy argument"_err_en_US);
}
}
}
}

// REDUCE (F'2023 16.9.173)
static void CheckReduce(
evaluate::ActualArguments &arguments, evaluate::FoldingContext &context) {
Expand Down Expand Up @@ -1678,6 +1702,8 @@ static void CheckSpecificIntrinsic(evaluate::ActualArguments &arguments,
CheckAssociated(arguments, context, scope);
} else if (intrinsic.name == "move_alloc") {
CheckMove_Alloc(arguments, context.foldingContext().messages());
} else if (intrinsic.name == "present") {
CheckPresent(arguments, context.foldingContext().messages());
} else if (intrinsic.name == "reduce") {
CheckReduce(arguments, context.foldingContext());
} else if (intrinsic.name == "transfer") {
Expand Down
21 changes: 21 additions & 0 deletions flang/test/Semantics/present01.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
! RUN: %python %S/test_errors.py %s %flang_fc1
module m
type dt
real a
end type
contains
subroutine s(a,b,p,unl)
type(dt), optional :: a(:), b
procedure(sin), optional :: p
type(*), optional :: unl
print *, present(a) ! ok
print *, present(p) ! ok
print *, present(unl) ! ok
!ERROR: Argument of PRESENT() must be the name of a whole OPTIONAL dummy argument
print *, present(a(1))
!ERROR: Argument of PRESENT() must be the name of a whole OPTIONAL dummy argument
print *, present(b%a)
!ERROR: Argument of PRESENT() must be the name of a whole OPTIONAL dummy argument
print *, present(a(1)%a)
end
end