[flang] Pointers returned from functions are not definable as pointers
authorPeter Klausler <pklausler@nvidia.com>
Wed, 8 Feb 2023 21:31:25 +0000 (13:31 -0800)
committerPeter Klausler <pklausler@nvidia.com>
Mon, 13 Feb 2023 23:59:02 +0000 (15:59 -0800)
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

flang/lib/Semantics/definable.cpp
flang/test/Semantics/definable04.f90 [new file with mode: 0644]

index 79d57d0b394fa342b7ea71cdc9a5faf9ffafb5df..aa9246fb223e08908fd6ea5720a44e20931a2f49 100644 (file)
@@ -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
@@ -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
diff --git a/flang/test/Semantics/definable04.f90 b/flang/test/Semantics/definable04.f90
new file mode 100644 (file)
index 0000000..f9a5e04
--- /dev/null
@@ -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