[clang][regression] lambda call in unevaluated context in generic lambda in template function fails from clang 15 onwards
Example code could be found below or on godbolt. clang 14, gcc and MSVC all compile fine. clang 15 onwards fail.
The problem is triggered only if tc_lazy is called in a template function and the lambda in tc_lazy is a generic lambda.
#include <type_traits>
#include <utility>
#define HYBRIDSTR(str) \
[](auto t) constexpr noexcept -> decltype(auto) { \
using ArgCharT = typename decltype(t)::type; \
if constexpr(std::is_same<ArgCharT, char>::value) { \
return str; \
} else if constexpr(std::is_same<ArgCharT, wchar_t>::value) { \
return L ## str; \
} else if constexpr(std::is_same<ArgCharT, char16_t>::value) { \
return u ## str; \
} else { \
static_assert(std::is_same<ArgCharT, char32_t>::value); \
return U ## str; \
} \
}
namespace tc {
template<typename T>
constexpr std::conditional_t<
std::is_rvalue_reference<T&&>::value,
std::decay_t<T>,
T
> lvalue_or_decay(T&& t) {
return std::forward<T>(t);
}
namespace make_lazy_adl {
template<typename Func>
struct make_lazy : Func {
constexpr explicit make_lazy(Func func) noexcept : Func(std::move(func)) {}
constexpr operator decltype(std::declval<Func const&>()())() const&& {
return (*this)();
}
};
}
using make_lazy_adl::make_lazy;
namespace lazy_detail {
template<bool bReference, typename T>
decltype(auto) lvalue_or_decay(T&& t) noexcept {
if constexpr (bReference) {
return tc::lvalue_or_decay(std::forward<T>(t));
}
}
}
}
#define tc_lazy( ... ) tc::make_lazy([&](auto&&...) -> decltype(auto) { \
if constexpr (std::is_reference<decltype((__VA_ARGS__))>::value) { \
return tc::lazy_detail::lvalue_or_decay<std::is_reference<decltype((__VA_ARGS__))>::value>(__VA_ARGS__); \
} else { \
return __VA_ARGS__; \
} \
})
template<typename Char>
auto foo() {
return tc_lazy(HYBRIDSTR("abc")(std::type_identity<Char>()))(); // fails in template function from clang 15 onwards
}
void bar() {
tc_lazy(0)();
tc_lazy(HYBRIDSTR("abc")(std::type_identity<char16_t>()))(); // compiles
foo<char16_t>();
}
@llvm/issue-subscribers-clang-frontend
Author: Han (wanghan02)
The problem is triggered only if tc_lazy is called in a template function and the lambda in tc_lazy is a generic lambda.
#include <type_traits>
#include <utility>
#define HYBRIDSTR(str) \
[](auto t) constexpr noexcept -> decltype(auto) { \
using ArgCharT = typename decltype(t)::type; \
if constexpr(std::is_same<ArgCharT, char>::value) { \
return str; \
} else if constexpr(std::is_same<ArgCharT, wchar_t>::value) { \
return L ## str; \
} else if constexpr(std::is_same<ArgCharT, char16_t>::value) { \
return u ## str; \
} else { \
static_assert(std::is_same<ArgCharT, char32_t>::value); \
return U ## str; \
} \
}
namespace tc {
template<typename T>
constexpr std::conditional_t<
std::is_rvalue_reference<T&&>::value,
std::decay_t<T>,
T
> lvalue_or_decay(T&& t) {
return std::forward<T>(t);
}
namespace make_lazy_adl {
template<typename Func>
struct make_lazy : Func {
constexpr explicit make_lazy(Func func) noexcept : Func(std::move(func)) {}
constexpr operator decltype(std::declval<Func const&>()())() const&& {
return (*this)();
}
};
}
using make_lazy_adl::make_lazy;
namespace lazy_detail {
template<bool bReference, typename T>
decltype(auto) lvalue_or_decay(T&& t) noexcept {
if constexpr (bReference) {
return tc::lvalue_or_decay(std::forward<T>(t));
}
}
}
}
#define tc_lazy( ... ) tc::make_lazy([&](auto&&...) -> decltype(auto) { \
if constexpr (std::is_reference<decltype((__VA_ARGS__))>::value) { \
return tc::lazy_detail::lvalue_or_decay<std::is_reference<decltype((__VA_ARGS__))>::value>(__VA_ARGS__); \
} else { \
return __VA_ARGS__; \
} \
})
template<typename Char>
auto foo() {
return tc_lazy(HYBRIDSTR("abc")(std::type_identity<Char>()))(); // fails in template function from clang 15 onwards
}
void bar() {
tc_lazy(0)();
tc_lazy(HYBRIDSTR("abc")(std::type_identity<char16_t>()))(); // compiles
foo<char16_t>();
}
CC @cor3ntin
Reduced:
template<typename> void foo() {
[](auto...) {
decltype([](auto) {}(0))();
}();
}
template void foo<void>();
Error:
<source>:3:17: error: no matching function for call to object of type '(lambda at <source>:3:17)'
3 | decltype([](auto) {}(0))();
| ^~~~~~~~~~~
<source>:2:17: note: while substituting into a lambda expression here
2 | [](auto...) {
| ^
<source>:7:15: note: in instantiation of function template specialization 'foo<void>' requested here
7 | template void foo<void>();
| ^
<source>:3:17: note: candidate template ignored: couldn't infer template argument 'auto:1'
3 | decltype([](auto) {}(0))();
| ^
1 error generated.
Compiler returned: 1
If the inner lambda is variadic,
template<typename> void foo() {
[](auto...) {
decltype([](auto...) {}())();
}();
}
template void foo<void>();
...clang crashes, looking possibly related to #28461.
Assertion:
clang++: /root/llvm-project/clang/lib/Sema/SemaTemplateDeduction.cpp:880:
unsigned int {anonymous}::PackDeductionScope::addPacks(clang::TemplateArgument):
Assertion `!Packs.empty() && "Pack expansion without unexpanded packs?"' failed.
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 -fno-verbose-asm -S --gcc-toolchain=/opt/compiler-explorer/gcc-snapshot -fcolor-diagnostics -fno-crash-diagnostics -std=c++20 <source>
1. <source>:7:26: current parser token ';'
2. <source>:1:25: instantiating function definition 'foo<void>'
#0 0x0000000003bf5af8 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x3bf5af8)
#1 0x0000000003bf37fc llvm::sys::CleanupOnSignal(unsigned long) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x3bf37fc)
#2 0x0000000003b40ee8 CrashRecoverySignalHandler(int) CrashRecoveryContext.cpp:0:0
#3 0x00007e5612a42520 (/lib/x86_64-linux-gnu/libc.so.6+0x42520)
#4 0x00007e5612a969fc pthread_kill (/lib/x86_64-linux-gnu/libc.so.6+0x969fc)
#5 0x00007e5612a42476 gsignal (/lib/x86_64-linux-gnu/libc.so.6+0x42476)
#6 0x00007e5612a287f3 abort (/lib/x86_64-linux-gnu/libc.so.6+0x287f3)
#7 0x00007e5612a2871b (/lib/x86_64-linux-gnu/libc.so.6+0x2871b)
#8 0x00007e5612a39e96 (/lib/x86_64-linux-gnu/libc.so.6+0x39e96)
#9 0x0000000006f588ad clang::Sema::DeduceTemplateArguments(clang::FunctionTemplateDecl*, clang::TemplateArgumentListInfo*, llvm::ArrayRef<clang::Expr*>, clang::FunctionDecl*&, clang::sema::TemplateDeductionInfo&, bool, bool, clang::QualType, clang::Expr::Classification, llvm::function_ref<bool (llvm::ArrayRef<clang::QualType>)>) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6f588ad)
#10 0x0000000006de4d1d clang::Sema::AddMethodTemplateCandidate(clang::FunctionTemplateDecl*, clang::DeclAccessPair, clang::CXXRecordDecl*, clang::TemplateArgumentListInfo*, clang::QualType, clang::Expr::Classification, llvm::ArrayRef<clang::Expr*>, clang::OverloadCandidateSet&, bool, bool, clang::OverloadCandidateParamOrder) (.constprop.0) SemaOverload.cpp:0:0
#11 0x0000000006de56a1 clang::Sema::AddMethodCandidate(clang::DeclAccessPair, clang::QualType, clang::Expr::Classification, llvm::ArrayRef<clang::Expr*>, clang::OverloadCandidateSet&, bool, clang::OverloadCandidateParamOrder) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6de56a1)
#12 0x0000000006df5ad2 clang::Sema::BuildCallToObjectOfClassType(clang::Scope*, clang::Expr*, clang::SourceLocation, llvm::MutableArrayRef<clang::Expr*>, clang::SourceLocation) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6df5ad2)
#13 0x0000000006a46c83 clang::Sema::BuildCallExpr(clang::Scope*, clang::Expr*, clang::SourceLocation, llvm::MutableArrayRef<clang::Expr*>, clang::SourceLocation, clang::Expr*, bool, bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6a46c83)
#14 0x0000000006a4755c clang::Sema::ActOnCallExpr(clang::Scope*, clang::Expr*, clang::SourceLocation, llvm::MutableArrayRef<clang::Expr*>, clang::SourceLocation, clang::Expr*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6a4755c)
#15 0x000000000701ee30 clang::TreeTransform<(anonymous namespace)::TemplateInstantiator>::TransformCallExpr(clang::CallExpr*) SemaTemplateInstantiate.cpp:0:0
#16 0x000000000700c30e clang::TreeTransform<(anonymous namespace)::TemplateInstantiator>::TransformExpr(clang::Expr*) SemaTemplateInstantiate.cpp:0:0
#17 0x0000000007003667 clang::TreeTransform<(anonymous namespace)::TemplateInstantiator>::TransformType(clang::TypeLocBuilder&, clang::TypeLoc) SemaTemplateInstantiate.cpp:0:0
#18 0x000000000700836a clang::TreeTransform<(anonymous namespace)::TemplateInstantiator>::TransformType(clang::TypeSourceInfo*) SemaTemplateInstantiate.cpp:0:0
#19 0x000000000702a765 clang::TreeTransform<(anonymous namespace)::TemplateInstantiator>::TransformTypeWithDeducedTST(clang::TypeSourceInfo*) SemaTemplateInstantiate.cpp:0:0
#20 0x000000000702d4ef clang::TreeTransform<(anonymous namespace)::TemplateInstantiator>::TransformCXXUnresolvedConstructExpr(clang::CXXUnresolvedConstructExpr*) SemaTemplateInstantiate.cpp:0:0
#21 0x000000000700c2c4 clang::TreeTransform<(anonymous namespace)::TemplateInstantiator>::TransformExpr(clang::Expr*) SemaTemplateInstantiate.cpp:0:0
#22 0x000000000703869f clang::TreeTransform<(anonymous namespace)::TemplateInstantiator>::TransformStmt(clang::Stmt*, clang::TreeTransform<(anonymous namespace)::TemplateInstantiator>::StmtDiscardKind) SemaTemplateInstantiate.cpp:0:0
#23 0x0000000007039224 clang::TreeTransform<(anonymous namespace)::TemplateInstantiator>::TransformCompoundStmt(clang::CompoundStmt*, bool) SemaTemplateInstantiate.cpp:0:0
#24 0x000000000700b581 clang::TreeTransform<(anonymous namespace)::TemplateInstantiator>::TransformLambdaExpr(clang::LambdaExpr*) SemaTemplateInstantiate.cpp:0:0
#25 0x000000000700cac6 clang::TreeTransform<(anonymous namespace)::TemplateInstantiator>::TransformExpr(clang::Expr*) SemaTemplateInstantiate.cpp:0:0
#26 0x000000000701ed5e clang::TreeTransform<(anonymous namespace)::TemplateInstantiator>::TransformCallExpr(clang::CallExpr*) SemaTemplateInstantiate.cpp:0:0
#27 0x000000000700c30e clang::TreeTransform<(anonymous namespace)::TemplateInstantiator>::TransformExpr(clang::Expr*) SemaTemplateInstantiate.cpp:0:0
#28 0x000000000703869f clang::TreeTransform<(anonymous namespace)::TemplateInstantiator>::TransformStmt(clang::Stmt*, clang::TreeTransform<(anonymous namespace)::TemplateInstantiator>::StmtDiscardKind) SemaTemplateInstantiate.cpp:0:0
#29 0x0000000007039224 clang::TreeTransform<(anonymous namespace)::TemplateInstantiator>::TransformCompoundStmt(clang::CompoundStmt*, bool) SemaTemplateInstantiate.cpp:0:0
#30 0x0000000007040d04 clang::Sema::SubstStmt(clang::Stmt*, clang::MultiLevelTemplateArgumentList const&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x7040d04)
#31 0x00000000070b163f clang::Sema::InstantiateFunctionDefinition(clang::SourceLocation, clang::FunctionDecl*, bool, bool, bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x70b163f)
#32 0x0000000006ea7e21 clang::Sema::ActOnExplicitInstantiation(clang::Scope*, clang::SourceLocation, clang::SourceLocation, clang::Declarator&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6ea7e21)
#33 0x000000000655207d clang::Parser::ParseDeclarationAfterDeclaratorAndAttributes(clang::Declarator&, clang::Parser::ParsedTemplateInfo const&, clang::Parser::ForRangeInit*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x655207d)
#34 0x0000000006561519 clang::Parser::ParseDeclGroup(clang::ParsingDeclSpec&, clang::DeclaratorContext, clang::ParsedAttributes&, clang::Parser::ParsedTemplateInfo&, clang::SourceLocation*, clang::Parser::ForRangeInit*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6561519)
#35 0x000000000662bbc6 clang::Parser::ParseDeclarationAfterTemplate(clang::DeclaratorContext, clang::Parser::ParsedTemplateInfo&, clang::ParsingDeclRAIIObject&, clang::SourceLocation&, clang::ParsedAttributes&, clang::AccessSpecifier) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x662bbc6)
#36 0x000000000662c6b6 clang::Parser::ParseExplicitInstantiation(clang::DeclaratorContext, clang::SourceLocation, clang::SourceLocation, clang::SourceLocation&, clang::ParsedAttributes&, clang::AccessSpecifier) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x662c6b6)
#37 0x0000000006639878 clang::Parser::ParseDeclarationStartingWithTemplate(clang::DeclaratorContext, clang::SourceLocation&, clang::ParsedAttributes&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6639878)
#38 0x000000000656a4a3 clang::Parser::ParseDeclaration(clang::DeclaratorContext, clang::SourceLocation&, clang::ParsedAttributes&, clang::ParsedAttributes&, clang::SourceLocation*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x656a4a3)
#39 0x0000000006529407 clang::Parser::ParseExternalDeclaration(clang::ParsedAttributes&, clang::ParsedAttributes&, clang::ParsingDeclSpec*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6529407)
#40 0x000000000652a2ad clang::Parser::ParseTopLevelDecl(clang::OpaquePtr<clang::DeclGroupRef>&, clang::Sema::ModuleImportState&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x652a2ad)
#41 0x000000000651c7ca clang::ParseAST(clang::Sema&, bool, bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x651c7ca)
#42 0x0000000004578f98 clang::CodeGenAction::ExecuteAction() (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x4578f98)
#43 0x0000000004833eb9 clang::FrontendAction::Execute() (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x4833eb9)
#44 0x00000000047b3dce clang::CompilerInstance::ExecuteAction(clang::FrontendAction&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x47b3dce)
#45 0x000000000491edde clang::ExecuteCompilerInvocation(clang::CompilerInstance*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x491edde)
#46 0x0000000000caf871 cc1_main(llvm::ArrayRef<char const*>, char const*, void*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0xcaf871)
#47 0x0000000000ca73fa ExecuteCC1Tool(llvm::SmallVectorImpl<char const*>&, llvm::ToolContext const&) driver.cpp:0:0
#48 0x00000000045bc1f9 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
#49 0x0000000003b41394 llvm::CrashRecoveryContext::RunSafely(llvm::function_ref<void ()>) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x3b41394)
#50 0x00000000045bc7ef 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
#51 0x0000000004582a7d clang::driver::Compilation::ExecuteCommand(clang::driver::Command const&, clang::driver::Command const*&, bool) const (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x4582a7d)
#52 0x0000000004583b6d 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+++0x4583b6d)
#53 0x000000000458af25 clang::driver::Driver::ExecuteCompilation(clang::driver::Compilation&, llvm::SmallVectorImpl<std::pair<int, clang::driver::Command const*>>&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x458af25)
#54 0x0000000000cac6a3 clang_main(int, char**, llvm::ToolContext const&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0xcac6a3)
#55 0x0000000000b7fe54 main (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0xb7fe54)
#56 0x00007e5612a29d90 (/lib/x86_64-linux-gnu/libc.so.6+0x29d90)
#57 0x00007e5612a29e40 __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x29e40)
#58 0x0000000000ca6eae _start (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0xca6eae)
See Compiler Explorer: https://godbolt.org/z/47v9bGKEG
@zyn0217
I tested locally and this can be fixed by #107942