[clang] [clang] CTAD: Track template template type parameters that referenced in (PR #85405)

Haojian Wu via cfe-commits cfe-commits at lists.llvm.org
Tue Apr 2 06:05:09 PDT 2024


https://github.com/hokein updated https://github.com/llvm/llvm-project/pull/85405

>From ff2ff4cdb17c53d031c5cf3729477e63c33bef02 Mon Sep 17 00:00:00 2001
From: Haojian Wu <hokein.wu at gmail.com>
Date: Fri, 15 Mar 2024 15:09:10 +0100
Subject: [PATCH 1/2] [clang] CTAD: Track template template type parameters
 that referenced in the template arguments of the RHS.

Fixes https://github.com/llvm/llvm-project/issues/85385.

The Finder was missing for this case, for the crash test, the template
parameter TTP was incorrectly considered as not referenced/appeared in
the template arguments of the right hand side of the alias template
decl, thus the synthesized deduction decl doesn't contain this TTP in
the template parameter list, but we have references in the declaration,
thus it caused crashes.
---
 clang/lib/Sema/SemaTemplate.cpp              |  6 ++++++
 clang/test/SemaCXX/cxx20-ctad-type-alias.cpp | 16 ++++++++++++++++
 2 files changed, 22 insertions(+)

diff --git a/clang/lib/Sema/SemaTemplate.cpp b/clang/lib/Sema/SemaTemplate.cpp
index 1a2d5e9310dbe1..57c0d47f3b7ec9 100644
--- a/clang/lib/Sema/SemaTemplate.cpp
+++ b/clang/lib/Sema/SemaTemplate.cpp
@@ -2719,6 +2719,12 @@ SmallVector<unsigned> TemplateParamsReferencedInTemplateArgumentList(
       return true;
     }
 
+    bool TraverseTemplateName(TemplateName Template) {
+      if (auto *TD = Template.getAsTemplateDecl())
+        MarkAppeared(TD);
+      return RecursiveASTVisitor::TraverseTemplateName(Template);
+    }
+
     void MarkAppeared(NamedDecl *ND) {
       if (TemplateParams.contains(ND))
         ReferencedTemplateParams.insert(ND);
diff --git a/clang/test/SemaCXX/cxx20-ctad-type-alias.cpp b/clang/test/SemaCXX/cxx20-ctad-type-alias.cpp
index ce403285b0f531..ff1d0ef030b9c7 100644
--- a/clang/test/SemaCXX/cxx20-ctad-type-alias.cpp
+++ b/clang/test/SemaCXX/cxx20-ctad-type-alias.cpp
@@ -259,3 +259,19 @@ using Bar2 = Foo<K>; // expected-error {{extraneous template parameter list in a
 
 Bar2 b = 1; // expected-error {{no viable constructor or deduction guide for deduction of template arguments}}
 } // namespace test19
+
+// GH85385
+namespace test20 {
+template <template <typename> typename T>
+struct K {};
+
+template <typename U>
+class Foo {};
+
+// Verify that template template type parameter TTP is referenced/used in the
+// template arguments of the RHS.
+template <template<typename> typename TTP>
+using Bar = Foo<K<TTP>>; // expected-note {{candidate template ignored: could not match 'Foo<K<>>' against 'int'}}
+
+Bar s = 1; // expected-error {{no viable constructor or deduction guide for deduction of template arguments of}}
+}

>From 56784ace7f9bbf17ba9de432ac573f7678d4ff6b Mon Sep 17 00:00:00 2001
From: Haojian Wu <hokein.wu at gmail.com>
Date: Wed, 27 Mar 2024 10:50:24 +0100
Subject: [PATCH 2/2] Add a testcase to cover the valid code.

---
 clang/test/SemaCXX/cxx20-ctad-type-alias.cpp | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/clang/test/SemaCXX/cxx20-ctad-type-alias.cpp b/clang/test/SemaCXX/cxx20-ctad-type-alias.cpp
index ff1d0ef030b9c7..d241212135f38b 100644
--- a/clang/test/SemaCXX/cxx20-ctad-type-alias.cpp
+++ b/clang/test/SemaCXX/cxx20-ctad-type-alias.cpp
@@ -273,5 +273,9 @@ class Foo {};
 template <template<typename> typename TTP>
 using Bar = Foo<K<TTP>>; // expected-note {{candidate template ignored: could not match 'Foo<K<>>' against 'int'}}
 
+template <class T>
+class Container {};
+Bar t = Foo<K<Container>>();
+
 Bar s = 1; // expected-error {{no viable constructor or deduction guide for deduction of template arguments of}}
 }



More information about the cfe-commits mailing list