Daniel Dunbar | a572887 | 2009-12-15 20:14:24 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -fsyntax-only -verify -fblocks %s |
Douglas Gregor | 30c4240 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 2 | |
| 3 | #define bool _Bool |
Steve Naroff | c0febd5 | 2008-12-10 17:49:55 +0000 | [diff] [blame] | 4 | @protocol NSObject; |
| 5 | |
| 6 | void bar(id(^)(void)); |
| 7 | void foo(id <NSObject>(^objectCreationBlock)(void)) { |
| 8 | return bar(objectCreationBlock); |
| 9 | } |
| 10 | |
| 11 | void bar2(id(*)(void)); |
| 12 | void foo2(id <NSObject>(*objectCreationBlock)(void)) { |
| 13 | return bar2(objectCreationBlock); |
| 14 | } |
| 15 | |
| 16 | void bar3(id(*)()); |
| 17 | void foo3(id (*objectCreationBlock)(int)) { |
| 18 | return bar3(objectCreationBlock); |
| 19 | } |
| 20 | |
| 21 | void bar4(id(^)()); |
| 22 | void foo4(id (^objectCreationBlock)(int)) { |
Mike Stump | aab0f7a | 2009-04-01 01:17:39 +0000 | [diff] [blame] | 23 | return bar4(objectCreationBlock); |
Steve Naroff | c0febd5 | 2008-12-10 17:49:55 +0000 | [diff] [blame] | 24 | } |
Douglas Gregor | 2a7e58d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 25 | |
Douglas Gregor | 8987b23 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 26 | void bar5(id(^)(void)); // expected-note 3{{passing argument to parameter here}} |
Douglas Gregor | 30c4240 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 27 | void foo5(id (^objectCreationBlock)(bool)) { |
Douglas Gregor | 8987b23 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 28 | bar5(objectCreationBlock); // expected-error {{incompatible block pointer types passing 'id (^)(bool)' to parameter of type 'id (^)(void)'}} |
| 29 | #undef bool |
| 30 | bar5(objectCreationBlock); // expected-error {{incompatible block pointer types passing 'id (^)(_Bool)' to parameter of type 'id (^)(void)'}} |
| 31 | #define bool int |
| 32 | bar5(objectCreationBlock); // expected-error {{incompatible block pointer types passing 'id (^)(_Bool)' to parameter of type 'id (^)(void)'}} |
Mike Stump | aab0f7a | 2009-04-01 01:17:39 +0000 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | void bar6(id(^)(int)); |
| 36 | void foo6(id (^objectCreationBlock)()) { |
Eli Friedman | 687abff | 2009-06-08 04:24:21 +0000 | [diff] [blame] | 37 | return bar6(objectCreationBlock); |
Mike Stump | aab0f7a | 2009-04-01 01:17:39 +0000 | [diff] [blame] | 38 | } |
| 39 | |
Chris Lattner | bb74982 | 2009-04-11 19:17:25 +0000 | [diff] [blame] | 40 | void foo7(id (^x)(int)) { |
Douglas Gregor | 2a7e58d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 41 | if (x) { } |
| 42 | } |
Chris Lattner | bb74982 | 2009-04-11 19:17:25 +0000 | [diff] [blame] | 43 | |
| 44 | @interface itf |
| 45 | @end |
| 46 | |
| 47 | void foo8() { |
Richard Trieu | 2fe9b7f | 2011-12-15 00:38:15 +0000 | [diff] [blame] | 48 | void *P = ^(itf x) {}; // expected-error {{interface type 'itf' cannot be passed by value; did you forget * in 'itf'}} |
| 49 | P = ^itf(int x) {}; // expected-error {{interface type 'itf' cannot be returned by value; did you forget * in 'itf'}} |
| 50 | P = ^itf() {}; // expected-error {{interface type 'itf' cannot be returned by value; did you forget * in 'itf'}} |
| 51 | P = ^itf{}; // expected-error {{interface type 'itf' cannot be returned by value; did you forget * in 'itf'}} |
Chris Lattner | bb74982 | 2009-04-11 19:17:25 +0000 | [diff] [blame] | 52 | } |
Fariborz Jahanian | a5e42a8 | 2009-08-14 21:53:27 +0000 | [diff] [blame] | 53 | |
| 54 | |
| 55 | int foo9() { |
| 56 | typedef void (^DVTOperationGroupScheduler)(); |
| 57 | id _suboperationSchedulers; |
| 58 | |
| 59 | for (DVTOperationGroupScheduler scheduler in _suboperationSchedulers) { |
| 60 | ; |
| 61 | } |
| 62 | |
| 63 | } |
Fariborz Jahanian | 2263f82 | 2010-03-09 18:34:52 +0000 | [diff] [blame] | 64 | |
| 65 | // rdar 7725203 |
| 66 | @class NSString; |
| 67 | |
| 68 | extern void NSLog(NSString *format, ...) __attribute__((format(__NSString__, 1, 2))); |
| 69 | |
| 70 | void foo10() { |
| 71 | void(^myBlock)(void) = ^{ |
| 72 | }; |
| 73 | NSLog(@"%@", myBlock); |
| 74 | } |
| 75 | |
Jordan Rose | 81a5641 | 2012-06-21 05:54:55 +0000 | [diff] [blame^] | 76 | |
| 77 | // In C, enum constants have the type of the underlying integer type, not the |
| 78 | // enumeration they are part of. We pretend the constants have enum type when |
| 79 | // inferring block return types, so that they can be mixed-and-matched with |
| 80 | // other expressions of enum type. |
| 81 | enum CStyleEnum { |
| 82 | CSE_Value = 1 |
| 83 | }; |
| 84 | enum CStyleEnum getCSE(); |
| 85 | typedef enum CStyleEnum (^cse_block_t)(); |
| 86 | |
| 87 | void testCStyleEnumInference(bool arg) { |
| 88 | cse_block_t a; |
| 89 | |
| 90 | // No warnings here. |
| 91 | a = ^{ return CSE_Value; }; |
| 92 | a = ^{ return getCSE(); }; |
| 93 | |
| 94 | a = ^{ // expected-error {{incompatible block pointer types assigning to 'cse_block_t' (aka 'enum CStyleEnum (^)()') from 'int (^)(void)'}} |
| 95 | return 1; |
| 96 | }; |
| 97 | |
| 98 | // No warnings here. |
| 99 | a = ^{ if (arg) return CSE_Value; else return CSE_Value; }; |
| 100 | a = ^{ if (arg) return getCSE(); else return getCSE(); }; |
| 101 | a = ^{ if (arg) return CSE_Value; else return getCSE(); }; |
| 102 | a = ^{ if (arg) return getCSE(); else return CSE_Value; }; |
| 103 | |
| 104 | // Technically these two blocks should return 'int'. |
| 105 | // The first case is easy to handle -- just don't cast the enum constant |
| 106 | // to the enum type. However, the second guess would require going back |
| 107 | // and REMOVING the cast from the first return statement, which isn't really |
| 108 | // feasible (there may be more than one previous return statement with enum |
| 109 | // type). For symmetry, we just treat them the same way. |
| 110 | a = ^{ // expected-error {{incompatible block pointer types assigning to 'cse_block_t' (aka 'enum CStyleEnum (^)()') from 'int (^)(void)'}} |
| 111 | if (arg) |
| 112 | return 1; |
| 113 | else |
| 114 | return CSE_Value; // expected-error {{return type 'enum CStyleEnum' must match previous return type 'int'}} |
| 115 | }; |
| 116 | |
| 117 | a = ^{ |
| 118 | if (arg) |
| 119 | return CSE_Value; |
| 120 | else |
| 121 | return 1; // expected-error {{return type 'int' must match previous return type 'enum CStyleEnum'}} |
| 122 | }; |
| 123 | } |
| 124 | |
| 125 | |
| 126 | enum FixedTypeEnum : unsigned { |
| 127 | FTE_Value = 1U |
| 128 | }; |
| 129 | enum FixedTypeEnum getFTE(); |
| 130 | typedef enum FixedTypeEnum (^fte_block_t)(); |
| 131 | |
| 132 | void testFixedTypeEnumInference(bool arg) { |
| 133 | fte_block_t a; |
| 134 | |
| 135 | // No warnings here. |
| 136 | a = ^{ return FTE_Value; }; |
| 137 | a = ^{ return getFTE(); }; |
| 138 | |
| 139 | // Since we fixed the underlying type of the enum, this is considered a |
| 140 | // compatible block type. |
| 141 | a = ^{ |
| 142 | return 1U; |
| 143 | }; |
| 144 | |
| 145 | // No warnings here. |
| 146 | a = ^{ if (arg) return FTE_Value; else return FTE_Value; }; |
| 147 | a = ^{ if (arg) return getFTE(); else return getFTE(); }; |
| 148 | a = ^{ if (arg) return FTE_Value; else return getFTE(); }; |
| 149 | a = ^{ if (arg) return getFTE(); else return FTE_Value; }; |
| 150 | |
| 151 | // Technically these two blocks should return 'unsigned'. |
| 152 | // The first case is easy to handle -- just don't cast the enum constant |
| 153 | // to the enum type. However, the second guess would require going back |
| 154 | // and REMOVING the cast from the first return statement, which isn't really |
| 155 | // feasible (there may be more than one previous return statement with enum |
| 156 | // type). For symmetry, we just treat them the same way. |
| 157 | a = ^{ |
| 158 | if (arg) |
| 159 | return 1U; |
| 160 | else |
| 161 | return FTE_Value; // expected-error{{return type 'enum FixedTypeEnum' must match previous return type 'unsigned int'}} |
| 162 | }; |
| 163 | |
| 164 | a = ^{ |
| 165 | if (arg) |
| 166 | return FTE_Value; |
| 167 | else |
| 168 | return 1U; // expected-error{{return type 'unsigned int' must match previous return type 'enum FixedTypeEnum'}} |
| 169 | }; |
| 170 | } |
| 171 | |
| 172 | |
| 173 | enum { |
| 174 | AnonymousValue = 1 |
| 175 | }; |
| 176 | |
| 177 | enum : short { |
| 178 | FixedAnonymousValue = 1 |
| 179 | }; |
| 180 | |
| 181 | typedef enum { |
| 182 | TDE_Value |
| 183 | } TypeDefEnum; |
| 184 | |
| 185 | typedef enum : short { |
| 186 | TDFTE_Value |
| 187 | } TypeDefFixedTypeEnum; |
| 188 | |
| 189 | |
| 190 | typedef int (^int_block_t)(); |
| 191 | typedef short (^short_block_t)(); |
| 192 | void testAnonymousEnumTypes() { |
| 193 | int_block_t IB; |
| 194 | IB = ^{ return AnonymousValue; }; |
| 195 | IB = ^{ return TDE_Value; }; // expected-error {{incompatible block pointer types assigning to 'int_block_t' (aka 'int (^)()') from 'TypeDefEnum (^)(void)'}} |
| 196 | IB = ^{ return CSE_Value; }; // expected-error {{incompatible block pointer types assigning to 'int_block_t' (aka 'int (^)()') from 'enum CStyleEnum (^)(void)'}} |
| 197 | |
| 198 | short_block_t SB; |
| 199 | SB = ^{ return FixedAnonymousValue; }; |
| 200 | // This is not an error anyway since the enum has a fixed underlying type. |
| 201 | SB = ^{ return TDFTE_Value; }; |
| 202 | } |