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

    <tr>
        <th>Summary</th>
        <td>
            [flang] Flang is unable to compile a module with an interface block for a external proceure
        </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 0cc2cd781594aec741f7262df7a48d73a7d09a18)`
- Reduced test case:
```f90
! foo.f90
module foo
  interface
    subroutine doFoo(i)
      implicit none
    end subroutine doFoo
  end interface
end module foo
```
- `flang-new` invocation is: `flang-new foo.f90`
- Expected behavior is: The code compiles successfully without errors.
- Actual behavior: Compilation aborts with the following stack trace:
```
fatal internal error: nullptr dereference at /root/llvm-project/flang/lib/Semantics/mod-file.cpp(958)
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-20240917/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-20240917/lib/clang/20 -mframe-pointer=all -O0 -o CMakeFiles/main.dir/main.f90.o -x f95-cpp-input /app/main.f90
 #0 0x00000000035f7288 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/opt/compiler-explorer/clang-llvmflang-trunk-20240917/bin/flang-new+0x35f7288)
 #1 0x00000000035f4c2c SignalHandler(int) Signals.cpp:0:0
 #2 0x00007ce86e042520 (/lib/x86_64-linux-gnu/libc.so.6+0x42520)
 #3 0x00007ce86e0969fc pthread_kill (/lib/x86_64-linux-gnu/libc.so.6+0x969fc)
 #4 0x00007ce86e042476 gsignal (/lib/x86_64-linux-gnu/libc.so.6+0x42476)
 #5 0x00007ce86e0287f3 abort (/lib/x86_64-linux-gnu/libc.so.6+0x287f3)
 #6 0x0000000004c1c029 (/opt/compiler-explorer/clang-llvmflang-trunk-20240917/bin/flang-new+0x4c1c029)
 #7 0x00000000041c9f38 (/opt/compiler-explorer/clang-llvmflang-trunk-20240917/bin/flang-new+0x41c9f38)
 #8 0x00000000041ca8ac Fortran::semantics::ModFileWriter::PutEntity(llvm::raw_ostream&, Fortran::semantics::Symbol const&, std::function<void ()>, Fortran::semantics::Attrs) (/opt/compiler-explorer/clang-llvmflang-trunk-20240917/bin/flang-new+0x41ca8ac)
 #9 0x00000000041ce00d Fortran::semantics::ModFileWriter::PutObjectEntity(llvm::raw_ostream&, Fortran::semantics::Symbol const&) (.part.0) mod-file.cpp:0:0
#10 0x00000000041ce7dd auto Fortran::semantics::ModFileWriter::PutSymbol(llvm::raw_ostream&, Fortran::semantics::Symbol const&)::'lambda10'(auto const&)::operator()<Fortran::semantics::EntityDetails>(auto const&) const (.isra.0) mod-file.cpp:0:0
#11 0x00000000041ce9d5 Fortran::semantics::ModFileWriter::PutSymbol(llvm::raw_ostream&, Fortran::semantics::Symbol const&) (/opt/compiler-explorer/clang-llvmflang-trunk-20240917/bin/flang-new+0x41ce9d5)
#12 0x00000000041cfc3a Fortran::semantics::ModFileWriter::PutSubprogram(Fortran::semantics::Symbol const&) (.part.0) mod-file.cpp:0:0
#13 0x00000000041cebaa Fortran::semantics::ModFileWriter::PutSymbol(llvm::raw_ostream&, Fortran::semantics::Symbol const&) (/opt/compiler-explorer/clang-llvmflang-trunk-20240917/bin/flang-new+0x41cebaa)
#14 0x00000000041d4fce Fortran::semantics::ModFileWriter::PutSymbols(Fortran::semantics::Scope const&, std::set<Fortran::common::Reference<Fortran::semantics::Symbol const>, Fortran::semantics::SymbolAddressCompare, std::allocator<Fortran::common::Reference<Fortran::semantics::Symbol const>>>*) (/opt/compiler-explorer/clang-llvmflang-trunk-20240917/bin/flang-new+0x41d4fce)
#15 0x00000000041d344e Fortran::semantics::ModFileWriter::Write(Fortran::semantics::Symbol const&) (/opt/compiler-explorer/clang-llvmflang-trunk-20240917/bin/flang-new+0x41d344e)
#16 0x00000000041d55e0 Fortran::semantics::ModFileWriter::WriteAll() (/opt/compiler-explorer/clang-llvmflang-trunk-20240917/bin/flang-new+0x41d55e0)
#17 0x0000000003feb235 Fortran::semantics::Semantics::Perform() (/opt/compiler-explorer/clang-llvmflang-trunk-20240917/bin/flang-new+0x3feb235)
#18 0x000000000364fe1d Fortran::frontend::FrontendAction::runSemanticChecks() (/opt/compiler-explorer/clang-llvmflang-trunk-20240917/bin/flang-new+0x364fe1d)
#19 0x000000000398691a Fortran::frontend::CodeGenAction::beginSourceFileAction() (/opt/compiler-explorer/clang-llvmflang-trunk-20240917/bin/flang-new+0x398691a)
#20 0x000000000364f025 Fortran::frontend::FrontendAction::beginSourceFile(Fortran::frontend::CompilerInstance&, Fortran::frontend::FrontendInputFile const&) (/opt/compiler-explorer/clang-llvmflang-trunk-20240917/bin/flang-new+0x364f025)
#21 0x000000000363e447 Fortran::frontend::CompilerInstance::executeAction(Fortran::frontend::FrontendAction&) (/opt/compiler-explorer/clang-llvmflang-trunk-20240917/bin/flang-new+0x363e447)
#22 0x00000000036558ad Fortran::frontend::executeCompilerInvocation(Fortran::frontend::CompilerInstance*) (/opt/compiler-explorer/clang-llvmflang-trunk-20240917/bin/flang-new+0x36558ad)
#23 0x0000000001cdd5c8 fc1_main(llvm::ArrayRef<char const*>, char const*) (/opt/compiler-explorer/clang-llvmflang-trunk-20240917/bin/flang-new+0x1cdd5c8)
#24 0x0000000001bbe1ac main (/opt/compiler-explorer/clang-llvmflang-trunk-20240917/bin/flang-new+0x1bbe1ac)
#25 0x00007ce86e029d90 (/lib/x86_64-linux-gnu/libc.so.6+0x29d90)
#26 0x00007ce86e029e40 __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x29e40)
#27 0x0000000001cdc52e _start (/opt/compiler-explorer/clang-llvmflang-trunk-20240917/bin/flang-new+0x1cdc52e)
flang-new: error: unable to execute command: Aborted (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 0cc2cd781594aec741f7262df7a48d73a7d09a18)
```
- Additional info: Removing `implicit none` and compiling with `flang-new -fimplicit-none foo.f90` also reproduces the issue.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzUWV9v5CgS_zTkBbll4_8P_dCbTN-tdKsdTVa6xwhDuZsNBgtwJvn2J7C7E5y5ZJOb1t2NemZsoP78flWFsYtaKw4KYIvKX1B5c0Und9RmKxS9FwkdqDOCXnWaP20TjKq0l1QdEgXfUZXiBzBWaIWFRfkumj1PkXSTbtKDcBiR5ujc6JciskdkfxDuOHUbpgdE9lI-nP5LRqP_BOY2XipljDBeN1nZFhRYXWR9TSrC-5oWDa9zWvO0pVmDSIuqFKU3KN0l-BvwiQHHDqzDjFrwRsOcXxR-fbusRiTDvdab88Cg-STBj833GAvlwPSUwWkAYzt1Rk9OKMBc77VGpBHeh_MCjMUwSsGEw0qrF5Kg-GvpZdbPrYz5obVHZxAnvOvACPWgGXX_JjYntM_yXx5HYA447uBIH4Q2i9gfR8BMc__PMAoJFtuJMbC2n6R8wt-FO-rJYTBGG7s5adsxN1F51uUVXQf52SPaaeNsEMbu6GFJqb8LdcDWUXaPnfHgXwVsvu2po3LmSFE5W_YG1CTl6AzmYKAHA4oBpj7p9kZrt8osRPaBDT8sOkT2tzBQ5QSziOwHzZNeSNiwcUSkacvmHNev__iyu_3iozcIhynupgM2MGrjsNP4Y9mNyF5YO4E3iWmIO5MTh0AJM9QecUfZfSBjYfY20MOnYTyzk25Q2n41-mDogKk5TAMoN0ec7PXorSyhMwk8jlIbMH4s5IJ3Z84KZyZ1n5CUFGmb1YjsO6FOJIWUSXqW4cQZMUrAj011VxXJpO6V_q4SK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