overloaded-operator.cpp revision 96176b3575823ea996c6140380dd17d9240c9766
1eaebc75ef6ff21fbc9f25ab4175cba465e4e0e43Douglas Gregor// RUN: clang -fsyntax-only -verify %s
2eaebc75ef6ff21fbc9f25ab4175cba465e4e0e43Douglas Gregorclass X { };
3eaebc75ef6ff21fbc9f25ab4175cba465e4e0e43Douglas Gregor
4eaebc75ef6ff21fbc9f25ab4175cba465e4e0e43Douglas GregorX operator+(X, X);
5eaebc75ef6ff21fbc9f25ab4175cba465e4e0e43Douglas Gregor
6eaebc75ef6ff21fbc9f25ab4175cba465e4e0e43Douglas Gregorvoid f(X x) {
7eaebc75ef6ff21fbc9f25ab4175cba465e4e0e43Douglas Gregor  x = x + x;
8eaebc75ef6ff21fbc9f25ab4175cba465e4e0e43Douglas Gregor}
9eaebc75ef6ff21fbc9f25ab4175cba465e4e0e43Douglas Gregor
10eaebc75ef6ff21fbc9f25ab4175cba465e4e0e43Douglas Gregorstruct Y;
11eaebc75ef6ff21fbc9f25ab4175cba465e4e0e43Douglas Gregorstruct Z;
12eaebc75ef6ff21fbc9f25ab4175cba465e4e0e43Douglas Gregor
13eaebc75ef6ff21fbc9f25ab4175cba465e4e0e43Douglas Gregorstruct Y {
14eaebc75ef6ff21fbc9f25ab4175cba465e4e0e43Douglas Gregor  Y(const Z&);
15eaebc75ef6ff21fbc9f25ab4175cba465e4e0e43Douglas Gregor};
16eaebc75ef6ff21fbc9f25ab4175cba465e4e0e43Douglas Gregor
17eaebc75ef6ff21fbc9f25ab4175cba465e4e0e43Douglas Gregorstruct Z {
18eaebc75ef6ff21fbc9f25ab4175cba465e4e0e43Douglas Gregor  Z(const Y&);
19eaebc75ef6ff21fbc9f25ab4175cba465e4e0e43Douglas Gregor};
20eaebc75ef6ff21fbc9f25ab4175cba465e4e0e43Douglas Gregor
21eaebc75ef6ff21fbc9f25ab4175cba465e4e0e43Douglas GregorY operator+(Y, Y);
22eaebc75ef6ff21fbc9f25ab4175cba465e4e0e43Douglas Gregorbool operator-(Y, Y); // expected-note{{candidate function}}
23eaebc75ef6ff21fbc9f25ab4175cba465e4e0e43Douglas Gregorbool operator-(Z, Z); // expected-note{{candidate function}}
24eaebc75ef6ff21fbc9f25ab4175cba465e4e0e43Douglas Gregor
25eaebc75ef6ff21fbc9f25ab4175cba465e4e0e43Douglas Gregorvoid g(Y y, Z z) {
26eaebc75ef6ff21fbc9f25ab4175cba465e4e0e43Douglas Gregor  y = y + z;
27eaebc75ef6ff21fbc9f25ab4175cba465e4e0e43Douglas Gregor  bool b = y - z; // expected-error{{use of overloaded operator '-' is ambiguous; candidates are:}}
28eaebc75ef6ff21fbc9f25ab4175cba465e4e0e43Douglas Gregor}
29eaebc75ef6ff21fbc9f25ab4175cba465e4e0e43Douglas Gregor
3096176b3575823ea996c6140380dd17d9240c9766Douglas Gregorstruct A {
3196176b3575823ea996c6140380dd17d9240c9766Douglas Gregor  bool operator==(Z&); // expected-note{{candidate function}}
3296176b3575823ea996c6140380dd17d9240c9766Douglas Gregor};
3396176b3575823ea996c6140380dd17d9240c9766Douglas Gregor
3496176b3575823ea996c6140380dd17d9240c9766Douglas GregorA make_A();
35eaebc75ef6ff21fbc9f25ab4175cba465e4e0e43Douglas Gregor
3696176b3575823ea996c6140380dd17d9240c9766Douglas Gregorbool operator==(A&, Z&); // expected-note{{candidate function}}
3796176b3575823ea996c6140380dd17d9240c9766Douglas Gregor
3896176b3575823ea996c6140380dd17d9240c9766Douglas Gregorvoid h(A a, const A ac, Z z) {
3996176b3575823ea996c6140380dd17d9240c9766Douglas Gregor  make_A() == z;
4096176b3575823ea996c6140380dd17d9240c9766Douglas Gregor  a == z; // expected-error{{use of overloaded operator '==' is ambiguous; candidates are:}}
4196176b3575823ea996c6140380dd17d9240c9766Douglas Gregor  ac == z; // expected-error{{invalid operands to binary expression ('struct A const' and 'struct Z')}}
4296176b3575823ea996c6140380dd17d9240c9766Douglas Gregor}
4396176b3575823ea996c6140380dd17d9240c9766Douglas Gregor
4496176b3575823ea996c6140380dd17d9240c9766Douglas Gregorstruct B {
4596176b3575823ea996c6140380dd17d9240c9766Douglas Gregor  bool operator==(const B&) const;
4696176b3575823ea996c6140380dd17d9240c9766Douglas Gregor
4796176b3575823ea996c6140380dd17d9240c9766Douglas Gregor  void test(Z z) {
4896176b3575823ea996c6140380dd17d9240c9766Douglas Gregor    make_A() == z;
4996176b3575823ea996c6140380dd17d9240c9766Douglas Gregor  }
5096176b3575823ea996c6140380dd17d9240c9766Douglas Gregor};
51