[clang] [clang] [C++20] Defaulted operator== doesn't lookup in using-directive properly #97087 (PR #99542)

via cfe-commits cfe-commits at lists.llvm.org
Sun Jul 21 11:17:33 PDT 2024


https://github.com/ofAlpaca updated https://github.com/llvm/llvm-project/pull/99542

>From 2a381b47002db3c7a119a893c1a70e0b1604151f Mon Sep 17 00:00:00 2001
From: schiang <frank70199 at gmail.com>
Date: Thu, 18 Jul 2024 09:53:35 +0800
Subject: [PATCH 1/3] Pass 'Scope *' for 'SetDeclDefaulted()' and related utlis

---
 clang/include/clang/Sema/Sema.h                | 4 ++--
 clang/lib/Parse/ParseCXXInlineMethods.cpp      | 2 +-
 clang/lib/Parse/Parser.cpp                     | 2 +-
 clang/lib/Sema/SemaDeclCXX.cpp                 | 8 ++++----
 clang/lib/Sema/SemaTemplateInstantiateDecl.cpp | 4 ++--
 5 files changed, 10 insertions(+), 10 deletions(-)

diff --git a/clang/include/clang/Sema/Sema.h b/clang/include/clang/Sema/Sema.h
index 48dff1b76cc57..f887fc261a07e 100644
--- a/clang/include/clang/Sema/Sema.h
+++ b/clang/include/clang/Sema/Sema.h
@@ -5995,9 +5995,9 @@ class Sema final : public SemaBase {
   void ActOnPureSpecifier(Decl *D, SourceLocation PureSpecLoc);
   void SetDeclDeleted(Decl *dcl, SourceLocation DelLoc,
                       StringLiteral *Message = nullptr);
-  void SetDeclDefaulted(Decl *dcl, SourceLocation DefaultLoc);
+  void SetDeclDefaulted(Scope *S, Decl *dcl, SourceLocation DefaultLoc);
 
-  void SetFunctionBodyKind(Decl *D, SourceLocation Loc, FnBodyKind BodyKind,
+  void SetFunctionBodyKind(Scope *S, Decl *D, SourceLocation Loc, FnBodyKind BodyKind,
                            StringLiteral *DeletedMessage = nullptr);
   void ActOnStartTrailingRequiresClause(Scope *S, Declarator &D);
   ExprResult ActOnFinishTrailingRequiresClause(ExprResult ConstraintExpr);
diff --git a/clang/lib/Parse/ParseCXXInlineMethods.cpp b/clang/lib/Parse/ParseCXXInlineMethods.cpp
index 9ccbbf9a7d5d0..75a835f512144 100644
--- a/clang/lib/Parse/ParseCXXInlineMethods.cpp
+++ b/clang/lib/Parse/ParseCXXInlineMethods.cpp
@@ -124,7 +124,7 @@ NamedDecl *Parser::ParseCXXInlineMethodDef(
                       ? diag::warn_cxx98_compat_defaulted_deleted_function
                       : diag::ext_defaulted_deleted_function)
         << 0 /* defaulted */;
-      Actions.SetDeclDefaulted(FnD, KWLoc);
+      Actions.SetDeclDefaulted(nullptr, FnD, KWLoc);
       if (auto *DeclAsFunction = dyn_cast<FunctionDecl>(FnD)) {
         DeclAsFunction->setRangeEnd(KWEndLoc);
       }
diff --git a/clang/lib/Parse/Parser.cpp b/clang/lib/Parse/Parser.cpp
index 5ebe71e496a2e..f1d98bced80b2 100644
--- a/clang/lib/Parse/Parser.cpp
+++ b/clang/lib/Parse/Parser.cpp
@@ -1479,7 +1479,7 @@ Decl *Parser::ParseFunctionDefinition(ParsingDeclarator &D,
   D.getMutableDeclSpec().abort();
 
   if (BodyKind != Sema::FnBodyKind::Other) {
-    Actions.SetFunctionBodyKind(Res, KWLoc, BodyKind, DeletedMessage);
+    Actions.SetFunctionBodyKind(getCurScope(), Res, KWLoc, BodyKind, DeletedMessage);
     Stmt *GeneratedBody = Res ? Res->getBody() : nullptr;
     Actions.ActOnFinishFunctionBody(Res, GeneratedBody, false);
     return Res;
diff --git a/clang/lib/Sema/SemaDeclCXX.cpp b/clang/lib/Sema/SemaDeclCXX.cpp
index f24912cde275a..e57057fac10c6 100644
--- a/clang/lib/Sema/SemaDeclCXX.cpp
+++ b/clang/lib/Sema/SemaDeclCXX.cpp
@@ -17940,7 +17940,7 @@ void Sema::SetDeclDeleted(Decl *Dcl, SourceLocation DelLoc,
   Fn->setDeletedAsWritten(true, Message);
 }
 
-void Sema::SetDeclDefaulted(Decl *Dcl, SourceLocation DefaultLoc) {
+void Sema::SetDeclDefaulted(Scope *S, Decl *Dcl, SourceLocation DefaultLoc) {
   if (!Dcl || Dcl->isInvalidDecl())
     return;
 
@@ -18016,7 +18016,7 @@ void Sema::SetDeclDefaulted(Decl *Dcl, SourceLocation DefaultLoc) {
   }
 
   if (DefKind.isComparison()) {
-    if (CheckExplicitlyDefaultedComparison(nullptr, FD, DefKind.asComparison()))
+    if (CheckExplicitlyDefaultedComparison(S, FD, DefKind.asComparison()))
       FD->setInvalidDecl();
     else
       DefineDefaultedComparison(DefaultLoc, FD, DefKind.asComparison());
@@ -18050,14 +18050,14 @@ void Sema::DiagnoseReturnInConstructorExceptionHandler(CXXTryStmt *TryBlock) {
   }
 }
 
-void Sema::SetFunctionBodyKind(Decl *D, SourceLocation Loc, FnBodyKind BodyKind,
+void Sema::SetFunctionBodyKind(Scope *S, Decl *D, SourceLocation Loc, FnBodyKind BodyKind,
                                StringLiteral *DeletedMessage) {
   switch (BodyKind) {
   case FnBodyKind::Delete:
     SetDeclDeleted(D, Loc, DeletedMessage);
     break;
   case FnBodyKind::Default:
-    SetDeclDefaulted(D, Loc);
+    SetDeclDefaulted(S, D, Loc);
     break;
   case FnBodyKind::Other:
     llvm_unreachable(
diff --git a/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp b/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
index 01432301633ed..65a755cb69048 100644
--- a/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
+++ b/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
@@ -4856,7 +4856,7 @@ bool TemplateDeclInstantiator::SubstDefaultedFunction(FunctionDecl *New,
                    : DFI);
   }
 
-  SemaRef.SetDeclDefaulted(New, Tmpl->getLocation());
+  SemaRef.SetDeclDefaulted(nullptr, New, Tmpl->getLocation());
   return false;
 }
 
@@ -5133,7 +5133,7 @@ void Sema::InstantiateFunctionDefinition(SourceLocation PointOfInstantiation,
 
   if (PatternDecl->isDefaulted()) {
     RebuildTypeSourceInfoForDefaultSpecialMembers();
-    SetDeclDefaulted(Function, PatternDecl->getLocation());
+    SetDeclDefaulted(nullptr, Function, PatternDecl->getLocation());
   } else {
     MultiLevelTemplateArgumentList TemplateArgs = getTemplateInstantiationArgs(
         Function, Function->getLexicalDeclContext(), /*Final=*/false,

>From 5dbfe6988c489344c4afff359d23cd0306b32611 Mon Sep 17 00:00:00 2001
From: schiang <frank70199 at gmail.com>
Date: Fri, 19 Jul 2024 01:00:44 +0800
Subject: [PATCH 2/3] git clang-format changes

---
 clang/include/clang/Sema/Sema.h | 3 ++-
 clang/lib/Parse/Parser.cpp      | 3 ++-
 clang/lib/Sema/SemaDeclCXX.cpp  | 3 ++-
 3 files changed, 6 insertions(+), 3 deletions(-)

diff --git a/clang/include/clang/Sema/Sema.h b/clang/include/clang/Sema/Sema.h
index f887fc261a07e..622b107b80081 100644
--- a/clang/include/clang/Sema/Sema.h
+++ b/clang/include/clang/Sema/Sema.h
@@ -5997,7 +5997,8 @@ class Sema final : public SemaBase {
                       StringLiteral *Message = nullptr);
   void SetDeclDefaulted(Scope *S, Decl *dcl, SourceLocation DefaultLoc);
 
-  void SetFunctionBodyKind(Scope *S, Decl *D, SourceLocation Loc, FnBodyKind BodyKind,
+  void SetFunctionBodyKind(Scope *S, Decl *D, SourceLocation Loc,
+                           FnBodyKind BodyKind,
                            StringLiteral *DeletedMessage = nullptr);
   void ActOnStartTrailingRequiresClause(Scope *S, Declarator &D);
   ExprResult ActOnFinishTrailingRequiresClause(ExprResult ConstraintExpr);
diff --git a/clang/lib/Parse/Parser.cpp b/clang/lib/Parse/Parser.cpp
index f1d98bced80b2..eaf67ed633016 100644
--- a/clang/lib/Parse/Parser.cpp
+++ b/clang/lib/Parse/Parser.cpp
@@ -1479,7 +1479,8 @@ Decl *Parser::ParseFunctionDefinition(ParsingDeclarator &D,
   D.getMutableDeclSpec().abort();
 
   if (BodyKind != Sema::FnBodyKind::Other) {
-    Actions.SetFunctionBodyKind(getCurScope(), Res, KWLoc, BodyKind, DeletedMessage);
+    Actions.SetFunctionBodyKind(getCurScope(), Res, KWLoc, BodyKind,
+                                DeletedMessage);
     Stmt *GeneratedBody = Res ? Res->getBody() : nullptr;
     Actions.ActOnFinishFunctionBody(Res, GeneratedBody, false);
     return Res;
diff --git a/clang/lib/Sema/SemaDeclCXX.cpp b/clang/lib/Sema/SemaDeclCXX.cpp
index e57057fac10c6..0f0fe10b57517 100644
--- a/clang/lib/Sema/SemaDeclCXX.cpp
+++ b/clang/lib/Sema/SemaDeclCXX.cpp
@@ -18050,7 +18050,8 @@ void Sema::DiagnoseReturnInConstructorExceptionHandler(CXXTryStmt *TryBlock) {
   }
 }
 
-void Sema::SetFunctionBodyKind(Scope *S, Decl *D, SourceLocation Loc, FnBodyKind BodyKind,
+void Sema::SetFunctionBodyKind(Scope *S, Decl *D, SourceLocation Loc,
+                               FnBodyKind BodyKind,
                                StringLiteral *DeletedMessage) {
   switch (BodyKind) {
   case FnBodyKind::Delete:

>From b4bbcee9d928d73c7c3327056dbb325ab250f0a4 Mon Sep 17 00:00:00 2001
From: schiang <frank70199 at gmail.com>
Date: Mon, 22 Jul 2024 02:16:34 +0800
Subject: [PATCH 3/3] Add release note and testcase

---
 clang/docs/ReleaseNotes.rst    |  2 ++
 clang/test/SemaCXX/GH97087.cpp | 12 ++++++++++++
 2 files changed, 14 insertions(+)
 create mode 100644 clang/test/SemaCXX/GH97087.cpp

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 969856a8f978c..36f00cda724fa 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -826,6 +826,8 @@ Bug Fixes in This Version
 - ``__is_trivially_equality_comparable`` no longer returns true for types which
   have a constrained defaulted comparison operator (#GH89293).
 
+- Fixed defaulted `operator==` doesn't lookup its scope properly (#GH97087).
+
 Bug Fixes to Compiler Builtins
 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 
diff --git a/clang/test/SemaCXX/GH97087.cpp b/clang/test/SemaCXX/GH97087.cpp
new file mode 100644
index 0000000000000..06902db4bf860
--- /dev/null
+++ b/clang/test/SemaCXX/GH97087.cpp
@@ -0,0 +1,12 @@
+// RUN: %clang_cc1 -std=c++20 -fsyntax-only %s
+// expected-no-diagnostics 
+struct X {};
+namespace NS {
+    bool operator==(X, X);
+}
+using namespace NS;
+struct Y {
+    X x;
+    friend bool operator==(Y, Y);
+};
+bool operator==(Y, Y) = default;



More information about the cfe-commits mailing list