[flang-commits] [PATCH] D153465: [flang] Fix looping on LEN type parameter usage

Peter Klausler via Phabricator via flang-commits flang-commits at lists.llvm.org
Wed Jun 21 13:50:27 PDT 2023


klausler created this revision.
klausler added a reviewer: PeteSteinfeld.
klausler added a project: Flang.
Herald added subscribers: sunshaoce, jdoerfert.
Herald added a reviewer: sscalpone.
Herald added a project: All.
klausler requested review of this revision.

When a LEN type parameter of one PDT is being used as the value 
of a LEN type parameter in another PDT, expression rewriting can  
loop infinitely due to an incorrect assumption that the same PDT's
parameters are being referenced.

Fixes LLVM bug https://github.com/llvm/llvm-project/issues/63198


https://reviews.llvm.org/D153465

Files:
  flang/include/flang/Evaluate/common.h
  flang/lib/Evaluate/fold-integer.cpp
  flang/test/Semantics/pdt01.f90


Index: flang/test/Semantics/pdt01.f90
===================================================================
--- /dev/null
+++ flang/test/Semantics/pdt01.f90
@@ -0,0 +1,16 @@
+! RUN: %python %S/test_errors.py %s %flang_fc1
+! Catch error instead of crashing with infinite recursion
+! when a LEN PDT from one type is being used to define a
+! LEN PDT in another type's instantiation.
+program main
+  type t1(lp)
+    integer, len :: lp
+  end type
+  type t2(lp)
+    integer, len :: lp
+    type(t1(lp)) :: c
+  end type
+  integer local
+  !ERROR: Invalid specification expression: reference to local entity 'local'
+  type(t2(local)) :: x
+end
Index: flang/lib/Evaluate/fold-integer.cpp
===================================================================
--- flang/lib/Evaluate/fold-integer.cpp
+++ flang/lib/Evaluate/fold-integer.cpp
@@ -1269,10 +1269,11 @@
     }
   } else {
     // A "bare" type parameter: replace with its value, if that's now known
-    // in a current derived type instantiation, for KIND type parameters.
+    // in a current derived type instantiation.
     if (const auto *pdt{context.pdtInstance()}) {
+      auto restorer{context.WithoutPDTInstance()}; // don't loop
       bool isLen{false};
-      if (const semantics::Scope * scope{context.pdtInstance()->scope()}) {
+      if (const semantics::Scope * scope{pdt->scope()}) {
         auto iter{scope->find(parameterName)};
         if (iter != scope->end()) {
           const Symbol &symbol{*iter->second};
Index: flang/include/flang/Evaluate/common.h
===================================================================
--- flang/include/flang/Evaluate/common.h
+++ flang/include/flang/Evaluate/common.h
@@ -260,6 +260,9 @@
       const semantics::DerivedTypeSpec &spec) {
     return common::ScopedSet(pdtInstance_, &spec);
   }
+  common::Restorer<const semantics::DerivedTypeSpec *> WithoutPDTInstance() {
+    return common::ScopedSet(pdtInstance_, nullptr);
+  }
 
 private:
   parser::ContextualMessages messages_;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D153465.533383.patch
Type: text/x-patch
Size: 2006 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/flang-commits/attachments/20230621/68f9afa5/attachment-0001.bin>


More information about the flang-commits mailing list