[clang] [Clang] Fix finding instantiated decls for class template specializations during instantiation (PR #72346)

Yuxuan Chen via cfe-commits cfe-commits at lists.llvm.org
Wed Nov 15 10:10:34 PST 2023


https://github.com/yuxuanchen1997 updated https://github.com/llvm/llvm-project/pull/72346

>From f238608b792f69b93eb445ee596125f3e20acf39 Mon Sep 17 00:00:00 2001
From: Yuxuan Chen <ych at meta.com>
Date: Tue, 14 Nov 2023 20:52:21 -0800
Subject: [PATCH 1/5] [Clang] Fix ICE caused by mishandling template
 specialization in instantiation lookup

---
 clang/lib/Sema/SemaTemplateInstantiateDecl.cpp | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)

diff --git a/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp b/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
index 011356e08a04297..2ef0986dd4d2235 100644
--- a/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
+++ b/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
@@ -6211,9 +6211,8 @@ NamedDecl *Sema::FindInstantiatedDecl(SourceLocation Loc, NamedDecl *D,
     ClassTemplateDecl *ClassTemplate = Record->getDescribedClassTemplate();
     if (ClassTemplate)
       ClassTemplate = ClassTemplate->getCanonicalDecl();
-    else if (ClassTemplatePartialSpecializationDecl *PartialSpec
-               = dyn_cast<ClassTemplatePartialSpecializationDecl>(Record))
-      ClassTemplate = PartialSpec->getSpecializedTemplate()->getCanonicalDecl();
+    else if (ClassTemplateSpecializationDecl *Spec = dyn_cast<ClassTemplateSpecializationDecl>(Record))
+      ClassTemplate = Spec->getSpecializedTemplate()->getCanonicalDecl();
 
     // Walk the current context to find either the record or an instantiation of
     // it.

>From 9a79db980951299c93cc4530230717af6f4668b3 Mon Sep 17 00:00:00 2001
From: Yuxuan Chen <ych at meta.com>
Date: Tue, 14 Nov 2023 20:55:10 -0800
Subject: [PATCH 2/5] formatting

---
 clang/lib/Sema/SemaTemplateInstantiateDecl.cpp | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp b/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
index 2ef0986dd4d2235..07b3488a21e670b 100644
--- a/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
+++ b/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
@@ -6211,7 +6211,8 @@ NamedDecl *Sema::FindInstantiatedDecl(SourceLocation Loc, NamedDecl *D,
     ClassTemplateDecl *ClassTemplate = Record->getDescribedClassTemplate();
     if (ClassTemplate)
       ClassTemplate = ClassTemplate->getCanonicalDecl();
-    else if (ClassTemplateSpecializationDecl *Spec = dyn_cast<ClassTemplateSpecializationDecl>(Record))
+    else if (ClassTemplateSpecializationDecl *Spec =
+                 dyn_cast<ClassTemplateSpecializationDecl>(Record))
       ClassTemplate = Spec->getSpecializedTemplate()->getCanonicalDecl();
 
     // Walk the current context to find either the record or an instantiation of

>From df884c7127bb2f0956521adc479a923c62220d7c Mon Sep 17 00:00:00 2001
From: Yuxuan Chen <ych at meta.com>
Date: Tue, 14 Nov 2023 21:04:00 -0800
Subject: [PATCH 3/5] Provide test case

---
 .../member-template-specialization.cpp        | 31 +++++++++++++++++++
 1 file changed, 31 insertions(+)
 create mode 100644 clang/test/SemaCXX/member-template-specialization.cpp

diff --git a/clang/test/SemaCXX/member-template-specialization.cpp b/clang/test/SemaCXX/member-template-specialization.cpp
new file mode 100644
index 000000000000000..baf5bd6ae7bb544
--- /dev/null
+++ b/clang/test/SemaCXX/member-template-specialization.cpp
@@ -0,0 +1,31 @@
+// RUN: %clang_cc1 -verify -fsyntax-only %s
+// Verify that the inner template specialization can be found
+
+template <typename Ty>
+struct S {
+  static void bar() {
+    Ty t;
+    t.foo();
+  }
+
+  static void take(Ty&) {}
+};
+
+template <typename P>
+struct Outer {
+  template <typename C>
+  struct Inner;
+
+  using U = S<Inner<P>>;
+
+  template <>
+  struct Inner<void> {
+    void foo() {
+      U::take(*this);
+    }
+  };
+};
+
+int main() {
+  Outer<void>::U::bar();
+}

>From 7cdbce7944e051aed5b1f8ab789ce1e43f3e58b3 Mon Sep 17 00:00:00 2001
From: Yuxuan Chen <ych at meta.com>
Date: Tue, 14 Nov 2023 21:13:23 -0800
Subject: [PATCH 4/5] missing expected-no-diagnostics

---
 clang/test/SemaCXX/member-template-specialization.cpp | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/clang/test/SemaCXX/member-template-specialization.cpp b/clang/test/SemaCXX/member-template-specialization.cpp
index baf5bd6ae7bb544..29d46ec9c1e44fc 100644
--- a/clang/test/SemaCXX/member-template-specialization.cpp
+++ b/clang/test/SemaCXX/member-template-specialization.cpp
@@ -1,4 +1,6 @@
 // RUN: %clang_cc1 -verify -fsyntax-only %s
+// expected-no-diagnostics
+
 // Verify that the inner template specialization can be found
 
 template <typename Ty>

>From 23e4a9457c9d4428245a9d6593b97a33ed538ce7 Mon Sep 17 00:00:00 2001
From: Yuxuan Chen <ych at meta.com>
Date: Wed, 15 Nov 2023 10:10:02 -0800
Subject: [PATCH 5/5] change stale comment

---
 clang/lib/Sema/SemaTemplateInstantiateDecl.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp b/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
index 07b3488a21e670b..08f4ba00fc9f7de 100644
--- a/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
+++ b/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
@@ -6207,7 +6207,7 @@ NamedDecl *Sema::FindInstantiatedDecl(SourceLocation Loc, NamedDecl *D,
       return D;
 
     // Determine whether this record is the "templated" declaration describing
-    // a class template or class template partial specialization.
+    // a class template or class template specialization.
     ClassTemplateDecl *ClassTemplate = Record->getDescribedClassTemplate();
     if (ClassTemplate)
       ClassTemplate = ClassTemplate->getCanonicalDecl();



More information about the cfe-commits mailing list