[clang] Assertion `NewFunction && "Failed to instantiate function template"' failed.
- Dominant language
- LLVM
- Stars
- 40.5k
- Forks
- 18.7k
- PR merge metrics
- PR metrics pending
Description
Although it does not reproduce on Godbolt, the following reproducer triggers an assertion failure with the clang I built today.
Reproducer:
```cpp
template constexpr int foo(T);
const int i = foo(42);
template constexpr int foo(T) { return bar; }
```
Backtrace:
```console
#0 0x00007a258e3d4257 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int)
#1 0x00007a258e3d10fe llvm::sys::RunSignalHandlers()
#2 0x00007a258e3d2dde llvm::sys::CleanupOnSignal(unsigned long)
#3 0x00007a258e2e6f78 CrashRecoverySignalHandler(int) CrashRecoveryContext.cpp:0:0
#4 0x00007a258da42520 (/lib/x86_64-linux-gnu/libc.so.6+0x42520)
#5 0x00007a258da969fc __pthread_kill_implementation ./nptl/pthread_kill.c:44:76
#6 0x00007a258da969fc __pthread_kill_internal ./nptl/pthread_kill.c:78:10
#7 0x00007a258da969fc pthread_kill ./nptl/pthread_kill.c:89:10
#8 0x00007a258da42476 gsignal ./signal/../sysdeps/posix/raise.c:27:6
#9 0x00007a258da287f3 abort ./stdlib/abort.c:81:7
#10 0x00007a258da2871b _nl_load_domain ./intl/loadmsgcat.c:1177:9
#11 0x00007a258da39e96 (/lib/x86_64-linux-gnu/libc.so.6+0x39e96)
#12 0x00007a258b07969c clang::Sema::InstantiateFunctionDefinition(clang::SourceLocation, clang::FunctionDecl*, bool, bool, bool)
#13 0x00007a258b07cae7 clang::Sema::PerformPendingInstantiations(bool, bool)
#14 0x00007a258a711e78 clang::Sema::ActOnEndOfTranslationUnitFragment(clang::TUFragmentKind)
#15 0x00007a258a7127e6 clang::Sema::ActOnEndOfTranslationUnit()
#16 0x00007a258d1a49c4 clang::Parser::ParseTopLevelDecl(clang::OpaquePtr&, clang::Sema::ModuleImportState&)
#17 0x00007a258d0a9ade clang::ParseAST(clang::Sema&, bool, bool)
#18 0x00007a2590971346 clang::FrontendAction::Execute()
#19 0x00007a25908cd2a8 clang::CompilerInstance::ExecuteAction(clang::FrontendAction&)
#20 0x00007a25948ef8b5 clang::ExecuteCompilerInvocation(clang::CompilerInstance*)
#21 0x000064a5ca072e31 cc1_main(llvm::ArrayRef, char const*, void*)
#22 0x000064a5ca06ef88 ExecuteCC1Tool(llvm::SmallVectorImpl&, llvm::ToolContext const&, llvm::IntrusiveRefCntPtr) driver.cpp:0:0
#23 0x000064a5ca071359 int llvm::function_ref&)>::callback_fn(long, llvm::SmallVectorImpl&) driver.cpp:0:0
#24 0x00007a2590cc3259 void llvm::function_ref::callback_fn>, std::__cxx11::basic_string, std::allocator>*, bool*) const::$_1>(long) Job.cpp:0:0
#25 0x00007a258e2e6c8b llvm::CrashRecoveryContext::RunSafely(llvm::function_ref)
#26 0x00007a2590cc2537 clang::driver::CC1Command::Execute(llvm::ArrayRef>, std::__cxx11::basic_string, std::allocator>*, bool*) const
#27 0x00007a2590c745b3 clang::driver::Compilation::ExecuteCommand(clang::driver::Command const&, clang::driver::Command const*&, bool) const
#28 0x00007a2590c748b7 clang::driver::Compilation::ExecuteJobs(clang::driver::JobList const&, llvm::SmallVectorImpl>&, bool) const
#29 0x00007a2590c9ce48 clang::driver::Driver::ExecuteCompilation(clang::driver::Compilation&, llvm::SmallVectorImpl>&)
#30 0x000064a5ca06e58c clang_main(int, char**, llvm::ToolContext const&)
#31 0x000064a5ca07e5cd main
#32 0x00007a258da29d90 __libc_start_call_main ./csu/../sysdeps/nptl/libc_start_call_main.h:58:16
#33 0x00007a258da29e40 call_init ./csu/../csu/libc-start.c:128:20
#34 0x00007a258da29e40 __libc_start_main ./csu/../csu/libc-start.c:379:5
#35 0x000064a5ca06c8e5 _start
clang++: error: clang frontend command failed due to signal (use -v to see invocation)
clang version 23.0.0git (https://github.com/llvm/llvm-project.git baa69e929b67543682452e9d9f9ebbd76790ad42)
Target: x86_64-unknown-linux-gnu
Thread model: posix
```
Assertion failure:
```console
llvm-project/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp:6012: void clang::Sema::InstantiateFunctionDefinition(clang::SourceLocation, clang::FunctionDecl *, bool, bool, bool): Assertion `NewFunction && "Failed to instantiate function template"' failed.
```
Contributor guide
Research direction
Start by compiling the supplied C++ reproducer with clang++ and inspect clang/lib/Sema/SemaTemplateInstantiateDecl.cpp at the assertion in InstantiateFunctionDefinition, following PerformPendingInstantiations from the backtrace. Done means the reproducer no longer aborts, with a regression test covering the case.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cpp
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 50/100