186 lines · cpp
1// RUN: %clang_cc1 -fsyntax-only -verify -std=c++11 %s2 3// New exciting ambiguities in C++114 5// final 'context sensitive' mess.6namespace 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 successfully18 // parse as a variable but not as a class. DR1318's wording suggests that19 // this disambiguation is only performed on an ambiguity, but that was not20 // 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 to29 // 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 an34// enum-base, but we disambiguate anyway for better error recovery.35namespace bitfield {36 enum E {};37 38 struct T {39 constexpr T() {}40 constexpr T(int) {}41 constexpr T(T, T, T, T) {}42 constexpr T operator=(T) const { return *this; }43 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 enum56 };57 // Ambiguous.58 struct S4 {59 enum E : int { a = 1 }; // ok, defines an enum60 };61 // This could be a bit-field, but would be ill-formed due to the anonymous62 // 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 {72 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 an79 // enum definition, because 'id(U())' is not a constant-expression.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 would86 // 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 94namespace 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 {105 void f() {106 // This parses as a function declaration, but DR1223 makes the presence of107 // '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 declaration111 auto(b)(int())->n; // ok, function declaration112 using T = decltype(a);113 using T = auto() -> n;114 }115 }116}117 118namespace 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-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 h(T(...)); // function type, expected-error {{unexpanded parameter pack}}128 void h(T...); // pack expansion, ok129 void i(int(T...)); // expected-note {{here}}130 void i(int(T...a)); // expected-error {{redeclared}}131 void i(int(T, ...)); // function type, expected-error {{unexpanded parameter pack}}132 void i(int(T, ...a)); // expected-error {{expected ')'}} expected-note {{to match}} expected-error {{unexpanded parameter pack}}133 void j(int(int...)); // function type, ok134 void j(int(int...a)); // expected-error {{does not contain any unexpanded parameter packs}}135 void j(T(int...)); // expected-error {{unexpanded parameter pack}}136 void j(T(T...)); // expected-error {{unexpanded parameter pack}}137 void k(int(...)(T)); // expected-error {{cannot return function type}}138 void k(int ...(T));139 void l(int(&...)(T)); // expected-warning {{ISO C++11 requires a parenthesized pack declaration to have a name}}140 void l(int(*...)(T)); // expected-warning {{ISO C++11 requires a parenthesized pack declaration to have a name}}141 void l(int(S<int>::*...)(T)); // expected-warning {{ISO C++11 requires a parenthesized pack declaration to have a name}}142 };143 144 struct CtorSink {145 template <typename ...T> constexpr CtorSink(T &&...t) { }146 constexpr operator int() const { return 42; }147 };148 149 template <unsigned ...N> struct UnsignedTmplArgSink;150 151 template <typename ...T>152 void foo(int x, T ...t) {153 // Have a variety of cases where the syntax is technically unambiguous, but hinges on careful treatment of ellipses.154 CtorSink(t ...), x; // ok, expression; expected-warning 2{{expression result unused}}155 156 int x0(CtorSink(t ...)); // ok, declares object x0157 int *p0 = &x0;158 (void)p0;159 160 CtorSink x1(int(t) ..., int(x)); // ok, declares object x1161 CtorSink *p1 = &x1;162 (void)p1;163 164 UnsignedTmplArgSink<T(CtorSink(t ...)) ...> *t0; // ok165 UnsignedTmplArgSink<((T *)0, 42u) ...> **t0p = &t0; // expected-warning 2{{left operand of comma operator has no effect}}166 }167 168 template void foo(int, int, int); // expected-note {{in instantiation of function template specialization 'ellipsis::foo<int, int>' requested here}}169}170 171namespace braced_init_list {172 struct X {173 void foo() {}174 };175 176 void (*pf1)() {};177 void (X::*pmf1)() {&X::foo};178 void (X::*pmf2)() = {&X::foo};179 180 void test() {181 void (*pf2)() {};182 void (X::*pmf3)() {&X::foo};183 void (X::*pmf4)() = {&X::foo};184 }185}186