When compiled with an asserts-enabled build of clang, the following code template<typename> struct wrapper { template<typename> friend void friend_function_template() {} }; wrapper<bool> x; wrapper<int> y; Causes the following error: clang-8: /home/david/llvm/clang/lib/AST/DeclTemplate.cpp:334: void clang::FunctionTemplateDecl::mergePrevDecl(clang::FunctionTemplateDecl *): Assertion `ThisCommon->Specializations.size() == 0 && !ThisCommon->InstantiatedFromMember.getPointer() && !ThisCommon->InstantiatedFromMember.getInt() && "Can't merge incompatible declarations!"' failed. Stack dump: 0. Program arguments: /home/david/llvm/build/bin/clang-8 -cc1 -triple x86_64-unknown-linux-gnu -emit-obj -mrelax-all -disable-free -main-file-name concatenate_view.cpp -mrelocation-model static -mthread-model posix -mdisable-fp-elim -fmath-errno -masm-verbose -mconstructor-aliases -munwind-tables -fuse-init-array -target-cpu x86-64 -dwarf-column-info -debugger-tuning=gdb -coverage-notes-file /home/david/tm1/bounded_integer/build/concatenate_view.gcno -resource-dir /home/david/llvm/build/lib/clang/8.0.0 -internal-isystem /usr/lib/gcc/x86_64-pc-linux-gnu/8.2.0/include/g++-v8 -internal-isystem /usr/lib/gcc/x86_64-pc-linux-gnu/8.2.0/include/g++-v8/x86_64-pc-linux-gnu -internal-isystem /usr/lib/gcc/x86_64-pc-linux-gnu/8.2.0/include/g++-v8/backward -internal-isystem /usr/local/include -internal-isystem /home/david/llvm/build/lib/clang/8.0.0/include -internal-externc-isystem /include -internal-externc-isystem /usr/include -fdeprecated-macro -fdebug-compilation-dir /home/david/tm1/bounded_integer/build -ferror-limit 19 -fmessage-length 211 -fobjc-runtime=gcc -fcxx-exceptions -fexceptions -fdiagnostics-show-option -fcolor-diagnostics -o concatenate_view.o -x c++ ../source/containers/algorithms/concatenate_view.cpp -faddrsig 1. ../source/containers/algorithms/concatenate_view.cpp:8:15: current parser token ';' 2. ../source/containers/algorithms/concatenate_view.cpp:2:8: instantiating class definition 'wrapper<int>' #0 0x00000000017a8cfd PrintStackTraceSignalHandler(void*) (.llvm.12021122941227921363) (/home/david/llvm/build/bin/clang-8+0x17a8cfd) #1 0x00000000017a651e llvm::sys::RunSignalHandlers() (/home/david/llvm/build/bin/clang-8+0x17a651e) #2 0x00000000017a8eb8 SignalHandler(int) (/home/david/llvm/build/bin/clang-8+0x17a8eb8) #3 0x00007f46c3365360 __restore_rt (/lib64/libpthread.so.0+0x14360) #4 0x00007f46c261114b gsignal (/lib64/libc.so.6+0x3814b) #5 0x00007f46c25fa41d abort (/lib64/libc.so.6+0x2141d) #6 0x00007f46c25fa2ef __tls_get_addr (/lib64/libc.so.6+0x212ef) #7 0x00007f46c2608a52 (/lib64/libc.so.6+0x2fa52) #8 0x0000000003867748 clang::FunctionTemplateDecl::mergePrevDecl(clang::FunctionTemplateDecl*) (/home/david/llvm/build/bin/clang-8+0x3867748) #9 0x0000000002f8cfc7 clang::Sema::CheckFunctionDeclaration(clang::Scope*, clang::FunctionDecl*, clang::LookupResult&, bool) (/home/david/llvm/build/bin/clang-8+0x2f8cfc7) #10 0x000000000355bcd6 clang::TemplateDeclInstantiator::VisitFunctionDecl(clang::FunctionDecl*, clang::TemplateParameterList*) (/home/david/llvm/build/bin/clang-8+0x355bcd6) #11 0x00000000035593c4 clang::TemplateDeclInstantiator::VisitFunctionTemplateDecl(clang::FunctionTemplateDecl*) (/home/david/llvm/build/bin/clang-8+0x35593c4) #12 0x00000000035552c3 clang::TemplateDeclInstantiator::VisitFriendDecl(clang::FriendDecl*) (/home/david/llvm/build/bin/clang-8+0x35552c3) #13 0x0000000003517db7 clang::Sema::InstantiateClass(clang::SourceLocation, clang::CXXRecordDecl*, clang::CXXRecordDecl*, clang::MultiLevelTemplateArgumentList const&, clang::TemplateSpecializationKind, bool) (/home/david/llvm/build/bin/clang-8+0x3517db7) #14 0x000000000351a026 clang::Sema::InstantiateClassTemplateSpecialization(clang::SourceLocation, clang::ClassTemplateSpecializationDecl*, clang::TemplateSpecializationKind, bool) (/home/david/llvm/build/bin/clang-8+0x351a026) #15 0x00000000035a367d clang::Sema::RequireCompleteTypeImpl(clang::SourceLocation, clang::QualType, clang::Sema::TypeDiagnoser*) (/home/david/llvm/build/bin/clang-8+0x35a367d) #16 0x00000000035a3023 clang::Sema::RequireCompleteType(clang::SourceLocation, clang::QualType, clang::Sema::TypeDiagnoser&) (/home/david/llvm/build/bin/clang-8+0x35a3023) #17 0x0000000002f6e6d7 clang::Sema::ActOnUninitializedDecl(clang::Decl*) (/home/david/llvm/build/bin/clang-8+0x2f6e6d7) #18 0x0000000002bb1956 clang::Parser::ParseDeclarationAfterDeclaratorAndAttributes(clang::Declarator&, clang::Parser::ParsedTemplateInfo const&, clang::Parser::ForRangeInit*) (/home/david/llvm/build/bin/clang-8+0x2bb1956) #19 0x0000000002baff4e clang::Parser::ParseDeclGroup(clang::ParsingDeclSpec&, clang::DeclaratorContext, clang::SourceLocation*, clang::Parser::ForRangeInit*) (/home/david/llvm/build/bin/clang-8+0x2baff4e) #20 0x0000000002b8e658 clang::Parser::ParseDeclOrFunctionDefInternal(clang::Parser::ParsedAttributesWithRange&, clang::ParsingDeclSpec&, clang::AccessSpecifier) (/home/david/llvm/build/bin/clang-8+0x2b8e658) #21 0x0000000002b8ded9 clang::Parser::ParseDeclarationOrFunctionDefinition(clang::Parser::ParsedAttributesWithRange&, clang::ParsingDeclSpec*, clang::AccessSpecifier) (/home/david/llvm/build/bin/clang-8+0x2b8ded9) #22 0x0000000002b8c3ec clang::Parser::ParseExternalDeclaration(clang::Parser::ParsedAttributesWithRange&, clang::ParsingDeclSpec*) (/home/david/llvm/build/bin/clang-8+0x2b8c3ec) #23 0x0000000002b8b2ad clang::Parser::ParseTopLevelDecl(clang::OpaquePtr<clang::DeclGroupRef>&) (/home/david/llvm/build/bin/clang-8+0x2b8b2ad) #24 0x0000000002b86446 clang::ParseAST(clang::Sema&, bool, bool) (/home/david/llvm/build/bin/clang-8+0x2b86446) #25 0x0000000001f9e06b clang::FrontendAction::Execute() (/home/david/llvm/build/bin/clang-8+0x1f9e06b) #26 0x0000000001f36f01 clang::CompilerInstance::ExecuteAction(clang::FrontendAction&) (/home/david/llvm/build/bin/clang-8+0x1f36f01) #27 0x00000000020511b2 clang::ExecuteCompilerInvocation(clang::CompilerInstance*) (/home/david/llvm/build/bin/clang-8+0x20511b2) #28 0x00000000007fe77d cc1_main(llvm::ArrayRef<char const*>, char const*, void*) (/home/david/llvm/build/bin/clang-8+0x7fe77d) #29 0x00000000007fc825 main (/home/david/llvm/build/bin/clang-8+0x7fc825) #30 0x00007f46c25fc4eb __libc_start_main (/lib64/libc.so.6+0x234eb) #31 0x00000000007f92ba _start (/home/david/llvm/build/bin/clang-8+0x7f92ba) clang-8: error: unable to execute command: Aborted (core dumped) clang-8: error: clang frontend command failed due to signal (use -v to see invocation) clang version 8.0.0 Target: x86_64-unknown-linux-gnu Thread model: posix InstalledDir: /home/david/llvm/build/bin clang-8: note: diagnostic msg: PLEASE submit a bug report to https://bugs.llvm.org/ and include the crash backtrace, preprocessed source, and associated run script. clang-8: note: diagnostic msg: ******************** PLEASE ATTACH THE FOLLOWING FILES TO THE BUG REPORT: Preprocessed source(s) and associated run script(s) are located at: clang-8: note: diagnostic msg: /tmp/concatenate_view-c22b50.cpp clang-8: note: diagnostic msg: /tmp/concatenate_view-c22b50.sh clang-8: note: diagnostic msg: ********************
We have also run into the same problem. It is fairly high priority for us.
This bug bisects to r344157 [Sema] Fix a multiple definition bug with friends and templates The problem was that MergeFunctionDecl sometimes needs the injected template arguments of a FunctionTemplateDecl, but is called before adding the new template to the redecl chain. This leads to multiple common pointers in the same redecl chain, each with their own identical instantiation. Fix this by merging the the common state before inserting the new template into the redecl chain.
Fixed in r348325. Thanks!
The code presented in this issue is ill-formed, - the two instantiations of `wrapper` creates two definitions of `friend_function_template`. This error is detected with patch https://reviews.llvm.org/D21508.