p14.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. // RUN: %clang_cc1 -fcxx-exceptions -fexceptions -verify -std=c++11 %s
  2. struct A { };
  3. struct B { };
  4. struct C { };
  5. // Destructor
  6. struct X0 {
  7. virtual ~X0() throw(A); // expected-note{{overridden virtual function is here}}
  8. };
  9. struct X1 {
  10. virtual ~X1() throw(B); // expected-note{{overridden virtual function is here}}
  11. };
  12. struct X2 : public X0, public X1 { }; // expected-error 2{{exception specification of overriding function is more lax than base version}}
  13. // Copy-assignment operator.
  14. struct CA0 {
  15. CA0 &operator=(const CA0&) throw(A);
  16. };
  17. struct CA1 {
  18. CA1 &operator=(const CA1&) throw(B);
  19. };
  20. struct CA2 : CA0, CA1 { };
  21. void test_CA() {
  22. CA2 &(CA2::*captr1)(const CA2&) throw(A, B) = &CA2::operator=;
  23. CA2 &(CA2::*captr2)(const CA2&) throw(A, B, C) = &CA2::operator=;
  24. CA2 &(CA2::*captr3)(const CA2&) throw(A) = &CA2::operator=; // expected-error{{target exception specification is not superset of source}}
  25. CA2 &(CA2::*captr4)(const CA2&) throw(B) = &CA2::operator=; // expected-error{{target exception specification is not superset of source}}
  26. }
  27. // In-class member initializers.
  28. struct IC0 {
  29. int inClassInit = 0;
  30. };
  31. struct IC1 {
  32. int inClassInit = (throw B(), 0);
  33. };
  34. // FIXME: the exception specification on the default constructor is wrong:
  35. // we cannot currently compute the set of thrown types.
  36. static_assert(noexcept(IC0()), "IC0() does not throw");
  37. static_assert(!noexcept(IC1()), "IC1() throws");
  38. namespace PR13381 {
  39. struct NoThrowMove {
  40. NoThrowMove(const NoThrowMove &);
  41. NoThrowMove(NoThrowMove &&) noexcept;
  42. NoThrowMove &operator=(const NoThrowMove &) const;
  43. NoThrowMove &operator=(NoThrowMove &&) const noexcept;
  44. };
  45. struct NoThrowMoveOnly {
  46. NoThrowMoveOnly(NoThrowMoveOnly &&) noexcept;
  47. NoThrowMoveOnly &operator=(NoThrowMoveOnly &&) noexcept;
  48. };
  49. struct X {
  50. const NoThrowMove a;
  51. NoThrowMoveOnly b;
  52. static X val();
  53. static X &ref();
  54. };
  55. // These both perform a move, but that copy might throw, because it calls
  56. // NoThrowMove's copy constructor (because PR13381::a is const).
  57. static_assert(!noexcept(X(X::val())), "");
  58. static_assert(!noexcept(X::ref() = X::val()), "");
  59. }
  60. namespace PR14141 {
  61. // Part of DR1351: the implicit exception-specification is noexcept(false) if
  62. // the set of potential exceptions of the special member function contains
  63. // "any". Hence it is compatible with noexcept(false).
  64. struct ThrowingBase {
  65. ThrowingBase() noexcept(false);
  66. ThrowingBase(const ThrowingBase&) noexcept(false);
  67. ThrowingBase(ThrowingBase&&) noexcept(false);
  68. ThrowingBase &operator=(const ThrowingBase&) noexcept(false);
  69. ThrowingBase &operator=(ThrowingBase&&) noexcept(false);
  70. ~ThrowingBase() noexcept(false);
  71. };
  72. struct Derived : ThrowingBase {
  73. Derived() noexcept(false) = default;
  74. Derived(const Derived&) noexcept(false) = default;
  75. Derived(Derived&&) noexcept(false) = default;
  76. Derived &operator=(const Derived&) noexcept(false) = default;
  77. Derived &operator=(Derived&&) noexcept(false) = default;
  78. ~Derived() noexcept(false) = default;
  79. };
  80. struct Derived2 : ThrowingBase {
  81. Derived2() = default;
  82. Derived2(const Derived2&) = default;
  83. Derived2(Derived2&&) = default;
  84. Derived2 &operator=(const Derived2&) = default;
  85. Derived2 &operator=(Derived2&&) = default;
  86. ~Derived2() = default;
  87. };
  88. struct Derived3 : ThrowingBase {
  89. Derived3() noexcept(true) = default; // expected-error {{does not match the calculated}}
  90. Derived3(const Derived3&) noexcept(true) = default; // expected-error {{does not match the calculated}}
  91. Derived3(Derived3&&) noexcept(true) = default; // expected-error {{does not match the calculated}}
  92. Derived3 &operator=(const Derived3&) noexcept(true) = default; // expected-error {{does not match the calculated}}
  93. Derived3 &operator=(Derived3&&) noexcept(true) = default; // expected-error {{does not match the calculated}}
  94. ~Derived3() noexcept(true) = default; // expected-error {{does not match the calculated}}
  95. };
  96. }
  97. namespace rdar13017229 {
  98. struct Base {
  99. virtual ~Base() {}
  100. };
  101. struct Derived : Base {
  102. virtual ~Derived();
  103. Typo foo(); // expected-error{{unknown type name 'Typo'}}
  104. };
  105. }
  106. namespace InhCtor {
  107. template<int> struct X {};
  108. struct Base {
  109. Base(X<0>) noexcept(true);
  110. Base(X<1>) noexcept(false);
  111. Base(X<2>) throw(X<2>);
  112. template<typename T> Base(T) throw(T);
  113. };
  114. template<typename T> struct Throw {
  115. Throw() throw(T);
  116. };
  117. struct Derived : Base, Throw<X<3>> {
  118. using Base::Base;
  119. Throw<X<4>> x;
  120. };
  121. struct Test {
  122. friend Derived::Derived(X<0>) throw(X<3>, X<4>);
  123. friend Derived::Derived(X<1>) noexcept(false);
  124. friend Derived::Derived(X<2>) throw(X<2>, X<3>, X<4>);
  125. };
  126. static_assert(!noexcept(Derived{X<5>{}}), "");
  127. }