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

    <tr>
        <th>Summary</th>
        <td>
            [flang] Flang crashes compiling a program with a derived type that extends an undefined derived type
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            flang
      </td>
    </tr>

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

    <tr>
      <th>Reporter</th>
      <td>
          inaki-amatria
      </td>
    </tr>
</table>

<pre>
    - `flang-new` version is: `flang-new version 20.0.0git (https://github.com/llvm/llvm-project.git 999313debe8a87760b128e4469f17ec0ce1a4a8f)`
- Reduced test case:
```f90
! foo.f90
program main
  type :: foo_t
 class(bar_t), pointer :: bar_t_ptr
  end type foo_t
  type, extends(bar_t) :: baz_t
  end type baz_t
end program main
```
- `flang-new` invocation is: `flang-new foo.f90`
- Expected behavior is: The code errors with `The derived type 'bar_t' has not been defined`.
- Actual behavior: Compilation aborts with the following stack trace:
```
 "/opt/compiler-explorer/clang-llvmflang-trunk-20240919/bin/flang-new" -fc1 -triple x86_64-unknown-linux-gnu -emit-obj -mrelocation-model pic -pic-level 2 -pic-is-pie -target-cpu x86-64 -debug-info-kind=standalone -resource-dir /opt/compiler-explorer/clang-llvmflang-trunk-20240919/lib/clang/20 -mframe-pointer=all -O0 -o CMakeFiles/main.dir/main.f90.o -x f95-cpp-input /app/main.f90
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace.
Stack dump:
0.      Program arguments: /opt/compiler-explorer/clang-llvmflang-trunk-20240919/bin/flang-new -fc1 -triple x86_64-unknown-linux-gnu -emit-obj -mrelocation-model pic -pic-level 2 -pic-is-pie -target-cpu x86-64 -debug-info-kind=standalone -resource-dir /opt/compiler-explorer/clang-llvmflang-trunk-20240919/lib/clang/20 -mframe-pointer=all -O0 -o CMakeFiles/main.dir/main.f90.o -x f95-cpp-input /app/main.f90
 #0 0x00000000035fd368 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/opt/compiler-explorer/clang-llvmflang-trunk-20240919/bin/flang-new+0x35fd368)
 #1 0x00000000035fad0c SignalHandler(int) Signals.cpp:0:0
 #2 0x0000754bc4642520 (/lib/x86_64-linux-gnu/libc.so.6+0x42520)
 #3 0x000000000403a78b Fortran::semantics::DerivedTypeSpec::set_scope(Fortran::semantics::Scope const&) (/opt/compiler-explorer/clang-llvmflang-trunk-20240919/bin/flang-new+0x403a78b)
 #4 0x0000000003f8f3de Fortran::semantics::DeclarationVisitor::Post(Fortran::parser::DerivedTypeStmt const&) (/opt/compiler-explorer/clang-llvmflang-trunk-20240919/bin/flang-new+0x3f8f3de)
 #5 0x0000000003f8f501 void Fortran::parser::Walk<Fortran::parser::DerivedTypeStmt, Fortran::semantics::ResolveNamesVisitor>(Fortran::parser::Statement<Fortran::parser::DerivedTypeStmt> const&, Fortran::semantics::ResolveNamesVisitor&) (/opt/compiler-explorer/clang-llvmflang-trunk-20240919/bin/flang-new+0x3f8f501)
 #6 0x0000000003f9ab0a Fortran::semantics::DeclarationVisitor::Pre(Fortran::parser::DerivedTypeDef const&) (/opt/compiler-explorer/clang-llvmflang-trunk-20240919/bin/flang-new+0x3f9ab0a)
 #7 0x0000000003f9b074 std::enable_if<TupleTrait<Fortran::parser::DerivedTypeDef>, void>::type Fortran::parser::Walk<Fortran::parser::DerivedTypeDef, Fortran::semantics::ResolveNamesVisitor>(Fortran::parser::DerivedTypeDef const&, Fortran::semantics::ResolveNamesVisitor&) (/opt/compiler-explorer/clang-llvmflang-trunk-20240919/bin/flang-new+0x3f9b074)
 #8 0x0000000003fa27e6 void Fortran::parser::Walk<Fortran::parser::DeclarationConstruct, Fortran::semantics::ResolveNamesVisitor>(std::__cxx11::list<Fortran::parser::DeclarationConstruct, std::allocator<Fortran::parser::DeclarationConstruct>> const&, Fortran::semantics::ResolveNamesVisitor&) (/opt/compiler-explorer/clang-llvmflang-trunk-20240919/bin/flang-new+0x3fa27e6)
 #9 0x0000000003fa3e55 Fortran::semantics::ResolveNamesVisitor::Pre(Fortran::parser::SpecificationPart const&) (/opt/compiler-explorer/clang-llvmflang-trunk-20240919/bin/flang-new+0x3fa3e55)
#10 0x0000000003fa1ed9 std::enable_if<TupleTrait<Fortran::parser::SpecificationPart>, void>::type Fortran::parser::Walk<Fortran::parser::SpecificationPart, Fortran::semantics::ResolveNamesVisitor>(Fortran::parser::SpecificationPart const&, Fortran::semantics::ResolveNamesVisitor&) (/opt/compiler-explorer/clang-llvmflang-trunk-20240919/bin/flang-new+0x3fa1ed9)
#11 0x0000000003fc8c64 Fortran::semantics::ResolveNamesVisitor::ResolveSpecificationParts(Fortran::semantics::ProgramTree&) (.part.0) resolve-names.cpp:0:0
#12 0x0000000003fc9306 Fortran::semantics::ResolveNamesVisitor::Pre(Fortran::parser::ProgramUnit const&) (/opt/compiler-explorer/clang-llvmflang-trunk-20240919/bin/flang-new+0x3fc9306)
#13 0x0000000003fca864 Fortran::semantics::ResolveNames(Fortran::semantics::SemanticsContext&, Fortran::parser::Program const&, Fortran::semantics::Scope&) (/opt/compiler-explorer/clang-llvmflang-trunk-20240919/bin/flang-new+0x3fca864)
#14 0x0000000004023459 Fortran::semantics::Semantics::Perform() (/opt/compiler-explorer/clang-llvmflang-trunk-20240919/bin/flang-new+0x4023459)
#15 0x00000000036570ad Fortran::frontend::FrontendAction::runSemanticChecks() (/opt/compiler-explorer/clang-llvmflang-trunk-20240919/bin/flang-new+0x36570ad)
#16 0x00000000039beafa Fortran::frontend::CodeGenAction::beginSourceFileAction() (/opt/compiler-explorer/clang-llvmflang-trunk-20240919/bin/flang-new+0x39beafa)
#17 0x00000000036556ad Fortran::frontend::FrontendAction::beginSourceFile(Fortran::frontend::CompilerInstance&, Fortran::frontend::FrontendInputFile const&) (/opt/compiler-explorer/clang-llvmflang-trunk-20240919/bin/flang-new+0x36556ad)
#18 0x0000000003644fa7 Fortran::frontend::CompilerInstance::executeAction(Fortran::frontend::FrontendAction&) (/opt/compiler-explorer/clang-llvmflang-trunk-20240919/bin/flang-new+0x3644fa7)
#19 0x000000000365dd9d Fortran::frontend::executeCompilerInvocation(Fortran::frontend::CompilerInstance*) (/opt/compiler-explorer/clang-llvmflang-trunk-20240919/bin/flang-new+0x365dd9d)
#20 0x0000000001cdfc98 fc1_main(llvm::ArrayRef<char const*>, char const*) (/opt/compiler-explorer/clang-llvmflang-trunk-20240919/bin/flang-new+0x1cdfc98)
#21 0x0000000001bc020c main (/opt/compiler-explorer/clang-llvmflang-trunk-20240919/bin/flang-new+0x1bc020c)
#22 0x0000754bc4629d90 (/lib/x86_64-linux-gnu/libc.so.6+0x29d90)
#23 0x0000754bc4629e40 __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x29e40)
#24 0x0000000001cdebfe _start (/opt/compiler-explorer/clang-llvmflang-trunk-20240919/bin/flang-new+0x1cdebfe)
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 20.0.0git (https://github.com/llvm/llvm-project.git 999313debe8a87760b128e4469f17ec0ce1a4a8f)
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /opt/compiler-explorer/clang-llvmflang-trunk-20240919/bin
flang-new: note: diagnostic msg: 
```
- Additional info: It looks like the following patch works around the issue, although I can't assess if this is a valid patch so I'd rather open this issue:
```diff
diff --git a/flang/lib/Semantics/resolve-names.cpp b/flang/lib/Semantics/resolve-names.cpp
index 0b4e01b76c56..4882e2f624a8 100644
--- a/flang/lib/Semantics/resolve-names.cpp
+++ b/flang/lib/Semantics/resolve-names.cpp
@@ -5539,7 +5539,7 @@ void DeclarationVisitor::Post(const parser::DerivedTypeStmt &x) {
     // existing.
     const Symbol &extendsSymbol{extendsType->typeSymbol()};
 auto restorer{common::ScopedSet(extendsName->symbol, nullptr)};
- if (OkToAddComponent(*extendsName, &extendsSymbol)) {
+    if (OkToAddComponent(*extendsName, &extendsSymbol) && extendsSymbol.scope()) {
       auto &comp{DeclareEntity<ObjectEntityDetails>(*extendsName, Attrs{})};
       comp.attrs().set(
           Attr::PRIVATE, extendsSymbol.attrs().test(Attr::PRIVATE));
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzsWl9z27qO_zTKC0Yeifpj6SEPjhPvdmZ3T6fJnvvooUjI5olMakgqde6nv0NKciw7TZs27j0P15NOJRIE8ANAEBRJjREbiXgdZDdBdntFO7tV-lpI-ihCuqNWC3pVKf58HUKQR3VD5SaU-DXII3hCbYSSIEyQLCa9hy4SzaJZtBEWAlJsrW0daUBWAVlthN121YypXUBWTfM0_he2Wv2FzM7cqLIskzjhWGFBi_k8j6qYFJimeVnHc2QRw5imtKgDUgZ5FES3QbQI4QvyjiEHi8YCowadUN_niPxfXQ7UAYmhVmp2aGi12mi6gx0Vsm8BsM8tgmOSLBzx2g4drKHGBKSoqF5bpwNZQquEtKhHct-1bq0eeaHkPb9jRr7Fjca9RcmPeb4w-ucL-YHJUatrO1f-gHg0zqkXhXxSjNpvOHI0zcv4u32LzCKHCrf0SSg9DHvYIjDFEVBrpQ18FXbreLl2jlo84aByQOYDtjlsqQGpLFSIEjjWQiIP8mg2Clsw29HmIMrJWapdK5peYVopbQdRdutM2jTqq5AbMJayR7CaslecP1gxICQgK9XagKyY54o6xH3bKI3atXkruJjs7WF1Jx9DEpE0KuMyIKtKyICsXqxJCIQ1iyG0WrQNwr7I13kadvJRqq8ybITs9uFGdhDiTthQVX9BuNPYDPYPd4pjA61gELaChQ0-YQOkfxEmbAVCaKneoA1Z2zn2YZ5CyLHqNqGQtQofheRBcmsslZw2SiKEGo3qNMOQCw2_hrcR1UgWkBWJINzVmu4wHGI-SG5p00D4RwShguX_0kdciQZNQFYuIGdc6PGxLqOZgnAPdZmFrG1DIdvOZYkVbdsjot5Vn__nbnF_B6ardsICharbgMZWaQtWwfvySkBWwpjOawVUchCSNR1HH0BMU7OFirJHHzpDHN77YOLdrj3EkgvR8vMw26jedDuUtp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