Skip to content

[Clang] prevent errors for deduction guides using deduced type aliases #117450

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Nov 27, 2024

Conversation

a-tarasyuk
Copy link
Member

@a-tarasyuk a-tarasyuk commented Nov 23, 2024

Fixes #54909


Clang incorrectly produces diagnostics for alias templates in deduction guides, treating them as separate from their underlying types. This issue arises because Clang doesn't properly handle TypeAliasTemplateDecl when comparing template names for equality in the context of deduction guides, resulting in diagnostics that don't align with the C++ standard. As the C++ standard specifies - an alias template is considered a synonym for its underlying type

With this change, Clang now correctly resolves alias templates to their underlying types in deduction guides, ensuring compliance with the C++ standard.

@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Nov 23, 2024
@llvmbot
Copy link
Member

llvmbot commented Nov 23, 2024

@llvm/pr-subscribers-clang

Author: Oleksandr T. (a-tarasyuk)

Changes

Fixes #54909


Full diff: https://github.com/llvm/llvm-project/pull/117450.diff

3 Files Affected:

  • (modified) clang/docs/ReleaseNotes.rst (+2)
  • (modified) clang/lib/Sema/SemaDeclCXX.cpp (+5-1)
  • (modified) clang/test/CXX/temp/temp.deduct.guide/p3.cpp (+11-1)
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 8bd06fadfdc984..2437def6322764 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -583,6 +583,8 @@ Improvements to Clang's diagnostics
 - For an rvalue reference bound to a temporary struct with an integer member, Clang will detect constant integer overflow
   in the initializer for the integer member (#GH46755).
 
+- Clang now prevents errors for deduction guides with deduced type aliases (#GH54909).
+
 Improvements to Clang's time-trace
 ----------------------------------
 
diff --git a/clang/lib/Sema/SemaDeclCXX.cpp b/clang/lib/Sema/SemaDeclCXX.cpp
index 26041e53de5061..9d0eb098841a3a 100644
--- a/clang/lib/Sema/SemaDeclCXX.cpp
+++ b/clang/lib/Sema/SemaDeclCXX.cpp
@@ -11451,7 +11451,11 @@ bool Sema::CheckDeductionGuideDeclarator(Declarator &D, QualType &R,
     bool MightInstantiateToSpecialization = false;
     if (auto RetTST =
             TSI->getTypeLoc().getAsAdjusted<TemplateSpecializationTypeLoc>()) {
-      TemplateName SpecifiedName = RetTST.getTypePtr()->getTemplateName();
+      const TemplateSpecializationType *TST = RetTST.getTypePtr();
+      while (TST->isTypeAlias())
+        TST = TST->getAliasedType()->getAs<TemplateSpecializationType>();
+
+      TemplateName SpecifiedName = TST->getTemplateName();
       bool TemplateMatches = Context.hasSameTemplateName(
           SpecifiedName, GuidedTemplate, /*IgnoreDeduced=*/true);
 
diff --git a/clang/test/CXX/temp/temp.deduct.guide/p3.cpp b/clang/test/CXX/temp/temp.deduct.guide/p3.cpp
index c5404847beb066..36e0f75ccf9098 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<template<typename> 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 <typename T> A(T)->B<T>;         // expected-error {{deduced type 'B<T>' (aka 'A<T>') of deduction guide is not written as a specialization of template 'A'}}
+template <typename T> A(T)->B<T>;
 template<typename T> A(T*) -> const A<T>; // expected-error {{deduced type 'const A<T>' of deduction guide is not a specialization of template 'A'}}
 
 // A deduction-guide shall be declared in the same scope as the corresponding
@@ -71,3 +71,13 @@ namespace WrongScope {
     Local(int) -> Local<int>; // expected-error {{expected}}
   }
 }
+
+namespace GH54909 {
+template <typename T> struct A {};
+A(void) -> A<int>;
+
+template <typename T> using B = A<T>;
+template <typename T> using C = B<T>;
+template <typename T> using D = C<T>;
+template <typename T> A(T) -> D<T>;
+}

@a-tarasyuk a-tarasyuk requested a review from hokein November 24, 2024 20:51
@a-tarasyuk a-tarasyuk requested a review from zyn0217 November 25, 2024 12:52
@cor3ntin cor3ntin requested a review from antangelo November 26, 2024 08:43
@cor3ntin
Copy link
Contributor

Can you add more details to the description? Thanks!

Copy link
Collaborator

@hokein hokein left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, this looks good to me.

Copy link
Contributor

@antangelo antangelo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM other than one question

@a-tarasyuk a-tarasyuk requested a review from antangelo November 26, 2024 18:54
@a-tarasyuk a-tarasyuk merged commit eb00182 into llvm:main Nov 27, 2024
9 checks passed
@erichkeane
Copy link
Collaborator

Hmmm... See the comment on the issue. I'll have to look more closely, but this very well might be the incorrect change. This perhaps needs a revert.

a-tarasyuk added a commit that referenced this pull request Dec 2, 2024
… aliases (#118165)

Reverts #117450

---

Revert #117450 because the
prior behavior aligns with the standard - _`template-name`_ in the
_`simple-template-id`_ in the return type shall be the actual name of a
class template rather than some equivalent alias template. Details
#54909 (comment).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

bogus diagnostic deduction_guide_bad_trailing_return_type
7 participants