callback_unittest.nc 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. // Copyright (c) 2011 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. // This is a "No Compile Test" suite.
  5. // http://dev.chromium.org/developers/testing/no-compile-tests
  6. #include "base/callback.h"
  7. namespace base {
  8. class Parent {
  9. };
  10. class Child : Parent {
  11. };
  12. #if defined(NCTEST_EQUALS_REQUIRES_SAMETYPE) // [r"fatal error: invalid operands to binary expression \('RepeatingCallback<void \(\)>' and 'RepeatingCallback<int \(\)>'\)"]
  13. // Attempting to call comparison function on two callbacks of different type.
  14. //
  15. // This should be a compile time failure because each callback type should be
  16. // considered distinct.
  17. void WontCompile() {
  18. RepeatingCallback<void()> c1;
  19. RepeatingCallback<int()> c2;
  20. c1 == c2;
  21. }
  22. #elif defined(NCTEST_CONSTRUCTION_FROM_SUBTYPE) // [r"fatal error: no viable conversion from 'RepeatingCallback<Parent \(\)>' to 'RepeatingCallback<Child \(\)>'"]
  23. // Construction of RepeatingCallback<A> from RepeatingCallback<B> if A is
  24. // supertype of B.
  25. //
  26. // While this is technically safe, most people aren't used to it when coding
  27. // C++ so if this is happening, it is almost certainly an error.
  28. void WontCompile() {
  29. RepeatingCallback<Parent()> cb_a;
  30. RepeatingCallback<Child()> cb_b = cb_a;
  31. }
  32. #elif defined(NCTEST_ASSIGNMENT_FROM_SUBTYPE) // [r"fatal error: no viable overloaded '='"]
  33. // Assignment of RepeatingCallback<A> from RepeatingCallback<B> if A is
  34. // supertype of B. See explanation for NCTEST_CONSTRUCTION_FROM_SUBTYPE.
  35. void WontCompile() {
  36. RepeatingCallback<Parent()> cb_a;
  37. RepeatingCallback<Child()> cb_b;
  38. cb_a = cb_b;
  39. }
  40. #elif defined(NCTEST_ONCE_THEN_MISMATCH) // [r"static assertion failed due to requirement '.+': \|then\| callback's parameter must be constructible from return type of \|this\|\."]
  41. // Calling Then() with a callback that can't receive the original
  42. // callback's return type. Here we would pass `int*` to `float*`.
  43. void WontCompile() {
  44. OnceCallback<int*()> original;
  45. OnceCallback<void(float*)> then;
  46. std::move(original).Then(std::move(then));
  47. }
  48. #elif defined(NCTEST_ONCE_THEN_MISMATCH_VOID_RESULT) // [r"fatal error: static assertion failed due to requirement '.+': \|then\| callback cannot accept parameters if \|this\| has a void return type\."]
  49. // Calling Then() with a callback that can't receive the original
  50. // callback's return type. Here we would pass `void` to `float`.
  51. void WontCompile() {
  52. OnceCallback<void()> original;
  53. OnceCallback<void(float)> then;
  54. std::move(original).Then(std::move(then));
  55. }
  56. #elif defined(NCTEST_ONCE_THEN_MISMATCH_VOID_PARAM) // [r"fatal error: static assertion failed due to requirement '.+': \|then\| callback must accept exactly one parameter if \|this\| has a non-void return type\."]
  57. // Calling Then() with a callback that can't receive the original
  58. // callback's return type. Here we would pass `int` to `void`.
  59. void WontCompile() {
  60. OnceCallback<int()> original;
  61. OnceCallback<void()> then;
  62. std::move(original).Then(std::move(then));
  63. }
  64. #elif defined(NCTEST_REPEATINGRVALUE_THEN_MISMATCH) // [r"static assertion failed due to requirement '.+': \|then\| callback's parameter must be constructible from return type of \|this\|\."]
  65. // Calling Then() with a callback that can't receive the original
  66. // callback's return type. Here we would pass `int*` to `float*`.
  67. void WontCompile() {
  68. RepeatingCallback<int*()> original;
  69. RepeatingCallback<void(float*)> then;
  70. std::move(original).Then(std::move(then));
  71. }
  72. #elif defined(NCTEST_REPEATINGRVALUE_THEN_MISMATCH_VOID_RESULT) // [r"fatal error: static assertion failed due to requirement '.+': \|then\| callback cannot accept parameters if \|this\| has a void return type\."]
  73. // Calling Then() with a callback that can't receive the original
  74. // callback's return type. Here we would pass `void` to `float`.
  75. void WontCompile() {
  76. RepeatingCallback<void()> original;
  77. RepeatingCallback<void(float)> then;
  78. std::move(original).Then(std::move(then));
  79. }
  80. #elif defined(NCTEST_REPEATINGRVALUE_THEN_MISMATCH_VOID_PARAM) // [r"fatal error: static assertion failed due to requirement '.+': \|then\| callback must accept exactly one parameter if \|this\| has a non-void return type\."]
  81. // Calling Then() with a callback that can't receive the original
  82. // callback's return type. Here we would pass `int` to `void`.
  83. void WontCompile() {
  84. RepeatingCallback<int()> original;
  85. RepeatingCallback<void()> then;
  86. std::move(original).Then(std::move(then));
  87. }
  88. #elif defined(NCTEST_REPEATINGLVALUE_THEN_MISMATCH) // [r"static assertion failed due to requirement '.+': \|then\| callback's parameter must be constructible from return type of \|this\|\."]
  89. // Calling Then() with a callback that can't receive the original
  90. // callback's return type. Here we would pass `int*` to `float*`.
  91. void WontCompile() {
  92. RepeatingCallback<int*()> original;
  93. RepeatingCallback<void(float*)> then;
  94. original.Then(then);
  95. }
  96. #elif defined(NCTEST_REPEATINGLVALUE_THEN_MISMATCH_VOID_RESULT) // [r"fatal error: static assertion failed due to requirement '.+': \|then\| callback cannot accept parameters if \|this\| has a void return type\."]
  97. // Calling Then() with a callback that can't receive the original
  98. // callback's return type. Here we would pass `void` to `float`.
  99. void WontCompile() {
  100. RepeatingCallback<void()> original;
  101. RepeatingCallback<void(float)> then;
  102. original.Then(then);
  103. }
  104. #elif defined(NCTEST_REPEATINGLVALUE_THEN_MISMATCH_VOID_PARAM) // [r"fatal error: static assertion failed due to requirement '.+': \|then\| callback must accept exactly one parameter if \|this\| has a non-void return type\."]
  105. // Calling Then() with a callback that can't receive the original
  106. // callback's return type. Here we would pass `int` to `void`.
  107. void WontCompile() {
  108. RepeatingCallback<int()> original;
  109. RepeatingCallback<void()> then;
  110. original.Then(then);
  111. }
  112. #endif
  113. } // namespace base