[flang-commits] [PATCH] D139049: [flang] Enforce restrictions on intrinsic assignment

Peter Klausler via Phabricator via flang-commits flang-commits at lists.llvm.org
Wed Nov 30 15:02:15 PST 2022


klausler created this revision.
klausler added a reviewer: vdonaldson.
klausler added a project: Flang.
Herald added a subscriber: jdoerfert.
Herald added a reviewer: sscalpone.
Herald added a project: All.
klausler requested review of this revision.

When the left-hand side of an intrinsic assignment statement is
polymorphic, the LHS must be a whole allocatable variable or
component and may not be a coarray (10.2.2.1p1(1)).


https://reviews.llvm.org/D139049

Files:
  flang/lib/Semantics/expression.cpp
  flang/test/Semantics/assign11.f90
  flang/test/Semantics/call28.f90


Index: flang/test/Semantics/call28.f90
===================================================================
--- /dev/null
+++ flang/test/Semantics/call28.f90
@@ -0,0 +1,23 @@
+! RUN: %python %S/test_errors.py %s %flang_fc1
+
+module m1
+  type :: t
+  end type
+ contains
+  pure subroutine s1(x)
+    class(t), intent(in out) :: x
+    call s2(x)
+    call s3(x)
+  end subroutine
+  pure subroutine s2(x)
+    class(t), intent(in out) :: x
+    !ERROR: Left-hand side of assignment may not be polymorphic unless assignment is to an entire allocatable
+    !ERROR: Left-hand side of assignment is not definable
+    !BECAUSE: 'x' is polymorphic in a pure subprogram
+    x = t()
+  end subroutine
+  pure subroutine s3(x)
+    !ERROR: An INTENT(OUT) dummy argument of a pure subroutine may not be polymorphic
+    class(t), intent(out) :: x
+  end subroutine
+end module
Index: flang/test/Semantics/assign11.f90
===================================================================
--- /dev/null
+++ flang/test/Semantics/assign11.f90
@@ -0,0 +1,12 @@
+! RUN: %python %S/test_errors.py %s %flang_fc1
+! 10.2.1.2p1(1)
+program test
+  class(*), allocatable :: pa
+  class(*), pointer :: pp
+  class(*), allocatable :: pac[:]
+  pa = 1 ! ok
+  !ERROR: Left-hand side of assignment may not be polymorphic unless assignment is to an entire allocatable
+  pp = 1
+  !ERROR: Left-hand side of assignment may not be polymorphic if it is a coarray
+  pac = 1
+end
Index: flang/lib/Semantics/expression.cpp
===================================================================
--- flang/lib/Semantics/expression.cpp
+++ flang/lib/Semantics/expression.cpp
@@ -2600,6 +2600,18 @@
       if (!procRef) {
         analyzer.CheckForNullPointer(
             "in a non-pointer intrinsic assignment statement");
+        const Expr<SomeType> &lhs{analyzer.GetExpr(0)};
+        if (auto dyType{lhs.GetType()};
+            dyType && dyType->IsPolymorphic()) { // 10.2.1.2p1(1)
+          const Symbol *lastWhole0{UnwrapWholeSymbolOrComponentDataRef(lhs)};
+          const Symbol *lastWhole{
+              lastWhole0 ? &lastWhole0->GetUltimate() : nullptr};
+          if (!lastWhole || !IsAllocatable(*lastWhole)) {
+            Say("Left-hand side of assignment may not be polymorphic unless assignment is to an entire allocatable"_err_en_US);
+          } else if (evaluate::IsCoarray(*lastWhole)) {
+            Say("Left-hand side of assignment may not be polymorphic if it is a coarray"_err_en_US);
+          }
+        }
       }
       assignment.emplace(analyzer.MoveExpr(0), analyzer.MoveExpr(1));
       if (procRef) {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D139049.479080.patch
Type: text/x-patch
Size: 2611 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/flang-commits/attachments/20221130/ce640051/attachment-0001.bin>


More information about the flang-commits mailing list