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

Congcong Cai via cfe-commits cfe-commits at lists.llvm.org
Tue Oct 31 20:00:14 PDT 2023


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

Fixes #21483 

>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/2] [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/2] 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



More information about the cfe-commits mailing list