Issue 96485
Summary Friend-injecting a template function causes an ICE if you inject after trying to instantiate that function
Labels new issue
Assignees
Reporter HolyBlackCat
    The following causes an internal compiler error in Clang 18 and trunk: https://gcc.godbolt.org/z/r9vvWGnnY

GCC also segfaults here, while MSVC compiles and runs this successfully, calling `bar<10,20>()`.
```cpp
template <int X, int Y> void bar() {}

template <typename T>
struct Reader
{
    template <int X>
    friend void foo(Reader<T>);
};

template <typename T, int Y>
struct Writer
{
    template <int X>
    friend void foo(Reader<T>) {bar<X, Y>();}
};

int main()
{
 foo<10>(Reader<int>{});
    Writer<int, 20>{};
}
```
Clang says:
```
<source>:14:33: error: cannot compile this l-value _expression_ yet
   14 |     friend void foo(Reader<T>) {bar<X, Y>();}
      | ^~~~~~~~~
```
And then spits out the stacktrace.

But if you e.g. try to `cout` `X` and `Y`, it doesn't print this error and immediately proceeds to the stacktrace.

<details>
<summary>Stack</summary>

```
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-trunk/bin/clang++ -gdwarf-4 -g -o /app/output.s -mllvm --x86-asm-syntax=intel -S --gcc-toolchain=/opt/compiler-explorer/gcc-snapshot -fcolor-diagnostics -fno-crash-diagnostics <source>
1.	<eof> parser at end of file
2.	Per-file LLVM IR generation
3.	<source>:14:17: Generating code for declaration 'foo'
 #0 0x00000000037a10a8 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x37a10a8)
 #1 0x000000000379f1f4 llvm::sys::CleanupOnSignal(unsigned long) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x379f1f4)
 #2 0x00000000036e77c8 CrashRecoverySignalHandler(int) CrashRecoveryContext.cpp:0:0
 #3 0x000079f5cc442520 (/lib/x86_64-linux-gnu/libc.so.6+0x42520)
 #4 0x0000000003e3b6e7 arrangeFreeFunctionLikeCall(clang::CodeGen::CodeGenTypes&, clang::CodeGen::CodeGenModule&, clang::CodeGen::CallArgList const&, clang::FunctionType const*, unsigned int, bool) CGCall.cpp:0:0
 #5 0x0000000003ed9879 clang::CodeGen::CodeGenFunction::EmitCall(clang::QualType, clang::CodeGen::CGCallee const&, clang::CallExpr const*, clang::CodeGen::ReturnValueSlot, llvm::Value*) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x3ed9879)
 #6 0x0000000003edb237 clang::CodeGen::CodeGenFunction::EmitCallExpr(clang::CallExpr const*, clang::CodeGen::ReturnValueSlot) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x3edb237)
 #7 0x0000000003f24c7a (anonymous namespace)::ScalarExprEmitter::VisitCallExpr(clang::CallExpr const*) CGExprScalar.cpp:0:0
 #8 0x0000000003f1b6a1 clang::StmtVisitorBase<std::add_pointer, (anonymous namespace)::ScalarExprEmitter, llvm::Value*>::Visit(clang::Stmt*) CGExprScalar.cpp:0:0
 #9 0x0000000003f20a92 clang::CodeGen::CodeGenFunction::EmitScalarExpr(clang::Expr const*, bool) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x3f20a92)
#10 0x0000000003ebce8e clang::CodeGen::CodeGenFunction::EmitAnyExpr(clang::Expr const*, clang::CodeGen::AggValueSlot, bool) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x3ebce8e)
#11 0x0000000003ed750d clang::CodeGen::CodeGenFunction::EmitIgnoredExpr(clang::Expr const*) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x3ed750d)
#12 0x0000000003acf3be clang::CodeGen::CodeGenFunction::EmitStmt(clang::Stmt const*, llvm::ArrayRef<clang::Attr const*>) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x3acf3be)
#13 0x0000000003ad5db9 clang::CodeGen::CodeGenFunction::EmitCompoundStmtWithoutScope(clang::CompoundStmt const&, bool, clang::CodeGen::AggValueSlot) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x3ad5db9)
#14 0x0000000003b36a96 clang::CodeGen::CodeGenFunction::EmitFunctionBody(clang::Stmt const*) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x3b36a96)
#15 0x0000000003b439f8 clang::CodeGen::CodeGenFunction::GenerateCode(clang::GlobalDecl, llvm::Function*, clang::CodeGen::CGFunctionInfo const&) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x3b439f8)
#16 0x0000000003b9b73b clang::CodeGen::CodeGenModule::EmitGlobalFunctionDefinition(clang::GlobalDecl, llvm::GlobalValue*) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x3b9b73b)
#17 0x0000000003b971c5 clang::CodeGen::CodeGenModule::EmitGlobalDefinition(clang::GlobalDecl, llvm::GlobalValue*) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x3b971c5)
#18 0x0000000003ba1e51 clang::CodeGen::CodeGenModule::EmitDeferred() (/opt/compiler-explorer/clang-trunk/bin/clang+++0x3ba1e51)
#19 0x0000000003ba42a3 clang::CodeGen::CodeGenModule::Release() (/opt/compiler-explorer/clang-trunk/bin/clang+++0x3ba42a3)
#20 0x000000000403fa7e (anonymous namespace)::CodeGeneratorImpl::HandleTranslationUnit(clang::ASTContext&) ModuleBuilder.cpp:0:0
#21 0x000000000403dac5 clang::BackendConsumer::HandleTranslationUnit(clang::ASTContext&) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x403dac5)
#22 0x0000000005f5e2ac clang::ParseAST(clang::Sema&, bool, bool) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x5f5e2ac)
#23 0x000000000403e245 clang::CodeGenAction::ExecuteAction() (/opt/compiler-explorer/clang-trunk/bin/clang+++0x403e245)
#24 0x00000000042ddde1 clang::FrontendAction::Execute() (/opt/compiler-explorer/clang-trunk/bin/clang+++0x42ddde1)
#25 0x00000000042616db clang::CompilerInstance::ExecuteAction(clang::FrontendAction&) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x42616db)
#26 0x00000000043c4b13 clang::ExecuteCompilerInvocation(clang::CompilerInstance*) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x43c4b13)
#27 0x0000000000c7467c cc1_main(llvm::ArrayRef<char const*>, char const*, void*) (/opt/compiler-explorer/clang-trunk/bin/clang+++0xc7467c)
#28 0x0000000000c6e77d ExecuteCC1Tool(llvm::SmallVectorImpl<char const*>&, llvm::ToolContext const&) driver.cpp:0:0
#29 0x0000000004087889 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
#30 0x00000000036e7bf3 llvm::CrashRecoveryContext::RunSafely(llvm::function_ref<void ()>) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x36e7bf3)
#31 0x0000000004087aa9 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
#32 0x000000000404d287 clang::driver::Compilation::ExecuteCommand(clang::driver::Command const&, clang::driver::Command const*&, bool) const (/opt/compiler-explorer/clang-trunk/bin/clang+++0x404d287)
#33 0x000000000404dc3d 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+++0x404dc3d)
#34 0x0000000004057d9c clang::driver::Driver::ExecuteCompilation(clang::driver::Compilation&, llvm::SmallVectorImpl<std::pair<int, clang::driver::Command const*>>&) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x4057d9c)
#35 0x0000000000c71b21 clang_main(int, char**, llvm::ToolContext const&) (/opt/compiler-explorer/clang-trunk/bin/clang+++0xc71b21)
#36 0x0000000000b4d804 main (/opt/compiler-explorer/clang-trunk/bin/clang+++0xb4d804)
#37 0x000079f5cc429d90 (/lib/x86_64-linux-gnu/libc.so.6+0x29d90)
#38 0x000079f5cc429e40 __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x29e40)
#39 0x0000000000c6e21e _start (/opt/compiler-explorer/clang-trunk/bin/clang+++0xc6e21e)
```
</details>

_______________________________________________
llvm-bugs mailing list
llvm-bugs@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to