[flang-commits] [flang] bc83d1c - [flang] Enforce restrictions on intrinsic assignment

Peter Klausler via flang-commits flang-commits at lists.llvm.org
Fri Dec 2 15:23:46 PST 2022


Author: Peter Klausler
Date: 2022-12-02T15:23:38-08:00
New Revision: bc83d1c655c13c58e34515d8efe7784c62012437

URL: https://github.com/llvm/llvm-project/commit/bc83d1c655c13c58e34515d8efe7784c62012437
DIFF: https://github.com/llvm/llvm-project/commit/bc83d1c655c13c58e34515d8efe7784c62012437.diff

LOG: [flang] Enforce restrictions on intrinsic assignment

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)).

Differential Revision: https://reviews.llvm.org/D139049

Added: 
    flang/test/Semantics/assign11.f90

Modified: 
    flang/lib/Semantics/expression.cpp
    flang/test/Semantics/call28.f90

Removed: 
    


################################################################################
diff  --git a/flang/lib/Semantics/expression.cpp b/flang/lib/Semantics/expression.cpp
index 1398f59aa2a6..7b9b367e9c13 100644
--- a/flang/lib/Semantics/expression.cpp
+++ b/flang/lib/Semantics/expression.cpp
@@ -2633,6 +2633,18 @@ const Assignment *ExpressionAnalyzer::Analyze(const parser::AssignmentStmt &x) {
       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) {

diff  --git a/flang/test/Semantics/assign11.f90 b/flang/test/Semantics/assign11.f90
new file mode 100644
index 000000000000..eaa953340950
--- /dev/null
+++ b/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

diff  --git a/flang/test/Semantics/call28.f90 b/flang/test/Semantics/call28.f90
index 4b7a52ed4a01..51430853d663 100644
--- a/flang/test/Semantics/call28.f90
+++ b/flang/test/Semantics/call28.f90
@@ -11,6 +11,7 @@ pure subroutine s1(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()


        


More information about the flang-commits mailing list