<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/65572>65572</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            [Flang] Endless recursion in compilation of derived types that have a triangular reference
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            new issue,
            flang
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          yus3710-fj
      </td>
    </tr>
</table>

<pre>
    This is an issue from Fujitsu testsuite.

Flang-new terminates abnormally when compiling derived types that have a triangular reference.
If the variable of the derived type isn't in a common block, this issue isn't reproduced.

The following are the test program, Flang-new and gfortran compilation result.

```fortran
! test.f90
module mod
  type ty1
 sequence
    type (ty2),pointer :: aaa1
  end type
  type ty2
 sequence
    type (ty3),pointer :: aaa2
  end type
  type ty3
 sequence
    type (ty1),pointer :: aaa3
  end type
end

! only test compilation, not execution result
use mod
common sss
type (ty1) :: sss
end
```
```console
$ flang-new -v test.f90
flang-new version 18.0.0 (https://github.com/llvm/llvm-project.git 701e6f7630474b637e0bc45d009bf2ec47f2d3fd)
Target: aarch64-unknown-linux-gnu
Thread model: posix
InstalledDir: /path/to/install/bin
Found candidate GCC installation: /usr/lib/gcc/aarch64-redhat-linux/8
Selected GCC installation: /usr/lib/gcc/aarch64-redhat-linux/8
Candidate multilib: .;@m64
Selected multilib: .;@m64
 "/path/to/install/bin/flang-new" -fc1 -triple aarch64-unknown-linux-gnu -emit-obj -fcolor-diagnostics -mrelocation-model pic -pic-level 2 -pic-is-pie -target-cpu generic -target-feature +neon -target-feature +v8a -o /tmp/test-b3bbcd.o -x f95-cpp-input test.f90
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace.
Stack dump:
0.      Program arguments: /path/to/install/bin/flang-new -fc1 -triple aarch64-unknown-linux-gnu -emit-obj -fcolor-diagnostics -mrelocation-model pic -pic-level 2 -pic-is-pie -target-cpu generic -target-feature +neon -target-feature +v8a -o /tmp/test-b3bbcd.o -x f95-cpp-input test.f90
  #0 0x0000ffffada65f60 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/path/to/install/bin/../lib/libLLVMSupport.so.18git+0x1a5f60)
  #1 0x0000ffffada63b40 llvm::sys::RunSignalHandlers() (/path/to/install/bin/../lib/libLLVMSupport.so.18git+0x1a3b40)
  #2 0x0000ffffada63cb0 SignalHandler(int) Signals.cpp:0:0
  #3 0x0000ffffaf54066c (linux-vdso.so.1+0x66c)
  #4 0x0000ffffa9a002c8 Fortran::semantics::DeclarationVisitor::CheckCommonBlockDerivedType(Fortran::parser::CharBlock const&, Fortran::semantics::Symbol const&) (/path/to/install/bin/../lib/../lib/libFortranSemantics.so.18git+0x4f02c8)
 #5 0x0000ffffa9a003cc Fortran::semantics::DeclarationVisitor::CheckCommonBlockDerivedType(Fortran::parser::CharBlock const&, Fortran::semantics::Symbol const&) (/path/to/install/bin/../lib/../lib/libFortranSemantics.so.18git+0x4f03cc)
 #6 0x0000ffffa9a003cc Fortran::semantics::DeclarationVisitor::CheckCommonBlockDerivedType(Fortran::parser::CharBlock const&, Fortran::semantics::Symbol const&) (/path/to/install/bin/../lib/../lib/libFortranSemantics.so.18git+0x4f03cc)
 #7 0x0000ffffa9a003cc Fortran::semantics::DeclarationVisitor::CheckCommonBlockDerivedType(Fortran::parser::CharBlock const&, Fortran::semantics::Symbol const&) (/path/to/install/bin/../lib/../lib/libFortranSemantics.so.18git+0x4f03cc)
 #8 0x0000ffffa9a003cc Fortran::semantics::DeclarationVisitor::CheckCommonBlockDerivedType(Fortran::parser::CharBlock const&, Fortran::semantics::Symbol const&) (/path/to/install/bin/../lib/../lib/libFortranSemantics.so.18git+0x4f03cc)
 #9 0x0000ffffa9a003cc Fortran::semantics::DeclarationVisitor::CheckCommonBlockDerivedType(Fortran::parser::CharBlock const&, Fortran::semantics::Symbol const&) (/path/to/install/bin/../lib/../lib/libFortranSemantics.so.18git+0x4f03cc)
 #10 0x0000ffffa9a003cc Fortran::semantics::DeclarationVisitor::CheckCommonBlockDerivedType(Fortran::parser::CharBlock const&, Fortran::semantics::Symbol const&) (/path/to/install/bin/../lib/../lib/libFortranSemantics.so.18git+0x4f03cc)
 #11 0x0000ffffa9a003cc Fortran::semantics::DeclarationVisitor::CheckCommonBlockDerivedType(Fortran::parser::CharBlock const&, Fortran::semantics::Symbol const&) (/path/to/install/bin/../lib/../lib/libFortranSemantics.so.18git+0x4f03cc)
 #12 0x0000ffffa9a003cc Fortran::semantics::DeclarationVisitor::CheckCommonBlockDerivedType(Fortran::parser::CharBlock const&, Fortran::semantics::Symbol const&) (/path/to/install/bin/../lib/../lib/libFortranSemantics.so.18git+0x4f03cc)
 #13 0x0000ffffa9a003cc Fortran::semantics::DeclarationVisitor::CheckCommonBlockDerivedType(Fortran::parser::CharBlock const&, Fortran::semantics::Symbol const&) (/path/to/install/bin/../lib/../lib/libFortranSemantics.so.18git+0x4f03cc)
 #14 0x0000ffffa9a003cc Fortran::semantics::DeclarationVisitor::CheckCommonBlockDerivedType(Fortran::parser::CharBlock const&, Fortran::semantics::Symbol const&) (/path/to/install/bin/../lib/../lib/libFortranSemantics.so.18git+0x4f03cc)
 #15 0x0000ffffa9a003cc Fortran::semantics::DeclarationVisitor::CheckCommonBlockDerivedType(Fortran::parser::CharBlock const&, Fortran::semantics::Symbol const&) (/path/to/install/bin/../lib/../lib/libFortranSemantics.so.18git+0x4f03cc)
 #16 0x0000ffffa9a003cc Fortran::semantics::DeclarationVisitor::CheckCommonBlockDerivedType(Fortran::parser::CharBlock const&, Fortran::semantics::Symbol const&) (/path/to/install/bin/../lib/../lib/libFortranSemantics.so.18git+0x4f03cc)
 #17 0x0000ffffa9a003cc Fortran::semantics::DeclarationVisitor::CheckCommonBlockDerivedType(Fortran::parser::CharBlock const&, Fortran::semantics::Symbol const&) (/path/to/install/bin/../lib/../lib/libFortranSemantics.so.18git+0x4f03cc)
 :
#255 0x0000ffffa9a003cc Fortran::semantics::DeclarationVisitor::CheckCommonBlockDerivedType(Fortran::parser::CharBlock const&, Fortran::semantics::Symbol const&) (/path/to/install/bin/../lib/../lib/libFortranSemantics.so.18git+0x4f03cc)
flang-new: error: unable to execute command: Segmentation fault (core dumped)
flang-new: error: flang frontend command failed due to signal (use -v to see invocation)
flang-new version 18.0.0 (https://github.com/llvm/llvm-project.git 701e6f7630474b637e0bc45d009bf2ec47f2d3fd)
Target: aarch64-unknown-linux-gnu
Thread model: posix
InstalledDir: /path/to/install/bin
flang-new: note: diagnostic msg: 
********************

PLEASE ATTACH THE FOLLOWING FILES TO THE BUG REPORT:
Preprocessed source(s) and associated run script(s) are located at:
flang-new: note: diagnostic msg: /tmp/test-7ead1f
flang-new: note: diagnostic msg: /tmp/test-7ead1f.sh
flang-new: note: diagnostic msg: 

********************
```
```console
$ gfortran -v test.f90
Driving: gfortran -v test.f90 -l gfortran -l m -shared-libgcc
Using built-in specs.
COLLECT_GCC=gfortran
COLLECT_LTO_WRAPPER=/usr/libexec/gcc/aarch64-redhat-linux/8/lto-wrapper
Target: aarch64-redhat-linux
Configured with: ../configure --enable-bootstrap --enable-languages=c,c++,fortran,lto --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-shared --enable-threads=posix --enable-checking=release --enable-multilib --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --with-gcc-major-version-only --with-linker-hash-style=gnu --enable-plugin --enable-initfini-array --with-isl --disable-libmpx --enable-gnu-indirect-function --build=aarch64-redhat-linux
Thread model: posix
gcc version 8.4.1 20200928 (Red Hat 8.4.1-1) (GCC) 
 :
$
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzsWklz4zbT_jX0pQsqCtR68MGWrJlU-fvGZTvJ0QUCTQpjEGCweMmvfwsgtXnG41Eyt8hFS8LWePrpRgOFJnNO1hrxPBtfZuP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