block-return.c revision 1eb4433ac451dc16f4133a88af2d002ac26c58ef
1// RUN: clang-cc -fsyntax-only %s -verify -fblocks
2
3typedef void (^CL)(void);
4
5CL foo() {
6  short y;
7  short (^add1)(void) = ^{ return y+1; }; // expected-error {{incompatible block pointer types initializing 'int (^)(void)', expected 'short (^)(void)'}}
8
9  CL X = ^{
10    if (2)
11      return;
12    return 1;  // expected-error {{void block should not return a value}}
13  };
14
15  int (^Y) (void)  = ^{
16    if (3)
17      return 1;
18    else
19      return; // expected-error {{non-void block should return a value}}
20  };
21
22  char *(^Z)(void) = ^{
23    if (3)
24      return "";
25    else
26      return (char*)0;
27  };
28
29  double (^A)(void) = ^ { // expected-error {{incompatible block pointer types initializing 'float (^)(void)', expected 'double (^)(void)'}}
30    if (1)
31      return (float)1.0;
32    else
33      if (2)
34        return (double)2.0;
35    return 1;
36  };
37  char *(^B)(void) = ^{
38    if (3)
39      return "";
40    else
41      return 2; // expected-warning {{incompatible integer to pointer conversion returning 'int', expected 'char *'}}
42  };
43
44  return ^{ return 1; }; // expected-error {{incompatible block pointer types returning 'int (^)(void)', expected 'CL'}}
45}
46
47typedef int (^CL2)(void);
48
49CL2 foo2() {
50  return ^{ return 1; };
51}
52
53typedef unsigned int * uintptr_t;
54typedef char Boolean;
55typedef int CFBasicHash;
56
57#define INVOKE_CALLBACK2(P, A, B) (P)(A, B)
58
59typedef struct {
60    Boolean (^isEqual)(const CFBasicHash *, uintptr_t stack_value_or_key1, uintptr_t stack_value_or_key2, Boolean is_key);
61} CFBasicHashCallbacks;
62
63int foo3() {
64    CFBasicHashCallbacks cb;
65
66    Boolean (*value_equal)(uintptr_t, uintptr_t) = 0;
67
68    cb.isEqual = ^(const CFBasicHash *table, uintptr_t stack_value_or_key1, uintptr_t stack_value_or_key2, Boolean is_key) {
69      return (Boolean)(uintptr_t)INVOKE_CALLBACK2(value_equal, (uintptr_t)stack_value_or_key1, (uintptr_t)stack_value_or_key2);
70    };
71}
72
73static int funk(char *s) {
74  if (^{} == ((void*)0))
75    return 1;
76  else
77    return 0;
78}
79void foo4() {
80  int (^xx)(const char *s) = ^(char *s) { return 1; }; // expected-error {{incompatible block pointer types initializing 'int (^)(char *)', expected 'int (^)(char const *)'}}
81  int (*yy)(const char *s) = funk; // expected-warning {{incompatible pointer types initializing 'int (char *)', expected 'int (*)(char const *)'}}
82
83  int (^nested)(char *s) = ^(char *str) { void (^nest)(void) = ^(void) { printf("%s\n", str); }; next(); return 1; }; // expected-warning{{implicitly declaring C library function 'printf' with type 'int (char const *, ...)'}} \
84  // expected-note{{please include the header <stdio.h> or explicitly provide a declaration for 'printf'}}
85}
86
87typedef void (^bptr)(void);
88
89bptr foo5(int j) {
90  __block int i;
91  if (j)
92    return ^{ ^{ i=0; }(); };  // expected-error {{returning block that lives on the local stack}}
93  return ^{ i=0; };  // expected-error {{returning block that lives on the local stack}}
94  return (^{ i=0; });  // expected-error {{returning block that lives on the local stack}}
95  return (void*)(^{ i=0; });  // expected-error {{returning block that lives on the local stack}}
96}
97
98int (*funcptr3[5])(long);
99int sz8 = sizeof(^int (*[5])(long) {return funcptr3;}); // expected-error {{block declared as returning an array}}
100
101void foo6() {
102  int (^b)(int) __attribute__((noreturn));
103  b = ^ (int i) __attribute__((noreturn)) { return 1; };  // expected-error {{block declared 'noreturn' should not return}}
104  b(1);
105  int (^c)(void) __attribute__((noreturn)) = ^ __attribute__((noreturn)) { return 100; }; // expected-error {{block declared 'noreturn' should not return}}
106}
107
108
109void foo7()
110{
111 const int (^BB) (void) = ^{ const int i = 1; return i; }; // OK
112 const int (^CC) (void)  = ^const int{ const int i = 1; return i; }; // OK
113
114  int i;
115  int (^FF) (void)  = ^{ return i; }; // OK
116  int (^EE) (void)  = ^{ return i+1; }; // OK
117
118  __block int j;
119  int (^JJ) (void)  = ^{ return j; }; // OK
120  int (^KK) (void)  = ^{ return j+1; }; // OK
121
122  __block const int k;
123  const int cint = 100;
124
125  int (^MM) (void)  = ^{ return k; }; // expected-error {{incompatible block pointer types initializing 'int const (^)(void)', expected 'int (^)(void)'}}
126  int (^NN) (void)  = ^{ return cint; }; // expected-error {{incompatible block pointer types initializing 'int const (^)(void)', expected 'int (^)(void)'}}
127
128}
129
130
131