Richard Smith | 1e3b0f0 | 2012-02-23 01:36:12 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -fsyntax-only -verify -std=c++11 %s |
| 2 | |
| 3 | // New exciting ambiguities in C++11 |
| 4 | |
| 5 | // final 'context sensitive' mess. |
| 6 | namespace final { |
| 7 | struct S { int n; }; |
Richard Smith | bfdb108 | 2012-03-12 08:56:40 +0000 | [diff] [blame] | 8 | struct T { int n; }; |
Richard Smith | 1e3b0f0 | 2012-02-23 01:36:12 +0000 | [diff] [blame] | 9 | namespace N { |
| 10 | int n; |
Richard Smith | bfdb108 | 2012-03-12 08:56:40 +0000 | [diff] [blame] | 11 | // These declare variables named final.. |
| 12 | extern struct S final; |
| 13 | extern struct S final [[]]; |
| 14 | extern struct S final, foo; |
| 15 | struct S final = S(); |
| 16 | |
Richard Smith | 1e3b0f0 | 2012-02-23 01:36:12 +0000 | [diff] [blame] | 17 | // This defines a class, not a variable, even though it would successfully |
| 18 | // parse as a variable but not as a class. DR1318's wording suggests that |
| 19 | // this disambiguation is only performed on an ambiguity, but that was not |
| 20 | // the intent. |
Richard Smith | bfdb108 | 2012-03-12 08:56:40 +0000 | [diff] [blame] | 21 | struct S final { // expected-note {{here}} |
Richard Smith | 1e3b0f0 | 2012-02-23 01:36:12 +0000 | [diff] [blame] | 22 | int(n) // expected-error {{expected ';'}} |
| 23 | }; |
Richard Smith | bfdb108 | 2012-03-12 08:56:40 +0000 | [diff] [blame] | 24 | // This too. |
| 25 | struct T final : S {}; // expected-error {{base 'S' is marked 'final'}} |
David Blaikie | 9933a5a | 2012-03-12 15:39:49 +0000 | [diff] [blame] | 26 | struct T bar : S {}; // expected-error {{expected ';' after top level declarator}} expected-error {{expected unqualified-id}} |
Richard Smith | 1e3b0f0 | 2012-02-23 01:36:12 +0000 | [diff] [blame] | 27 | } |
Richard Smith | 434516c | 2013-02-22 06:46:23 +0000 | [diff] [blame] | 28 | // _Alignas isn't allowed in the places where alignas is. We used to |
| 29 | // assert on this. |
| 30 | struct U final _Alignas(4) {}; // expected-error 3{{}} expected-note {{}} |
Richard Smith | 1e3b0f0 | 2012-02-23 01:36:12 +0000 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | // enum versus bitfield mess. |
| 34 | namespace bitfield { |
| 35 | enum E {}; |
| 36 | |
| 37 | struct T { |
| 38 | constexpr T() {} |
| 39 | constexpr T(int) {} |
| 40 | constexpr T(T, T, T, T) {} |
Richard Smith | 034185c | 2013-04-21 01:08:50 +0000 | [diff] [blame] | 41 | constexpr T operator=(T) const { return *this; } |
| 42 | constexpr operator int() const { return 4; } |
Richard Smith | 1e3b0f0 | 2012-02-23 01:36:12 +0000 | [diff] [blame] | 43 | }; |
| 44 | constexpr T a, b, c, d; |
| 45 | |
| 46 | struct S1 { |
| 47 | enum E : T ( a = 1, b = 2, c = 3, 4 ); // ok, declares a bitfield |
| 48 | }; |
| 49 | // This could be a bit-field. |
| 50 | struct S2 { |
Serge Pavlov | 2e3ecb6 | 2013-12-31 06:26:03 +0000 | [diff] [blame] | 51 | enum E : T { a = 1, b = 2, c = 3, 4 }; // expected-error {{non-integral type}} expected-error {{expected identifier}} |
Richard Smith | 1e3b0f0 | 2012-02-23 01:36:12 +0000 | [diff] [blame] | 52 | }; |
| 53 | struct S3 { |
| 54 | enum E : int { a = 1, b = 2, c = 3, d }; // ok, defines an enum |
| 55 | }; |
| 56 | // Ambiguous. |
| 57 | struct S4 { |
| 58 | enum E : int { a = 1 }; // ok, defines an enum |
| 59 | }; |
| 60 | // This could be a bit-field, but would be ill-formed due to the anonymous |
| 61 | // member being initialized. |
| 62 | struct S5 { |
Richard Smith | 369b9f9 | 2012-06-25 21:37:02 +0000 | [diff] [blame] | 63 | enum E : int { a = 1 } { b = 2 }; // expected-error {{expected ';' after enum}} expected-error {{expected member name}} |
Richard Smith | 1e3b0f0 | 2012-02-23 01:36:12 +0000 | [diff] [blame] | 64 | }; |
| 65 | // This could be a bit-field. |
| 66 | struct S6 { |
Serge Pavlov | 2e3ecb6 | 2013-12-31 06:26:03 +0000 | [diff] [blame] | 67 | enum E : int { 1 }; // expected-error {{expected identifier}} |
Richard Smith | 1e3b0f0 | 2012-02-23 01:36:12 +0000 | [diff] [blame] | 68 | }; |
| 69 | |
| 70 | struct U { |
Richard Smith | 034185c | 2013-04-21 01:08:50 +0000 | [diff] [blame] | 71 | constexpr operator T() const { return T(); } // expected-note 2{{candidate}} |
Richard Smith | 1e3b0f0 | 2012-02-23 01:36:12 +0000 | [diff] [blame] | 72 | }; |
| 73 | // This could be a bit-field. |
| 74 | struct S7 { |
| 75 | enum E : int { a = U() }; // expected-error {{no viable conversion}} |
| 76 | }; |
| 77 | // This could be a bit-field, and does not conform to the grammar of an |
| 78 | // enum definition, because 'id(U())' is not a constant-expression. |
| 79 | constexpr const U &id(const U &u) { return u; } |
| 80 | struct S8 { |
| 81 | enum E : int { a = id(U()) }; // expected-error {{no viable conversion}} |
| 82 | }; |
| 83 | } |
| 84 | |
| 85 | namespace trailing_return { |
| 86 | typedef int n; |
| 87 | int a; |
| 88 | |
| 89 | struct S { |
| 90 | S(int); |
Richard Smith | 45fbfc7 | 2012-07-23 21:41:30 +0000 | [diff] [blame] | 91 | S *operator()(...) const; |
Richard Smith | 1e3b0f0 | 2012-02-23 01:36:12 +0000 | [diff] [blame] | 92 | int n; |
| 93 | }; |
| 94 | |
| 95 | namespace N { |
| 96 | void f() { |
| 97 | // This parses as a function declaration, but DR1223 makes the presence of |
| 98 | // 'auto' be used for disambiguation. |
| 99 | S(a)()->n; // ok, expression; expected-warning{{expression result unused}} |
Richard Smith | 45fbfc7 | 2012-07-23 21:41:30 +0000 | [diff] [blame] | 100 | S(a)(int())->n; // ok, expression; expected-warning{{expression result unused}} |
Richard Smith | 1e3b0f0 | 2012-02-23 01:36:12 +0000 | [diff] [blame] | 101 | auto(a)()->n; // ok, function declaration |
Richard Smith | 45fbfc7 | 2012-07-23 21:41:30 +0000 | [diff] [blame] | 102 | auto(b)(int())->n; // ok, function declaration |
Richard Smith | 1e3b0f0 | 2012-02-23 01:36:12 +0000 | [diff] [blame] | 103 | using T = decltype(a); |
| 104 | using T = auto() -> n; |
| 105 | } |
| 106 | } |
| 107 | } |
Richard Smith | 43f340f | 2012-03-27 23:05:05 +0000 | [diff] [blame] | 108 | |
| 109 | namespace ellipsis { |
| 110 | template<typename...T> |
| 111 | struct S { |
Richard Smith | 74f0234 | 2017-01-19 21:00:13 +0000 | [diff] [blame] | 112 | void e(S::S()); // expected-error {{is a constructor name}} |
Nico Weber | 71e377d | 2015-01-17 02:33:17 +0000 | [diff] [blame] | 113 | void f(S(...args[sizeof(T)])); // expected-note {{here}} expected-note {{here}} |
| 114 | void f(S(...args)[sizeof(T)]); // expected-error {{redeclared}} |
Richard Smith | 43f340f | 2012-03-27 23:05:05 +0000 | [diff] [blame] | 115 | void f(S ...args[sizeof(T)]); // expected-error {{redeclared}} |
Richard Smith | b19337f | 2013-02-20 20:19:27 +0000 | [diff] [blame] | 116 | void g(S(...[sizeof(T)])); // expected-note {{here}} expected-warning {{ISO C++11 requires a parenthesized pack declaration to have a name}} |
Richard Smith | 43f340f | 2012-03-27 23:05:05 +0000 | [diff] [blame] | 117 | void g(S(...)[sizeof(T)]); // expected-error {{function cannot return array type}} |
| 118 | void g(S ...[sizeof(T)]); // expected-error {{redeclared}} |
| 119 | void h(T(...)); // function type, expected-error {{unexpanded parameter pack}} |
| 120 | void h(T...); // pack expansion, ok |
| 121 | void i(int(T...)); // expected-note {{here}} |
| 122 | void i(int(T...a)); // expected-error {{redeclared}} |
| 123 | void i(int(T, ...)); // function type, expected-error {{unexpanded parameter pack}} |
| 124 | void i(int(T, ...a)); // expected-error {{expected ')'}} expected-note {{to match}} expected-error {{unexpanded parameter pack}} |
| 125 | void j(int(int...)); // function type, ok |
| 126 | void j(int(int...a)); // expected-error {{does not contain any unexpanded parameter packs}} |
| 127 | void j(T(int...)); // expected-error {{unexpanded parameter pack}} |
| 128 | void j(T(T...)); // expected-error {{unexpanded parameter pack}} |
| 129 | void k(int(...)(T)); // expected-error {{cannot return function type}} |
| 130 | void k(int ...(T)); |
Richard Smith | b19337f | 2013-02-20 20:19:27 +0000 | [diff] [blame] | 131 | void l(int(&...)(T)); // expected-warning {{ISO C++11 requires a parenthesized pack declaration to have a name}} |
| 132 | void l(int(*...)(T)); // expected-warning {{ISO C++11 requires a parenthesized pack declaration to have a name}} |
| 133 | void l(int(S<int>::*...)(T)); // expected-warning {{ISO C++11 requires a parenthesized pack declaration to have a name}} |
Richard Smith | 43f340f | 2012-03-27 23:05:05 +0000 | [diff] [blame] | 134 | }; |
Hubert Tong | 605eaca | 2017-05-20 00:21:55 +0000 | [diff] [blame^] | 135 | |
| 136 | struct CtorSink { |
| 137 | template <typename ...T> constexpr CtorSink(T &&...t) { } |
| 138 | constexpr operator int() const { return 42; } |
| 139 | }; |
| 140 | |
| 141 | template <unsigned ...N> struct UnsignedTmplArgSink; |
| 142 | |
| 143 | template <typename ...T> |
| 144 | void foo(int x, T ...t) { |
| 145 | // Have a variety of cases where the syntax is technically unambiguous, but hinges on careful treatment of ellipses. |
| 146 | CtorSink(t ...), x; // ok, expression; expected-warning 2{{expression result unused}} |
| 147 | |
| 148 | int x0(CtorSink(t ...)); // ok, declares object x0 |
| 149 | int *p0 = &x0; |
| 150 | (void)p0; |
| 151 | |
| 152 | CtorSink x1(int(t) ..., int(x)); // ok, declares object x1 |
| 153 | CtorSink *p1 = &x1; |
| 154 | (void)p1; |
| 155 | |
| 156 | UnsignedTmplArgSink<T(CtorSink(t ...)) ...> *t0; // ok |
| 157 | UnsignedTmplArgSink<((T *)0, 42u) ...> **t0p = &t0; |
| 158 | } |
| 159 | |
| 160 | template void foo(int, int, int); // expected-note {{in instantiation of function template specialization 'ellipsis::foo<int, int>' requested here}} |
Richard Smith | 43f340f | 2012-03-27 23:05:05 +0000 | [diff] [blame] | 161 | } |
Richard Smith | 22c7c41 | 2013-03-20 03:35:02 +0000 | [diff] [blame] | 162 | |
| 163 | namespace braced_init_list { |
| 164 | struct X { |
| 165 | void foo() {} |
| 166 | }; |
| 167 | |
| 168 | void (*pf1)() {}; |
| 169 | void (X::*pmf1)() {&X::foo}; |
| 170 | void (X::*pmf2)() = {&X::foo}; |
| 171 | |
| 172 | void test() { |
| 173 | void (*pf2)() {}; |
| 174 | void (X::*pmf3)() {&X::foo}; |
| 175 | void (X::*pmf4)() = {&X::foo}; |
| 176 | } |
| 177 | } |