2012-02-09 08:47:04 +08:00
// RUN: %clang_cc1 -std=c++0x -Wno-unused-value -fsyntax-only -verify -fblocks %s
2012-01-07 09:08:17 +08:00
2012-01-07 12:59:52 +08:00
namespace std { class type_info ; } ;
2012-01-07 09:08:17 +08:00
namespace ExplicitCapture {
class C {
2012-01-07 12:59:52 +08:00
int Member ;
static void Overload ( int ) ;
void Overload ( ) ;
virtual C & Overload ( float ) ;
2012-01-07 09:08:17 +08:00
2012-01-07 12:59:52 +08:00
void ImplicitThisCapture ( ) {
2012-02-09 16:26:42 +08:00
[ ] ( ) { ( void ) Member ; } ; // expected-error {{'this' cannot be implicitly captured in this context}}
[ & ] ( ) { ( void ) Member ; } ;
[ this ] ( ) { ( void ) Member ; } ;
[ this ] { [ this ] { } ; } ;
[ ] { [ this ] { } ; } ; // expected-error {{'this' cannot be implicitly captured in this context}}
[ ] { Overload ( 3 ) ; } ;
[ ] { Overload ( ) ; } ; // expected-error {{'this' cannot be implicitly captured in this context}}
[ ] { ( void ) typeid ( Overload ( ) ) ; } ;
[ ] { ( void ) typeid ( Overload ( .5f ) ) ; } ; // expected-error {{'this' cannot be implicitly captured in this context}}
2012-01-07 12:59:52 +08:00
}
2012-01-07 09:08:17 +08:00
} ;
void f ( ) {
2012-02-09 16:26:42 +08:00
[ this ] ( ) { } ; // expected-error {{'this' cannot be captured in this context}}
2012-01-07 09:08:17 +08:00
}
}
2012-01-26 11:00:14 +08:00
namespace ReturnDeduction {
void test ( ) {
2012-02-09 16:26:42 +08:00
[ ] ( ) { return 1 ; } ;
[ ] ( ) { return 1 ; } ;
[ ] ( ) { return ( { return 1 ; 1 ; } ) ; } ;
2012-02-09 18:18:50 +08:00
[ ] ( ) { return ( { return ' c ' ; 1 ; } ) ; } ; / / expected - error { { must match previous return type } } \
// expected-warning{{omitted result type}}
2012-02-09 16:26:42 +08:00
[ ] ( ) - > int { return ' c ' ; return 1 ; } ;
[ ] ( ) { return ' c ' ; return 1 ; } ; // expected-error {{must match previous return type}}
[ ] ( ) { return ; return ( void ) 0 ; } ;
2012-02-09 18:18:50 +08:00
[ ] ( ) { return 1 ; return 1 ; } ; // expected-warning{{omitted result type}}
2012-01-26 11:00:14 +08:00
}
}
2012-02-04 06:47:37 +08:00
namespace ImplicitCapture {
void test ( ) {
2012-02-04 07:06:43 +08:00
int a = 0 ; // expected-note 5 {{declared}}
2012-02-09 16:26:42 +08:00
[ ] ( ) { return a ; } ; // expected-error {{variable 'a' cannot be implicitly captured in a lambda with no capture-default specified}} expected-note {{begins here}}
[ & ] ( ) { return a ; } ;
[ = ] ( ) { return a ; } ;
[ = ] ( ) { int * b = & a ; } ; // expected-error {{cannot initialize a variable of type 'int *' with an rvalue of type 'const int *'}}
[ = ] ( ) { return [ & ] ( ) { return a ; } ; } ;
[ ] ( ) { return [ & ] ( ) { return a ; } ; } ; // expected-error {{variable 'a' cannot be implicitly captured in a lambda with no capture-default specified}} expected-note {{lambda expression begins here}}
[ ] ( ) { return ^ { return a ; } ; } ; // expected-error {{variable 'a' cannot be implicitly captured in a lambda with no capture-default specified}} expected-note {{lambda expression begins here}}
[ ] ( ) { return [ & a ] { return a ; } ; } ; // expected-error 2 {{variable 'a' cannot be implicitly captured in a lambda with no capture-default specified}} expected-note 2 {{lambda expression begins here}}
[ = ] ( ) { return [ & a ] { return a ; } ; } ; //
2012-02-04 06:47:37 +08:00
const int b = 2 ;
2012-02-09 16:26:42 +08:00
[ ] ( ) { return b ; } ;
2012-02-04 06:47:37 +08:00
union { // expected-note {{declared}}
int c ;
float d ;
} ;
d = 3 ;
2012-02-09 16:26:42 +08:00
[ = ] ( ) { return c ; } ; // expected-error {{unnamed variable cannot be implicitly captured in a lambda expression}}
2012-02-04 06:47:37 +08:00
2012-02-04 07:06:43 +08:00
__block int e ; // expected-note 3 {{declared}}
2012-02-09 16:26:42 +08:00
[ & ] ( ) { return e ; } ; // expected-error {{__block variable 'e' cannot be captured in a lambda expression}}
[ & e ] ( ) { return e ; } ; // expected-error 2 {{__block variable 'e' cannot be captured in a lambda expression}}
2012-02-04 06:47:37 +08:00
int f [ 10 ] ; // expected-note {{declared}}
2012-02-09 16:26:42 +08:00
[ & ] ( ) { return f [ 2 ] ; } ;
2012-02-13 02:42:33 +08:00
( void ) ^ { return [ ] ( ) { return f [ 2 ] ; } ; } ; / / expected - error { { variable ' f ' cannot be implicitly captured in a lambda with no capture - default specified } } \
// expected-note{{lambda expression begins here}}
2012-02-04 06:47:37 +08:00
struct G { G ( ) ; G ( G & ) ; int a ; } ; // expected-note 6 {{not viable}}
G g ;
2012-02-09 18:18:50 +08:00
[ = ] ( ) { const G * gg = & g ; return gg - > a ; } ; // expected-warning{{omitted result type}}
2012-02-18 17:37:24 +08:00
[ = ] ( ) { return [ = ] { const G * gg = & g ; return gg - > a ; } ( ) ; } ; / / expected - error { { no matching constructor for initialization of ' ImplicitCapture : : G ' } } \
2012-02-09 18:18:50 +08:00
// expected-warning{{omitted result type}}
( void ) ^ { return [ = ] { const G * gg = & g ; return gg - > a ; } ( ) ; } ; / / expected - error 2 { { no matching constructor for initialization of ' const ImplicitCapture : : G ' } } \
// expected-warning{{omitted result type}}
2012-02-07 05:50:18 +08:00
const int h = a ; // expected-note {{declared}}
2012-02-09 16:26:42 +08:00
[ ] ( ) { return h ; } ; // expected-error {{variable 'h' cannot be implicitly captured in a lambda with no capture-default specified}} expected-note {{lambda expression begins here}}
2012-02-04 06:47:37 +08:00
}
}
2012-02-22 04:05:31 +08:00
namespace PR12031 {
struct X {
template < typename T >
X ( const T & ) ;
~ X ( ) ;
} ;
void f ( int i , X x ) ;
void g ( ) {
const int v = 10 ;
f ( v , [ ] ( ) { } ) ;
}
}
2012-02-25 06:12:32 +08:00
namespace NullPtr {
int & f ( int * p ) ;
char & f ( . . . ) ;
void g ( ) {
int n = 0 ;
[ = ] {
char & k = f ( n ) ; // not a null pointer constant
} ( ) ;
const int m = 0 ;
[ = ] {
int & k = f ( m ) ; // a null pointer constant
} ( ) ;
[ = ] ( ) - > bool {
2012-02-25 07:21:24 +08:00
int & k = f ( m ) ; // a null pointer constant
return & m = = 0 ;
2012-02-25 06:12:32 +08:00
} ( ) ;
[ m ] {
2012-02-25 07:21:24 +08:00
int & k = f ( m ) ; // a null pointer constant
2012-02-25 06:12:32 +08:00
} ( ) ;
}
}