https://github.com/usx95 created https://github.com/llvm/llvm-project/pull/70217
The namespace lookup should give a matching friend operator!= as friend functions belong to the namespace scope (as opposed to the lexical class scope). Thus to resolve ambiguity of operator== with itself, defining a corresponding friend operator!= should suffice. Fixes: https://github.com/llvm/llvm-project/issues/70210 >From 26b9570fadf428e2f55efe0f2d9433cf18305239 Mon Sep 17 00:00:00 2001 From: Utkarsh Saxena <u...@google.com> Date: Wed, 25 Oct 2023 16:55:15 +0200 Subject: [PATCH] friend operator!= --- clang/lib/Sema/SemaOverload.cpp | 11 +++++---- .../over.match.oper/p3-2a.cpp | 23 +++++++++++++++++++ 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/clang/lib/Sema/SemaOverload.cpp b/clang/lib/Sema/SemaOverload.cpp index db386fef0661c05..ce7503162ef3ba3 100644 --- a/clang/lib/Sema/SemaOverload.cpp +++ b/clang/lib/Sema/SemaOverload.cpp @@ -960,15 +960,18 @@ static bool shouldAddReversedEqEq(Sema &S, SourceLocation OpLoc, if (FunctionsCorrespond(S.Context, EqFD, Op->getAsFunction())) return false; return true; - } + } // Otherwise the search scope is the namespace scope of which F is a member. - for (NamedDecl *Op : EqFD->getEnclosingNamespaceContext()->lookup(NotEqOp)) { + DeclContext *EqDC = EqFD->getEnclosingNamespaceContext(); + for (NamedDecl *Op : EqDC->lookup(NotEqOp)) { auto *NotEqFD = Op->getAsFunction(); + DeclContext *NotEqDC = Op->getFriendObjectKind() + ? NotEqFD->getEnclosingNamespaceContext() + : Op->getLexicalDeclContext(); if (auto *UD = dyn_cast<UsingShadowDecl>(Op)) NotEqFD = UD->getUnderlyingDecl()->getAsFunction(); if (FunctionsCorrespond(S.Context, EqFD, NotEqFD) && S.isVisible(NotEqFD) && - declaresSameEntity(cast<Decl>(EqFD->getEnclosingNamespaceContext()), - cast<Decl>(Op->getLexicalDeclContext()))) + declaresSameEntity(cast<Decl>(EqDC), cast<Decl>(NotEqDC))) return false; } return true; diff --git a/clang/test/CXX/over/over.match/over.match.funcs/over.match.oper/p3-2a.cpp b/clang/test/CXX/over/over.match/over.match.funcs/over.match.oper/p3-2a.cpp index d83a176ec07eec9..603c91bd535f434 100644 --- a/clang/test/CXX/over/over.match/over.match.funcs/over.match.oper/p3-2a.cpp +++ b/clang/test/CXX/over/over.match/over.match.funcs/over.match.oper/p3-2a.cpp @@ -324,6 +324,29 @@ bool x = X() == X(); // expected-warning {{ambiguous}} } } // namespace P2468R2 +namespace friend_opNE_GH{ +namespace test1 { +struct S { + operator int(); + friend bool operator==(const S &, int); // expected-note {{reversed}} +}; +struct A : S {}; +struct B : S {}; +bool x = A{} == B{}; // expected-warning {{ambiguous}} +} // namespace test1 + +namespace test2 { +struct S { + operator int(); + friend bool operator==(const S &, int); + friend bool operator!=(const S &, int); +}; +struct A : public P {}; +struct B : public P {}; +bool check(A a, B b) { return a == b; } // expected-warning {{use of overloaded operator '==' (with operand types 'A' and 'B') to be ambiguous}} +} // namespace test2 +} // namespace friend_opNE_GH + namespace ADL_GH68901{ namespace test1 { namespace A { _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits