Skip to content

Commit 6218992

Browse files
committed
[clang] CTAD: fix the aggregate deduction guide for alias templates.
For alias templates, the way we construct their aggregate deduction guides is not following the standard way. We should do the same thing as we do for implicit deduction guides.
1 parent 4036514 commit 6218992

File tree

3 files changed

+23
-60
lines changed

3 files changed

+23
-60
lines changed

clang/lib/Sema/SemaTemplate.cpp

Lines changed: 2 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -3053,66 +3053,8 @@ FunctionTemplateDecl *DeclareAggregateDeductionGuideForTypeAlias(
30533053
RHSTemplate, ParamTypes, Loc);
30543054
if (!RHSDeductionGuide)
30553055
return nullptr;
3056-
3057-
LocalInstantiationScope Scope(SemaRef);
3058-
Sema::InstantiatingTemplate BuildingDeductionGuides(
3059-
SemaRef, AliasTemplate->getLocation(), RHSDeductionGuide,
3060-
Sema::InstantiatingTemplate::BuildingDeductionGuidesTag{});
3061-
if (BuildingDeductionGuides.isInvalid())
3062-
return nullptr;
3063-
3064-
// Build a new template parameter list for the synthesized aggregate deduction
3065-
// guide by transforming the one from RHSDeductionGuide.
3066-
SmallVector<NamedDecl *> TransformedTemplateParams;
3067-
// Template args that refer to the rebuilt template parameters.
3068-
// All template arguments must be initialized in advance.
3069-
SmallVector<TemplateArgument> TransformedTemplateArgs(
3070-
RHSDeductionGuide->getTemplateParameters()->size());
3071-
for (auto *TP : *RHSDeductionGuide->getTemplateParameters()) {
3072-
// Rebuild any internal references to earlier parameters and reindex as
3073-
// we go.
3074-
MultiLevelTemplateArgumentList Args;
3075-
Args.setKind(TemplateSubstitutionKind::Rewrite);
3076-
Args.addOuterTemplateArguments(TransformedTemplateArgs);
3077-
NamedDecl *NewParam = transformTemplateParameter(
3078-
SemaRef, AliasTemplate->getDeclContext(), TP, Args,
3079-
/*NewIndex=*/TransformedTemplateParams.size());
3080-
3081-
TransformedTemplateArgs[TransformedTemplateParams.size()] =
3082-
SemaRef.Context.getCanonicalTemplateArgument(
3083-
SemaRef.Context.getInjectedTemplateArg(NewParam));
3084-
TransformedTemplateParams.push_back(NewParam);
3085-
}
3086-
// FIXME: implement the is_deducible constraint per C++
3087-
// [over.match.class.deduct]p3.3.
3088-
Expr *TransformedRequiresClause = transformRequireClause(
3089-
SemaRef, RHSDeductionGuide, TransformedTemplateArgs);
3090-
auto *TransformedTemplateParameterList = TemplateParameterList::Create(
3091-
SemaRef.Context, AliasTemplate->getTemplateParameters()->getTemplateLoc(),
3092-
AliasTemplate->getTemplateParameters()->getLAngleLoc(),
3093-
TransformedTemplateParams,
3094-
AliasTemplate->getTemplateParameters()->getRAngleLoc(),
3095-
TransformedRequiresClause);
3096-
auto *TransformedTemplateArgList = TemplateArgumentList::CreateCopy(
3097-
SemaRef.Context, TransformedTemplateArgs);
3098-
3099-
if (auto *TransformedDeductionGuide = SemaRef.InstantiateFunctionDeclaration(
3100-
RHSDeductionGuide, TransformedTemplateArgList,
3101-
AliasTemplate->getLocation(),
3102-
Sema::CodeSynthesisContext::BuildingDeductionGuides)) {
3103-
auto *GD =
3104-
llvm::dyn_cast<clang::CXXDeductionGuideDecl>(TransformedDeductionGuide);
3105-
FunctionTemplateDecl *Result = buildDeductionGuide(
3106-
SemaRef, AliasTemplate, TransformedTemplateParameterList,
3107-
GD->getCorrespondingConstructor(), GD->getExplicitSpecifier(),
3108-
GD->getTypeSourceInfo(), AliasTemplate->getBeginLoc(),
3109-
AliasTemplate->getLocation(), AliasTemplate->getEndLoc(),
3110-
GD->isImplicit());
3111-
cast<CXXDeductionGuideDecl>(Result->getTemplatedDecl())
3112-
->setDeductionCandidateKind(DeductionCandidate::Aggregate);
3113-
return Result;
3114-
}
3115-
return nullptr;
3056+
return BuildDeductionGuideForTypeAlias(SemaRef, AliasTemplate,
3057+
RHSDeductionGuide, Loc);
31163058
}
31173059

31183060
} // namespace

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,3 +307,17 @@ using AFoo = Foo<int, Derived<U>>;
307307

308308
AFoo a(Derived<int>{});
309309
} // namespace test22
310+
311+
namespace test23 {
312+
// We have an aggregate deduction guide "G(T) -> G<T>".
313+
template<typename T>
314+
struct G { T t1; };
315+
316+
template<typename X = int>
317+
using AG = G<int>;
318+
319+
AG ag(1.0);
320+
// Verify that the aggregate deduction guide "AG(int) -> AG<int>" is built and
321+
// choosen.
322+
static_assert(__is_same(decltype(ag.t1), int));
323+
} // namespace test23

clang/test/SemaTemplate/deduction-guide.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,13 @@ AG ag = {1};
261261
// CHECK: | `-BuiltinType {{.*}} 'int'
262262
// CHECK: `-ParmVarDecl {{.*}} 'int'
263263

264+
template <typename X = int>
265+
using BG = G<int>;
266+
BG bg(1.0);
267+
// CHECK-LABEL: Dumping <deduction guide for BG>
268+
// CHECK: FunctionTemplateDecl {{.*}} implicit <deduction guide for BG>
269+
// CHECK: |-CXXDeductionGuideDecl {{.*}} 'auto (int) -> G<int>' aggregate
270+
264271
template <typename D>
265272
requires (sizeof(D) == 4)
266273
struct Foo {

0 commit comments

Comments
 (0)