Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Clang] Expansion of pack indexing expression that is a pack in a lambda triggers ICE #91885

Closed
frederick-vs-ja opened this issue May 12, 2024 · 4 comments · Fixed by #91933
Closed
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema" crash Prefer [crash-on-valid] or [crash-on-invalid] lambda C++11 lambda expressions

Comments

@frederick-vs-ja
Copy link
Contributor

It seems that some branches are missing for this case. Godbolt link.

#include <cstddef>
#include <utility>

void operate_one(auto&&) {}

void operate_multi(auto&&...args)
{
    [&]<std::size_t... idx>(std::index_sequence<idx...>)
    {
        (::operate_one(args...[idx]), ...);
    }(std::make_index_sequence<sizeof...(args)>{});
}

int main()
{
    ::operate_multi(1, 2, 3);
}
mangling a placeholder type
UNREACHABLE executed at /root/llvm-project/clang/lib/AST/ItaniumMangle.cpp:3339!
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-assertions-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 -std=c++26 <source>
1.	<eof> parser at end of file
2.	<source>:6:6: instantiating function definition 'operate_multi<int, int, int>'
3.	<source>:4:6: instantiating function definition 'operate_one<<dependent type> &>'
4.	<source>:4:6: LLVM IR generation of declaration 'operate_one'
5.	<source>:4:6: Mangling declaration 'operate_one'
 #0 0x00000000039a02d8 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x39a02d8)
 #1 0x000000000399dfbc llvm::sys::CleanupOnSignal(unsigned long) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x399dfbc)
 #2 0x00000000038ef028 CrashRecoverySignalHandler(int) CrashRecoveryContext.cpp:0:0
 #3 0x0000752ae9242520 (/lib/x86_64-linux-gnu/libc.so.6+0x42520)
 #4 0x0000752ae92969fc pthread_kill (/lib/x86_64-linux-gnu/libc.so.6+0x969fc)
 #5 0x0000752ae9242476 gsignal (/lib/x86_64-linux-gnu/libc.so.6+0x42476)
 #6 0x0000752ae92287f3 abort (/lib/x86_64-linux-gnu/libc.so.6+0x287f3)
 #7 0x00000000038fa34a (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x38fa34a)
 #8 0x000000000774cdb4 (anonymous namespace)::CXXNameMangler::mangleType(clang::QualType) ItaniumMangle.cpp:0:0
 #9 0x0000000007743d03 (anonymous namespace)::CXXNameMangler::mangleType(clang::QualType) ItaniumMangle.cpp:0:0
#10 0x0000000007762408 (anonymous namespace)::CXXNameMangler::mangleTemplateArg(clang::TemplateArgument, bool) ItaniumMangle.cpp:0:0
#11 0x000000000775f59f (anonymous namespace)::CXXNameMangler::mangleTemplateArgs(clang::TemplateName, clang::TemplateArgumentList const&) ItaniumMangle.cpp:0:0
#12 0x0000000007740843 (anonymous namespace)::CXXNameMangler::mangleNameWithAbiTags(clang::GlobalDecl, llvm::SmallVector<llvm::StringRef, 4u> const*) ItaniumMangle.cpp:0:0
#13 0x000000000774144a (anonymous namespace)::CXXNameMangler::mangleName(clang::GlobalDecl) ItaniumMangle.cpp:0:0
#14 0x000000000775875d (anonymous namespace)::CXXNameMangler::mangleFunctionEncoding(clang::GlobalDecl) ItaniumMangle.cpp:0:0
#15 0x000000000775c70e (anonymous namespace)::ItaniumMangleContextImpl::mangleCXXName(clang::GlobalDecl, llvm::raw_ostream&) ItaniumMangle.cpp:0:0
#16 0x0000000007781523 clang::MangleContext::mangleName(clang::GlobalDecl, llvm::raw_ostream&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x7781523)
#17 0x0000000003d743dc getMangledNameImpl(clang::CodeGen::CodeGenModule&, clang::GlobalDecl, clang::NamedDecl const*, bool) CodeGenModule.cpp:0:0
#18 0x0000000003d85fad clang::CodeGen::CodeGenModule::getMangledName(clang::GlobalDecl) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x3d85fad)
#19 0x0000000003db795c clang::CodeGen::CodeGenModule::EmitGlobal(clang::GlobalDecl) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x3db795c)
#20 0x0000000003dc1de3 clang::CodeGen::CodeGenModule::EmitTopLevelDecl(clang::Decl*) (.part.0) CodeGenModule.cpp:0:0
#21 0x000000000426ff86 (anonymous namespace)::CodeGeneratorImpl::HandleTopLevelDecl(clang::DeclGroupRef) ModuleBuilder.cpp:0:0
#22 0x00000000042604c8 clang::BackendConsumer::HandleTopLevelDecl(clang::DeclGroupRef) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x42604c8)
#23 0x0000000006f3816a clang::Sema::InstantiateFunctionDefinition(clang::SourceLocation, clang::FunctionDecl*, bool, bool, bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6f3816a)
#24 0x0000000006f3698f clang::Sema::PerformPendingInstantiations(bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6f3698f)
#25 0x0000000006f389b6 clang::Sema::InstantiateFunctionDefinition(clang::SourceLocation, clang::FunctionDecl*, bool, bool, bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6f389b6)
#26 0x0000000006f3698f clang::Sema::PerformPendingInstantiations(bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6f3698f)
#27 0x00000000064473bf clang::Sema::ActOnEndOfTranslationUnitFragment(clang::Sema::TUFragmentKind) (.part.0) Sema.cpp:0:0
#28 0x0000000006447ba2 clang::Sema::ActOnEndOfTranslationUnit() (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6447ba2)
#29 0x00000000062c2d0a clang::Parser::ParseTopLevelDecl(clang::OpaquePtr<clang::DeclGroupRef>&, clang::Sema::ModuleImportState&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x62c2d0a)
#30 0x00000000062b66ea clang::ParseAST(clang::Sema&, bool, bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x62b66ea)
#31 0x000000000426db08 clang::CodeGenAction::ExecuteAction() (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x426db08)
#32 0x00000000044f1349 clang::FrontendAction::Execute() (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x44f1349)
#33 0x0000000004478a2e clang::CompilerInstance::ExecuteAction(clang::FrontendAction&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x4478a2e)
#34 0x00000000045d805e clang::ExecuteCompilerInvocation(clang::CompilerInstance*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x45d805e)
#35 0x0000000000c526cc cc1_main(llvm::ArrayRef<char const*>, char const*, void*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0xc526cc)
#36 0x0000000000c4ba2a ExecuteCC1Tool(llvm::SmallVectorImpl<char const*>&, llvm::ToolContext const&) driver.cpp:0:0
#37 0x00000000042b0cd9 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
#38 0x00000000038ef4d4 llvm::CrashRecoveryContext::RunSafely(llvm::function_ref<void ()>) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x38ef4d4)
#39 0x00000000042b12cf 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
#40 0x00000000042773d5 clang::driver::Compilation::ExecuteCommand(clang::driver::Command const&, clang::driver::Command const*&, bool) const (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x42773d5)
#41 0x0000000004277e3d clang::driver::Compilation::ExecuteJobs(clang::driver::JobList const&, llvm::SmallVectorImpl<std::pair<int, clang::driver::Command const*>>&, bool) const (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x4277e3d)
#42 0x000000000427fb35 clang::driver::Driver::ExecuteCompilation(clang::driver::Compilation&, llvm::SmallVectorImpl<std::pair<int, clang::driver::Command const*>>&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x427fb35)
#43 0x0000000000c4fbd5 clang_main(int, char**, llvm::ToolContext const&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0xc4fbd5)
#44 0x0000000000b31264 main (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0xb31264)
#45 0x0000752ae9229d90 (/lib/x86_64-linux-gnu/libc.so.6+0x29d90)
#46 0x0000752ae9229e40 __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x29e40)
#47 0x0000000000c4b4de _start (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0xc4b4de)
clang++: error: clang frontend command failed with exit code 134 (use -v to see invocation)
Compiler returned: 134
@github-actions github-actions bot added the clang Clang issues not falling into any other category label May 12, 2024
@EugeneZelenko EugeneZelenko added clang:frontend Language frontend issues, e.g. anything involving "Sema" crash Prefer [crash-on-valid] or [crash-on-invalid] lambda C++11 lambda expressions and removed clang Clang issues not falling into any other category labels May 12, 2024
@llvmbot
Copy link
Collaborator

llvmbot commented May 12, 2024

@llvm/issue-subscribers-clang-frontend

Author: A. Jiang (frederick-vs-ja)

It seems that some branches are missing for this case. [Godbolt link](https://godbolt.org/z/8YK9MGYdb). ```C++ #include <cstddef> #include <utility>

void operate_one(auto&&) {}

void operate_multi(auto&&...args)
{
[&]<std::size_t... idx>(std::index_sequence<idx...>)
{
(::operate_one(args...[idx]), ...);
}(std::make_index_sequence<sizeof...(args)>{});
}

int main()
{
::operate_multi(1, 2, 3);
}


mangling a placeholder type
UNREACHABLE executed at /root/llvm-project/clang/lib/AST/ItaniumMangle.cpp:3339!
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-assertions-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 -std=c++26 <source>

  1. <eof> parser at end of file
  2. <source>:6:6: instantiating function definition 'operate_multi<int, int, int>'
  3. <source>:4:6: instantiating function definition 'operate_one<<dependent type> &>'
  4. <source>:4:6: LLVM IR generation of declaration 'operate_one'
  5. <source>:4:6: Mangling declaration 'operate_one'
    #0 0x00000000039a02d8 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x39a02d8)
    #1 0x000000000399dfbc llvm::sys::CleanupOnSignal(unsigned long) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x399dfbc)
    #2 0x00000000038ef028 CrashRecoverySignalHandler(int) CrashRecoveryContext.cpp:0:0
    #3 0x0000752ae9242520 (/lib/x86_64-linux-gnu/libc.so.6+0x42520)
    #4 0x0000752ae92969fc pthread_kill (/lib/x86_64-linux-gnu/libc.so.6+0x969fc)
    #5 0x0000752ae9242476 gsignal (/lib/x86_64-linux-gnu/libc.so.6+0x42476)
    #6 0x0000752ae92287f3 abort (/lib/x86_64-linux-gnu/libc.so.6+0x287f3)
    #7 0x00000000038fa34a (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x38fa34a)
    #8 0x000000000774cdb4 (anonymous namespace)::CXXNameMangler::mangleType(clang::QualType) ItaniumMangle.cpp:0:0
    #9 0x0000000007743d03 (anonymous namespace)::CXXNameMangler::mangleType(clang::QualType) ItaniumMangle.cpp:0:0
    #10 0x0000000007762408 (anonymous namespace)::CXXNameMangler::mangleTemplateArg(clang::TemplateArgument, bool) ItaniumMangle.cpp:0:0
    #11 0x000000000775f59f (anonymous namespace)::CXXNameMangler::mangleTemplateArgs(clang::TemplateName, clang::TemplateArgumentList const&) ItaniumMangle.cpp:0:0
    #12 0x0000000007740843 (anonymous namespace)::CXXNameMangler::mangleNameWithAbiTags(clang::GlobalDecl, llvm::SmallVector<llvm::StringRef, 4u> const*) ItaniumMangle.cpp:0:0
    #13 0x000000000774144a (anonymous namespace)::CXXNameMangler::mangleName(clang::GlobalDecl) ItaniumMangle.cpp:0:0
    #14 0x000000000775875d (anonymous namespace)::CXXNameMangler::mangleFunctionEncoding(clang::GlobalDecl) ItaniumMangle.cpp:0:0
    #15 0x000000000775c70e (anonymous namespace)::ItaniumMangleContextImpl::mangleCXXName(clang::GlobalDecl, llvm::raw_ostream&) ItaniumMangle.cpp:0:0
    #16 0x0000000007781523 clang::MangleContext::mangleName(clang::GlobalDecl, llvm::raw_ostream&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x7781523)
    #17 0x0000000003d743dc getMangledNameImpl(clang::CodeGen::CodeGenModule&, clang::GlobalDecl, clang::NamedDecl const*, bool) CodeGenModule.cpp:0:0
    #18 0x0000000003d85fad clang::CodeGen::CodeGenModule::getMangledName(clang::GlobalDecl) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x3d85fad)
    #19 0x0000000003db795c clang::CodeGen::CodeGenModule::EmitGlobal(clang::GlobalDecl) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x3db795c)
    #20 0x0000000003dc1de3 clang::CodeGen::CodeGenModule::EmitTopLevelDecl(clang::Decl*) (.part.0) CodeGenModule.cpp:0:0
    #21 0x000000000426ff86 (anonymous namespace)::CodeGeneratorImpl::HandleTopLevelDecl(clang::DeclGroupRef) ModuleBuilder.cpp:0:0
    #22 0x00000000042604c8 clang::BackendConsumer::HandleTopLevelDecl(clang::DeclGroupRef) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x42604c8)
    #23 0x0000000006f3816a clang::Sema::InstantiateFunctionDefinition(clang::SourceLocation, clang::FunctionDecl*, bool, bool, bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6f3816a)
    #24 0x0000000006f3698f clang::Sema::PerformPendingInstantiations(bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6f3698f)
    #25 0x0000000006f389b6 clang::Sema::InstantiateFunctionDefinition(clang::SourceLocation, clang::FunctionDecl*, bool, bool, bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6f389b6)
    #26 0x0000000006f3698f clang::Sema::PerformPendingInstantiations(bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6f3698f)
    #27 0x00000000064473bf clang::Sema::ActOnEndOfTranslationUnitFragment(clang::Sema::TUFragmentKind) (.part.0) Sema.cpp:0:0
    #28 0x0000000006447ba2 clang::Sema::ActOnEndOfTranslationUnit() (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6447ba2)
    #29 0x00000000062c2d0a clang::Parser::ParseTopLevelDecl(clang::OpaquePtr<clang::DeclGroupRef>&, clang::Sema::ModuleImportState&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x62c2d0a)
    #30 0x00000000062b66ea clang::ParseAST(clang::Sema&, bool, bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x62b66ea)
    #31 0x000000000426db08 clang::CodeGenAction::ExecuteAction() (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x426db08)
    #32 0x00000000044f1349 clang::FrontendAction::Execute() (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x44f1349)
    #33 0x0000000004478a2e clang::CompilerInstance::ExecuteAction(clang::FrontendAction&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x4478a2e)
    #34 0x00000000045d805e clang::ExecuteCompilerInvocation(clang::CompilerInstance*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x45d805e)
    #35 0x0000000000c526cc cc1_main(llvm::ArrayRef<char const*>, char const*, void*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0xc526cc)
    #36 0x0000000000c4ba2a ExecuteCC1Tool(llvm::SmallVectorImpl<char const*>&, llvm::ToolContext const&) driver.cpp:0:0
    #37 0x00000000042b0cd9 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
    #38 0x00000000038ef4d4 llvm::CrashRecoveryContext::RunSafely(llvm::function_ref<void ()>) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x38ef4d4)
    #39 0x00000000042b12cf 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
    #40 0x00000000042773d5 clang::driver::Compilation::ExecuteCommand(clang::driver::Command const&, clang::driver::Command const*&, bool) const (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x42773d5)
    #41 0x0000000004277e3d clang::driver::Compilation::ExecuteJobs(clang::driver::JobList const&, llvm::SmallVectorImpl<std::pair<int, clang::driver::Command const*>>&, bool) const (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x4277e3d)
    #42 0x000000000427fb35 clang::driver::Driver::ExecuteCompilation(clang::driver::Compilation&, llvm::SmallVectorImpl<std::pair<int, clang::driver::Command const*>>&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x427fb35)
    #43 0x0000000000c4fbd5 clang_main(int, char**, llvm::ToolContext const&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0xc4fbd5)
    #44 0x0000000000b31264 main (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0xb31264)
    #45 0x0000752ae9229d90 (/lib/x86_64-linux-gnu/libc.so.6+0x29d90)
    #46 0x0000752ae9229e40 __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x29e40)
    #47 0x0000000000c4b4de _start (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0xc4b4de)
    clang++: error: clang frontend command failed with exit code 134 (use -v to see invocation)
    Compiler returned: 134
</details>

@RungeCC
Copy link

RungeCC commented May 12, 2024

After reducing this example I get another ICE with a different crash report:

void ICE(auto...args){
    [&]<int idx>(){
        using R = decltype( args...[idx] ) ;
    }.template operator()<0>();
}

int main( ) {
    ICE(1);
}
clang-19: /root/llvm-project/clang/include/clang/AST/ExprCXX.h:4444: clang::Expr* clang::PackIndexingExpr::getSelectedExpr() const: Assertion `Index && "extracting the indexed expression of a dependant pack"' failed.
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-assertions-trunk-20240512/bin/clang-19 -cc1 -triple x86_64-unknown-linux-gnu -emit-obj -dumpdir /app/output.s- -disable-free -clear-ast-before-backend -main-file-name example.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=4 -debugger-tuning=gdb -fdebug-compilation-dir=/app -fcoverage-compilation-dir=/app -resource-dir /opt/compiler-explorer/clang-assertions-trunk-20240512/lib/clang/19 -internal-isystem /opt/compiler-explorer/gcc-snapshot/lib/gcc/x86_64-linux-gnu/15.0.0/../../../../include/c++/15.0.0 -internal-isystem /opt/compiler-explorer/gcc-snapshot/lib/gcc/x86_64-linux-gnu/15.0.0/../../../../include/c++/15.0.0/x86_64-linux-gnu -internal-isystem /opt/compiler-explorer/gcc-snapshot/lib/gcc/x86_64-linux-gnu/15.0.0/../../../../include/c++/15.0.0/backward -internal-isystem /opt/compiler-explorer/clang-assertions-trunk-20240512/lib/clang/19/include -internal-isystem /usr/local/include -internal-isystem /opt/compiler-explorer/gcc-snapshot/lib/gcc/x86_64-linux-gnu/15.0.0/../../../../x86_64-linux-gnu/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -std=c++26 -fdeprecated-macro -ferror-limit 19 -fgnuc-version=4.2.1 -fno-implicit-modules -fskip-odr-check-in-gmf -fcxx-exceptions -fexceptions -fno-relaxed-template-template-args -fcolor-diagnostics -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /tmp/example-51e65e.o -x c++ <source>
1.	<eof> parser at end of file
2.	<source>:1:6: instantiating function definition 'ICE<int>'
 #0 0x00000000039a02d8 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/opt/compiler-explorer/clang-assertions-trunk-20240512/bin/clang-19+0x39a02d8)
 #1 0x000000000399da2c SignalHandler(int) Signals.cpp:0:0
 #2 0x00007ff9b6042520 (/lib/x86_64-linux-gnu/libc.so.6+0x42520)
 #3 0x00007ff9b60969fc pthread_kill (/lib/x86_64-linux-gnu/libc.so.6+0x969fc)
 #4 0x00007ff9b6042476 gsignal (/lib/x86_64-linux-gnu/libc.so.6+0x42476)
 #5 0x00007ff9b60287f3 abort (/lib/x86_64-linux-gnu/libc.so.6+0x287f3)
 #6 0x00007ff9b602871b (/lib/x86_64-linux-gnu/libc.so.6+0x2871b)
 #7 0x00007ff9b6039e96 (/lib/x86_64-linux-gnu/libc.so.6+0x39e96)
 #8 0x0000000006f76848 clang::Sema::BuildDecltypeType(clang::Expr*, bool) (/opt/compiler-explorer/clang-assertions-trunk-20240512/bin/clang-19+0x6f76848)
 #9 0x0000000006eacf28 clang::TreeTransform<(anonymous namespace)::TemplateInstantiator>::TransformType(clang::TypeLocBuilder&, clang::TypeLoc) SemaTemplateInstantiate.cpp:0:0
#10 0x0000000006eb35fa clang::TreeTransform<(anonymous namespace)::TemplateInstantiator>::TransformType(clang::TypeSourceInfo*) SemaTemplateInstantiate.cpp:0:0
#11 0x0000000006ebd46a clang::Sema::SubstType(clang::TypeSourceInfo*, clang::MultiLevelTemplateArgumentList const&, clang::SourceLocation, clang::DeclarationName, bool) (/opt/compiler-explorer/clang-assertions-trunk-20240512/bin/clang-19+0x6ebd46a)
#12 0x0000000006f41171 clang::TemplateDeclInstantiator::InstantiateTypedefNameDecl(clang::TypedefNameDecl*, bool) (/opt/compiler-explorer/clang-assertions-trunk-20240512/bin/clang-19+0x6f41171)
#13 0x0000000006f418b4 clang::TemplateDeclInstantiator::VisitTypeAliasDecl(clang::TypeAliasDecl*) (/opt/compiler-explorer/clang-assertions-trunk-20240512/bin/clang-19+0x6f418b4)
#14 0x0000000006f436a4 void llvm::function_ref<void ()>::callback_fn<clang::Sema::SubstDecl(clang::Decl*, clang::DeclContext*, clang::MultiLevelTemplateArgumentList const&)::'lambda'()>(long) SemaTemplateInstantiateDecl.cpp:0:0
#15 0x0000000006424ce1 clang::Sema::runWithSufficientStackSpace(clang::SourceLocation, llvm::function_ref<void ()>) (/opt/compiler-explorer/clang-assertions-trunk-20240512/bin/clang-19+0x6424ce1)
#16 0x0000000006eeb48a clang::Sema::SubstDecl(clang::Decl*, clang::DeclContext*, clang::MultiLevelTemplateArgumentList const&) (/opt/compiler-explorer/clang-assertions-trunk-20240512/bin/clang-19+0x6eeb48a)
#17 0x0000000006e96b45 clang::TreeTransform<(anonymous namespace)::TemplateInstantiator>::TransformDeclStmt(clang::DeclStmt*) SemaTemplateInstantiate.cpp:0:0
#18 0x0000000006ed9ed4 clang::TreeTransform<(anonymous namespace)::TemplateInstantiator>::TransformCompoundStmt(clang::CompoundStmt*, bool) SemaTemplateInstantiate.cpp:0:0
#19 0x0000000006e9ca50 clang::TreeTransform<(anonymous namespace)::TemplateInstantiator>::TransformLambdaExpr(clang::LambdaExpr*) SemaTemplateInstantiate.cpp:0:0
#20 0x0000000006e9d7d7 clang::TreeTransform<(anonymous namespace)::TemplateInstantiator>::TransformExpr(clang::Expr*) SemaTemplateInstantiate.cpp:0:0
#21 0x0000000006ed1eeb clang::TreeTransform<(anonymous namespace)::TemplateInstantiator>::TransformCXXDependentScopeMemberExpr(clang::CXXDependentScopeMemberExpr*) SemaTemplateInstantiate.cpp:0:0
#22 0x0000000006e9d4e6 clang::TreeTransform<(anonymous namespace)::TemplateInstantiator>::TransformExpr(clang::Expr*) SemaTemplateInstantiate.cpp:0:0
#23 0x0000000006ea9401 clang::TreeTransform<(anonymous namespace)::TemplateInstantiator>::TransformCallExpr(clang::CallExpr*) SemaTemplateInstantiate.cpp:0:0
#24 0x0000000006e9d5bf clang::TreeTransform<(anonymous namespace)::TemplateInstantiator>::TransformExpr(clang::Expr*) SemaTemplateInstantiate.cpp:0:0
#25 0x0000000006ed90ef clang::TreeTransform<(anonymous namespace)::TemplateInstantiator>::TransformStmt(clang::Stmt*, clang::TreeTransform<(anonymous namespace)::TemplateInstantiator>::StmtDiscardKind) SemaTemplateInstantiate.cpp:0:0
#26 0x0000000006ed9ed4 clang::TreeTransform<(anonymous namespace)::TemplateInstantiator>::TransformCompoundStmt(clang::CompoundStmt*, bool) SemaTemplateInstantiate.cpp:0:0
#27 0x0000000006ee12da clang::Sema::SubstStmt(clang::Stmt*, clang::MultiLevelTemplateArgumentList const&) (/opt/compiler-explorer/clang-assertions-trunk-20240512/bin/clang-19+0x6ee12da)
#28 0x0000000006f38708 clang::Sema::InstantiateFunctionDefinition(clang::SourceLocation, clang::FunctionDecl*, bool, bool, bool) (/opt/compiler-explorer/clang-assertions-trunk-20240512/bin/clang-19+0x6f38708)
#29 0x0000000006f3698f clang::Sema::PerformPendingInstantiations(bool) (/opt/compiler-explorer/clang-assertions-trunk-20240512/bin/clang-19+0x6f3698f)
#30 0x00000000064473bf clang::Sema::ActOnEndOfTranslationUnitFragment(clang::Sema::TUFragmentKind) (.part.0) Sema.cpp:0:0
#31 0x0000000006447ba2 clang::Sema::ActOnEndOfTranslationUnit() (/opt/compiler-explorer/clang-assertions-trunk-20240512/bin/clang-19+0x6447ba2)
#32 0x00000000062c2d0a clang::Parser::ParseTopLevelDecl(clang::OpaquePtr<clang::DeclGroupRef>&, clang::Sema::ModuleImportState&) (/opt/compiler-explorer/clang-assertions-trunk-20240512/bin/clang-19+0x62c2d0a)
#33 0x00000000062b66ea clang::ParseAST(clang::Sema&, bool, bool) (/opt/compiler-explorer/clang-assertions-trunk-20240512/bin/clang-19+0x62b66ea)
#34 0x000000000426db08 clang::CodeGenAction::ExecuteAction() (/opt/compiler-explorer/clang-assertions-trunk-20240512/bin/clang-19+0x426db08)
#35 0x00000000044f1349 clang::FrontendAction::Execute() (/opt/compiler-explorer/clang-assertions-trunk-20240512/bin/clang-19+0x44f1349)
#36 0x0000000004478a2e clang::CompilerInstance::ExecuteAction(clang::FrontendAction&) (/opt/compiler-explorer/clang-assertions-trunk-20240512/bin/clang-19+0x4478a2e)
#37 0x00000000045d805e clang::ExecuteCompilerInvocation(clang::CompilerInstance*) (/opt/compiler-explorer/clang-assertions-trunk-20240512/bin/clang-19+0x45d805e)
#38 0x0000000000c526cc cc1_main(llvm::ArrayRef<char const*>, char const*, void*) (/opt/compiler-explorer/clang-assertions-trunk-20240512/bin/clang-19+0xc526cc)
#39 0x0000000000c4ba2a ExecuteCC1Tool(llvm::SmallVectorImpl<char const*>&, llvm::ToolContext const&) driver.cpp:0:0
#40 0x0000000000c4f35e clang_main(int, char**, llvm::ToolContext const&) (/opt/compiler-explorer/clang-assertions-trunk-20240512/bin/clang-19+0xc4f35e)
#41 0x0000000000b31264 main (/opt/compiler-explorer/clang-assertions-trunk-20240512/bin/clang-19+0xb31264)
#42 0x00007ff9b6029d90 (/lib/x86_64-linux-gnu/libc.so.6+0x29d90)
#43 0x00007ff9b6029e40 __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x29e40)
#44 0x0000000000c4b4de _start (/opt/compiler-explorer/clang-assertions-trunk-20240512/bin/clang-19+0xc4b4de)
clang++: error: unable to execute command: Aborted (core dumped)
clang++: error: clang frontend command failed due to signal (use -v to see invocation)

See https://godbolt.org/z/Ge5cbEM7e. Shall I open a new issue?

@RungeCC
Copy link

RungeCC commented May 12, 2024

It seems that the issue is related to the following function:

ExprDependence clang::computeDependence(PackIndexingExpr *E) {
ExprDependence D = E->getIndexExpr()->getDependence();
ArrayRef<Expr *> Exprs = E->getExpressions();
if (Exprs.empty())
D |= (E->getPackIdExpression()->getDependence() |
ExprDependence::TypeValueInstantiation) &
~ExprDependence::UnexpandedPack;
else if (!E->getIndexExpr()->isInstantiationDependent()) {
std::optional<unsigned> Index = E->getSelectedIndex();
assert(Index && *Index < Exprs.size() && "pack index out of bound");
D |= Exprs[*Index]->getDependence();
}
return D;
}

Let's consider the following code snippets:

void ICE(auto...args){
    [&]<int idx>(){
        using R = decltype( args...[idx] ) ;
    }.template operator()<0>();
}

int main( ) {
    ICE(1);
}

When the transformation of substituting the template parameters of function ICE into the lambda function, we need to rebuild the decltype type.

Currently:

  1. Exprs are non-empty (args... is replaced by the argument of ICE) and
  2. E->getIndexExpr()->isInstantiationDependent() is true (idx has not been substituted at that time),
    hence the function return with the value D = E->getIndexExpr()->getDependence() and in this case it will be ValueInstantiation.

That's wrong since actually,

  1. p...[idx] may be type dependent for a pack p declared by auto... ,
  2. when try to get the type of p...[idx] for a pack p the types of elements of which are all same, there are still two possible results, a) idx is not out-of-bound then the result type is identical with the type of every pack element or b) constant evaluation fails, hence, it seems we should also treat expression as a type dependent one.

Then in:

QualType Sema::getDecltypeForExpr(Expr *E) {
if (E->isTypeDependent())
return Context.DependentTy;
Expr *IDExpr = E;
if (auto *ImplCastExpr = dyn_cast<ImplicitCastExpr>(E))
IDExpr = ImplCastExpr->getSubExpr();
if (auto *PackExpr = dyn_cast<PackIndexingExpr>(E))
IDExpr = PackExpr->getSelectedExpr();

Since E->isTypeDependent() == false due to the above reason, internal assertion fails when we try to perform getSelectedExpr for a dependent index, which is impossible.

Possible fix:

diff --git a/clang/lib/AST/ComputeDependence.cpp b/clang/lib/AST/ComputeDependence.cpp
index bad8e75b2f87..8d39431c889f 100644
--- a/clang/lib/AST/ComputeDependence.cpp
+++ b/clang/lib/AST/ComputeDependence.cpp
@@ -385,6 +385,10 @@ ExprDependence clang::computeDependence(PackIndexingExpr *E) {
     std::optional<unsigned> Index = E->getSelectedIndex();
     assert(Index && *Index < Exprs.size() && "pack index out of bound");
     D |= Exprs[*Index]->getDependence();
+  } else {
+    /// non-empty PackIndexingExpr is type-dependent
+    /// if IndexExpr is instantiation-dependent
+    D |= ExprDependence::Type;
   }
   return D;
 }

CC: @cor3ntin

@cor3ntin
Copy link
Contributor

@RungeCC Thanks, that was exactly the issue :)

cor3ntin added a commit to cor3ntin/llvm-project that referenced this issue May 13, 2024
Given `foo...[idx]` if idx is value dependent, the expression
is type dependent.

Fixes llvm#91885
Fixes llvm#91884
cor3ntin added a commit to cor3ntin/llvm-project that referenced this issue May 13, 2024
Given `foo...[idx]` if idx is value dependent, the expression
is type dependent.

Fixes llvm#91885
Fixes llvm#91884
cor3ntin added a commit that referenced this issue May 14, 2024
Given `foo...[idx]` if idx is value dependent, the expression is type
dependent.

Fixes #91885
Fixes #91884
mub-at-arm pushed a commit to mub-at-arm/llvm-project that referenced this issue May 16, 2024
…#91933)

Given `foo...[idx]` if idx is value dependent, the expression is type
dependent.

Fixes llvm#91885
Fixes llvm#91884
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema" crash Prefer [crash-on-valid] or [crash-on-invalid] lambda C++11 lambda expressions
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants