Daniel Dunbar | a572887 | 2009-12-15 20:14:24 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -fsyntax-only -verify %s |
Douglas Gregor | dda7889 | 2008-12-18 23:43:31 +0000 | [diff] [blame] | 2 | @interface Foo |
| 3 | @end |
| 4 | |
| 5 | @implementation Foo |
| 6 | |
| 7 | void func(id); |
| 8 | |
| 9 | + zone { |
| 10 | func(self); |
| 11 | return self; |
| 12 | } |
Douglas Gregor | a46969d | 2008-12-19 19:16:37 +0000 | [diff] [blame] | 13 | @end |
Douglas Gregor | dda7889 | 2008-12-18 23:43:31 +0000 | [diff] [blame] | 14 | |
Douglas Gregor | 7ca0976 | 2008-11-27 01:19:21 +0000 | [diff] [blame] | 15 | @protocol P0 |
| 16 | @end |
| 17 | |
| 18 | @protocol P1 |
| 19 | @end |
| 20 | |
| 21 | @interface A <P0> |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 22 | @end |
| 23 | |
| 24 | @interface B : A |
| 25 | @end |
| 26 | |
Douglas Gregor | 7ca0976 | 2008-11-27 01:19:21 +0000 | [diff] [blame] | 27 | @interface C <P1> |
| 28 | @end |
| 29 | |
John McCall | 6d09da0 | 2010-10-26 06:23:29 +0000 | [diff] [blame^] | 30 | int& f(A*); // expected-note {{candidate}} |
| 31 | float& f(B*); // expected-note {{candidate}} |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 32 | void g(A*); |
| 33 | |
| 34 | int& h(A*); |
| 35 | float& h(id); |
| 36 | |
John McCall | 6d09da0 | 2010-10-26 06:23:29 +0000 | [diff] [blame^] | 37 | void test0(A* a, B* b, id val) { |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 38 | int& i1 = f(a); |
| 39 | float& f1 = f(b); |
John McCall | 6d09da0 | 2010-10-26 06:23:29 +0000 | [diff] [blame^] | 40 | |
| 41 | // GCC succeeds here, which is clearly ridiculous. |
| 42 | float& f2 = f(val); // expected-error {{ambiguous}} |
| 43 | |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 44 | g(a); |
| 45 | g(b); |
| 46 | g(val); |
| 47 | int& i2 = h(a); |
| 48 | float& f3 = h(val); |
| 49 | // int& i3 = h(b); FIXME: we match GCC here, but shouldn't this work? |
| 50 | } |
| 51 | |
John McCall | 6d09da0 | 2010-10-26 06:23:29 +0000 | [diff] [blame^] | 52 | // We make these errors instead of warnings. Is that okay? |
| 53 | void test1(A* a) { |
| 54 | B* b = a; // expected-error{{cannot initialize a variable of type 'B *' with an lvalue of type 'A *'}} |
| 55 | B *c; c = a; // expected-error{{assigning to 'B *' from incompatible type 'A *'}} |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 56 | } |
| 57 | |
John McCall | 6d09da0 | 2010-10-26 06:23:29 +0000 | [diff] [blame^] | 58 | void test2(A** ap) { |
| 59 | B** bp = ap; // expected-warning{{incompatible pointer types converting 'A **' to type 'B **'}} |
| 60 | bp = ap; // expected-warning{{incompatible pointer types assigning to 'A **' from 'B **'}} |
| 61 | } |
| 62 | |
| 63 | // FIXME: we should either allow overloading here or give a better diagnostic |
| 64 | int& cv(A*); // expected-note {{previous declaration}} expected-note 2 {{not viable}} |
| 65 | float& cv(const A*); // expected-error {{cannot be overloaded}} |
| 66 | |
| 67 | int& cv2(void*); // expected-note 2 {{candidate}} |
| 68 | float& cv2(const void*); // expected-note 2 {{candidate}} |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 69 | |
| 70 | void cv_test(A* a, B* b, const A* ac, const B* bc) { |
| 71 | int &i1 = cv(a); |
| 72 | int &i2 = cv(b); |
John McCall | 6d09da0 | 2010-10-26 06:23:29 +0000 | [diff] [blame^] | 73 | float &f1 = cv(ac); // expected-error {{no matching function}} |
| 74 | float &f2 = cv(bc); // expected-error {{no matching function}} |
| 75 | |
| 76 | // FIXME: these should not be ambiguous |
| 77 | int& i3 = cv2(a); // expected-error {{ambiguous}} |
| 78 | float& f3 = cv2(ac); // expected-error {{ambiguous}} |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 79 | } |
Douglas Gregor | 7ca0976 | 2008-11-27 01:19:21 +0000 | [diff] [blame] | 80 | |
John McCall | 6d09da0 | 2010-10-26 06:23:29 +0000 | [diff] [blame^] | 81 | // We agree with GCC that these can't be overloaded. |
| 82 | int& qualid(id<P0>); // expected-note {{previous declaration}} expected-note {{not viable}} |
| 83 | float& qualid(id<P1>); // expected-error {{cannot be overloaded}} |
Douglas Gregor | 7ca0976 | 2008-11-27 01:19:21 +0000 | [diff] [blame] | 84 | |
| 85 | void qualid_test(A *a, B *b, C *c) { |
| 86 | int& i1 = qualid(a); |
| 87 | int& i2 = qualid(b); |
John McCall | 6d09da0 | 2010-10-26 06:23:29 +0000 | [diff] [blame^] | 88 | |
| 89 | // This doesn't work only because the overload was rejected above. |
| 90 | float& f1 = qualid(c); // expected-error {{no matching function}} |
Douglas Gregor | 27b09ac | 2008-12-22 20:51:52 +0000 | [diff] [blame] | 91 | |
| 92 | id<P0> p1 = 0; |
| 93 | p1 = 0; |
Douglas Gregor | 7ca0976 | 2008-11-27 01:19:21 +0000 | [diff] [blame] | 94 | } |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 95 | |
| 96 | |
| 97 | @class NSException; |
| 98 | typedef struct { |
| 99 | void (*throw_exc)(id); |
| 100 | } |
| 101 | objc_exception_functions_t; |
| 102 | |
| 103 | void (*_NSExceptionRaiser(void))(NSException *) { |
| 104 | objc_exception_functions_t exc_funcs; |
John McCall | 6d09da0 | 2010-10-26 06:23:29 +0000 | [diff] [blame^] | 105 | return exc_funcs.throw_exc; // expected-warning{{incompatible pointer types converting 'void (*)(id)' to type 'void (*)(NSException *)'}} |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 106 | } |
John McCall | ddb0ce7 | 2010-06-11 10:04:22 +0000 | [diff] [blame] | 107 | |
| 108 | namespace test5 { |
| 109 | void foo(bool); |
| 110 | void foo(void *); |
| 111 | |
| 112 | void test(id p) { |
| 113 | foo(p); |
| 114 | } |
| 115 | } |
John McCall | 6d09da0 | 2010-10-26 06:23:29 +0000 | [diff] [blame^] | 116 | |
| 117 | // rdar://problem/8592139 |
| 118 | namespace test6 { |
| 119 | void foo(id); // expected-note {{candidate}} |
| 120 | void foo(A*) __attribute__((unavailable)); // expected-note {{explicitly made unavailable}} |
| 121 | |
| 122 | void test(B *b) { |
| 123 | foo(b); // expected-error {{call to unavailable function}} |
| 124 | } |
| 125 | } |