From a8235518c9326c7e3c28931313d5d4344ce8c031 Mon Sep 17 00:00:00 2001 From: Oleksandr T Date: Sat, 30 Nov 2024 11:38:10 +0200 Subject: [PATCH] Revert [Clang] prevent errors for deduction guides using deduced type aliases --- clang/docs/ReleaseNotes.rst | 3 -- clang/lib/Sema/SemaDeclCXX.cpp | 40 +++++++++----------- clang/test/CXX/temp/temp.deduct.guide/p3.cpp | 10 +---- 3 files changed, 18 insertions(+), 35 deletions(-) diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst index e44aefa90ab38..d867f2ea3e8e2 100644 --- a/clang/docs/ReleaseNotes.rst +++ b/clang/docs/ReleaseNotes.rst @@ -624,9 +624,6 @@ Improvements to Clang's diagnostics - Fixed a false negative ``-Wunused-private-field`` diagnostic when a defaulted comparison operator is defined out of class (#GH116961). -- Clang now supports using alias templates in deduction guides, aligning with the C++ standard, - which treats alias templates as synonyms for their underlying types (#GH54909). - - Clang now diagnoses dangling references for C++20's parenthesized aggregate initialization (#101957). Improvements to Clang's time-trace diff --git a/clang/lib/Sema/SemaDeclCXX.cpp b/clang/lib/Sema/SemaDeclCXX.cpp index c2ae9a9d5e1a0..7e8e321c4b90e 100644 --- a/clang/lib/Sema/SemaDeclCXX.cpp +++ b/clang/lib/Sema/SemaDeclCXX.cpp @@ -11451,29 +11451,23 @@ bool Sema::CheckDeductionGuideDeclarator(Declarator &D, QualType &R, bool MightInstantiateToSpecialization = false; if (auto RetTST = TSI->getTypeLoc().getAsAdjusted()) { - const TemplateSpecializationType *TST = RetTST.getTypePtr(); - while (TST && TST->isTypeAlias()) - TST = TST->getAliasedType()->getAs(); - - if (TST) { - TemplateName SpecifiedName = TST->getTemplateName(); - bool TemplateMatches = Context.hasSameTemplateName( - SpecifiedName, GuidedTemplate, /*IgnoreDeduced=*/true); - - const QualifiedTemplateName *Qualifiers = - SpecifiedName.getAsQualifiedTemplateName(); - assert(Qualifiers && "expected QualifiedTemplate"); - bool SimplyWritten = !Qualifiers->hasTemplateKeyword() && - Qualifiers->getQualifier() == nullptr; - if (SimplyWritten && TemplateMatches) - AcceptableReturnType = true; - else { - // This could still instantiate to the right type, unless we know it - // names the wrong class template. - auto *TD = SpecifiedName.getAsTemplateDecl(); - MightInstantiateToSpecialization = - !(TD && isa(TD) && !TemplateMatches); - } + TemplateName SpecifiedName = RetTST.getTypePtr()->getTemplateName(); + bool TemplateMatches = Context.hasSameTemplateName( + SpecifiedName, GuidedTemplate, /*IgnoreDeduced=*/true); + + const QualifiedTemplateName *Qualifiers = + SpecifiedName.getAsQualifiedTemplateName(); + assert(Qualifiers && "expected QualifiedTemplate"); + bool SimplyWritten = !Qualifiers->hasTemplateKeyword() && + Qualifiers->getQualifier() == nullptr; + if (SimplyWritten && TemplateMatches) + AcceptableReturnType = true; + else { + // This could still instantiate to the right type, unless we know it + // names the wrong class template. + auto *TD = SpecifiedName.getAsTemplateDecl(); + MightInstantiateToSpecialization = + !(TD && isa(TD) && !TemplateMatches); } } else if (!RetTy.hasQualifiers() && RetTy->isDependentType()) { MightInstantiateToSpecialization = true; diff --git a/clang/test/CXX/temp/temp.deduct.guide/p3.cpp b/clang/test/CXX/temp/temp.deduct.guide/p3.cpp index 195ba2831439d..c5404847beb06 100644 --- a/clang/test/CXX/temp/temp.deduct.guide/p3.cpp +++ b/clang/test/CXX/temp/temp.deduct.guide/p3.cpp @@ -33,7 +33,7 @@ template typename TT> struct E { // expected-note 2{{template }; A(int) -> int; // expected-error {{deduced type 'int' of deduction guide is not a specialization of template 'A'}} -template A(T)->B; +template A(T)->B; // expected-error {{deduced type 'B' (aka 'A') of deduction guide is not written as a specialization of template 'A'}} template A(T*) -> const A; // expected-error {{deduced type 'const A' of deduction guide is not a specialization of template 'A'}} // A deduction-guide shall be declared in the same scope as the corresponding @@ -71,11 +71,3 @@ namespace WrongScope { Local(int) -> Local; // expected-error {{expected}} } } - -namespace GH54909 { -template struct A {}; -struct B {}; - -template using C = B; -template A() -> C; // expected-error {{deduced type 'C' (aka 'GH54909::B') of deduction guide is not a specialization of template 'A'}} -}