[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
Mon Nov 20 10:14:00 PST 2023


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

>From 739f1802bbfa4a7dc454e24535423b64701ac500 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/8] [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 9f823a95f56dd743421886f348bb10451e509cf1 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/8] 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 0b0c94bbce2e02d5ddebc961d991274bae03f8ef 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/8] 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 3cddae9a22c67e43456c5ff4e682b208c0b727b9 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/8] 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 52820b186c733ef52c464094395c0b1f3f7c3a00 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/8] 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();

>From d1ad1786da29bfa40226ba95b555396b02a134fb Mon Sep 17 00:00:00 2001
From: Yuxuan Chen <ych at meta.com>
Date: Mon, 20 Nov 2023 09:30:13 -0800
Subject: [PATCH 6/8] Add release note entry

---
 clang/docs/ReleaseNotes.rst | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 5fdc061c32cb998..40060073ec825eb 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -591,6 +591,8 @@ Bug Fixes in This Version
 
 - Fixed an issue that a benign assertion might hit when instantiating a pack expansion
   inside a lambda. (`#61460 <https://github.com/llvm/llvm-project/issues/61460>`_)
+- Fixed a crash during instantiation of nested class template specializations.
+  Fixes (`#70375 <https://github.com/llvm/llvm-project/issues/70375>`_)
 
 Bug Fixes to Compiler Builtins
 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

>From 47e5bfabfde69219b518f384e335655cddb0f30c Mon Sep 17 00:00:00 2001
From: Yuxuan Chen <ych at meta.com>
Date: Mon, 20 Nov 2023 10:12:23 -0800
Subject: [PATCH 7/8] Add release note for fix

---
 clang/docs/ReleaseNotes.rst | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 40060073ec825eb..4702e0e1046e32c 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -591,8 +591,8 @@ Bug Fixes in This Version
 
 - Fixed an issue that a benign assertion might hit when instantiating a pack expansion
   inside a lambda. (`#61460 <https://github.com/llvm/llvm-project/issues/61460>`_)
-- Fixed a crash during instantiation of nested class template specializations.
-  Fixes (`#70375 <https://github.com/llvm/llvm-project/issues/70375>`_)
+- Fix crash during instantiation of some class template specializations within class
+  templates. Fixes (`#70375 <https://github.com/llvm/llvm-project/issues/70375>`_)
 
 Bug Fixes to Compiler Builtins
 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

>From 43d34efd092dc5f7e84cedc3dbb2565a801a8336 Mon Sep 17 00:00:00 2001
From: Yuxuan Chen <ych at meta.com>
Date: Mon, 20 Nov 2023 10:13:00 -0800
Subject: [PATCH 8/8] move tests to existing file

---
 .../member-template-specialization.cpp        | 33 -------------------
 .../test/SemaCXX/template-specialization.cpp  | 33 +++++++++++++++++++
 2 files changed, 33 insertions(+), 33 deletions(-)
 delete 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
deleted file mode 100644
index 29d46ec9c1e44fc..000000000000000
--- a/clang/test/SemaCXX/member-template-specialization.cpp
+++ /dev/null
@@ -1,33 +0,0 @@
-// RUN: %clang_cc1 -verify -fsyntax-only %s
-// expected-no-diagnostics
-
-// 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();
-}
diff --git a/clang/test/SemaCXX/template-specialization.cpp b/clang/test/SemaCXX/template-specialization.cpp
index ae7bc332fccee1d..7b26ff9f5c5ba49 100644
--- a/clang/test/SemaCXX/template-specialization.cpp
+++ b/clang/test/SemaCXX/template-specialization.cpp
@@ -19,3 +19,36 @@ int main() {
   B::foo<4>(); // expected-note {{in instantiation of function template specialization 'B::foo<4>'}}
   return 0;
 }
+
+namespace GH70375 {
+
+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);
+    }
+  };
+};
+
+void instantiate() {
+  Outer<void>::U::bar();
+}
+
+}



More information about the cfe-commits mailing list