Skip to content

Commit

Permalink
[flang] Pointers returned from functions are not definable as pointers
Browse files Browse the repository at this point in the history
A reference to a pointer-valued function is a "variable" in the argot of
the Fortran standard, and can be the left-hand side of an assignment
statement or passed as a definable actual argument -- but it is not a
definable pointer, and cannot be associated with a pointer dummy argument
that is not INTENT(IN).

Differential Revision: https://reviews.llvm.org/D143827
  • Loading branch information
klausler committed Feb 13, 2023
1 parent 632fd9f commit 30d9323
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 10 deletions.
20 changes: 10 additions & 10 deletions flang/lib/Semantics/definable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -267,11 +267,10 @@ std::optional<parser::Message> WhyNotDefinable(parser::CharBlock at,
expr.AsFortran());
}
return WhyNotDefinable(at, scope, flags, *dataRef);
}
if (evaluate::IsVariable(expr)) {
return std::nullopt; // result of function returning a pointer - ok
}
if (flags.test(DefinabilityFlag::PointerDefinition)) {
} else if (evaluate::IsNullPointer(expr)) {
return parser::Message{
at, "'%s' is a null pointer"_because_en_US, expr.AsFortran()};
} else if (flags.test(DefinabilityFlag::PointerDefinition)) {
if (const auto *procDesignator{
std::get_if<evaluate::ProcedureDesignator>(&expr.u)}) {
// Defining a procedure pointer
Expand All @@ -288,13 +287,14 @@ std::optional<parser::Message> WhyNotDefinable(parser::CharBlock at,
}
}
}
}
if (evaluate::IsNullPointer(expr)) {
return parser::Message{
at, "'%s' is a null pointer"_because_en_US, expr.AsFortran()};
at, "'%s' is not a definable pointer"_because_en_US, expr.AsFortran()};
} else if (!evaluate::IsVariable(expr)) {
return parser::Message{at,
"'%s' is not a variable or pointer"_because_en_US, expr.AsFortran()};
} else {
return std::nullopt;
}
return parser::Message{
at, "'%s' is not a variable or pointer"_because_en_US, expr.AsFortran()};
}

} // namespace Fortran::semantics
27 changes: 27 additions & 0 deletions flang/test/Semantics/definable04.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
! RUN: %python %S/test_errors.py %s %flang_fc1
module m
integer, target :: n
contains
function ptr()
integer, pointer :: ptr
ptr => n
end
subroutine s1(p)
integer, pointer, intent(in) :: p
end
subroutine s2(p)
integer, pointer, intent(in out) :: p
end
end

program test
use m
integer, pointer :: p
p => ptr() ! ok
ptr() = 1 ! ok
call s1(ptr()) ! ok
call s1(null()) ! ok
!ERROR: Actual argument associated with INTENT(IN OUT) dummy argument 'p=' is not definable
!BECAUSE: 'ptr()' is not a definable pointer
call s2(ptr())
end

0 comments on commit 30d9323

Please sign in to comment.