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

    <tr>
        <th>Summary</th>
        <td>
            Clang crash on template friend function definition
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            clang
      </td>
    </tr>

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

    <tr>
      <th>Reporter</th>
      <td>
          poconn
      </td>
    </tr>
</table>

<pre>
    The below code is accepted by GCC and MSVC, but on all versions of Clang fails with "cannot compile this scalar expression yet". Seems that it's not instantiating `foo<int>`, it works when uncommenting the earlier usage of it.

https://godbolt.org/z/Edaq3Tsqz
```cpp
template <typename T>
struct foo {
  template <typename U>
  friend foo<U> make_foo() { return foo<U>{}; }
};

template <typename U>
foo<U> make_foo();

// Uncommenting this fixes the problem:
// foo<int> x;

void f() {
  make_foo<int>();
}
```

Compiler output:
```
<source>:4:37: error: cannot compile this scalar expression yet
    4 |   friend foo<U> make_foo() { return foo<U>{}; }
      | ^~~~~~~~
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace, preprocessed source, and associated run script.
Stack dump:
0.      Program arguments: /opt/compiler-explorer/clang-18.1.0/bin/clang++ -gdwarf-4 -g -o /app/output.s -mllvm --x86-asm-syntax=intel -S --gcc-toolchain=/opt/compiler-explorer/gcc-13.2.0 -fcolor-diagnostics -fno-crash-diagnostics <source>
1.      <eof> parser at end of file
2.      Per-file LLVM IR generation
3.      <source>:4:17: Generating code for declaration 'make_foo'
 #0 0x000000000351ae38 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/opt/compiler-explorer/clang-18.1.0/bin/clang+++0x351ae38)
 #1 0x0000000003518f84 llvm::sys::CleanupOnSignal(unsigned long) (/opt/compiler-explorer/clang-18.1.0/bin/clang+++0x3518f84)
 #2 0x0000000003467018 CrashRecoverySignalHandler(int) CrashRecoveryContext.cpp:0:0
 #3 0x00007f2ebe242520 (/lib/x86_64-linux-gnu/libc.so.6+0x42520)
 #4 0x0000000002df7e18 llvm::UndefValue::get(llvm::Type*) (/opt/compiler-explorer/clang-18.1.0/bin/clang+++0x2df7e18)
 #5 0x0000000003c405b6 (anonymous namespace)::ScalarExprEmitter::Visit(clang::Expr*) CGExprScalar.cpp:0:0
 #6 0x0000000003c428c3 clang::CodeGen::CodeGenFunction::EmitScalarExpr(clang::Expr const*, bool) (/opt/compiler-explorer/clang-18.1.0/bin/clang+++0x3c428c3)
 #7 0x0000000003819973 clang::CodeGen::CodeGenFunction::EmitReturnStmt(clang::ReturnStmt const&) (/opt/compiler-explorer/clang-18.1.0/bin/clang+++0x3819973)
 #8 0x00000000038222af clang::CodeGen::CodeGenFunction::EmitStmt(clang::Stmt const*, llvm::ArrayRef<clang::Attr const*>) (/opt/compiler-explorer/clang-18.1.0/bin/clang+++0x38222af)
 #9 0x0000000003827b39 clang::CodeGen::CodeGenFunction::EmitCompoundStmtWithoutScope(clang::CompoundStmt const&, bool, clang::CodeGen::AggValueSlot) (/opt/compiler-explorer/clang-18.1.0/bin/clang+++0x3827b39)
#10 0x0000000003885ec4 clang::CodeGen::CodeGenFunction::EmitFunctionBody(clang::Stmt const*) (/opt/compiler-explorer/clang-18.1.0/bin/clang+++0x3885ec4)
#11 0x0000000003891492 clang::CodeGen::CodeGenFunction::GenerateCode(clang::GlobalDecl, llvm::Function*, clang::CodeGen::CGFunctionInfo const&) (/opt/compiler-explorer/clang-18.1.0/bin/clang+++0x3891492)
#12 0x00000000038ec95b clang::CodeGen::CodeGenModule::EmitGlobalFunctionDefinition(clang::GlobalDecl, llvm::GlobalValue*) (/opt/compiler-explorer/clang-18.1.0/bin/clang+++0x38ec95b)
#13 0x00000000038e86d5 clang::CodeGen::CodeGenModule::EmitGlobalDefinition(clang::GlobalDecl, llvm::GlobalValue*) (/opt/compiler-explorer/clang-18.1.0/bin/clang+++0x38e86d5)
#14 0x00000000038f23e1 clang::CodeGen::CodeGenModule::EmitDeferred() (/opt/compiler-explorer/clang-18.1.0/bin/clang+++0x38f23e1)
#15 0x00000000038f4b53 clang::CodeGen::CodeGenModule::Release() (/opt/compiler-explorer/clang-18.1.0/bin/clang+++0x38f4b53)
#16 0x0000000003d5c35a (anonymous namespace)::CodeGeneratorImpl::HandleTranslationUnit(clang::ASTContext&) ModuleBuilder.cpp:0:0
#17 0x0000000003d5b074 clang::BackendConsumer::HandleTranslationUnit(clang::ASTContext&) (/opt/compiler-explorer/clang-18.1.0/bin/clang+++0x3d5b074)
#18 0x0000000005b965d9 clang::ParseAST(clang::Sema&, bool, bool) (/opt/compiler-explorer/clang-18.1.0/bin/clang+++0x5b965d9)
#19 0x0000000003d5a725 clang::CodeGenAction::ExecuteAction() (/opt/compiler-explorer/clang-18.1.0/bin/clang+++0x3d5a725)
#20 0x0000000003fe2aa1 clang::FrontendAction::Execute() (/opt/compiler-explorer/clang-18.1.0/bin/clang+++0x3fe2aa1)
#21 0x0000000003f6010b clang::CompilerInstance::ExecuteAction(clang::FrontendAction&) (/opt/compiler-explorer/clang-18.1.0/bin/clang+++0x3f6010b)
#22 0x00000000040c2473 clang::ExecuteCompilerInvocation(clang::CompilerInstance*) (/opt/compiler-explorer/clang-18.1.0/bin/clang+++0x40c2473)
#23 0x0000000000c19746 cc1_main(llvm::ArrayRef<char const*>, char const*, void*) (/opt/compiler-explorer/clang-18.1.0/bin/clang+++0xc19746)
#24 0x0000000000c1211d ExecuteCC1Tool(llvm::SmallVectorImpl<char const*>&, llvm::ToolContext const&) driver.cpp:0:0
#25 0x0000000003da1979 void llvm::function_ref<void ()>::callback_fn<clang::driver::CC1Command::Execute(llvm::ArrayRef<std::optional<llvm::StringRef>>, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>*, bool*) const::'lambda'()>(long) Job.cpp:0:0
#26 0x0000000003467443 llvm::CrashRecoveryContext::RunSafely(llvm::function_ref<void ()>) (/opt/compiler-explorer/clang-18.1.0/bin/clang+++0x3467443)
#27 0x0000000003da1b99 clang::driver::CC1Command::Execute(llvm::ArrayRef<std::optional<llvm::StringRef>>, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>*, bool*) const (.part.0) Job.cpp:0:0
#28 0x0000000003d69d17 clang::driver::Compilation::ExecuteCommand(clang::driver::Command const&, clang::driver::Command const*&, bool) const (/opt/compiler-explorer/clang-18.1.0/bin/clang+++0x3d69d17)
#29 0x0000000003d6a6cd clang::driver::Compilation::ExecuteJobs(clang::driver::JobList const&, llvm::SmallVectorImpl<std::pair<int, clang::driver::Command const*>>&, bool) const (/opt/compiler-explorer/clang-18.1.0/bin/clang+++0x3d6a6cd)
#30 0x0000000003d7445c clang::driver::Driver::ExecuteCompilation(clang::driver::Compilation&, llvm::SmallVectorImpl<std::pair<int, clang::driver::Command const*>>&) (/opt/compiler-explorer/clang-18.1.0/bin/clang+++0x3d7445c)
#31 0x0000000000c16a21 clang_main(int, char**, llvm::ToolContext const&) (/opt/compiler-explorer/clang-18.1.0/bin/clang+++0xc16a21)
#32 0x0000000000b0fa84 main (/opt/compiler-explorer/clang-18.1.0/bin/clang+++0xb0fa84)
#33 0x00007f2ebe229d90 (/lib/x86_64-linux-gnu/libc.so.6+0x29d90)
#34 0x00007f2ebe229e40 __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x29e40)
#35 0x0000000000c1191e _start (/opt/compiler-explorer/clang-18.1.0/bin/clang+++0xc1191e)
clang++: error: clang frontend command failed with exit code 139 (use -v to see invocation)
Compiler returned: 139
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzkWk1z2zgS_TX0pUsqEqT4cfBBlu1spjK1qcjJHlUg2JSwIQEOADryHPa3b4GgZIKx87Wara1a1UwsggD6vdcNdJMQ1ZrvBeJ1sLoJVrdXtDcHqa47yaQQV6Wsnq4fDgglNvILMFkhcA2UMewMVlA-wZvNBqio4Pftp01ANlD2BqQA2jTwiEpzKTTIGjYNFXuoKW80fOHmAAEhjAohDTDZdrxBMAeuQT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