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

    <tr>
        <th>Summary</th>
        <td>
            [Clang] [C++20] Compile error in special cases
        </td>
    </tr>

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

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

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

<pre>
    ```cpp
template<typename>
struct A {
  template<typename>
  friend void func(A);
};

template<typename, typename T>
struct B {
 template<typename>
  friend void func(A<int>) {
    T _{};
 }
};

auto main() -> int {
  B<int, int> _{};
 func<int>(A<int>{});
}
```

#### error message:

```txt
Stack dump:
0.      Program arguments: /opt/compiler-explorer/clang-trunk/bin/clang++ -gdwarf-4 -g -o /app/output.s -mllvm --x86-asm-syntax=intel -fno-verbose-asm -S --gcc-toolchain=/opt/compiler-explorer/gcc-snapshot -fcolor-diagnostics -fno-crash-diagnostics -std=c++20 <source> -isystem/opt/compiler-explorer/libs/google-benchmark/trunk/include
1.      <eof> parser at end of file
2.      Per-file LLVM IR generation
3.      <source>:11:15: Generating code for declaration 'func'
 #0 0x00000000038b7b48 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x38b7b48)
 #1 0x00000000038b5cb4 llvm::sys::CleanupOnSignal(unsigned long) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x38b5cb4)
 #2 0x00000000037ffc48 CrashRecoverySignalHandler(int) CrashRecoveryContext.cpp:0:0
 #3 0x000078a9b4c42520 (/lib/x86_64-linux-gnu/libc.so.6+0x42520)
 #4 0x00000000072fc220 clang::ASTContext::getTypeInfoImpl(clang::Type const*) const (/opt/compiler-explorer/clang-trunk/bin/clang+++0x72fc220)
 #5 0x00000000072e65ab clang::ASTContext::getTypeInfo(clang::Type const*) const (/opt/compiler-explorer/clang-trunk/bin/clang+++0x72e65ab)
 #6 0x00000000072fbe2b clang::ASTContext::getPreferredTypeAlign(clang::Type const*) const (/opt/compiler-explorer/clang-trunk/bin/clang+++0x72fbe2b)
 #7 0x000000000730517c clang::ASTContext::getDeclAlign(clang::Decl const*, bool) const (/opt/compiler-explorer/clang-trunk/bin/clang+++0x730517c)
 #8 0x000000000400580d clang::CodeGen::CodeGenFunction::EmitAutoVarAlloca(clang::VarDecl const&) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x400580d)
 #9 0x000000000400c1c1 clang::CodeGen::CodeGenFunction::EmitVarDecl(clang::VarDecl const&) (.part.0) CGDecl.cpp:0:0
#10 0x000000000400c298 clang::CodeGen::CodeGenFunction::EmitDecl(clang::Decl const&) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x400c298)
#11 0x0000000003beb14f clang::CodeGen::CodeGenFunction::EmitDeclStmt(clang::DeclStmt const&) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x3beb14f)
#12 0x0000000003bff53a clang::CodeGen::CodeGenFunction::EmitSimpleStmt(clang::Stmt const*, llvm::ArrayRef<clang::Attr const*>) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x3bff53a)
#13 0x0000000003bf8392 clang::CodeGen::CodeGenFunction::EmitStmt(clang::Stmt const*, llvm::ArrayRef<clang::Attr const*>) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x3bf8392)
#14 0x0000000003bfefe9 clang::CodeGen::CodeGenFunction::EmitCompoundStmtWithoutScope(clang::CompoundStmt const&, bool, clang::CodeGen::AggValueSlot) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x3bfefe9)
#15 0x0000000003c6658a clang::CodeGen::CodeGenFunction::EmitFunctionBody(clang::Stmt const*) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x3c6658a)
#16 0x0000000003c71828 clang::CodeGen::CodeGenFunction::GenerateCode(clang::GlobalDecl, llvm::Function*, clang::CodeGen::CGFunctionInfo const&) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x3c71828)
#17 0x0000000003ccd69b clang::CodeGen::CodeGenModule::EmitGlobalFunctionDefinition(clang::GlobalDecl, llvm::GlobalValue*) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x3ccd69b)
#18 0x0000000003cc8ee5 clang::CodeGen::CodeGenModule::EmitGlobalDefinition(clang::GlobalDecl, llvm::GlobalValue*) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x3cc8ee5)
#19 0x0000000003cd4001 clang::CodeGen::CodeGenModule::EmitDeferred() (/opt/compiler-explorer/clang-trunk/bin/clang+++0x3cd4001)
#20 0x0000000003cd5f93 clang::CodeGen::CodeGenModule::Release() (/opt/compiler-explorer/clang-trunk/bin/clang+++0x3cd5f93)
#21 0x00000000041c8a9e (anonymous namespace)::CodeGeneratorImpl::HandleTranslationUnit(clang::ASTContext&) ModuleBuilder.cpp:0:0
#22 0x00000000041c67f5 clang::BackendConsumer::HandleTranslationUnit(clang::ASTContext&) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x41c67f5)
#23 0x00000000061a544c clang::ParseAST(clang::Sema&, bool, bool) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x61a544c)
#24 0x00000000041c71e5 clang::CodeGenAction::ExecuteAction() (/opt/compiler-explorer/clang-trunk/bin/clang+++0x41c71e5)
#25 0x0000000004472741 clang::FrontendAction::Execute() (/opt/compiler-explorer/clang-trunk/bin/clang+++0x4472741)
#26 0x00000000043f493b clang::CompilerInstance::ExecuteAction(clang::FrontendAction&) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x43f493b)
#27 0x0000000004559193 clang::ExecuteCompilerInvocation(clang::CompilerInstance*) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x4559193)
#28 0x0000000000cc5dfc cc1_main(llvm::ArrayRef<char const*>, char const*, void*) (/opt/compiler-explorer/clang-trunk/bin/clang+++0xcc5dfc)
#29 0x0000000000cbfd3d ExecuteCC1Tool(llvm::SmallVectorImpl<char const*>&, llvm::ToolContext const&) driver.cpp:0:0
#30 0x0000000004210999 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
#31 0x0000000003800073 llvm::CrashRecoveryContext::RunSafely(llvm::function_ref<void ()>) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x3800073)
#32 0x0000000004210bb9 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
#33 0x00000000041d67f7 clang::driver::Compilation::ExecuteCommand(clang::driver::Command const&, clang::driver::Command const*&, bool) const (/opt/compiler-explorer/clang-trunk/bin/clang+++0x41d67f7)
#34 0x00000000041d71ad clang::driver::Compilation::ExecuteJobs(clang::driver::JobList const&, llvm::SmallVectorImpl<std::pair<int, clang::driver::Command const*>>&, bool) const (/opt/compiler-explorer/clang-trunk/bin/clang+++0x41d71ad)
#35 0x00000000041e119c clang::driver::Driver::ExecuteCompilation(clang::driver::Compilation&, llvm::SmallVectorImpl<std::pair<int, clang::driver::Command const*>>&) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x41e119c)
#36 0x0000000000cc30e1 clang_main(int, char**, llvm::ToolContext const&) (/opt/compiler-explorer/clang-trunk/bin/clang+++0xcc30e1)
#37 0x0000000000b98984 main (/opt/compiler-explorer/clang-trunk/bin/clang+++0xb98984)
#38 0x000078a9b4c29d90 (/lib/x86_64-linux-gnu/libc.so.6+0x29d90)
#39 0x000078a9b4c29e40 __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x29e40)
#40 0x0000000000cbf7de _start (/opt/compiler-explorer/clang-trunk/bin/clang+++0xcbf7de)
clang++: error: clang frontend command failed with exit code 139 (use -v to see invocation)
Compiler returned: 139
```

#### Additional Information:

Version: clang version 18.1.8
compilation parameters: -std=c++20
Link: [godbolt](https://godbolt.org/z/YdqKEEK45)

PS: The code compiles successfully on gcc and msvc.
 
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzkWlFznDgS_jX4RcUUCBjgwQ_jmTiX3WxdKvbl6p6mhGjGXITEScJr36-_ksTMIGwntovd2qrbyiaMaKm_71Or1QiIUu2BA1wG2VWQ7S7IoO-EvPwiGJGfW16T9UUl6sfLYB25P7Tvg2gXRBsNXc-IhiDZ6sceOOkgSD64e0rLgWq0QUF-5VoQ-pE9Qo1sgdfoXrQ1agZOA1xsAlwGydg_yHfn6xf94y06XqPbOZqrCZp3gEm2LdfGCJdTWgjdor35PQGIzI8XYJNBC9SRlge4MEOFQfIBtVxPx7waneEtcj6ferCwzpCm-JzlTDt3cZzEKaAAJ6c_CKQUEnWgFDlAkGw8w2Nv_aBdy40m9Duqh64_mUarICq_SHGQpENEHoYOuFZBskEBvha9DvA1FV3fMpAhPPRMSJCmjRF-CLUc-PcAX1dGHdcW4KsAX6HwUP9OZBOmKDygUJjBSN-bIQfdD3qlUNgxdt-hMHwo1iFRXageuSYPQbJruQaGwoaL8B5kJRSY-yi8QWF4oDTUQjB6Z2Yk2f0QpDFWnPTqTmgUNlQwIcO6JQculG6pcj6oJOrOb1a6DpIddVxwhIJkq8QgqYk5FLbqUWnofuiatZUyCIQ4MAgr4PSuI9JoddSs5ZQNNbhZiM0sBMkWRGNc9EQqkIhoZMJaNKhp2WiJ7XyBDE0T-vz522_o01d0AA6S6FZwZ5WM451QB8kmjs1fmZnaj6M5PyAqakCNkKgGyogbAwU4d8soP64PnEQoeoiO_yVFlVdpgcwUmlBKNupRuYsvsuXaBtqtJBQCXJyNJPl9L5SWQLoAr4_LxaxPs7beH24BvooeRkxmIZ1AxzPQGa3S50BvGRA-9H_nN-2BExbgYuA2z9aICeNlMYgGgQcRexDzpqFpgbYmJr8CFfcgHx2mvxFeM-OpGDXzbLaCa3jQK5Ptk01k_z-5SEYXeUHKKqUpzkx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