-
Notifications
You must be signed in to change notification settings - Fork 13.6k
[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
Conversation
@llvm/pr-subscribers-clang Author: Oleksandr T. (a-tarasyuk) ChangesFixes #54909 Full diff: https://github.com/llvm/llvm-project/pull/117450.diff 3 Files Affected:
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>;
+}
|
Can you add more details to the description? Thanks! |
There was a problem hiding this 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.
There was a problem hiding this 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
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. |
… 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).
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 typeWith this change, Clang now correctly resolves alias templates to their underlying types in deduction guides, ensuring compliance with the C++ standard.