bind_unittest.nc 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  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 <utility>
  7. #include "base/bind.h"
  8. #include "base/callback.h"
  9. #include "base/memory/ref_counted.h"
  10. #include "base/memory/raw_ptr.h"
  11. #include "base/memory/raw_ref.h"
  12. #include "base/test/bind.h"
  13. namespace base {
  14. // Do not put everything inside an anonymous namespace. If you do, many of the
  15. // helper function declarations will generate unused definition warnings.
  16. static const int kParentValue = 1;
  17. static const int kChildValue = 2;
  18. class NoRef {
  19. public:
  20. void VoidMethod0() {}
  21. void VoidConstMethod0() const {}
  22. int IntMethod0() { return 1; }
  23. };
  24. class HasRef : public NoRef, public base::RefCounted<HasRef> {
  25. };
  26. class Parent {
  27. public:
  28. void AddRef() const {}
  29. void Release() const {}
  30. virtual void VirtualSet() { value = kParentValue; }
  31. void NonVirtualSet() { value = kParentValue; }
  32. int value;
  33. };
  34. class Child : public Parent {
  35. public:
  36. virtual void VirtualSet() { value = kChildValue; }
  37. void NonVirtualSet() { value = kChildValue; }
  38. };
  39. class NoRefParent {
  40. public:
  41. virtual void VirtualSet() { value = kParentValue; }
  42. void NonVirtualSet() { value = kParentValue; }
  43. int value;
  44. };
  45. class NoRefChild : public NoRefParent {
  46. virtual void VirtualSet() { value = kChildValue; }
  47. void NonVirtualSet() { value = kChildValue; }
  48. };
  49. template <typename T>
  50. T PolymorphicIdentity(T t) {
  51. return t;
  52. }
  53. int UnwrapParentRef(Parent& p) {
  54. return p.value;
  55. }
  56. template <typename T>
  57. void VoidPolymorphic1(T t) {
  58. }
  59. void TakesMoveOnly(std::unique_ptr<int>) {
  60. }
  61. void TakesIntRef(int& ref) {}
  62. struct NonEmptyFunctor {
  63. int x;
  64. void operator()() const {}
  65. };
  66. #if defined(NCTEST_METHOD_ON_CONST_OBJECT) // [r"static assertion failed.+?BindArgument<0>::ForwardedAs<.+?>::ToParamWithType<.+?>::kCanBeForwardedToBoundFunctor.+?Type mismatch between bound argument and bound functor's parameter\."]
  67. // Method bound to const-object.
  68. //
  69. // Only const methods should be allowed to work with const objects.
  70. void WontCompile() {
  71. HasRef has_ref;
  72. const HasRef* const_has_ref_ptr_ = &has_ref;
  73. RepeatingCallback<void()> method_to_const_cb =
  74. BindRepeating(&HasRef::VoidMethod0, const_has_ref_ptr_);
  75. method_to_const_cb.Run();
  76. }
  77. #elif defined(NCTEST_METHOD_BIND_NEEDS_REFCOUNTED_OBJECT) // [r"fatal error: static assertion failed due to requirement '!IsPointerV<base::NoRef \*> \|\| IsRefCountedType<base::NoRef, void>::value': Receivers may not be raw pointers. If using a raw pointer here is safe and has no lifetime concerns, use base::Unretained\(\) and document why it's safe."]
  78. // Method bound to non-refcounted object.
  79. //
  80. // We require refcounts unless you have Unretained().
  81. void WontCompile() {
  82. NoRef no_ref;
  83. RepeatingCallback<void()> no_ref_cb =
  84. BindRepeating(&NoRef::VoidMethod0, &no_ref);
  85. no_ref_cb.Run();
  86. }
  87. #elif defined(NCTEST_CONST_METHOD_BIND_NEEDS_REFCOUNTED_OBJECT) // [r"fatal error: static assertion failed due to requirement '!IsPointerV<base::NoRef \*> \|\| IsRefCountedType<base::NoRef, void>::value': Receivers may not be raw pointers. If using a raw pointer here is safe and has no lifetime concerns, use base::Unretained\(\) and document why it's safe."]
  88. // Const Method bound to non-refcounted object.
  89. //
  90. // We require refcounts unless you have Unretained().
  91. void WontCompile() {
  92. NoRef no_ref;
  93. RepeatingCallback<void()> no_ref_const_cb =
  94. BindRepeating(&NoRef::VoidConstMethod0, &no_ref);
  95. no_ref_const_cb.Run();
  96. }
  97. #elif defined(NCTEST_METHOD_BIND_RAW_PTR_RECEIVER_NEEDS_REFCOUNTED_OBJECT) // [r"fatal error: static assertion failed due to requirement '!IsPointerV<base::raw_ptr<base::NoRef, [^>]+>?>> \|\| IsRefCountedType<base::NoRef, void>::value': Receivers may not be raw pointers. If using a raw pointer here is safe and has no lifetime concerns, use base::Unretained\(\) and document why it's safe."]
  98. // Method bound to non-refcounted object.
  99. //
  100. // We require refcounts unless you have Unretained().
  101. void WontCompile() {
  102. NoRef no_ref;
  103. raw_ptr<NoRef> rawptr(&no_ref);
  104. RepeatingCallback<void()> no_ref_cb =
  105. BindRepeating(&NoRef::VoidMethod0, rawptr);
  106. no_ref_cb.Run();
  107. }
  108. #elif defined(NCTEST_CONST_METHOD_BIND_RAW_PTR_RECEIVER_NEEDS_REFCOUNTED_OBJECT) // [r"fatal error: static assertion failed due to requirement '!IsPointerV<base::raw_ptr<base::NoRef, [^>]+>?>> \|\| IsRefCountedType<base::NoRef, void>::value': Receivers may not be raw pointers. If using a raw pointer here is safe and has no lifetime concerns, use base::Unretained\(\) and document why it's safe."]
  109. // Const Method bound to non-refcounted object.
  110. //
  111. // We require refcounts unless you have Unretained().
  112. void WontCompile() {
  113. NoRef no_ref;
  114. raw_ptr<NoRef> rawptr(&no_ref);
  115. RepeatingCallback<void()> no_ref_const_cb =
  116. BindRepeating(&NoRef::VoidConstMethod0, rawptr);
  117. no_ref_const_cb.Run();
  118. }
  119. #elif defined(NCTEST_METHOD_BIND_REF_WRAPPER_RECEIVER_NON_REFCOUNTED_OBJECT) // [r"fatal error: indirection requires pointer operand"]
  120. // Method bound to non-refcounted object. It fails to compile with
  121. // std::reference_wrapper.
  122. void WontCompile() {
  123. NoRef no_ref;
  124. RepeatingCallback<void()> no_ref_cb =
  125. BindRepeating(&NoRef::VoidMethod0, std::cref(no_ref));
  126. no_ref_cb.Run();
  127. }
  128. #elif defined(NCTEST_METHOD_BIND_NATIVE_REF_RECEIVER_NON_REFCOUNTED_OBJECT) // [r"fatal error: indirection requires pointer operand"]
  129. // Method bound to non-refcounted object. It fails to compile with
  130. // a native reference.
  131. void WontCompile() {
  132. NoRef no_ref;
  133. RepeatingCallback<void()> no_ref_cb =
  134. BindRepeating(&NoRef::VoidMethod0, no_ref);
  135. no_ref_cb.Run();
  136. }
  137. #elif defined(NCTEST_METHOD_BIND_RAW_REF_RECEIVER_NON_REFCOUNTED_OBJECT) // [r"fatal error: .*Receivers may not be raw_ref<T>\."]
  138. // Method bound to non-refcounted object. It fails to compile with
  139. // a raw_ref.
  140. void WontCompile() {
  141. NoRef no_ref;
  142. raw_ref<NoRef> rawref(no_ref);
  143. RepeatingCallback<void()> no_ref_cb =
  144. BindRepeating(&NoRef::VoidMethod0, rawref);
  145. no_ref_cb.Run();
  146. }
  147. #elif defined(NCTEST_METHOD_BIND_REF_WRAPPER_RECEIVER_REFCOUNTED_OBJECT) // [r"fatal error: indirection requires pointer operand"]
  148. // Method bound to non-refcounted object. It fails to compile with
  149. // std::reference_wrapper.
  150. void WontCompile() {
  151. HasRef has_ref;
  152. RepeatingCallback<void()> has_ref_cb =
  153. BindRepeating(&HasRef::VoidMethod0, std::cref(has_ref));
  154. has_ref_cb.Run();
  155. }
  156. #elif defined(NCTEST_METHOD_BIND_NATIVE_REF_RECEIVER_REFCOUNTED_OBJECT) // [r"fatal error: indirection requires pointer operand"]
  157. // Method bound to non-refcounted object. It fails to compile with
  158. // a native reference.
  159. void WontCompile() {
  160. HasRef has_ref;
  161. RepeatingCallback<void()> has_ref_cb =
  162. BindRepeating(&HasRef::VoidMethod0, has_ref);
  163. has_ref_cb.Run();
  164. }
  165. #elif defined(NCTEST_METHOD_BIND_RAW_REF_RECEIVER_REFCOUNTED_OBJECT) // [r"fatal error: .*Receivers may not be raw_ref<T>\."]
  166. // Method bound to non-refcounted object. It fails to compile with
  167. // a raw_ref.
  168. void WontCompile() {
  169. HasRef has_ref;
  170. raw_ref<HasRef> rawref(has_ref);
  171. RepeatingCallback<void()> has_ref_cb =
  172. BindRepeating(&HasRef::VoidMethod0, rawref);
  173. has_ref_cb.Run();
  174. }
  175. #elif defined(NCTEST_CONST_POINTER) // [r"static assertion failed.+?BindArgument<0>::ForwardedAs<.+?>::ToParamWithType<.+?>::kCanBeForwardedToBoundFunctor.+?Type mismatch between bound argument and bound functor's parameter\."]
  176. // Const argument used with non-const pointer parameter of same type.
  177. //
  178. // This is just a const-correctness check.
  179. void WontCompile() {
  180. const NoRef* const_no_ref_ptr;
  181. RepeatingCallback<NoRef*()> pointer_same_cb =
  182. BindRepeating(&PolymorphicIdentity<NoRef*>, const_no_ref_ptr);
  183. pointer_same_cb.Run();
  184. }
  185. #elif defined(NCTEST_CONST_POINTER_SUBTYPE) // [r"static assertion failed.+?BindArgument<0>::ForwardedAs<.+?>::ToParamWithType<.+?>::kCanBeForwardedToBoundFunctor.+?Type mismatch between bound argument and bound functor's parameter\."]
  186. // Const argument used with non-const pointer parameter of super type.
  187. //
  188. // This is just a const-correctness check.
  189. void WontCompile() {
  190. const NoRefChild* const_child_ptr;
  191. RepeatingCallback<NoRefParent*()> pointer_super_cb =
  192. BindRepeating(&PolymorphicIdentity<NoRefParent*>, const_child_ptr);
  193. pointer_super_cb.Run();
  194. }
  195. #elif defined(DISABLED_NCTEST_DISALLOW_NON_CONST_REF_PARAM) // [r"fatal error: no member named 'AddRef' in 'base::NoRef'"]
  196. // TODO(dcheng): I think there's a type safety promotion issue here where we can
  197. // pass a const ref to a non const-ref function, or vice versa accidentally. Or
  198. // we make a copy accidentally. Check.
  199. // Functions with reference parameters, unsupported.
  200. //
  201. // First, non-const reference parameters are disallowed by the Google
  202. // style guide. Second, since we are doing argument forwarding it becomes
  203. // very tricky to avoid copies, maintain const correctness, and not
  204. // accidentally have the function be modifying a temporary, or a copy.
  205. void WontCompile() {
  206. Parent p;
  207. RepeatingCallback<int(Parent&)> ref_arg_cb = BindRepeating(&UnwrapParentRef);
  208. ref_arg_cb.Run(p);
  209. }
  210. #elif defined(NCTEST_BIND_ONCE_WITH_NON_CONST_REF_PARAM) // [r"static assertion failed due to requirement 'BindArgument<0>::ForwardedAs<.+?>::ToParamWithType<.+?>::kNonConstRefParamMustBeWrapped': Bound argument for non-const reference parameter must be wrapped in std::ref\(\) or base::OwnedRef\(\)."]
  211. // Binding functions with reference parameters requires `std::ref()` or
  212. // 'base::OwnedRef()`.
  213. void WontCompile() {
  214. int v = 1;
  215. auto cb = BindOnce(&TakesIntRef, v);
  216. }
  217. #elif defined(NCTEST_BIND_REPEATING_WITH_NON_CONST_REF_PARAM) // [r"static assertion failed due to requirement 'BindArgument<0>::ForwardedAs<.+?>::ToParamWithType<.+?>::kNonConstRefParamMustBeWrapped': Bound argument for non-const reference parameter must be wrapped in std::ref\(\) or base::OwnedRef\(\)."]
  218. // Binding functions with reference parameters requires `std::ref()` or
  219. // 'base::OwnedRef()`.
  220. void WontCompile() {
  221. int v = 1;
  222. auto cb = BindRepeating(&TakesIntRef, v);
  223. }
  224. #elif defined(NCTEST_NON_CONST_REF_PARAM_WRONG_TYPE) // [r"static assertion failed due to requirement 'BindArgument<0>::ForwardedAs<.+?>::ToParamWithType<.+?>::kCanBeForwardedToBoundFunctor': Type mismatch between bound argument and bound functor's parameter."]
  225. // If the argument and parameter types mismatch then the compile error should be
  226. // the generic type mismatch error.
  227. void WontCompile() {
  228. float f = 1.0f;
  229. auto cb = BindOnce(&TakesIntRef, f);
  230. }
  231. #elif defined(NCTEST_NON_CONST_REF_PARAM_WRONG_TYPE_AND_WRAPPED) // [r"static assertion failed due to requirement 'BindArgument<0>::ForwardedAs<.+?>::ToParamWithType<.+?>::kCanBeForwardedToBoundFunctor': Type mismatch between bound argument and bound functor's parameter."]
  232. // If the argument and parameter types mismatch then the compile error should be
  233. // the generic type mismatch error even if the argument is wrapped in
  234. // base::OwnedRef().
  235. void WontCompile() {
  236. float f = 1.0f;
  237. auto cb = BindOnce(&TakesIntRef, base::OwnedRef(f));
  238. }
  239. #elif defined(NCTEST_NO_IMPLICIT_ARRAY_PTR_CONVERSION) // [r"fatal error: static assertion failed due to requirement '!std::is_array_v<base::HasRef\[10\]>': First bound argument to a method cannot be an array."]
  240. // A method should not be bindable with an array of objects.
  241. //
  242. // This is likely not wanted behavior. We specifically check for it though
  243. // because it is possible, depending on how you implement prebinding, to
  244. // implicitly convert an array type to a pointer type.
  245. void WontCompile() {
  246. HasRef p[10];
  247. RepeatingCallback<void()> method_bound_to_array_cb =
  248. BindRepeating(&HasRef::VoidMethod0, p);
  249. method_bound_to_array_cb.Run();
  250. }
  251. #elif defined(NCTEST_NO_RVALUE_RAW_REF_FOR_REFCOUNTED_TYPES) // [r"fatal error: static assertion failed due to requirement '!HasRefCountedTypeAsRawPtr<.*raw_ref<base::HasRef,.*: A parameter is a refcounted type and needs scoped_refptr."]
  252. // Refcounted types should not be bound as a raw pointer.
  253. void WontCompile() {
  254. HasRef has_ref;
  255. raw_ref<HasRef> rr(has_ref);
  256. int a;
  257. raw_ref<int> rr_a(a);
  258. RepeatingCallback<void()> ref_count_as_raw_ptr_a =
  259. BindRepeating(&VoidPolymorphic1<raw_ref<int>>, rr_a);
  260. RepeatingCallback<void()> ref_count_as_raw_ptr =
  261. BindRepeating(&VoidPolymorphic1<raw_ref<HasRef>>, rr);
  262. }
  263. #elif defined(NCTEST_NO_RVALUE_RAW_PTR_FOR_REFCOUNTED_TYPES) // [r"fatal error: static assertion failed due to requirement '!HasRefCountedTypeAsRawPtr<base::HasRef \*>::value': A parameter is a refcounted type and needs scoped_refptr."]
  264. // Refcounted types should not be bound as a raw pointer.
  265. void WontCompile() {
  266. HasRef for_raw_ptr;
  267. int a;
  268. RepeatingCallback<void()> ref_count_as_raw_ptr_a =
  269. BindRepeating(&VoidPolymorphic1<int*>, &a);
  270. RepeatingCallback<void()> ref_count_as_raw_ptr =
  271. BindRepeating(&VoidPolymorphic1<HasRef*>, &for_raw_ptr);
  272. }
  273. #elif defined(NCTEST_NO_LVALUE_RAW_PTR_FOR_REFCOUNTED_TYPES) // [r"fatal error: static assertion failed due to requirement '!HasRefCountedTypeAsRawPtr<base::HasRef \*>::value': A parameter is a refcounted type and needs scoped_refptr."]
  274. // Refcounted types should not be bound as a raw pointer.
  275. void WontCompile() {
  276. HasRef* for_raw_ptr = nullptr;
  277. RepeatingCallback<void()> ref_count_as_raw_ptr =
  278. BindRepeating(&VoidPolymorphic1<HasRef*>, for_raw_ptr);
  279. }
  280. #elif defined(NCTEST_NO_RVALUE_CONST_RAW_PTR_FOR_REFCOUNTED_TYPES) // [r"fatal error: static assertion failed due to requirement '!HasRefCountedTypeAsRawPtr<const base::HasRef \*>::value': A parameter is a refcounted type and needs scoped_refptr."]
  281. // Refcounted types should not be bound as a raw pointer.
  282. void WontCompile() {
  283. const HasRef for_raw_ptr;
  284. RepeatingCallback<void()> ref_count_as_raw_ptr =
  285. BindRepeating(&VoidPolymorphic1<const HasRef*>, &for_raw_ptr);
  286. }
  287. #elif defined(NCTEST_NO_LVALUE_CONST_RAW_PTR_FOR_REFCOUNTED_TYPES) // [r"fatal error: static assertion failed due to requirement '!HasRefCountedTypeAsRawPtr<const base::HasRef \*>::value': A parameter is a refcounted type and needs scoped_refptr."]
  288. // Refcounted types should not be bound as a raw pointer.
  289. void WontCompile() {
  290. const HasRef* for_raw_ptr = nullptr;
  291. RepeatingCallback<void()> ref_count_as_raw_ptr =
  292. BindRepeating(&VoidPolymorphic1<const HasRef*>, for_raw_ptr);
  293. }
  294. #elif defined(NCTEST_WEAKPTR_BIND_MUST_RETURN_VOID) // [r"fatal error: static assertion failed due to requirement 'std::is_void_v<int>': weak_ptrs can only bind to methods without return values"]
  295. // WeakPtrs cannot be bound to methods with return types.
  296. void WontCompile() {
  297. NoRef no_ref;
  298. WeakPtrFactory<NoRef> weak_factory(&no_ref);
  299. RepeatingCallback<int()> weak_ptr_with_non_void_return_type =
  300. BindRepeating(&NoRef::IntMethod0, weak_factory.GetWeakPtr());
  301. weak_ptr_with_non_void_return_type.Run();
  302. }
  303. #elif defined(NCTEST_DISALLOW_ASSIGN_DIFFERENT_TYPES) // [r"fatal error: no viable conversion from 'RepeatingCallback<internal::MakeUnboundRunType<void \(\*\)\(int\)>>' to 'RepeatingCallback<void \(\)>'"]
  304. // Bind result cannot be assigned to Callbacks with a mismatching type.
  305. void WontCompile() {
  306. RepeatingClosure callback_mismatches_bind_type =
  307. BindRepeating(&VoidPolymorphic1<int>);
  308. }
  309. #elif defined(NCTEST_DISALLOW_CAPTURING_LAMBDA) // [r"static assertion failed due to requirement 'FunctorTraits<\(lambda at [^)]+\), void>::is_stateless': Capturing lambdas and stateful lambdas are intentionally not supported\."]
  310. void WontCompile() {
  311. int i = 0, j = 0;
  312. BindOnce([i,&j]() {j = i;});
  313. }
  314. #elif defined(NCTEST_DISALLOW_ONCECALLBACK_RUN_ON_LVALUE) // [r"fatal error: static assertion failed due to requirement '!sizeof \(\*this\)': OnceCallback::Run\(\) may only be invoked on a non-const rvalue, i\.e\. std::move\(callback\).Run\(\)."]
  315. void WontCompile() {
  316. OnceClosure cb = BindOnce([] {});
  317. cb.Run();
  318. }
  319. #elif defined(NCTEST_DISALLOW_ONCECALLBACK_RUN_ON_CONST_LVALUE) // [r"fatal error: static assertion failed due to requirement '!sizeof \(\*this\)': OnceCallback::Run\(\) may only be invoked on a non-const rvalue, i\.e\. std::move\(callback\).Run\(\)."]
  320. void WontCompile() {
  321. const OnceClosure cb = BindOnce([] {});
  322. cb.Run();
  323. }
  324. #elif defined(NCTEST_DISALLOW_ONCECALLBACK_RUN_ON_CONST_RVALUE) // [r"fatal error: static assertion failed due to requirement '!sizeof \(\*this\)': OnceCallback::Run\(\) may only be invoked on a non-const rvalue, i\.e\. std::move\(callback\).Run\(\)."]
  325. void WontCompile() {
  326. const OnceClosure cb = BindOnce([] {});
  327. std::move(cb).Run();
  328. }
  329. #elif defined(NCTEST_DISALLOW_BIND_ONCECALLBACK) // [r"fatal error: static assertion failed due to requirement '!base::internal::IsOnceCallback<base::OnceCallback<void \(int\)> ?>\(\)': BindRepeating cannot bind OnceCallback. Use BindOnce with std::move\(\)."]
  330. void WontCompile() {
  331. BindRepeating(BindOnce([](int) {}), 42);
  332. }
  333. #elif defined(NCTEST_DISALLOW_BINDONCE_LVALUE_ONCECALLBACK) // [r"fatal error: static assertion failed due to requirement '!internal::IsOnceCallback<std::decay_t<OnceCallback<void (int)> &> >() || (std::is_rvalue_reference<OnceCallback<void (int)> &>() && !std::is_const<std::remove_reference_t<OnceCallback<void (int)> &> >())': BindOnce requires non-const rvalue for OnceCallback binding. I.e.: base::BindOnce(std::move(callback))."]
  334. void WontCompile() {
  335. auto cb = BindOnce([](int) {});
  336. BindOnce(cb, 42);
  337. }
  338. #elif defined(NCTEST_DISALLOW_BINDONCE_RVALUE_CONST_ONCECALLBACK) // [r"fatal error: static assertion failed due to requirement '!internal::IsOnceCallback<std::decay_t<const OnceCallback<void (int)> > >() || (std::is_rvalue_reference<const OnceCallback<void (int)> &&>() && !std::is_const<std::remove_reference_t<const OnceCallback<void (int)> > >())': BindOnce requires non-const rvalue for OnceCallback binding. I.e.: base::BindOnce(std::move(callback))."]
  339. void WontCompile() {
  340. const auto cb = BindOnce([](int) {});
  341. BindOnce(std::move(cb), 42);
  342. }
  343. #elif defined(NCTEST_BINDONCE_MOVEONLY_TYPE_BY_VALUE) // [r"static assertion failed.+?BindArgument<0>::BoundAs<.+?>::StoredAs<.+?>::kMoveOnlyTypeMustUseStdMove.+?Attempting to bind a move-only type\. Use std::move\(\) to transfer ownership to the created callback\."]
  344. void WontCompile() {
  345. std::unique_ptr<int> x;
  346. BindOnce(&TakesMoveOnly, x);
  347. }
  348. #elif defined(NCTEST_BIND_MOVEONLY_TYPE_BY_VALUE) // [r"static assertion failed.+?BindArgument<0>::ForwardedAs<.+?>::ToParamWithType<.+?>::kMoveOnlyTypeMustUseBasePassed.+?base::BindRepeating\(\) argument is a move-only type\. Use base::Passed\(\) instead of std::move\(\) to transfer ownership from the callback to the bound functor\."]
  349. void WontCompile() {
  350. std::unique_ptr<int> x;
  351. BindRepeating(&TakesMoveOnly, x);
  352. }
  353. #elif defined(NCTEST_BIND_MOVEONLY_TYPE_WITH_STDMOVE) // [r"static assertion failed.+?BindArgument<0>::ForwardedAs<.+?>::ToParamWithType<.+?>::kMoveOnlyTypeMustUseBasePassed.+?base::BindRepeating\(\) argument is a move-only type\. Use base::Passed\(\) instead of std::move\(\) to transfer ownership from the callback to the bound functor\."]
  354. void WontCompile() {
  355. std::unique_ptr<int> x;
  356. BindRepeating(&TakesMoveOnly, std::move(x));
  357. }
  358. #elif defined(NCTEST_BIND_NON_EMPTY_FUNCTOR) // [r"static assertion failed due to requirement 'FunctorTraits<base::NonEmptyFunctor, void>::is_stateless': Capturing lambdas and stateful lambdas are intentionally not supported\."]
  359. void WontCompile() {
  360. BindRepeating(NonEmptyFunctor());
  361. }
  362. #elif defined(NCTEST_DISALLOW_BINDLAMBDAFORTESTING_LVALUE_MUTABLE_LAMBDA) // [r"BindLambdaForTesting requires non-const rvalue for mutable lambda binding\. I\.e\.: base::BindLambdaForTesting\(std::move\(lambda\)\)."]
  363. void WontCompile() {
  364. int foo = 42;
  365. auto mutable_lambda = [&]() mutable {};
  366. BindLambdaForTesting(mutable_lambda);
  367. }
  368. #elif defined(NCTEST_DISALLOW_BINDLAMBDAFORTESTING_RVALUE_CONST_MUTABLE_LAMBDA) // [r"BindLambdaForTesting requires non-const rvalue for mutable lambda binding\. I\.e\.: base::BindLambdaForTesting\(std::move\(lambda\)\)."]
  369. void WontCompile() {
  370. int foo = 42;
  371. const auto mutable_lambda = [&]() mutable {};
  372. BindLambdaForTesting(std::move(mutable_lambda));
  373. }
  374. #elif defined(NCTEST_BIND_UNCOPYABLE_AND_UNMOVABLE_TYPE) // [r"static assertion failed.+?BindArgument<0>::BoundAs<.+?>::StoredAs<.+?>::kBindArgumentCanBeCaptured.+?Cannot capture argument: is the argument copyable or movable\?"]
  375. void WontCompile() {
  376. struct UncopyableUnmovable {
  377. UncopyableUnmovable() = default;
  378. UncopyableUnmovable(const UncopyableUnmovable&) = delete;
  379. UncopyableUnmovable& operator=(const UncopyableUnmovable&) = delete;
  380. };
  381. UncopyableUnmovable u;
  382. BindOnce([] (const UncopyableUnmovable&) {}, u);
  383. }
  384. #elif defined(NCTEST_BIND_ONCE_WITH_PASSED) // [r"static assertion failed.+?Use std::move\(\) instead of base::Passed\(\) with base::BindOnce\(\)"]
  385. void WontCompile() {
  386. std::unique_ptr<int> x;
  387. BindOnce([] (std::unique_ptr<int>) {}, Passed(&x));
  388. }
  389. #elif defined(NCTEST_BIND_ONCE_WITH_ADDRESS_OF_OVERLOADED_FUNCTION) // [r"fatal error: reference to overloaded function could not be resolved; did you mean to call it\?"]
  390. void F(int);
  391. void F(float);
  392. void WontCompile() {
  393. BindOnce(&F, 1, 2, 3);
  394. }
  395. #elif defined(NCTEST_BIND_REPEATING_WITH_ADDRESS_OF_OVERLOADED_FUNCTION) // [r"fatal error: reference to overloaded function could not be resolved; did you mean to call it\?"]
  396. void F(int);
  397. void F(float);
  398. void WontCompile() {
  399. BindRepeating(&F, 1, 2, 3);
  400. }
  401. #endif
  402. } // namespace base