[clang] [clang]get non-injected-class before finding instantiated decl (PR #70886)

Congcong Cai via cfe-commits cfe-commits at lists.llvm.org
Wed Nov 1 08:53:16 PDT 2023


https://github.com/HerrCai0907 updated https://github.com/llvm/llvm-project/pull/70886

>From 73471336857b84c69e51d4561838e588c7162bfa Mon Sep 17 00:00:00 2001
From: Congcong Cai <congcongcai0907 at 163.com>
Date: Tue, 31 Oct 2023 17:27:35 +0800
Subject: [PATCH 1/3] [clang]get non-injected-class before finding instantiated
 decl

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

diff --git a/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp b/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
index 78a7892a35a320b..83dac8ece06b20b 100644
--- a/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
+++ b/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
@@ -6191,6 +6191,9 @@ NamedDecl *Sema::FindInstantiatedDecl(SourceLocation Loc, NamedDecl *D,
   }
 
   if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) {
+    if (Record->isInjectedClassName())
+      Record = cast<CXXRecordDecl>(Record->getDeclContext());
+
     if (!Record->isDependentContext())
       return D;
 

>From ebda1b76c090b52273d2fe72fbc30744a5fb892d Mon Sep 17 00:00:00 2001
From: Congcong Cai <congcongcai0907 at 163.com>
Date: Wed, 1 Nov 2023 10:59:12 +0800
Subject: [PATCH 2/3] add test

---
 clang/docs/ReleaseNotes.rst   |  2 ++
 clang/test/SemaCXX/friend.cpp | 16 ++++++++++++++++
 2 files changed, 18 insertions(+)

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index bc28bb567f6932a..a8ae5fb2596c580 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -533,6 +533,8 @@ Bug Fixes in This Version
   Fixes (`#67687 <https://github.com/llvm/llvm-project/issues/67687>`_)
 - Fix crash from constexpr evaluator evaluating uninitialized arrays as rvalue.
   Fixes (`#67317 <https://github.com/llvm/llvm-project/issues/67317>`_)
+- Fix an issue when do name lookup which scope resolution operator in friend function.
+  Fixes (`#21483 <https://github.com/llvm/llvm-project/issues/21483>`_)
 
 Bug Fixes to Compiler Builtins
 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
diff --git a/clang/test/SemaCXX/friend.cpp b/clang/test/SemaCXX/friend.cpp
index 367d6a6c1807c92..7b8e50322c4f8f9 100644
--- a/clang/test/SemaCXX/friend.cpp
+++ b/clang/test/SemaCXX/friend.cpp
@@ -429,3 +429,19 @@ namespace qualified_friend_no_match {
     friend void Y::f(double); // expected-error {{friend declaration of 'f' does not match any declaration in 'qualified_friend_no_match::Y'}}
   };
 }
+
+namespace gh21483 {
+template <typename I>
+struct B {
+  struct mixin {
+    int type;
+    friend void foo(B<I>::mixin it) {
+      (void)it.mixin::type;
+    }
+  };
+};
+
+void bar() {
+  foo(B<int>::mixin{});
+}
+}
\ No newline at end of file

>From 8d1f90df3571cdeccc2cf0f1cb14091afd7593ad Mon Sep 17 00:00:00 2001
From: Congcong Cai <congcongcai0907 at 163.com>
Date: Wed, 1 Nov 2023 23:52:59 +0800
Subject: [PATCH 3/3] rename test

---
 clang/test/SemaCXX/friend.cpp | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)

diff --git a/clang/test/SemaCXX/friend.cpp b/clang/test/SemaCXX/friend.cpp
index 7b8e50322c4f8f9..8272fabb97f4da3 100644
--- a/clang/test/SemaCXX/friend.cpp
+++ b/clang/test/SemaCXX/friend.cpp
@@ -433,15 +433,15 @@ namespace qualified_friend_no_match {
 namespace gh21483 {
 template <typename I>
 struct B {
-  struct mixin {
-    int type;
-    friend void foo(B<I>::mixin it) {
-      (void)it.mixin::type;
+  struct T {
+    int v;
+    friend void foo(B<I>::T t) {
+      (void)t.T::v;
     }
   };
 };
 
 void bar() {
-  foo(B<int>::mixin{});
+  foo(B<int>::T{});
 }
 }
\ No newline at end of file



More information about the cfe-commits mailing list