Skip to content

Clang-19: Unable to find instantiation of declaration! UNREACHABLE executed at /llvm-project/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp:6437! #112069

@iamanonymouscs

Description

@iamanonymouscs

Description

The bug arises during Clang's template instantiation process when attempting to specialize a template for multi-dimensional arrays. The compiler fails with an internal error, specifically producing the message:

Unable to find instantiation of declaration!
UNREACHABLE executed at /root/llvm-project/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp:6437!

The issue can also be reproduced on Compiler Explorer: https://godbolt.org/z/MPrzKqPP1

Code

#include <typeinfo>
#include <iostream>

// generic template
template<class T>
struct Array_traits
{
  template<class U>
  struct Rebind
  {
    typedef U Type;
  };
};

// partial specialization for arrays
template<class T, int N>
struct Array_traits<T[N]>
{
  template<class U>
  struct Rebind
  {
    typedef Array_traits<T>::Rebind<U>::Type Type[N];
  };
};

int main()
{
  typedef int array_of_int[10][20][30];
  Array_traits<array_of_int>::Rebind<float>::Type array_of_float;

  // it prints out A10_A20_A30_i
  std::cout << typeid(array_of_int).name() << std::endl;

  // it should print A10_A20_A30_f, but it prints A10_f
  std::cout << typeid(array_of_float).name() << std::endl;
}

// Fix:
// generic template
template<class T, class U> 
struct Array_rebind
{
  typedef U Type;
};

// partial specialization
template<class T, int N, class U>
struct Array_rebind<T[N], U>
{
  typedef Array_rebind<T,U>::Type Type[N];
};

Command

# clang++ bugreport_0_1.cpp -std=c++11 -O0 -g -Wall -Wextra -pedantic
bugreport_0_1.cpp:22:13: warning: missing 'typename' prior to dependent type name Array_traits<T>::Rebind<U>::Type; implicit 'typename' is a C++20 extension [-Wc++20-extensions]
   22 |     typedef Array_traits<T>::Rebind<U>::Type Type[N];
      |             ^
      |             typename 
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: /usr/lib/llvm-19/bin/clang -cc1 -triple x86_64-pc-linux-gnu -emit-obj -mrelax-all -dumpdir a- -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name bugreport_0_1.cpp -mrelocation-model pic -pic-level 2 -pic-is-pie -mframe-pointer=all -fmath-errno -ffp-contract=on -fno-rounding-math -mconstructor-aliases -funwind-tables=2 -target-cpu x86-64 -tune-cpu generic -debug-info-kind=constructor -dwarf-version=5 -debugger-tuning=gdb -fdebug-compilation-dir=/root/gdbtest/gcctest/gcc_llvm/gcc/log2/NoAttachment/fixed/6327 -fcoverage-compilation-dir=/root/gdbtest/gcctest/gcc_llvm/gcc/log2/NoAttachment/fixed/6327 -resource-dir /usr/lib/llvm-19/lib/clang/19 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/7/../../../../include/c++/7 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/7/../../../../include/x86_64-linux-gnu/c++/7 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/7/../../../../include/c++/7/backward -internal-isystem /usr/lib/llvm-19/lib/clang/19/include -internal-isystem /usr/local/include -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/7/../../../../x86_64-linux-gnu/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -O0 -Wall -Wextra -pedantic -std=c++11 -fdeprecated-macro -ferror-limit 19 -fgnuc-version=4.2.1 -fskip-odr-check-in-gmf -fcxx-exceptions -fexceptions -fcolor-diagnostics -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /tmp/bugreport_0_1-2e2edc.o -x c++ bugreport_0_1.cpp
1.      bugreport_0_1.cpp:29:46: current parser token 'Type'
2.      bugreport_0_1.cpp:27:1: parsing function body 'main'
3.      bugreport_0_1.cpp:27:1: in compound statement ('{}')
4.      bugreport_0_1.cpp:20:10: instantiating class definition 'Array_traits<int[10][20][30]>::Rebind<float>'
 #0 0x00007f327e812216 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/usr/lib/llvm-19/bin/../lib/libLLVM.so.19.0+0xdc1216)
 #1 0x00007f327e80fec0 llvm::sys::RunSignalHandlers() (/usr/lib/llvm-19/bin/../lib/libLLVM.so.19.0+0xdbeec0)
 #2 0x00007f327e8128f0 (/usr/lib/llvm-19/bin/../lib/libLLVM.so.19.0+0xdc18f0)
 #3 0x00007f3289201980 __restore_rt (/lib/x86_64-linux-gnu/libpthread.so.0+0x12980)
 #4 0x00007f3286d846bd clang::Sema::FindInstantiatedDecl(clang::SourceLocation, clang::NamedDecl*, clang::MultiLevelTemplateArgumentList const&, bool) (/usr/lib/llvm-19/bin/../lib/libclang-cpp.so.19.0+0x1bda6bd)
 #5 0x00007f3286d53ceb (/usr/lib/llvm-19/bin/../lib/libclang-cpp.so.19.0+0x1ba9ceb)
 #6 0x00007f3286d6989f (/usr/lib/llvm-19/bin/../lib/libclang-cpp.so.19.0+0x1bbf89f)
 #7 0x00007f3286d53011 (/usr/lib/llvm-19/bin/../lib/libclang-cpp.so.19.0+0x1ba9011)
 #8 0x00007f3286d6b047 (/usr/lib/llvm-19/bin/../lib/libclang-cpp.so.19.0+0x1bc1047)
 #9 0x00007f3286d6be3a (/usr/lib/llvm-19/bin/../lib/libclang-cpp.so.19.0+0x1bc1e3a)
#10 0x00007f3286d4b090 (/usr/lib/llvm-19/bin/../lib/libclang-cpp.so.19.0+0x1ba1090)
#11 0x00007f3286d4ad5c clang::Sema::SubstType(clang::TypeSourceInfo*, clang::MultiLevelTemplateArgumentList const&, clang::SourceLocation, clang::DeclarationName, bool) (/usr/lib/llvm-19/bin/../lib/libclang-cpp.so.19.0+0x1ba0d5c)
#12 0x00007f3286d8394a clang::TemplateDeclInstantiator::InstantiateTypedefNameDecl(clang::TypedefNameDecl*, bool) (/usr/lib/llvm-19/bin/../lib/libclang-cpp.so.19.0+0x1bd994a)
#13 0x00007f3286d848cb clang::TemplateDeclInstantiator::VisitTypedefDecl(clang::TypedefDecl*) (/usr/lib/llvm-19/bin/../lib/libclang-cpp.so.19.0+0x1bda8cb)
#14 0x00007f3286d4e8b6 clang::Sema::InstantiateClass(clang::SourceLocation, clang::CXXRecordDecl*, clang::CXXRecordDecl*, clang::MultiLevelTemplateArgumentList const&, clang::TemplateSpecializationKind, bool) (/usr/lib/llvm-19/bin/../lib/libclang-cpp.so.19.0+0x1ba48b6)
#15 0x00007f3286d506a7 clang::Sema::InstantiateClassTemplateSpecialization(clang::SourceLocation, clang::ClassTemplateSpecializationDecl*, clang::TemplateSpecializationKind, bool) (/usr/lib/llvm-19/bin/../lib/libclang-cpp.so.19.0+0x1ba66a7)
#16 0x00007f3286e0102e (/usr/lib/llvm-19/bin/../lib/libclang-cpp.so.19.0+0x1c5702e)
#17 0x00007f32865e178d clang::Sema::runWithSufficientStackSpace(clang::SourceLocation, llvm::function_ref<void ()>) (/usr/lib/llvm-19/bin/../lib/libclang-cpp.so.19.0+0x143778d)
#18 0x00007f3286df2191 clang::Sema::RequireCompleteTypeImpl(clang::SourceLocation, clang::QualType, clang::Sema::CompleteTypeKind, clang::Sema::TypeDiagnoser*) (/usr/lib/llvm-19/bin/../lib/libclang-cpp.so.19.0+0x1c48191)
#19 0x00007f3286df194f clang::Sema::RequireCompleteType(clang::SourceLocation, clang::QualType, clang::Sema::CompleteTypeKind, clang::Sema::TypeDiagnoser&) (/usr/lib/llvm-19/bin/../lib/libclang-cpp.so.19.0+0x1c4794f)
#20 0x00007f3286662ba8 clang::Sema::RequireCompleteDeclContext(clang::CXXScopeSpec&, clang::DeclContext*) (/usr/lib/llvm-19/bin/../lib/libclang-cpp.so.19.0+0x14b8ba8)
#21 0x00007f3286acdd3d clang::Sema::LookupParsedName(clang::LookupResult&, clang::Scope*, clang::CXXScopeSpec*, bool, bool) (/usr/lib/llvm-19/bin/../lib/libclang-cpp.so.19.0+0x1923d3d)
#22 0x00007f32867626c8 clang::Sema::ClassifyName(clang::Scope*, clang::CXXScopeSpec&, clang::IdentifierInfo*&, clang::SourceLocation, clang::Token const&, clang::CorrectionCandidateCallback*) (/usr/lib/llvm-19/bin/../lib/libclang-cpp.so.19.0+0x15b86c8)
#23 0x00007f3285e26ea8 clang::Parser::TryAnnotateName(clang::CorrectionCandidateCallback*, clang::ImplicitTypenameContext) (/usr/lib/llvm-19/bin/../lib/libclang-cpp.so.19.0+0xc7cea8)
#24 0x00007f3285e026ed clang::Parser::ParseStatementOrDeclarationAfterAttributes(llvm::SmallVector<clang::Stmt*, 32u>&, clang::Parser::ParsedStmtContext, clang::SourceLocation*, clang::ParsedAttributes&, clang::ParsedAttributes&) (/usr/lib/llvm-19/bin/../lib/libclang-cpp.so.19.0+0xc586ed)
#25 0x00007f3285e022cb clang::Parser::ParseStatementOrDeclaration(llvm::SmallVector<clang::Stmt*, 32u>&, clang::Parser::ParsedStmtContext, clang::SourceLocation*) (/usr/lib/llvm-19/bin/../lib/libclang-cpp.so.19.0+0xc582cb)
#26 0x00007f3285e0ac80 clang::Parser::ParseCompoundStatementBody(bool) (/usr/lib/llvm-19/bin/../lib/libclang-cpp.so.19.0+0xc60c80)
#27 0x00007f3285e0bdef clang::Parser::ParseFunctionStatementBody(clang::Decl*, clang::Parser::ParseScope&) (/usr/lib/llvm-19/bin/../lib/libclang-cpp.so.19.0+0xc61def)
#28 0x00007f3285e25c36 clang::Parser::ParseFunctionDefinition(clang::ParsingDeclarator&, clang::Parser::ParsedTemplateInfo const&, clang::Parser::LateParsedAttrList*) (/usr/lib/llvm-19/bin/../lib/libclang-cpp.so.19.0+0xc7bc36)
#29 0x00007f3285d703a7 clang::Parser::ParseDeclGroup(clang::ParsingDeclSpec&, clang::DeclaratorContext, clang::ParsedAttributes&, clang::Parser::ParsedTemplateInfo&, clang::SourceLocation*, clang::Parser::ForRangeInit*) (/usr/lib/llvm-19/bin/../lib/libclang-cpp.so.19.0+0xbc63a7)
#30 0x00007f3285e24cb4 clang::Parser::ParseDeclOrFunctionDefInternal(clang::ParsedAttributes&, clang::ParsedAttributes&, clang::ParsingDeclSpec&, clang::AccessSpecifier) (/usr/lib/llvm-19/bin/../lib/libclang-cpp.so.19.0+0xc7acb4)
#31 0x00007f3285e2469b clang::Parser::ParseDeclarationOrFunctionDefinition(clang::ParsedAttributes&, clang::ParsedAttributes&, clang::ParsingDeclSpec*, clang::AccessSpecifier) (/usr/lib/llvm-19/bin/../lib/libclang-cpp.so.19.0+0xc7a69b)
#32 0x00007f3285e238ed clang::Parser::ParseExternalDeclaration(clang::ParsedAttributes&, clang::ParsedAttributes&, clang::ParsingDeclSpec*) (/usr/lib/llvm-19/bin/../lib/libclang-cpp.so.19.0+0xc798ed)
#33 0x00007f3285e21d3c clang::Parser::ParseTopLevelDecl(clang::OpaquePtr<clang::DeclGroupRef>&, clang::Sema::ModuleImportState&) (/usr/lib/llvm-19/bin/../lib/libclang-cpp.so.19.0+0xc77d3c)
#34 0x00007f3285d5a11e clang::ParseAST(clang::Sema&, bool, bool) (/usr/lib/llvm-19/bin/../lib/libclang-cpp.so.19.0+0xbb011e)
#35 0x00007f3287c57825 clang::FrontendAction::Execute() (/usr/lib/llvm-19/bin/../lib/libclang-cpp.so.19.0+0x2aad825)
#36 0x00007f3287bd30d4 clang::CompilerInstance::ExecuteAction(clang::FrontendAction&) (/usr/lib/llvm-19/bin/../lib/libclang-cpp.so.19.0+0x2a290d4)
#37 0x00007f3287cd0f7e clang::ExecuteCompilerInvocation(clang::CompilerInstance*) (/usr/lib/llvm-19/bin/../lib/libclang-cpp.so.19.0+0x2b26f7e)
#38 0x0000561f24649fad cc1_main(llvm::ArrayRef<char const*>, char const*, void*) (/usr/lib/llvm-19/bin/clang+0x12fad)
#39 0x0000561f24647075 (/usr/lib/llvm-19/bin/clang+0x10075)
#40 0x0000561f24646213 clang_main(int, char**, llvm::ToolContext const&) (/usr/lib/llvm-19/bin/clang+0xf213)
#41 0x0000561f24654556 main (/usr/lib/llvm-19/bin/clang+0x1d556)
#42 0x00007f327cc60c87 __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21c87)
#43 0x0000561f24643bfa _start (/usr/lib/llvm-19/bin/clang+0xcbfa)
clang++: error: unable to execute command: Segmentation fault (core dumped)
clang++: error: clang frontend command failed due to signal (use -v to see invocation)
Ubuntu clang version 19.0.0 (++20240301064251+dd426fa5f931-1~exp1~20240301184412.1845)
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/lib/llvm-19/bin
clang++: note: diagnostic msg: 
********************

PLEASE ATTACH THE FOLLOWING FILES TO THE BUG REPORT:
Preprocessed source(s) and associated run script(s) are located at:
clang++: note: diagnostic msg: /tmp/bugreport_0_1-399865.cpp
clang++: note: diagnostic msg: /tmp/bugreport_0_1-399865.sh
clang++: note: diagnostic msg: 

********************

Metadata

Metadata

Assignees

No one assigned

    Labels

    clang:frontendLanguage frontend issues, e.g. anything involving "Sema"confirmedVerified by a second partycrashPrefer [crash-on-valid] or [crash-on-invalid]regression:16Regression in 16 release

    Type

    No type

    Projects

    Status

    No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions