| Issue |
237766
|
| Summary |
[clang] Alias CTAD crashes or rejects valid code when a ctor template's `explicit(bool)` names its own template parameter
|
| Labels |
clang
|
| Assignees |
|
| Reporter |
0xd34df00d
|
When clang derives an alias template's deduction guide from a constructor template, the constructor's `explicit(bool)` is evaluated with the wrong template argument. A reference to the constructor template's own (defaulted, non-deduced) template parameter gets the alias template's first template argument instead. If that argument is a non-type, clang crashes. If it's a type, the guide gets the wrong explicitness.
### Crash
```cpp
template <class T>
struct Box
{
template <class U = int>
explicit (sizeof (U) == 1)
Box (T) {}
};
template<int K> struct Tag {};
template<int K> using TagBox = Box<Tag<K>>;
TagBox b { Tag<0> {} };
```
`clang++ -std=c++20 -fsyntax-only crash.cpp` segfaults (clang 23.1.1) with
<details>
<summary>this trace.</summary>
```
#10 0x00007ffb2534d8cd clang::Sema::SubstExpr(clang::Expr*, clang::MultiLevelTemplateArgumentList const&) (/usr/lib/llvm/23/bin/../lib64/libclang-cpp.so.23.1+0x254d8cd)
#11 0x00007ffb25386b0d clang::Sema::instantiateExplicitSpecifier(clang::MultiLevelTemplateArgumentList const&, clang::ExplicitSpecifier) (/usr/lib/llvm/23/bin/../lib64/libclang-cpp.so.23.1+0x2586b0d)
#12 0x00007ffb253df1ee clang::TemplateDeclInstantiator::VisitFunctionDecl(clang::FunctionDecl*, clang::TemplateParameterList*, clang::TemplateDeclInstantiator::RewriteKind) (/usr/lib/llvm/23/bin/../lib64/libclang-cpp.so.23.1+0x25df1ee)
#13 0x00007ffb253e0c55 clang::TemplateDeclInstantiator::VisitCXXDeductionGuideDecl(clang::CXXDeductionGuideDecl*) (/usr/lib/llvm/23/bin/../lib64/libclang-cpp.so.23.1+0x25e0c55)
#14 0x00007ffb253e4404 (/usr/lib/llvm/23/bin/../lib64/libclang-cpp.so.23.1+0x25e4404)
#15 0x00007ffb23b074be clang::StackExhaustionHandler::runWithSufficientStackSpace(clang::SourceLocation, llvm::function_ref<void ()>) (/usr/lib/llvm/23/bin/../lib64/libclang-cpp.so.23.1+0xd074be)
#16 0x00007ffb2538724a clang::Sema::SubstDecl(clang::Decl*, clang::DeclContext*, clang::MultiLevelTemplateArgumentList const&) (/usr/lib/llvm/23/bin/../lib64/libclang-cpp.so.23.1+0x258724a)
#17 0x00007ffb252adf03 clang::Sema::FinishTemplateArgumentDeduction(clang::FunctionTemplateDecl*, llvm::SmallVectorImpl<clang::DeducedTemplateArgument>&, unsigned int, clang::FunctionDecl*&, clang::sema::TemplateDeductionInfo&, llvm::SmallVectorImpl<clang::Sema::OriginalCallArg> const*, bool, bool, bool, llvm::function_ref<bool (bool)>) (/usr/lib/llvm/23/bin/../lib64/libclang-cpp.so.23.1+0x24adf03)
#18 0x00007ffb252aebf7 (/usr/lib/llvm/23/bin/../lib64/libclang-cpp.so.23.1+0x24aebf7)
#19 0x00007ffb23b074be clang::StackExhaustionHandler::runWithSufficientStackSpace(clang::SourceLocation, llvm::function_ref<void ()>) (/usr/lib/llvm/23/bin/../lib64/libclang-cpp.so.23.1+0xd074be)
#20 0x00007ffb2528ad8f clang::Sema::DeduceTemplateArguments(clang::FunctionTemplateDecl*, clang::TemplateArgumentListInfo*, llvm::ArrayRef<clang::Expr*>, clang::FunctionDecl*&, clang::sema::TemplateDeductionInfo&, bool, bool, bool, clang::QualType, clang::Expr::Classification, bool, llvm::function_ref<bool (llvm::ArrayRef<clang::QualType>, bool)>) (/usr/lib/llvm/23/bin/../lib64/libclang-cpp.so.23.1+0x248ad8f)
#21 0x00007ffb25111354 (/usr/lib/llvm/23/bin/../lib64/libclang-cpp.so.23.1+0x2311354)
#22 0x00007ffb25111903 clang::OverloadCandidateSet::InjectNonDeducedTemplateCandidates(clang::Sema&) (/usr/lib/llvm/23/bin/../lib64/libclang-cpp.so.23.1+0x2311903)
#23 0x00007ffb25111a2a clang::OverloadCandidateSet::BestViableFunction(clang::Sema&, clang::SourceLocation, clang::OverloadCandidate*&) (/usr/lib/llvm/23/bin/../lib64/libclang-cpp.so.23.1+0x2311a2a)
#24 0x00007ffb24eff1a9 (/usr/lib/llvm/23/bin/../lib64/libclang-cpp.so.23.1+0x20ff1a9)
#25 0x00007ffb24efffa4 clang::Sema::DeduceTemplateSpecializationFromInitializer(clang::TypeSourceInfo*, clang::InitializedEntity const&, clang::InitializationKind const&, llvm::MutableArrayRef<clang::Expr*>) (/usr/lib/llvm/23/bin/../lib64/libclang-cpp.so.23.1+0x20fffa4)
#26 0x00007ffb24bfae81 clang::Sema::deduceVarTypeFromInitializer(clang::VarDecl*, clang::DeclarationName, clang::QualType, clang::TypeSourceInfo*, clang::SourceRange, bool, clang::Expr*) (/usr/lib/llvm/23/bin/../lib64/libclang-cpp.so.23.1+0x1dfae81)
#27 0x00007ffb24c0509e clang::Sema::DeduceVariableDeclarationType(clang::VarDecl*, bool, clang::Expr*) (/usr/lib/llvm/23/bin/../lib64/libclang-cpp.so.23.1+0x1e0509e)
#28 0x00007ffb24c32611 clang::Sema::AddInitializerToDecl(clang::Decl*, clang::Expr*, bool) (/usr/lib/llvm/23/bin/../lib64/libclang-cpp.so.23.1+0x1e32611)
#29 0x00007ffb23cf7445 clang::Parser::ParseDeclarationAfterDeclaratorAndAttributes(clang::Declarator&, clang::Parser::ParsedTemplateInfo const&, clang::Parser::ForRangeInit*) (/usr/lib/llvm/23/bin/../lib64/libclang-cpp.so.23.1+0xef7445)
#30 0x00007ffb23d19ce3 clang::Parser::ParseDeclGroup(clang::ParsingDeclSpec&, clang::DeclaratorContext, clang::ParsedAttributes&, clang::Parser::ParsedTemplateInfo&, clang::SourceLocation*, clang::Parser::ForRangeInit*) (/usr/lib/llvm/23/bin/../lib64/libclang-cpp.so.23.1+0xf19ce3)
#31 0x00007ffb23e0bf6f clang::Parser::ParseDeclOrFunctionDefInternal(clang::ParsedAttributes&, clang::ParsedAttributes&, clang::ParsingDeclSpec&, clang::AccessSpecifier) (/usr/lib/llvm/23/bin/../lib64/libclang-cpp.so.23.1+0x100bf6f)
#32 0x00007ffb23e0c729 clang::Parser::ParseDeclarationOrFunctionDefinition(clang::ParsedAttributes&, clang::ParsedAttributes&, clang::ParsingDeclSpec*, clang::AccessSpecifier) (/usr/lib/llvm/23/bin/../lib64/libclang-cpp.so.23.1+0x100c729)
#33 0x00007ffb23e15dc8 clang::Parser::ParseExternalDeclaration(clang::ParsedAttributes&, clang::ParsedAttributes&, clang::ParsingDeclSpec*) (/usr/lib/llvm/23/bin/../lib64/libclang-cpp.so.23.1+0x1015dc8)
#34 0x00007ffb23e16f71 clang::Parser::ParseTopLevelDecl(clang::OpaquePtr<clang::DeclGroupRef>&, clang::Sema::ModuleImportState&) (/usr/lib/llvm/23/bin/../lib64/libclang-cpp.so.23.1+0x1016f71)
#35 0x00007ffb23cdef4c clang::ParseAST(clang::Sema&, bool, bool) (/usr/lib/llvm/23/bin/../lib64/libclang-cpp.so.23.1+0xedef4c)
#36 0x00007ffb261ed9ef clang::FrontendAction::Execute() (/usr/lib/llvm/23/bin/../lib64/libclang-cpp.so.23.1+0x33ed9ef)
#37 0x00007ffb2617d0ed clang::CompilerInstance::ExecuteAction(clang::FrontendAction&) (/usr/lib/llvm/23/bin/../lib64/libclang-cpp.so.23.1+0x337d0ed)
#38 0x00007ffb26283bbf clang::ExecuteCompilerInvocation(clang::CompilerInstance*) (/usr/lib/llvm/23/bin/../lib64/libclang-cpp.so.23.1+0x3483bbf)
#39 0x000055c321f52fef cc1_main(llvm::ArrayRef<char const*>, char const*, void*) (/usr/lib/llvm/23/bin/clang+0x17fef)
#40 0x000055c321f4ba1d (/usr/lib/llvm/23/bin/clang+0x10a1d)
#41 0x000055c321f4bee6 (/usr/lib/llvm/23/bin/clang+0x10ee6)
#42 0x00007ffb25d22499 (/usr/lib/llvm/23/bin/../lib64/libclang-cpp.so.23.1+0x2f22499)
#43 0x00007ffb197db704 llvm::CrashRecoveryContext::RunSafely(llvm::function_ref<void ()>) (/usr/lib/llvm/23/bin/../lib64/libLLVM.so.23.1+0xddb704)
#44 0x00007ffb25d24a3b clang::driver::CC1Command::Execute(llvm::ArrayRef<std::optional<llvm::StringRef>>, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>*, bool*) const (/usr/lib/llvm/23/bin/../lib64/libclang-cpp.so.23.1+0x2f24a3b)
#45 0x00007ffb25ccc119 clang::driver::Compilation::ExecuteCommand(clang::driver::Command const&, clang::driver::Command const*&, bool) const (/usr/lib/llvm/23/bin/../lib64/libclang-cpp.so.23.1+0x2ecc119)
#46 0x00007ffb25ccf4d6 clang::driver::Compilation::ExecuteJobs(clang::driver::JobList const&, llvm::SmallVectorImpl<std::pair<int, clang::driver::Command const*>>&, bool) const (/usr/lib/llvm/23/bin/../lib64/libclang-cpp.so.23.1+0x2ecf4d6)
#47 0x00007ffb25ced01c clang::driver::Driver::ExecuteCompilation(clang::driver::Compilation&, llvm::SmallVectorImpl<std::pair<int, clang::driver::Command const*>>&) (/usr/lib/llvm/23/bin/../lib64/libclang-cpp.so.23.1+0x2eed01c)
#48 0x000055c321f4da73 clang_main(int, char**, llvm::ToolContext const&) (/usr/lib/llvm/23/bin/clang+0x12a73)
```
</details>
(see also [godbolt](https://godbolt.org/z/jb85Gf54Y))
### Rejects-valid
The same `Box`, with a type alias parameter (also [godbolt](https://godbolt.org/z/TvKhPo993)):
```cpp
template<typename X> using PtrBox = Box<X*>;
PtrBox a = (long*) nullptr; // accepted
PtrBox b = (char*) nullptr; // rejected
```
```
error: no viable constructor or deduction guide for deduction of template arguments of 'PtrBox'
note: explicit constructor is not a candidate (explicit specifier evaluates to true)
```
With `U ≔ int`, `sizeof (U) == 1` is false, so the guide isn't explicit and both should be accepted, as gcc does. Clang evaluates `sizeof (X)` instead, which is 1 for `char` and 8 for `long`.
### `std::pair`
`pair(const T1&, const T2&)` has this shape in libc++ (`template <class _CheckArgsDep = __check_pair_construction<_T1, _T2>, ...> explicit(!_CheckArgsDep::template __is_implicit<...>())`) and in the MSVC STL (`template <class _Uty1 = _Ty1, class _Uty2 = _Ty2, ...> explicit(!conjunction_v<is_convertible<const _Uty1&, _Uty1>, ...>)`). So alias CTAD over `std::pair` with a non-type alias parameter crashes with `-stdlib=libc++` and with clang-cl (again [goldbolt](https://godbolt.org/z/hM18ccf11)):
```cpp
#include <utility>
template<int K> struct Tag {};
template<int K> using TagPair = std::pair<Tag<K>, Tag<K>>;
TagPair p { Tag<0> {}, Tag<0> {} };
```
libstdc++'s constructor doesn't do that so it doesn't crash.
### Versions
- crash and rejects-valid: clang 21.1.8, 22.1.8 and 23.1.1 on Gentoo x86_64, and trunk (`Debian clang version 24.0.0 (++20261030101127+97d25ba34f77)` from apt.llvm.org)
- the `std::pair` crash: trunk with libc++, and clang-cl 23.1.4 with the MSVC 14.51 STL
- gcc 16.2 accepts all three
These are fine:
- CTAD on `Box` itself, in every variant I tried.
- An alias that only renames, `template<class X> using Same = Box<X>;`.
- The same use of `U` in a `requires` clause instead of `explicit(...)`.
- An `explicit(...)` that names only `T`.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs