17f99f43b362a4a4a0f47515c88690d7291756447Douglas Gregor// RUN: %clang_cc1 -fsyntax-only -Wno-unused-value -verify -std=c++11 %s
2ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor
37796eb5643244f3134834253ce5ea89107ac21c1Richard Smithenum E { e };
47796eb5643244f3134834253ce5ea89107ac21c1Richard Smith
56bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hinesconstexpr int id(int n) { return n; }
66bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines
7ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregorclass C {
8ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor
9ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor  int f() {
10ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor    int foo, bar;
11ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor
12ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor    []; // expected-error {{expected body of lambda expression}}
13ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor    [+] {}; // expected-error {{expected variable name or 'this' in lambda capture list}}
14ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor    [foo+] {}; // expected-error {{expected ',' or ']' in lambda capture list}}
15ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor    [foo,&this] {}; // expected-error {{'this' cannot be captured by reference}}
16ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor    [&this] {}; // expected-error {{'this' cannot be captured by reference}}
172fe9b7fb07dff15dd15dd8755a9a9e6de0fe46fcRichard Trieu    [&,] {}; // expected-error {{expected variable name or 'this' in lambda capture list}}
182fe9b7fb07dff15dd15dd8755a9a9e6de0fe46fcRichard Trieu    [=,] {}; // expected-error {{expected variable name or 'this' in lambda capture list}}
19b326ca8ffbea96f9cc8a457b0f57be880304a6f5Douglas Gregor    [] {};
20b326ca8ffbea96f9cc8a457b0f57be880304a6f5Douglas Gregor    [=] (int i) {};
21b326ca8ffbea96f9cc8a457b0f57be880304a6f5Douglas Gregor    [&] (int) mutable -> void {};
22b326ca8ffbea96f9cc8a457b0f57be880304a6f5Douglas Gregor    [foo,bar] () { return 3; };
23b326ca8ffbea96f9cc8a457b0f57be880304a6f5Douglas Gregor    [=,&foo] () {};
24b326ca8ffbea96f9cc8a457b0f57be880304a6f5Douglas Gregor    [&,foo] () {};
25b326ca8ffbea96f9cc8a457b0f57be880304a6f5Douglas Gregor    [this] () {};
267796eb5643244f3134834253ce5ea89107ac21c1Richard Smith    [] () -> class C { return C(); };
277796eb5643244f3134834253ce5ea89107ac21c1Richard Smith    [] () -> enum E { return e; };
28ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor
29c9ecec404fe28d53aa49b3c830d8353e97bea5f2Douglas Gregor    [] -> int { return 0; }; // expected-error{{lambda requires '()' before return type}}
30c9ecec404fe28d53aa49b3c830d8353e97bea5f2Douglas Gregor    [] mutable -> int { return 0; }; // expected-error{{lambda requires '()' before 'mutable'}}
313bc22262af7b09a459b400976cfce3d9318b8ea9Richard Smith    [](int) -> {}; // PR13652 expected-error {{expected a type}}
32ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor    return 1;
33ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor  }
34ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor
35b3f323d6c41cb614a249dbc4af7493762786521aDouglas Gregor  void designator_or_lambda() {
36b3f323d6c41cb614a249dbc4af7493762786521aDouglas Gregor    typedef int T;
37b3f323d6c41cb614a249dbc4af7493762786521aDouglas Gregor    const int b = 0;
38b3f323d6c41cb614a249dbc4af7493762786521aDouglas Gregor    const int c = 1;
396bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    int d;
40651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    int a1[1] = {[b] (T()) {}}; // expected-error{{no viable conversion from '(lambda}}
41b3f323d6c41cb614a249dbc4af7493762786521aDouglas Gregor    int a2[1] = {[b] = 1 };
426bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    int a3[1] = {[b,c] = 1 }; // expected-error{{expected ']'}} expected-note {{to match}}
43b3f323d6c41cb614a249dbc4af7493762786521aDouglas Gregor    int a4[1] = {[&b] = 1 }; // expected-error{{integral constant expression must have integral or unscoped enumeration type, not 'const int *'}}
44b3f323d6c41cb614a249dbc4af7493762786521aDouglas Gregor    int a5[3] = { []{return 0;}() };
45b3f323d6c41cb614a249dbc4af7493762786521aDouglas Gregor    int a6[1] = {[this] = 1 }; // expected-error{{integral constant expression must have integral or unscoped enumeration type, not 'C *'}}
466bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    int a7[1] = {[d(0)] { return d; } ()}; // expected-warning{{extension}}
476bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    int a8[1] = {[d = 0] { return d; } ()}; // expected-warning{{extension}}
486bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    int a9[1] = {[d = 0] = 1}; // expected-error{{is not an integral constant expression}}
496bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    int a10[1] = {[id(0)] { return id; } ()}; // expected-warning{{extension}}
506bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    int a11[1] = {[id(0)] = 1};
51b3f323d6c41cb614a249dbc4af7493762786521aDouglas Gregor  }
52950435c15e413c55859f8af78d2c6e603743b42fRichard Smith
53950435c15e413c55859f8af78d2c6e603743b42fRichard Smith  void delete_lambda(int *p) {
54950435c15e413c55859f8af78d2c6e603743b42fRichard Smith    delete [] p;
55950435c15e413c55859f8af78d2c6e603743b42fRichard Smith    delete [] (int*) { new int }; // ok, compound-literal, not lambda
56950435c15e413c55859f8af78d2c6e603743b42fRichard Smith    delete [] { return new int; } (); // expected-error{{expected expression}}
57950435c15e413c55859f8af78d2c6e603743b42fRichard Smith    delete [&] { return new int; } (); // ok, lambda
58950435c15e413c55859f8af78d2c6e603743b42fRichard Smith  }
590a664b863255065d960342dd074a77d63c753d35Richard Smith
600a664b863255065d960342dd074a77d63c753d35Richard Smith  // We support init-captures in C++11 as an extension.
610a664b863255065d960342dd074a77d63c753d35Richard Smith  int z;
620a664b863255065d960342dd074a77d63c753d35Richard Smith  void init_capture() {
639beaf20b882eb83082da27a74760277bb9fc0bddRichard Smith    [n(0)] () mutable -> int { return ++n; }; // expected-warning{{extension}}
649beaf20b882eb83082da27a74760277bb9fc0bddRichard Smith    [n{0}] { return; }; // expected-error {{<initializer_list>}} expected-warning{{extension}}
659beaf20b882eb83082da27a74760277bb9fc0bddRichard Smith    [n = 0] { return ++n; }; // expected-error {{captured by copy in a non-mutable}} expected-warning{{extension}}
669beaf20b882eb83082da27a74760277bb9fc0bddRichard Smith    [n = {0}] { return; }; // expected-error {{<initializer_list>}} expected-warning{{extension}}
679beaf20b882eb83082da27a74760277bb9fc0bddRichard Smith    [a([&b = z]{})](){}; // expected-warning 2{{extension}}
680a664b863255065d960342dd074a77d63c753d35Richard Smith
690d8e9646bc000bab521ce52ed294209a92298cefRichard Smith    int x = 4;
709beaf20b882eb83082da27a74760277bb9fc0bddRichard Smith    auto y = [&r = x, x = x + 1]() -> int { // expected-warning 2{{extension}}
7104fa7a33279808dc3e5117c41b5f84c40eeb7362Richard Smith      r += 2;
7204fa7a33279808dc3e5117c41b5f84c40eeb7362Richard Smith      return x + 2;
730a664b863255065d960342dd074a77d63c753d35Richard Smith    } ();
740a664b863255065d960342dd074a77d63c753d35Richard Smith  }
75651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
76651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  void attributes() {
77651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    [] [[]] {}; // expected-error {{lambda requires '()' before attribute specifier}}
78651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    [] __attribute__((noreturn)) {}; // expected-error {{lambda requires '()' before attribute specifier}}
79651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    []() [[]]
80651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      mutable {}; // expected-error {{expected body of lambda expression}}
81651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
82651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    []() [[]] {};
83651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    []() [[]] -> void {};
84651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    []() mutable [[]] -> void {};
85651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    []() mutable noexcept [[]] -> void {};
86651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
87651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    // Testing GNU-style attributes on lambdas -- the attribute is specified
88651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    // before the mutable specifier instead of after (unlike C++11).
89651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    []() __attribute__((noreturn)) mutable { while(1); };
90651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    []() mutable
91651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      __attribute__((noreturn)) { while(1); }; // expected-error {{expected body of lambda expression}}
92651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  }
93ae7902c4293d9de8b9591759513f0d075f45022aDouglas Gregor};
94