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; }; 8 struct T { int n; }; 9 namespace N { 10 int n; 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 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. 21 struct S final { // expected-note {{here}} 22 int(n) // expected-error {{expected ';'}} 23 }; 24 // This too. 25 struct T final : S {}; // expected-error {{base 'S' is marked 'final'}} 26 struct T bar : S {}; // expected-error {{expected ';' after top level declarator}} expected-error {{expected unqualified-id}} 27 } 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 {{}} 31 } 32 33 // enum versus bitfield. These are always required to be treated as an 34 // enum-base, but we disambiguate anyway for better error recovery. 35 namespace bitfield { 36 enum E {}; 37 38 struct T { Tbitfield::T39 constexpr T() {} Tbitfield::T40 constexpr T(int) {} Tbitfield::T41 constexpr T(T, T, T, T) {} operator =bitfield::T42 constexpr T operator=(T) const { return *this; } operator intbitfield::T43 constexpr operator int() const { return 4; } 44 }; 45 constexpr T a, b, c, d; 46 47 struct S1 { 48 enum E : T ( a = 1, b = 2, c = 3, 4 ); // expected-error {{ISO C++ only allows ':' in member enumeration declaration to introduce a fixed underlying type, not an anonymous bit-field}} 49 }; 50 // Enum definition, not a bit-field. 51 struct S2 { 52 enum E : T { a = 1, b = 2, c = 3, 4 }; // expected-error {{non-integral type}} expected-error {{expected identifier}} 53 }; 54 struct S3 { 55 enum E : int { a = 1, b = 2, c = 3, d }; // ok, defines an enum 56 }; 57 // Ambiguous. 58 struct S4 { 59 enum E : int { a = 1 }; // ok, defines an enum 60 }; 61 // This could be a bit-field, but would be ill-formed due to the anonymous 62 // member being initialized. 63 struct S5 { 64 enum E : int { a = 1 } { b = 2 }; // expected-error {{expected ';' after enum}} expected-error {{expected member name}} 65 }; 66 // This could be a bit-field. 67 struct S6 { 68 enum E : int { 1 }; // expected-error {{expected identifier}} 69 }; 70 71 struct U { operator Tbitfield::U72 constexpr operator T() const { return T(); } // expected-note 2{{candidate}} 73 }; 74 // This could be a bit-field. 75 struct S7 { 76 enum E : int { a = U() }; // expected-error {{no viable conversion}} 77 }; 78 // This could be a bit-field, and does not conform to the grammar of an 79 // enum definition, because 'id(U())' is not a constant-expression. id(const U & u)80 constexpr const U &id(const U &u) { return u; } 81 struct S8 { 82 enum E : int { a = id(U()) }; // expected-error {{no viable conversion}} 83 }; 84 85 // PR26249: Disambiguate 'enum :' as an enum-base always, even if that would 86 // be ill-formed. It cannot be an elaborated-type-specifier. 87 struct S { 88 enum : undeclared_type { v = 0 }; // expected-error {{unknown type name 'undeclared_type'}} 89 enum E : undeclared_type { w = 0 }; // expected-error {{unknown type name 'undeclared_type'}} 90 enum X : undeclared_type { x = 0 }; // expected-error {{unknown type name 'undeclared_type'}} 91 }; 92 } 93 94 namespace trailing_return { 95 typedef int n; 96 int a; 97 98 struct S { 99 S(int); 100 S *operator()(...) const; 101 int n; 102 }; 103 104 namespace N { f()105 void f() { 106 // This parses as a function declaration, but DR1223 makes the presence of 107 // 'auto' be used for disambiguation. 108 S(a)()->n; // ok, expression; expected-warning{{expression result unused}} 109 S(a)(int())->n; // ok, expression; expected-warning{{expression result unused}} 110 auto(a)()->n; // ok, function declaration 111 auto(b)(int())->n; // ok, function declaration 112 using T = decltype(a); 113 using T = auto() -> n; 114 } 115 } 116 } 117 118 namespace ellipsis { 119 template<typename...T> 120 struct S { 121 void e(S::S()); // expected-error {{is a constructor name}} 122 void f(S(...args[sizeof(T)])); // expected-note {{here}} expected-note {{here}} 123 void f(S(...args)[sizeof(T)]); // expected-error {{redeclared}} 124 void f(S ...args[sizeof(T)]); // expected-error {{redeclared}} 125 void g(S(...[sizeof(T)])); // expected-note {{here}} expected-warning {{ISO C++11 requires a parenthesized pack declaration to have a name}} 126 void g(S(...)[sizeof(T)]); // expected-error {{function cannot return array type}} 127 void g(S ...[sizeof(T)]); // expected-error {{redeclared}} 128 void h(T(...)); // function type, expected-error {{unexpanded parameter pack}} 129 void h(T...); // pack expansion, ok 130 void i(int(T...)); // expected-note {{here}} 131 void i(int(T...a)); // expected-error {{redeclared}} 132 void i(int(T, ...)); // function type, expected-error {{unexpanded parameter pack}} 133 void i(int(T, ...a)); // expected-error {{expected ')'}} expected-note {{to match}} expected-error {{unexpanded parameter pack}} 134 void j(int(int...)); // function type, ok 135 void j(int(int...a)); // expected-error {{does not contain any unexpanded parameter packs}} 136 void j(T(int...)); // expected-error {{unexpanded parameter pack}} 137 void j(T(T...)); // expected-error {{unexpanded parameter pack}} 138 void k(int(...)(T)); // expected-error {{cannot return function type}} 139 void k(int ...(T)); 140 void l(int(&...)(T)); // expected-warning {{ISO C++11 requires a parenthesized pack declaration to have a name}} 141 void l(int(*...)(T)); // expected-warning {{ISO C++11 requires a parenthesized pack declaration to have a name}} 142 void l(int(S<int>::*...)(T)); // expected-warning {{ISO C++11 requires a parenthesized pack declaration to have a name}} 143 }; 144 145 struct CtorSink { CtorSinkellipsis::CtorSink146 template <typename ...T> constexpr CtorSink(T &&...t) { } operator intellipsis::CtorSink147 constexpr operator int() const { return 42; } 148 }; 149 150 template <unsigned ...N> struct UnsignedTmplArgSink; 151 152 template <typename ...T> foo(int x,T...t)153 void foo(int x, T ...t) { 154 // Have a variety of cases where the syntax is technically unambiguous, but hinges on careful treatment of ellipses. 155 CtorSink(t ...), x; // ok, expression; expected-warning 2{{expression result unused}} 156 157 int x0(CtorSink(t ...)); // ok, declares object x0 158 int *p0 = &x0; 159 (void)p0; 160 161 CtorSink x1(int(t) ..., int(x)); // ok, declares object x1 162 CtorSink *p1 = &x1; 163 (void)p1; 164 165 UnsignedTmplArgSink<T(CtorSink(t ...)) ...> *t0; // ok 166 UnsignedTmplArgSink<((T *)0, 42u) ...> **t0p = &t0; 167 } 168 169 template void foo(int, int, int); // expected-note {{in instantiation of function template specialization 'ellipsis::foo<int, int>' requested here}} 170 } 171 172 namespace braced_init_list { 173 struct X { foobraced_init_list::X174 void foo() {} 175 }; 176 177 void (*pf1)() {}; 178 void (X::*pmf1)() {&X::foo}; 179 void (X::*pmf2)() = {&X::foo}; 180 test()181 void test() { 182 void (*pf2)() {}; 183 void (X::*pmf3)() {&X::foo}; 184 void (X::*pmf4)() = {&X::foo}; 185 } 186 } 187