blob: b8504d4906500224bbf58388460d16f13ba32fd5 [file] [log] [blame]
Douglas Gregora30a0bc2012-02-09 18:19:44 +00001// RUN: %clang_cc1 -std=c++11 %s -Winvalid-noreturn -verify
Douglas Gregor6b8ef342012-02-01 17:18:19 +00002
Douglas Gregora30a0bc2012-02-09 18:19:44 +00003// An attribute-specifier-seq in a lambda-declarator appertains to the
4// type of the corresponding function call operator.
5void test_attributes() {
Douglas Gregorcf11eb72012-02-15 16:20:15 +00006 auto nrl = [](int x) -> int { if (x > 0) return x; }; // expected-warning{{control may reach end of non-void lambda}}
7
Richard Smith10876ef2013-01-17 01:30:42 +00008 // FIXME: GCC accepts the [[gnu::noreturn]] attribute here.
9 auto nrl2 = []() [[gnu::noreturn]] { return; }; // expected-warning{{attribute 'noreturn' ignored}}
Douglas Gregor6b8ef342012-02-01 17:18:19 +000010}
Douglas Gregora30a0bc2012-02-09 18:19:44 +000011
12template<typename T>
13struct bogus_override_if_virtual : public T {
Nick Lewycky2eeddfb2016-05-14 17:44:14 +000014 bogus_override_if_virtual() : T(*(T*)0) { } // expected-warning {{binding dereferenced null pointer to reference has undefined behavior}}
Douglas Gregora30a0bc2012-02-09 18:19:44 +000015 int operator()() const;
16};
17
18void test_quals() {
19 // This function call operator is declared const (9.3.1) if and only
20 // if the lambda- expression's parameter-declaration-clause is not
21 // followed by mutable.
Douglas Gregor12695102012-02-10 08:36:38 +000022 auto l = [=](){}; // expected-note{{method is not marked volatile}}
Douglas Gregora30a0bc2012-02-09 18:19:44 +000023 const decltype(l) lc = l;
24 l();
25 lc();
26
Douglas Gregor12695102012-02-10 08:36:38 +000027 auto ml = [=]() mutable{}; // expected-note{{method is not marked const}} \
28 // expected-note{{method is not marked volatile}}
Douglas Gregora30a0bc2012-02-09 18:19:44 +000029 const decltype(ml) mlc = ml;
30 ml();
31 mlc(); // expected-error{{no matching function for call to object of type}}
32
33 // It is neither virtual nor declared volatile.
34 volatile decltype(l) lv = l;
35 volatile decltype(ml) mlv = ml;
36 lv(); // expected-error{{no matching function for call to object of type}}
37 mlv(); // expected-error{{no matching function for call to object of type}}
38
Nick Lewycky2eeddfb2016-05-14 17:44:14 +000039 bogus_override_if_virtual<decltype(l)> bogus; // expected-note{{in instantiation of member function 'bogus_override_if_virtual<(lambda}}
Douglas Gregora30a0bc2012-02-09 18:19:44 +000040}
41
Richard Smith3cb4c632013-04-17 16:25:20 +000042// Core issue 974: default arguments (8.3.6) may be specified in the
43// parameter-declaration-clause of a lambda-declarator.
Douglas Gregora30a0bc2012-02-09 18:19:44 +000044int test_default_args() {
Richard Smith3cb4c632013-04-17 16:25:20 +000045 return [](int i = 5, int j = 17) { return i+j;}(5, 6);
Douglas Gregora30a0bc2012-02-09 18:19:44 +000046}
47
48// Any exception-specification specified on a lambda-expression
49// applies to the corresponding function call operator.
50void test_exception_spec() {
51 auto tl1 = []() throw(int) {};
52 auto tl2 = []() {};
53 static_assert(!noexcept(tl1()), "lambda can throw");
54 static_assert(!noexcept(tl2()), "lambda can throw");
55
56 auto ntl1 = []() throw() {};
57 auto ntl2 = []() noexcept(true) {};
58 auto ntl3 = []() noexcept {};
59 static_assert(noexcept(ntl1()), "lambda cannot throw");
60 static_assert(noexcept(ntl2()), "lambda cannot throw");
61 static_assert(noexcept(ntl3()), "lambda cannot throw");
62}
63