cxx0x-ambig.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. // RUN: %clang_cc1 -fsyntax-only -verify -std=c++11 %s
  2. // New exciting ambiguities in C++11
  3. // final 'context sensitive' mess.
  4. namespace final {
  5. struct S { int n; };
  6. struct T { int n; };
  7. namespace N {
  8. int n;
  9. // These declare variables named final..
  10. extern struct S final;
  11. extern struct S final [[]];
  12. extern struct S final, foo;
  13. struct S final = S();
  14. // This defines a class, not a variable, even though it would successfully
  15. // parse as a variable but not as a class. DR1318's wording suggests that
  16. // this disambiguation is only performed on an ambiguity, but that was not
  17. // the intent.
  18. struct S final { // expected-note {{here}}
  19. int(n) // expected-error {{expected ';'}}
  20. };
  21. // This too.
  22. struct T final : S {}; // expected-error {{base 'S' is marked 'final'}}
  23. struct T bar : S {}; // expected-error {{expected ';' after top level declarator}} expected-error {{expected unqualified-id}}
  24. }
  25. // _Alignas isn't allowed in the places where alignas is. We used to
  26. // assert on this.
  27. struct U final _Alignas(4) {}; // expected-error 3{{}} expected-note {{}}
  28. }
  29. // enum versus bitfield mess.
  30. namespace bitfield {
  31. enum E {};
  32. struct T {
  33. constexpr T() {}
  34. constexpr T(int) {}
  35. constexpr T(T, T, T, T) {}
  36. constexpr T operator=(T) const { return *this; }
  37. constexpr operator int() const { return 4; }
  38. };
  39. constexpr T a, b, c, d;
  40. struct S1 {
  41. enum E : T ( a = 1, b = 2, c = 3, 4 ); // ok, declares a bitfield
  42. };
  43. // This could be a bit-field.
  44. struct S2 {
  45. enum E : T { a = 1, b = 2, c = 3, 4 }; // expected-error {{non-integral type}} expected-error {{expected identifier}}
  46. };
  47. struct S3 {
  48. enum E : int { a = 1, b = 2, c = 3, d }; // ok, defines an enum
  49. };
  50. // Ambiguous.
  51. struct S4 {
  52. enum E : int { a = 1 }; // ok, defines an enum
  53. };
  54. // This could be a bit-field, but would be ill-formed due to the anonymous
  55. // member being initialized.
  56. struct S5 {
  57. enum E : int { a = 1 } { b = 2 }; // expected-error {{expected ';' after enum}} expected-error {{expected member name}}
  58. };
  59. // This could be a bit-field.
  60. struct S6 {
  61. enum E : int { 1 }; // expected-error {{expected identifier}}
  62. };
  63. struct U {
  64. constexpr operator T() const { return T(); } // expected-note 2{{candidate}}
  65. };
  66. // This could be a bit-field.
  67. struct S7 {
  68. enum E : int { a = U() }; // expected-error {{no viable conversion}}
  69. };
  70. // This could be a bit-field, and does not conform to the grammar of an
  71. // enum definition, because 'id(U())' is not a constant-expression.
  72. constexpr const U &id(const U &u) { return u; }
  73. struct S8 {
  74. enum E : int { a = id(U()) }; // expected-error {{no viable conversion}}
  75. };
  76. }
  77. namespace trailing_return {
  78. typedef int n;
  79. int a;
  80. struct S {
  81. S(int);
  82. S *operator()(...) const;
  83. int n;
  84. };
  85. namespace N {
  86. void f() {
  87. // This parses as a function declaration, but DR1223 makes the presence of
  88. // 'auto' be used for disambiguation.
  89. S(a)()->n; // ok, expression; expected-warning{{expression result unused}}
  90. S(a)(int())->n; // ok, expression; expected-warning{{expression result unused}}
  91. auto(a)()->n; // ok, function declaration
  92. auto(b)(int())->n; // ok, function declaration
  93. using T = decltype(a);
  94. using T = auto() -> n;
  95. }
  96. }
  97. }
  98. namespace ellipsis {
  99. template<typename...T>
  100. struct S {
  101. void e(S::S());
  102. void f(S(...args[sizeof(T)])); // expected-note {{here}} expected-note {{here}}
  103. void f(S(...args)[sizeof(T)]); // expected-error {{redeclared}}
  104. void f(S ...args[sizeof(T)]); // expected-error {{redeclared}}
  105. void g(S(...[sizeof(T)])); // expected-note {{here}} expected-warning {{ISO C++11 requires a parenthesized pack declaration to have a name}}
  106. void g(S(...)[sizeof(T)]); // expected-error {{function cannot return array type}}
  107. void g(S ...[sizeof(T)]); // expected-error {{redeclared}}
  108. void h(T(...)); // function type, expected-error {{unexpanded parameter pack}}
  109. void h(T...); // pack expansion, ok
  110. void i(int(T...)); // expected-note {{here}}
  111. void i(int(T...a)); // expected-error {{redeclared}}
  112. void i(int(T, ...)); // function type, expected-error {{unexpanded parameter pack}}
  113. void i(int(T, ...a)); // expected-error {{expected ')'}} expected-note {{to match}} expected-error {{unexpanded parameter pack}}
  114. void j(int(int...)); // function type, ok
  115. void j(int(int...a)); // expected-error {{does not contain any unexpanded parameter packs}}
  116. void j(T(int...)); // expected-error {{unexpanded parameter pack}}
  117. void j(T(T...)); // expected-error {{unexpanded parameter pack}}
  118. void k(int(...)(T)); // expected-error {{cannot return function type}}
  119. void k(int ...(T));
  120. void l(int(&...)(T)); // expected-warning {{ISO C++11 requires a parenthesized pack declaration to have a name}}
  121. void l(int(*...)(T)); // expected-warning {{ISO C++11 requires a parenthesized pack declaration to have a name}}
  122. void l(int(S<int>::*...)(T)); // expected-warning {{ISO C++11 requires a parenthesized pack declaration to have a name}}
  123. };
  124. }
  125. namespace braced_init_list {
  126. struct X {
  127. void foo() {}
  128. };
  129. void (*pf1)() {};
  130. void (X::*pmf1)() {&X::foo};
  131. void (X::*pmf2)() = {&X::foo};
  132. void test() {
  133. void (*pf2)() {};
  134. void (X::*pmf3)() {&X::foo};
  135. void (X::*pmf4)() = {&X::foo};
  136. }
  137. }