Skip to content

Commit 9330261

Browse files
authored
Use the correct namespace for looking up matching operator!= (#68922)
`S.getScopeForContext` determins the **active** scope associated with the given `declContext`. This fails to find the matching `operator!=` if candidate `operator==` was found via ADL since that scope is not active. Instead, just directly lookup using the namespace decl of `operator==` Fixes #68901
1 parent 596b598 commit 9330261

File tree

4 files changed

+138
-12
lines changed

4 files changed

+138
-12
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,10 @@ C/C++ Language Potentially Breaking Changes
8585
``__has_c_attribute(warn_unused_result)``, 202003, 0
8686
``__has_c_attribute(gnu::warn_unused_result)``, 202003, 1
8787

88+
- Fixed a bug in finding matching `operator!=` while adding reversed `operator==` as
89+
outlined in "The Equality Operator You Are Looking For" (`P2468 <http://wg21.link/p2468r2>`_).
90+
Fixes (`#68901: <https://github.com/llvm/llvm-project/issues/68901>`_).
91+
8892
C++ Specific Potentially Breaking Changes
8993
-----------------------------------------
9094
- The name mangling rules for function templates has been changed to take into

clang/lib/Sema/SemaOverload.cpp

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -962,18 +962,13 @@ static bool shouldAddReversedEqEq(Sema &S, SourceLocation OpLoc,
962962
return true;
963963
}
964964
// Otherwise the search scope is the namespace scope of which F is a member.
965-
LookupResult NonMembers(S, NotEqOp, OpLoc,
966-
Sema::LookupNameKind::LookupOperatorName);
967-
S.LookupName(NonMembers,
968-
S.getScopeForContext(EqFD->getEnclosingNamespaceContext()));
969-
NonMembers.suppressAccessDiagnostics();
970-
for (NamedDecl *Op : NonMembers) {
971-
auto *FD = Op->getAsFunction();
972-
if(auto* UD = dyn_cast<UsingShadowDecl>(Op))
973-
FD = UD->getUnderlyingDecl()->getAsFunction();
974-
if (FunctionsCorrespond(S.Context, EqFD, FD) &&
975-
declaresSameEntity(cast<Decl>(EqFD->getDeclContext()),
976-
cast<Decl>(Op->getDeclContext())))
965+
for (NamedDecl *Op : EqFD->getEnclosingNamespaceContext()->lookup(NotEqOp)) {
966+
auto *NotEqFD = Op->getAsFunction();
967+
if (auto *UD = dyn_cast<UsingShadowDecl>(Op))
968+
NotEqFD = UD->getUnderlyingDecl()->getAsFunction();
969+
if (FunctionsCorrespond(S.Context, EqFD, NotEqFD) && S.isVisible(NotEqFD) &&
970+
declaresSameEntity(cast<Decl>(EqFD->getEnclosingNamespaceContext()),
971+
cast<Decl>(Op->getLexicalDeclContext())))
977972
return false;
978973
}
979974
return true;
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// RUN: rm -rf %t
2+
// RUN: mkdir %t
3+
// RUN: split-file %s %t
4+
//
5+
// RUN: %clang_cc1 -std=c++20 %t/A.cppm -emit-module-interface -o %t/A.pcm
6+
// RUN: %clang_cc1 -std=c++20 -fprebuilt-module-path=%t -I%t %t/p2468r2.cpp -verify
7+
8+
//--- A.cppm
9+
module;
10+
export module A;
11+
export {
12+
namespace NS {
13+
struct S {};
14+
bool operator==(S, int);
15+
} // namespace NS
16+
}
17+
18+
namespace NS { bool operator!=(S, int); } // Not visible.
19+
20+
21+
//--- p2468r2.cpp
22+
// expected-no-diagnostics
23+
import A;
24+
bool x = 0 == NS::S(); // Ok. operator!= from module A is not visible.

clang/test/CXX/over/over.match/over.match.funcs/over.match.oper/p3-2a.cpp

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,109 @@ bool fine(A a, B b) { return a == b; } // Ok. Found a matching operator!=.
374374
}
375375
}
376376

377+
378+
namespace ADL_GH68901{
379+
namespace test1 {
380+
namespace A {
381+
struct S {};
382+
bool operator==(S, int); // expected-note {{no known conversion from 'int' to 'S' for 1st argument}}
383+
bool a = 0 == A::S(); // Ok. Operator!= not visible.
384+
bool operator!=(S, int);
385+
} // namespace A
386+
bool a = 0 == A::S(); // expected-error {{invalid operands to binary expression ('int' and 'A::S')}}
387+
} // namespace test1
388+
389+
namespace test2 {
390+
namespace B {
391+
struct Derived {};
392+
struct Base : Derived {};
393+
394+
bool operator==(Derived& a, Base& b);
395+
bool operator!=(Derived& a, Base& b);
396+
} // namespace B
397+
398+
bool foo() {
399+
B::Base a,b;
400+
return a == b;
401+
}
402+
} // namespace test2
403+
404+
405+
namespace template_ {
406+
namespace ns {
407+
template <class T> struct A {};
408+
template <class T> struct B : A<T> {};
409+
410+
template <class T> bool operator==(B<T>, A<T>); // expected-note {{candidate template ignored: could not match 'B' against 'A'}}
411+
template <class T> bool operator!=(B<T>, A<T>);
412+
}
413+
414+
void test() {
415+
ns::A<int> a;
416+
ns::B<int> b;
417+
a == b; // expected-error {{invalid operands to binary expression}}
418+
}
419+
} // namespace test3
420+
421+
namespace using_not_eq {
422+
namespace A {
423+
struct S {};
424+
namespace B {
425+
bool operator!=(S, int);
426+
}
427+
bool operator==(S, int); // expected-note {{candidate}}
428+
using B::operator!=;
429+
} // namespace A
430+
bool a = 0 == A::S(); // expected-error {{invalid operands to binary expression}}
431+
} // namespace reversed_lookup_not_like_ADL
432+
433+
namespace using_eqeq {
434+
namespace A {
435+
struct S {};
436+
namespace B {
437+
bool operator==(S, int); // expected-note {{candidate}}
438+
bool operator!=(S, int);
439+
}
440+
using B::operator==;
441+
} // namespace A
442+
bool a = 0 == A::S(); // expected-error {{invalid operands to binary expression}}
443+
}
444+
445+
} //namespace ADL_GH68901
446+
447+
namespace function_scope_operator_eqeq {
448+
// For non-members, we always lookup for matching operator!= in the namespace scope of
449+
// operator== (and not in the scope of operator==).
450+
struct X { operator int(); };
451+
namespace test1{
452+
bool h(X x) {
453+
bool operator==(X, int); // expected-note {{reversed}}
454+
return x == x; // expected-warning {{ambiguous}}
455+
}
456+
457+
bool g(X x) {
458+
bool operator==(X, int); // expected-note {{reversed}}
459+
bool operator!=(X, int);
460+
return x == x; // expected-warning {{ambiguous}}
461+
}
462+
} // namespace test1
463+
464+
namespace test2 {
465+
bool operator!=(X, int);
466+
467+
bool h(X x) {
468+
bool operator==(X, int);
469+
return x == x;
470+
}
471+
472+
bool i(X x) {
473+
bool operator==(X, int);
474+
bool operator!=(X, int);
475+
return x == x;
476+
}
477+
} // namespace test2
478+
} // namespace function_scope_operator_eqeq
479+
377480
namespace non_member_template_2 {
378481
struct P {};
379482
template<class S>

0 commit comments

Comments
 (0)