|
| 1 | +From 0f6ed4c394fd8f843029f6919230bf8df8618529 Mon Sep 17 00:00:00 2001 |
| 2 | +From: Younan Zhang < [email protected]> |
| 3 | +Date: Fri, 29 Mar 2024 23:28:54 +0800 |
| 4 | +Subject: [PATCH] [clang][Sema] Fix a CTAD regression after 42239d2e9 (#86914) |
| 5 | + |
| 6 | +The most recent declaration of a template as a friend can introduce a |
| 7 | +different template parameter depth compared to what we anticipate from a |
| 8 | +CTAD guide. |
| 9 | + |
| 10 | +Fixes https://github.com/llvm/llvm-project/issues/86769 |
| 11 | +--- |
| 12 | + clang/docs/ReleaseNotes.rst | 3 +++ |
| 13 | + clang/lib/Sema/SemaTemplate.cpp | 22 ++++++++++++++++- |
| 14 | + clang/test/SemaTemplate/concepts-friends.cpp | 26 ++++++++++++++++++++ |
| 15 | + clang/test/SemaTemplate/ctad.cpp | 2 +- |
| 16 | + 4 files changed, 51 insertions(+), 2 deletions(-) |
| 17 | + |
| 18 | +diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst |
| 19 | +index 232de0d7d8bb..c193c20fae5c 100644 |
| 20 | +--- a/clang/docs/ReleaseNotes.rst |
| 21 | ++++ b/clang/docs/ReleaseNotes.rst |
| 22 | +@@ -357,6 +357,9 @@ Bug Fixes in This Version |
| 23 | + - Fixes an assertion failure on invalid code when trying to define member |
| 24 | + functions in lambdas. |
| 25 | + |
| 26 | ++- Fixed a regression in CTAD that a friend declaration that befriends itself may cause |
| 27 | ++ incorrect constraint substitution. (#GH86769). |
| 28 | ++ |
| 29 | + Bug Fixes to Compiler Builtins |
| 30 | + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 31 | + |
| 32 | +diff --git a/clang/lib/Sema/SemaTemplate.cpp b/clang/lib/Sema/SemaTemplate.cpp |
| 33 | +index e575bb2df97f..9cd19d711af4 100644 |
| 34 | +--- a/clang/lib/Sema/SemaTemplate.cpp |
| 35 | ++++ b/clang/lib/Sema/SemaTemplate.cpp |
| 36 | +@@ -1836,7 +1836,27 @@ static TemplateParameterList *GetTemplateParameterList(TemplateDecl *TD) { |
| 37 | + // Make sure we get the template parameter list from the most |
| 38 | + // recent declaration, since that is the only one that is guaranteed to |
| 39 | + // have all the default template argument information. |
| 40 | +- return cast<TemplateDecl>(TD->getMostRecentDecl())->getTemplateParameters(); |
| 41 | ++ Decl *D = TD->getMostRecentDecl(); |
| 42 | ++ // C++11 N3337 [temp.param]p12: |
| 43 | ++ // A default template argument shall not be specified in a friend class |
| 44 | ++ // template declaration. |
| 45 | ++ // |
| 46 | ++ // Skip past friend *declarations* because they are not supposed to contain |
| 47 | ++ // default template arguments. Moreover, these declarations may introduce |
| 48 | ++ // template parameters living in different template depths than the |
| 49 | ++ // corresponding template parameters in TD, causing unmatched constraint |
| 50 | ++ // substitution. |
| 51 | ++ // |
| 52 | ++ // FIXME: Diagnose such cases within a class template: |
| 53 | ++ // template <class T> |
| 54 | ++ // struct S { |
| 55 | ++ // template <class = void> friend struct C; |
| 56 | ++ // }; |
| 57 | ++ // template struct S<int>; |
| 58 | ++ while (D->getFriendObjectKind() != Decl::FriendObjectKind::FOK_None && |
| 59 | ++ D->getPreviousDecl()) |
| 60 | ++ D = D->getPreviousDecl(); |
| 61 | ++ return cast<TemplateDecl>(D)->getTemplateParameters(); |
| 62 | + } |
| 63 | + |
| 64 | + DeclResult Sema::CheckClassTemplate( |
| 65 | +diff --git a/clang/test/SemaTemplate/concepts-friends.cpp b/clang/test/SemaTemplate/concepts-friends.cpp |
| 66 | +index 255b0858917f..91b797034ed6 100644 |
| 67 | +--- a/clang/test/SemaTemplate/concepts-friends.cpp |
| 68 | ++++ b/clang/test/SemaTemplate/concepts-friends.cpp |
| 69 | +@@ -478,3 +478,29 @@ template <Concept> class Foo { |
| 70 | + }; |
| 71 | + |
| 72 | + } // namespace FriendOfFriend |
| 73 | ++ |
| 74 | ++namespace GH86769 { |
| 75 | ++ |
| 76 | ++template <typename T> |
| 77 | ++concept X = true; |
| 78 | ++ |
| 79 | ++template <X T> struct Y { |
| 80 | ++ Y(T) {} |
| 81 | ++ template <X U> friend struct Y; |
| 82 | ++ template <X U> friend struct Y; |
| 83 | ++ template <X U> friend struct Y; |
| 84 | ++}; |
| 85 | ++ |
| 86 | ++template <class T> |
| 87 | ++struct Z { |
| 88 | ++ // FIXME: This is ill-formed per C++11 N3337 [temp.param]p12: |
| 89 | ++ // A default template argument shall not be specified in a friend class |
| 90 | ++ // template declaration. |
| 91 | ++ template <X U = void> friend struct Y; |
| 92 | ++}; |
| 93 | ++ |
| 94 | ++template struct Y<int>; |
| 95 | ++template struct Z<int>; |
| 96 | ++Y y(1); |
| 97 | ++ |
| 98 | ++} |
| 99 | +diff --git a/clang/test/SemaTemplate/ctad.cpp b/clang/test/SemaTemplate/ctad.cpp |
| 100 | +index 388ed7d4cced..ec144d4f44ba 100644 |
| 101 | +--- a/clang/test/SemaTemplate/ctad.cpp |
| 102 | ++++ b/clang/test/SemaTemplate/ctad.cpp |
| 103 | +@@ -53,4 +53,4 @@ X x; |
| 104 | + template<class T, class B> struct Y { Y(T); }; |
| 105 | + template<class T, class B=void> struct Y ; |
| 106 | + Y y(1); |
| 107 | +-}; |
| 108 | ++} |
| 109 | +-- |
| 110 | +2.44.0.478.gd926399ef9-goog |
| 111 | + |
0 commit comments