Skip to content

Commit 6404a77

Browse files
committed
Handle cases where the RHS of type alias is not a template
specialization type.
1 parent 5fd9e1f commit 6404a77

File tree

2 files changed

+27
-10
lines changed

2 files changed

+27
-10
lines changed

clang/lib/Sema/SemaInit.cpp

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#include "clang/Sema/SemaInternal.h"
3535
#include "clang/Sema/Template.h"
3636
#include "llvm/ADT/APInt.h"
37+
#include "llvm/ADT/ArrayRef.h"
3738
#include "llvm/ADT/FoldingSet.h"
3839
#include "llvm/ADT/PointerIntPair.h"
3940
#include "llvm/ADT/SmallString.h"
@@ -11003,16 +11004,29 @@ QualType Sema::DeduceTemplateSpecializationFromInitializer(
1100311004
dyn_cast_or_null<ClassTemplateDecl>(TemplateName.getAsTemplateDecl());
1100411005

1100511006
TypeAliasTemplateDecl* AliasTemplate = nullptr;
11007+
llvm::ArrayRef<TemplateArgument> AliasRhsTemplateArgs;
1100611008
if (!Template) {
11007-
if ((AliasTemplate = dyn_cast_or_null<TypeAliasTemplateDecl>(
11008-
TemplateName.getAsTemplateDecl()))) {
11009+
if (AliasTemplate = dyn_cast_or_null<TypeAliasTemplateDecl>(
11010+
TemplateName.getAsTemplateDecl()); AliasTemplate) {
11011+
llvm::errs() << "alias template decl\n";
1100911012
auto UnderlyingType = AliasTemplate->getTemplatedDecl()
1101011013
->getUnderlyingType()
1101111014
.getDesugaredType(Context);
1101211015
if (const auto *TST =
1101311016
UnderlyingType->getAs<TemplateSpecializationType>()) {
11017+
// normal cases: using AliasFoo = Foo<T, U>;
1101411018
Template = dyn_cast_or_null<ClassTemplateDecl>(
1101511019
TST->getTemplateName().getAsTemplateDecl());
11020+
AliasRhsTemplateArgs = TST->template_arguments();
11021+
} else if (const auto *RT = UnderlyingType->getAs<RecordType>()) {
11022+
// cases where template arguments in the RHS of the alias are not
11023+
// dependent. e.g.
11024+
// using AliasFoo = Foo<bool>;
11025+
if (const auto *CTSD = llvm::dyn_cast<ClassTemplateSpecializationDecl>(
11026+
RT->getAsCXXRecordDecl())) {
11027+
Template = CTSD->getSpecializedTemplate();
11028+
AliasRhsTemplateArgs = CTSD->getTemplateArgs().asArray();
11029+
}
1101611030
}
1101711031
}
1101811032
}
@@ -11098,15 +11112,10 @@ QualType Sema::DeduceTemplateSpecializationFromInitializer(
1109811112
//
1109911113
// The RHS of alias is f<int, U>, we deduced the template arguments of
1110011114
// the return type of the deduction guide from it: Y->int, X -> U
11101-
const auto* AliasRhsTST = AliasTemplate->getTemplatedDecl()
11102-
->getUnderlyingType()
11103-
.getDesugaredType(this->Context)
11104-
->getAs<TemplateSpecializationType>();
11105-
assert(AliasRhsTST);
11106-
1110711115
if (DeduceTemplateArguments(AliasTemplate->getTemplateParameters(),
1110811116
ReturnTST->template_arguments(),
11109-
AliasRhsTST->template_arguments(),
11117+
//AliasRhsTST->template_arguments(),
11118+
AliasRhsTemplateArgs,
1111011119
TDeduceInfo, DeduceResults,
1111111120
/*NumberOfArgumentsMustMatch*/ false)) {
1111211121
// FIXME: not all template arguments are deduced, we should continue

clang/test/SemaCXX/cxx20-ctad-type-alias.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,12 @@ void test1() {
6262
// FIXME: should select X<double> deduction guide
6363
// static_assert(__is_same(decltype(s.t), double));
6464
}
65-
}
65+
}
66+
67+
namespace test5 {
68+
template<int B>
69+
struct Foo {};
70+
template<int... C>
71+
using AF = Foo<1>;
72+
auto a = AF {};
73+
}

0 commit comments

Comments
 (0)