blob: 5c763faca525143de7671b99ca0440e83ee1d422 [file] [log] [blame]
Avi Drissmane4622aa2022-09-08 20:36:061// Copyright 2011 The Chromium Authors
Hans Wennborge5d14a82019-01-14 11:31:242// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5// This is a "No Compile Test" suite.
6// http://dev.chromium.org/developers/testing/no-compile-tests
7
8#include <utility>
9
10#include "base/bind.h"
11#include "base/callback.h"
Hans Wennborge5d14a82019-01-14 11:31:2412#include "base/memory/ref_counted.h"
danakj92061c12022-07-28 23:53:5013#include "base/memory/raw_ptr.h"
14#include "base/memory/raw_ref.h"
Guido Urdanetaef4e91942020-11-09 15:06:2415#include "base/test/bind.h"
Hans Wennborge5d14a82019-01-14 11:31:2416
17namespace base {
18
19// Do not put everything inside an anonymous namespace. If you do, many of the
20// helper function declarations will generate unused definition warnings.
21
22static const int kParentValue = 1;
23static const int kChildValue = 2;
24
25class NoRef {
26 public:
27 void VoidMethod0() {}
28 void VoidConstMethod0() const {}
29 int IntMethod0() { return 1; }
30};
31
32class HasRef : public NoRef, public base::RefCounted<HasRef> {
33};
34
35class Parent {
36 public:
37 void AddRef() const {}
38 void Release() const {}
39 virtual void VirtualSet() { value = kParentValue; }
40 void NonVirtualSet() { value = kParentValue; }
41 int value;
42};
43
44class Child : public Parent {
45 public:
46 virtual void VirtualSet() { value = kChildValue; }
47 void NonVirtualSet() { value = kChildValue; }
48};
49
50class NoRefParent {
51 public:
52 virtual void VirtualSet() { value = kParentValue; }
53 void NonVirtualSet() { value = kParentValue; }
54 int value;
55};
56
57class NoRefChild : public NoRefParent {
58 virtual void VirtualSet() { value = kChildValue; }
59 void NonVirtualSet() { value = kChildValue; }
60};
61
62template <typename T>
63T PolymorphicIdentity(T t) {
64 return t;
65}
66
67int UnwrapParentRef(Parent& p) {
68 return p.value;
69}
70
71template <typename T>
72void VoidPolymorphic1(T t) {
73}
74
75void TakesMoveOnly(std::unique_ptr<int>) {
76}
77
kylechar72e6f782021-03-17 17:43:3878void TakesIntRef(int& ref) {}
79
Hans Wennborge5d14a82019-01-14 11:31:2480struct NonEmptyFunctor {
81 int x;
82 void operator()() const {}
83};
84
Nico Weber318a073d2022-08-02 18:55:2285#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\."]
Daniel Cheng9a85adb2020-10-30 21:57:5186
Hans Wennborge5d14a82019-01-14 11:31:2487// Method bound to const-object.
88//
89// Only const methods should be allowed to work with const objects.
90void WontCompile() {
91 HasRef has_ref;
92 const HasRef* const_has_ref_ptr_ = &has_ref;
kylechar650caf02019-07-17 03:25:4193 RepeatingCallback<void()> method_to_const_cb =
94 BindRepeating(&HasRef::VoidMethod0, const_has_ref_ptr_);
Hans Wennborge5d14a82019-01-14 11:31:2495 method_to_const_cb.Run();
96}
97
Nico Weber318a073d2022-08-02 18:55:2298#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."]
Hans Wennborge5d14a82019-01-14 11:31:2499
100
101// Method bound to non-refcounted object.
102//
103// We require refcounts unless you have Unretained().
104void WontCompile() {
105 NoRef no_ref;
kylechar650caf02019-07-17 03:25:41106 RepeatingCallback<void()> no_ref_cb =
107 BindRepeating(&NoRef::VoidMethod0, &no_ref);
Hans Wennborge5d14a82019-01-14 11:31:24108 no_ref_cb.Run();
109}
110
Nico Weber318a073d2022-08-02 18:55:22111#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."]
Hans Wennborge5d14a82019-01-14 11:31:24112
113// Const Method bound to non-refcounted object.
114//
115// We require refcounts unless you have Unretained().
116void WontCompile() {
117 NoRef no_ref;
kylechar650caf02019-07-17 03:25:41118 RepeatingCallback<void()> no_ref_const_cb =
119 BindRepeating(&NoRef::VoidConstMethod0, &no_ref);
Hans Wennborge5d14a82019-01-14 11:31:24120 no_ref_const_cb.Run();
121}
122
Nico Weber318a073d2022-08-02 18:55:22123#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."]
Daniel Cheng00c072a2022-06-11 18:50:37124
125
126// Method bound to non-refcounted object.
127//
128// We require refcounts unless you have Unretained().
129void WontCompile() {
130 NoRef no_ref;
131 raw_ptr<NoRef> rawptr(&no_ref);
132 RepeatingCallback<void()> no_ref_cb =
133 BindRepeating(&NoRef::VoidMethod0, rawptr);
134 no_ref_cb.Run();
135}
136
Nico Weber318a073d2022-08-02 18:55:22137#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."]
Daniel Cheng00c072a2022-06-11 18:50:37138
139// Const Method bound to non-refcounted object.
140//
141// We require refcounts unless you have Unretained().
142void WontCompile() {
143 NoRef no_ref;
144 raw_ptr<NoRef> rawptr(&no_ref);
145 RepeatingCallback<void()> no_ref_const_cb =
146 BindRepeating(&NoRef::VoidConstMethod0, rawptr);
147 no_ref_const_cb.Run();
148}
149
danakj92061c12022-07-28 23:53:50150#elif defined(NCTEST_METHOD_BIND_REF_WRAPPER_RECEIVER_NON_REFCOUNTED_OBJECT) // [r"fatal error: indirection requires pointer operand"]
151
152// Method bound to non-refcounted object. It fails to compile with
153// std::reference_wrapper.
154void WontCompile() {
155 NoRef no_ref;
156 RepeatingCallback<void()> no_ref_cb =
157 BindRepeating(&NoRef::VoidMethod0, std::cref(no_ref));
158 no_ref_cb.Run();
159}
160
161#elif defined(NCTEST_METHOD_BIND_NATIVE_REF_RECEIVER_NON_REFCOUNTED_OBJECT) // [r"fatal error: indirection requires pointer operand"]
162
163// Method bound to non-refcounted object. It fails to compile with
164// a native reference.
165void WontCompile() {
166 NoRef no_ref;
167 RepeatingCallback<void()> no_ref_cb =
168 BindRepeating(&NoRef::VoidMethod0, no_ref);
169 no_ref_cb.Run();
170}
171
172#elif defined(NCTEST_METHOD_BIND_RAW_REF_RECEIVER_NON_REFCOUNTED_OBJECT) // [r"fatal error: .*Receivers may not be raw_ref<T>\."]
173
174// Method bound to non-refcounted object. It fails to compile with
175// a raw_ref.
176void WontCompile() {
177 NoRef no_ref;
178 raw_ref<NoRef> rawref(no_ref);
179 RepeatingCallback<void()> no_ref_cb =
180 BindRepeating(&NoRef::VoidMethod0, rawref);
181 no_ref_cb.Run();
182}
183
184#elif defined(NCTEST_METHOD_BIND_REF_WRAPPER_RECEIVER_REFCOUNTED_OBJECT) // [r"fatal error: indirection requires pointer operand"]
185
186// Method bound to non-refcounted object. It fails to compile with
187// std::reference_wrapper.
188void WontCompile() {
189 HasRef has_ref;
190 RepeatingCallback<void()> has_ref_cb =
191 BindRepeating(&HasRef::VoidMethod0, std::cref(has_ref));
192 has_ref_cb.Run();
193}
194
195#elif defined(NCTEST_METHOD_BIND_NATIVE_REF_RECEIVER_REFCOUNTED_OBJECT) // [r"fatal error: indirection requires pointer operand"]
196
197// Method bound to non-refcounted object. It fails to compile with
198// a native reference.
199void WontCompile() {
200 HasRef has_ref;
201 RepeatingCallback<void()> has_ref_cb =
202 BindRepeating(&HasRef::VoidMethod0, has_ref);
203 has_ref_cb.Run();
204}
205
206#elif defined(NCTEST_METHOD_BIND_RAW_REF_RECEIVER_REFCOUNTED_OBJECT) // [r"fatal error: .*Receivers may not be raw_ref<T>\."]
207
208// Method bound to non-refcounted object. It fails to compile with
209// a raw_ref.
210void WontCompile() {
211 HasRef has_ref;
212 raw_ref<HasRef> rawref(has_ref);
213 RepeatingCallback<void()> has_ref_cb =
214 BindRepeating(&HasRef::VoidMethod0, rawref);
215 has_ref_cb.Run();
216}
217
Nico Weber318a073d2022-08-02 18:55:22218#elif defined(NCTEST_CONST_POINTER) // [r"static assertion failed.+?BindArgument<0>::ForwardedAs<.+?>::ToParamWithType<.+?>::kCanBeForwardedToBoundFunctor.+?Type mismatch between bound argument and bound functor's parameter\."]
Hans Wennborge5d14a82019-01-14 11:31:24219// Const argument used with non-const pointer parameter of same type.
220//
221// This is just a const-correctness check.
222void WontCompile() {
223 const NoRef* const_no_ref_ptr;
kylechar650caf02019-07-17 03:25:41224 RepeatingCallback<NoRef*()> pointer_same_cb =
225 BindRepeating(&PolymorphicIdentity<NoRef*>, const_no_ref_ptr);
Hans Wennborge5d14a82019-01-14 11:31:24226 pointer_same_cb.Run();
227}
228
Nico Weber318a073d2022-08-02 18:55:22229#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\."]
Hans Wennborge5d14a82019-01-14 11:31:24230
231// Const argument used with non-const pointer parameter of super type.
232//
233// This is just a const-correctness check.
234void WontCompile() {
235 const NoRefChild* const_child_ptr;
kylechar650caf02019-07-17 03:25:41236 RepeatingCallback<NoRefParent*()> pointer_super_cb =
237 BindRepeating(&PolymorphicIdentity<NoRefParent*>, const_child_ptr);
Hans Wennborge5d14a82019-01-14 11:31:24238 pointer_super_cb.Run();
239}
240
241#elif defined(DISABLED_NCTEST_DISALLOW_NON_CONST_REF_PARAM) // [r"fatal error: no member named 'AddRef' in 'base::NoRef'"]
242// TODO(dcheng): I think there's a type safety promotion issue here where we can
243// pass a const ref to a non const-ref function, or vice versa accidentally. Or
244// we make a copy accidentally. Check.
245
246// Functions with reference parameters, unsupported.
247//
248// First, non-const reference parameters are disallowed by the Google
249// style guide. Second, since we are doing argument forwarding it becomes
250// very tricky to avoid copies, maintain const correctness, and not
251// accidentally have the function be modifying a temporary, or a copy.
252void WontCompile() {
253 Parent p;
kylechar650caf02019-07-17 03:25:41254 RepeatingCallback<int(Parent&)> ref_arg_cb = BindRepeating(&UnwrapParentRef);
Hans Wennborge5d14a82019-01-14 11:31:24255 ref_arg_cb.Run(p);
256}
257
Nico Weber318a073d2022-08-02 18:55:22258#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\(\)."]
Hans Wennborge5d14a82019-01-14 11:31:24259
kylechar72e6f782021-03-17 17:43:38260// Binding functions with reference parameters requires `std::ref()` or
261// 'base::OwnedRef()`.
Hans Wennborge5d14a82019-01-14 11:31:24262void WontCompile() {
kylechar72e6f782021-03-17 17:43:38263 int v = 1;
264 auto cb = BindOnce(&TakesIntRef, v);
265}
266
Nico Weber318a073d2022-08-02 18:55:22267#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\(\)."]
kylechar72e6f782021-03-17 17:43:38268
269// Binding functions with reference parameters requires `std::ref()` or
270// 'base::OwnedRef()`.
271void WontCompile() {
272 int v = 1;
273 auto cb = BindRepeating(&TakesIntRef, v);
274}
275
Nico Weber318a073d2022-08-02 18:55:22276#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."]
kylechar72e6f782021-03-17 17:43:38277
278// If the argument and parameter types mismatch then the compile error should be
279// the generic type mismatch error.
280void WontCompile() {
281 float f = 1.0f;
282 auto cb = BindOnce(&TakesIntRef, f);
283}
284
Nico Weber318a073d2022-08-02 18:55:22285#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."]
kylechar72e6f782021-03-17 17:43:38286
287// If the argument and parameter types mismatch then the compile error should be
288// the generic type mismatch error even if the argument is wrapped in
289// base::OwnedRef().
290void WontCompile() {
291 float f = 1.0f;
292 auto cb = BindOnce(&TakesIntRef, base::OwnedRef(f));
Hans Wennborge5d14a82019-01-14 11:31:24293}
294
Nico Weber318a073d2022-08-02 18:55:22295#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."]
Hans Wennborge5d14a82019-01-14 11:31:24296
297// A method should not be bindable with an array of objects.
298//
299// This is likely not wanted behavior. We specifically check for it though
300// because it is possible, depending on how you implement prebinding, to
301// implicitly convert an array type to a pointer type.
302void WontCompile() {
303 HasRef p[10];
kylechar650caf02019-07-17 03:25:41304 RepeatingCallback<void()> method_bound_to_array_cb =
305 BindRepeating(&HasRef::VoidMethod0, p);
Hans Wennborge5d14a82019-01-14 11:31:24306 method_bound_to_array_cb.Run();
307}
308
Nico Weber318a073d2022-08-02 18:55:22309#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."]
danakj92061c12022-07-28 23:53:50310
311// Refcounted types should not be bound as a raw pointer.
312void WontCompile() {
313 HasRef has_ref;
314 raw_ref<HasRef> rr(has_ref);
315 int a;
316 raw_ref<int> rr_a(a);
317 RepeatingCallback<void()> ref_count_as_raw_ptr_a =
318 BindRepeating(&VoidPolymorphic1<raw_ref<int>>, rr_a);
319 RepeatingCallback<void()> ref_count_as_raw_ptr =
320 BindRepeating(&VoidPolymorphic1<raw_ref<HasRef>>, rr);
321}
322
Nico Weber318a073d2022-08-02 18:55:22323#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."]
Hans Wennborge5d14a82019-01-14 11:31:24324
325// Refcounted types should not be bound as a raw pointer.
326void WontCompile() {
327 HasRef for_raw_ptr;
328 int a;
kylechar650caf02019-07-17 03:25:41329 RepeatingCallback<void()> ref_count_as_raw_ptr_a =
330 BindRepeating(&VoidPolymorphic1<int*>, &a);
331 RepeatingCallback<void()> ref_count_as_raw_ptr =
332 BindRepeating(&VoidPolymorphic1<HasRef*>, &for_raw_ptr);
Hans Wennborge5d14a82019-01-14 11:31:24333}
334
Nico Weber318a073d2022-08-02 18:55:22335#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."]
Hans Wennborge5d14a82019-01-14 11:31:24336
337// Refcounted types should not be bound as a raw pointer.
338void WontCompile() {
339 HasRef* for_raw_ptr = nullptr;
kylechar650caf02019-07-17 03:25:41340 RepeatingCallback<void()> ref_count_as_raw_ptr =
341 BindRepeating(&VoidPolymorphic1<HasRef*>, for_raw_ptr);
Hans Wennborge5d14a82019-01-14 11:31:24342}
343
Nico Weber318a073d2022-08-02 18:55:22344#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."]
Hans Wennborge5d14a82019-01-14 11:31:24345
346// Refcounted types should not be bound as a raw pointer.
347void WontCompile() {
348 const HasRef for_raw_ptr;
kylechar650caf02019-07-17 03:25:41349 RepeatingCallback<void()> ref_count_as_raw_ptr =
350 BindRepeating(&VoidPolymorphic1<const HasRef*>, &for_raw_ptr);
Hans Wennborge5d14a82019-01-14 11:31:24351}
352
Nico Weber318a073d2022-08-02 18:55:22353#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."]
Hans Wennborge5d14a82019-01-14 11:31:24354
355// Refcounted types should not be bound as a raw pointer.
356void WontCompile() {
357 const HasRef* for_raw_ptr = nullptr;
kylechar650caf02019-07-17 03:25:41358 RepeatingCallback<void()> ref_count_as_raw_ptr =
359 BindRepeating(&VoidPolymorphic1<const HasRef*>, for_raw_ptr);
Hans Wennborge5d14a82019-01-14 11:31:24360}
361
Nico Weber318a073d2022-08-02 18:55:22362#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"]
Hans Wennborge5d14a82019-01-14 11:31:24363
364// WeakPtrs cannot be bound to methods with return types.
365void WontCompile() {
366 NoRef no_ref;
367 WeakPtrFactory<NoRef> weak_factory(&no_ref);
kylechar650caf02019-07-17 03:25:41368 RepeatingCallback<int()> weak_ptr_with_non_void_return_type =
369 BindRepeating(&NoRef::IntMethod0, weak_factory.GetWeakPtr());
Hans Wennborge5d14a82019-01-14 11:31:24370 weak_ptr_with_non_void_return_type.Run();
371}
372
Daniel Chengcfb972d62021-03-04 13:18:04373#elif defined(NCTEST_DISALLOW_ASSIGN_DIFFERENT_TYPES) // [r"fatal error: no viable conversion from 'RepeatingCallback<internal::MakeUnboundRunType<void \(\*\)\(int\)>>' to 'RepeatingCallback<void \(\)>'"]
Hans Wennborge5d14a82019-01-14 11:31:24374
375// Bind result cannot be assigned to Callbacks with a mismatching type.
376void WontCompile() {
kylechar650caf02019-07-17 03:25:41377 RepeatingClosure callback_mismatches_bind_type =
378 BindRepeating(&VoidPolymorphic1<int>);
Hans Wennborge5d14a82019-01-14 11:31:24379}
380
Nico Weber318a073d2022-08-02 18:55:22381#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\."]
Hans Wennborge5d14a82019-01-14 11:31:24382
383void WontCompile() {
384 int i = 0, j = 0;
kylechar650caf02019-07-17 03:25:41385 BindOnce([i,&j]() {j = i;});
Hans Wennborge5d14a82019-01-14 11:31:24386}
387
Nico Weber318a073d2022-08-02 18:55:22388#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\(\)."]
Hans Wennborge5d14a82019-01-14 11:31:24389
390void WontCompile() {
kylechar650caf02019-07-17 03:25:41391 OnceClosure cb = BindOnce([] {});
Hans Wennborge5d14a82019-01-14 11:31:24392 cb.Run();
393}
394
Nico Weber318a073d2022-08-02 18:55:22395#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\(\)."]
Hans Wennborge5d14a82019-01-14 11:31:24396
397void WontCompile() {
kylechar650caf02019-07-17 03:25:41398 const OnceClosure cb = BindOnce([] {});
Hans Wennborge5d14a82019-01-14 11:31:24399 cb.Run();
400}
401
Nico Weber318a073d2022-08-02 18:55:22402#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\(\)."]
Hans Wennborge5d14a82019-01-14 11:31:24403
404void WontCompile() {
kylechar650caf02019-07-17 03:25:41405 const OnceClosure cb = BindOnce([] {});
Hans Wennborge5d14a82019-01-14 11:31:24406 std::move(cb).Run();
407}
408
Nico Weber318a073d2022-08-02 18:55:22409#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\(\)."]
Hans Wennborge5d14a82019-01-14 11:31:24410
411void WontCompile() {
kylechar650caf02019-07-17 03:25:41412 BindRepeating(BindOnce([](int) {}), 42);
Hans Wennborge5d14a82019-01-14 11:31:24413}
414
Nico Weber318a073d2022-08-02 18:55:22415#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))."]
Hans Wennborge5d14a82019-01-14 11:31:24416void WontCompile() {
417 auto cb = BindOnce([](int) {});
418 BindOnce(cb, 42);
419}
420
Nico Weber318a073d2022-08-02 18:55:22421#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))."]
Hans Wennborge5d14a82019-01-14 11:31:24422
423void WontCompile() {
424 const auto cb = BindOnce([](int) {});
425 BindOnce(std::move(cb), 42);
426}
427
Nico Weber318a073d2022-08-02 18:55:22428#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\."]
Hans Wennborge5d14a82019-01-14 11:31:24429
430void WontCompile() {
431 std::unique_ptr<int> x;
432 BindOnce(&TakesMoveOnly, x);
433}
434
Nico Weber318a073d2022-08-02 18:55:22435#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\."]
Hans Wennborge5d14a82019-01-14 11:31:24436
437void WontCompile() {
438 std::unique_ptr<int> x;
kylechar650caf02019-07-17 03:25:41439 BindRepeating(&TakesMoveOnly, x);
Hans Wennborge5d14a82019-01-14 11:31:24440}
441
Nico Weber318a073d2022-08-02 18:55:22442#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\."]
Hans Wennborge5d14a82019-01-14 11:31:24443
444void WontCompile() {
445 std::unique_ptr<int> x;
kylechar650caf02019-07-17 03:25:41446 BindRepeating(&TakesMoveOnly, std::move(x));
Hans Wennborge5d14a82019-01-14 11:31:24447}
448
Nico Weber318a073d2022-08-02 18:55:22449#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\."]
Hans Wennborge5d14a82019-01-14 11:31:24450
451void WontCompile() {
kylechar650caf02019-07-17 03:25:41452 BindRepeating(NonEmptyFunctor());
Hans Wennborge5d14a82019-01-14 11:31:24453}
454
Jan Wilken Dörrie4dc6fad72019-05-22 07:27:56455#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\)\)."]
456void WontCompile() {
457 int foo = 42;
458 auto mutable_lambda = [&]() mutable {};
459 BindLambdaForTesting(mutable_lambda);
460}
461
462#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\)\)."]
463
464void WontCompile() {
465 int foo = 42;
466 const auto mutable_lambda = [&]() mutable {};
467 BindLambdaForTesting(std::move(mutable_lambda));
468}
Daniel Cheng9a85adb2020-10-30 21:57:51469
Nico Weber318a073d2022-08-02 18:55:22470#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\?"]
Daniel Cheng9a85adb2020-10-30 21:57:51471
472void WontCompile() {
473 struct UncopyableUnmovable {
474 UncopyableUnmovable() = default;
475 UncopyableUnmovable(const UncopyableUnmovable&) = delete;
476 UncopyableUnmovable& operator=(const UncopyableUnmovable&) = delete;
477 };
478
479 UncopyableUnmovable u;
480 BindOnce([] (const UncopyableUnmovable&) {}, u);
481}
482
Nico Weber318a073d2022-08-02 18:55:22483#elif defined(NCTEST_BIND_ONCE_WITH_PASSED) // [r"static assertion failed.+?Use std::move\(\) instead of base::Passed\(\) with base::BindOnce\(\)"]
Daniel Chengad0da8e2021-02-22 21:07:22484
485void WontCompile() {
486 std::unique_ptr<int> x;
487 BindOnce([] (std::unique_ptr<int>) {}, Passed(&x));
488}
489
Daniel Cheng58ea45b2021-09-17 22:55:08490#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\?"]
491
492void F(int);
493void F(float);
494
495void WontCompile() {
496 BindOnce(&F, 1, 2, 3);
497}
498
499#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\?"]
500
501void F(int);
502void F(float);
503
504void WontCompile() {
505 BindRepeating(&F, 1, 2, 3);
506}
507
Hans Wennborge5d14a82019-01-14 11:31:24508#endif
509
510} // namespace base