blob: 8e26c1c8d970b748b5438eca588c00111fa11950 [file] [log] [blame]
[email protected]81814bce2011-09-10 03:03:001// 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
scheib0411d8f72015-10-19 19:29:555// This is a "No Compile Test" suite.
6// http://dev.chromium.org/developers/testing/no-compile-tests
7
dcheng172b6ad2016-09-24 05:05:578#include <utility>
9
[email protected]81814bce2011-09-10 03:03:0010#include "base/bind.h"
avi9b6f42932015-12-26 22:15:1411#include "base/callback.h"
12#include "base/macros.h"
[email protected]7296f2762011-11-21 19:23:4413#include "base/memory/ref_counted.h"
[email protected]81814bce2011-09-10 03:03:0014
15namespace base {
16
17// Do not put everything inside an anonymous namespace. If you do, many of the
[email protected]5dd6bdf2014-02-21 16:24:1318// helper function declarations will generate unused definition warnings.
[email protected]81814bce2011-09-10 03:03:0019
20static const int kParentValue = 1;
21static const int kChildValue = 2;
22
23class NoRef {
24 public:
25 void VoidMethod0() {}
26 void VoidConstMethod0() const {}
27 int IntMethod0() { return 1; }
28};
29
[email protected]7296f2762011-11-21 19:23:4430class HasRef : public NoRef, public base::RefCounted<HasRef> {
[email protected]81814bce2011-09-10 03:03:0031};
32
33class Parent {
34 public:
tzik3bc7779b2015-12-19 09:18:4635 void AddRef() const {}
36 void Release() const {}
[email protected]81814bce2011-09-10 03:03:0037 virtual void VirtualSet() { value = kParentValue; }
38 void NonVirtualSet() { value = kParentValue; }
39 int value;
40};
41
42class Child : public Parent {
43 public:
44 virtual void VirtualSet() { value = kChildValue; }
45 void NonVirtualSet() { value = kChildValue; }
46};
47
48class NoRefParent {
49 public:
50 virtual void VirtualSet() { value = kParentValue; }
51 void NonVirtualSet() { value = kParentValue; }
52 int value;
53};
54
55class NoRefChild : public NoRefParent {
56 virtual void VirtualSet() { value = kChildValue; }
57 void NonVirtualSet() { value = kChildValue; }
58};
59
60template <typename T>
61T PolymorphicIdentity(T t) {
62 return t;
63}
64
65int UnwrapParentRef(Parent& p) {
66 return p.value;
67}
68
69template <typename T>
70void VoidPolymorphic1(T t) {
71}
72
Daniel Chenga09cb602017-08-30 13:46:1973void TakesMoveOnly(std::unique_ptr<int>) {
74}
75
Hans Wennborg80235482017-09-15 18:34:5176// TODO(hans): Remove .* and update the static_assert expectations once we roll
77// past Clang r313315. https://crbug.com/765692.
78
79#if defined(NCTEST_METHOD_ON_CONST_OBJECT) // [r"fatal error: static_assert failed .*\"Bound argument \|i\| of type \|Arg\| cannot be forwarded as \|Unwrapped\| to the bound functor, which declares it as \|Param\|\.\""]
[email protected]81814bce2011-09-10 03:03:0080
81// Method bound to const-object.
82//
83// Only const methods should be allowed to work with const objects.
84void WontCompile() {
85 HasRef has_ref;
86 const HasRef* const_has_ref_ptr_ = &has_ref;
tzik3bc7779b2015-12-19 09:18:4687 Callback<void()> method_to_const_cb =
[email protected]81814bce2011-09-10 03:03:0088 Bind(&HasRef::VoidMethod0, const_has_ref_ptr_);
89 method_to_const_cb.Run();
90}
91
dchengb760d722014-11-03 21:29:3092#elif defined(NCTEST_METHOD_BIND_NEEDS_REFCOUNTED_OBJECT) // [r"fatal error: no member named 'AddRef' in 'base::NoRef'"]
[email protected]81814bce2011-09-10 03:03:0093
94// Method bound to non-refcounted object.
95//
96// We require refcounts unless you have Unretained().
97void WontCompile() {
98 NoRef no_ref;
tzik3bc7779b2015-12-19 09:18:4699 Callback<void()> no_ref_cb =
[email protected]81814bce2011-09-10 03:03:00100 Bind(&NoRef::VoidMethod0, &no_ref);
101 no_ref_cb.Run();
102}
103
dchengb760d722014-11-03 21:29:30104#elif defined(NCTEST_CONST_METHOD_NEEDS_REFCOUNTED_OBJECT) // [r"fatal error: no member named 'AddRef' in 'base::NoRef'"]
[email protected]81814bce2011-09-10 03:03:00105
106// Const Method bound to non-refcounted object.
107//
108// We require refcounts unless you have Unretained().
109void WontCompile() {
110 NoRef no_ref;
tzik3bc7779b2015-12-19 09:18:46111 Callback<void()> no_ref_const_cb =
[email protected]81814bce2011-09-10 03:03:00112 Bind(&NoRef::VoidConstMethod0, &no_ref);
113 no_ref_const_cb.Run();
114}
115
Hans Wennborg80235482017-09-15 18:34:51116#elif defined(NCTEST_CONST_POINTER) // [r"fatal error: static_assert failed .*\"Bound argument \|i\| of type \|Arg\| cannot be forwarded as \|Unwrapped\| to the bound functor, which declares it as \|Param\|\.\""]
[email protected]81814bce2011-09-10 03:03:00117
118// Const argument used with non-const pointer parameter of same type.
119//
120// This is just a const-correctness check.
121void WontCompile() {
122 const NoRef* const_no_ref_ptr;
tzik3bc7779b2015-12-19 09:18:46123 Callback<NoRef*()> pointer_same_cb =
[email protected]81814bce2011-09-10 03:03:00124 Bind(&PolymorphicIdentity<NoRef*>, const_no_ref_ptr);
125 pointer_same_cb.Run();
126}
127
Hans Wennborg80235482017-09-15 18:34:51128#elif defined(NCTEST_CONST_POINTER_SUBTYPE) // [r"fatal error: static_assert failed .*\"Bound argument \|i\| of type \|Arg\| cannot be forwarded as \|Unwrapped\| to the bound functor, which declares it as \|Param\|\.\""]
[email protected]81814bce2011-09-10 03:03:00129
130// Const argument used with non-const pointer parameter of super type.
131//
132// This is just a const-correctness check.
133void WontCompile() {
134 const NoRefChild* const_child_ptr;
tzik3bc7779b2015-12-19 09:18:46135 Callback<NoRefParent*()> pointer_super_cb =
[email protected]81814bce2011-09-10 03:03:00136 Bind(&PolymorphicIdentity<NoRefParent*>, const_child_ptr);
137 pointer_super_cb.Run();
138}
139
dchengb760d722014-11-03 21:29:30140#elif defined(DISABLED_NCTEST_DISALLOW_NON_CONST_REF_PARAM) // [r"fatal error: no member named 'AddRef' in 'base::NoRef'"]
141// TODO(dcheng): I think there's a type safety promotion issue here where we can
142// pass a const ref to a non const-ref function, or vice versa accidentally. Or
143// we make a copy accidentally. Check.
[email protected]81814bce2011-09-10 03:03:00144
145// Functions with reference parameters, unsupported.
146//
147// First, non-const reference parameters are disallowed by the Google
148// style guide. Second, since we are doing argument forwarding it becomes
149// very tricky to avoid copies, maintain const correctness, and not
150// accidentally have the function be modifying a temporary, or a copy.
151void WontCompile() {
152 Parent p;
153 Callback<int(Parent&)> ref_arg_cb = Bind(&UnwrapParentRef);
154 ref_arg_cb.Run(p);
155}
156
Hans Wennborg80235482017-09-15 18:34:51157#elif defined(NCTEST_DISALLOW_BIND_TO_NON_CONST_REF_PARAM) // [r"fatal error: static_assert failed .*\"Bound argument \|i\| of type \|Arg\| cannot be forwarded as \|Unwrapped\| to the bound functor, which declares it as \|Param\|\.\""]
[email protected]81814bce2011-09-10 03:03:00158
159// Binding functions with reference parameters, unsupported.
160//
161// See comment in NCTEST_DISALLOW_NON_CONST_REF_PARAM
162void WontCompile() {
163 Parent p;
tzik3bc7779b2015-12-19 09:18:46164 Callback<int()> ref_cb = Bind(&UnwrapParentRef, p);
[email protected]81814bce2011-09-10 03:03:00165 ref_cb.Run();
166}
167
Hans Wennborg80235482017-09-15 18:34:51168#elif defined(NCTEST_NO_IMPLICIT_ARRAY_PTR_CONVERSION) // [r"fatal error: static_assert failed .*\"First bound argument to a method cannot be an array\.\""]
[email protected]81814bce2011-09-10 03:03:00169
170// A method should not be bindable with an array of objects.
171//
172// This is likely not wanted behavior. We specifically check for it though
173// because it is possible, depending on how you implement prebinding, to
174// implicitly convert an array type to a pointer type.
175void WontCompile() {
176 HasRef p[10];
tzik3bc7779b2015-12-19 09:18:46177 Callback<void()> method_bound_to_array_cb =
[email protected]81814bce2011-09-10 03:03:00178 Bind(&HasRef::VoidMethod0, p);
179 method_bound_to_array_cb.Run();
180}
181
Hans Wennborg80235482017-09-15 18:34:51182#elif defined(NCTEST_NO_RVALUE_RAW_PTR_FOR_REFCOUNTED_TYPES) // [r"fatal error: static_assert failed .*\"A parameter is a refcounted type and needs scoped_refptr\.\""]
[email protected]81814bce2011-09-10 03:03:00183
184// Refcounted types should not be bound as a raw pointer.
185void WontCompile() {
186 HasRef for_raw_ptr;
[email protected]7296f2762011-11-21 19:23:44187 int a;
tzik3bc7779b2015-12-19 09:18:46188 Callback<void()> ref_count_as_raw_ptr_a =
[email protected]7296f2762011-11-21 19:23:44189 Bind(&VoidPolymorphic1<int*>, &a);
tzik3bc7779b2015-12-19 09:18:46190 Callback<void()> ref_count_as_raw_ptr =
[email protected]81814bce2011-09-10 03:03:00191 Bind(&VoidPolymorphic1<HasRef*>, &for_raw_ptr);
192}
193
Hans Wennborg80235482017-09-15 18:34:51194#elif defined(NCTEST_NO_LVALUE_RAW_PTR_FOR_REFCOUNTED_TYPES) // [r"fatal error: static_assert failed .*\"A parameter is a refcounted type and needs scoped_refptr\.\""]
tzik1d692a2e2017-07-03 11:01:26195
196// Refcounted types should not be bound as a raw pointer.
197void WontCompile() {
198 HasRef* for_raw_ptr = nullptr;
199 Callback<void()> ref_count_as_raw_ptr =
200 Bind(&VoidPolymorphic1<HasRef*>, for_raw_ptr);
201}
202
Hans Wennborg80235482017-09-15 18:34:51203#elif defined(NCTEST_NO_RVALUE_CONST_RAW_PTR_FOR_REFCOUNTED_TYPES) // [r"fatal error: static_assert failed .*\"A parameter is a refcounted type and needs scoped_refptr\.\""]
tzikf41b9332017-07-10 09:13:33204
205// Refcounted types should not be bound as a raw pointer.
206void WontCompile() {
207 const HasRef for_raw_ptr;
208 Callback<void()> ref_count_as_raw_ptr =
209 Bind(&VoidPolymorphic1<const HasRef*>, &for_raw_ptr);
210}
211
Hans Wennborg80235482017-09-15 18:34:51212#elif defined(NCTEST_NO_LVALUE_CONST_RAW_PTR_FOR_REFCOUNTED_TYPES) // [r"fatal error: static_assert failed .*\"A parameter is a refcounted type and needs scoped_refptr\.\""]
tzikf41b9332017-07-10 09:13:33213
214// Refcounted types should not be bound as a raw pointer.
215void WontCompile() {
216 const HasRef* for_raw_ptr = nullptr;
217 Callback<void()> ref_count_as_raw_ptr =
218 Bind(&VoidPolymorphic1<const HasRef*>, for_raw_ptr);
219}
220
Hans Wennborg80235482017-09-15 18:34:51221#elif defined(NCTEST_WEAKPTR_BIND_MUST_RETURN_VOID) // [r"fatal error: static_assert failed .*\"weak_ptrs can only bind to methods without return values\""]
[email protected]81814bce2011-09-10 03:03:00222
223// WeakPtrs cannot be bound to methods with return types.
224void WontCompile() {
225 NoRef no_ref;
226 WeakPtrFactory<NoRef> weak_factory(&no_ref);
tzik3bc7779b2015-12-19 09:18:46227 Callback<int()> weak_ptr_with_non_void_return_type =
[email protected]81814bce2011-09-10 03:03:00228 Bind(&NoRef::IntMethod0, weak_factory.GetWeakPtr());
229 weak_ptr_with_non_void_return_type.Run();
230}
231
tzikdaa4f5a2016-02-12 13:54:37232#elif defined(NCTEST_DISALLOW_ASSIGN_DIFFERENT_TYPES) // [r"fatal error: no viable conversion from 'Callback<MakeUnboundRunType<void \(\*\)\(int\)>>' to 'Callback<void \(\)>'"]
[email protected]81814bce2011-09-10 03:03:00233
234// Bind result cannot be assigned to Callbacks with a mismatching type.
235void WontCompile() {
236 Closure callback_mismatches_bind_type = Bind(&VoidPolymorphic1<int>);
237}
238
wychen58b75f592016-12-23 07:08:44239#elif defined(NCTEST_DISALLOW_CAPTURING_LAMBDA) // [r"fatal error: implicit instantiation of undefined template 'base::internal::FunctorTraits<\(lambda at (\.\./)+base/bind_unittest.nc:[0-9]+:[0-9]+\), void>'"]
tzikc1db72652016-07-08 09:42:38240
241void WontCompile() {
wychen7525a842017-02-25 00:14:32242 int i = 0, j = 0;
243 Bind([i,&j]() {j = i;});
tzikc1db72652016-07-08 09:42:38244}
245
Hans Wennborg80235482017-09-15 18:34:51246#elif defined(NCTEST_DISALLOW_ONCECALLBACK_RUN_ON_LVALUE) // [r"static_assert failed .*\"OnceCallback::Run\(\) may only be invoked on a non-const rvalue, i\.e\. std::move\(callback\)\.Run\(\)\.\""]
dcheng77172b92016-11-22 07:46:06247
248void WontCompile() {
249 OnceClosure cb = Bind([] {});
250 cb.Run();
251}
252
Hans Wennborg80235482017-09-15 18:34:51253#elif defined(NCTEST_DISALLOW_ONCECALLBACK_RUN_ON_CONST_LVALUE) // [r"static_assert failed .*\"OnceCallback::Run\(\) may only be invoked on a non-const rvalue, i\.e\. std::move\(callback\)\.Run\(\)\.\""]
dcheng3f2fd66e2016-12-20 23:56:59254
255void WontCompile() {
256 const OnceClosure cb = Bind([] {});
257 cb.Run();
258}
259
Hans Wennborg80235482017-09-15 18:34:51260#elif defined(NCTEST_DISALLOW_ONCECALLBACK_RUN_ON_CONST_RVALUE) // [r"static_assert failed .*\"OnceCallback::Run\(\) may only be invoked on a non-const rvalue, i\.e\. std::move\(callback\)\.Run\(\)\.\""]
dcheng3f2fd66e2016-12-20 23:56:59261
262void WontCompile() {
263 const OnceClosure cb = Bind([] {});
264 std::move(cb).Run();
265}
266
Hans Wennborg80235482017-09-15 18:34:51267#elif defined(NCTEST_DISALLOW_BIND_ONCECALLBACK) // [r"fatal error: static_assert failed .*\"BindRepeating cannot bind OnceCallback. Use BindOnce with std::move\(\)\.\""]
tzikae4202e2017-07-31 10:41:54268
269void WontCompile() {
270 Bind(BindOnce([](int) {}), 42);
271}
272
Hans Wennborg80235482017-09-15 18:34:51273#elif defined(NCTEST_DISALLOW_BINDONCE_LVALUE_ONCECALLBACK) // [r"fatal error: static_assert failed .*\"BindOnce requires non-const rvalue for OnceCallback binding\."]
tzikae4202e2017-07-31 10:41:54274void WontCompile() {
275 auto cb = BindOnce([](int) {});
276 BindOnce(cb, 42);
277}
278
Hans Wennborg80235482017-09-15 18:34:51279#elif defined(NCTEST_DISALLOW_BINDONCE_RVALUE_CONST_ONCECALLBACK) // [r"fatal error: static_assert failed .*\"BindOnce requires non-const rvalue for OnceCallback binding\."]
tzikae4202e2017-07-31 10:41:54280
281void WontCompile() {
282 const auto cb = BindOnce([](int) {});
283 BindOnce(std::move(cb), 42);
284}
285
Hans Wennborg80235482017-09-15 18:34:51286#elif defined(NCTEST_BINDONCE_MOVEONLY_TYPE_BY_VALUE) // [r"fatal error: static_assert failed .*\"Bound argument \|i\| is move-only but will be bound by copy\. Ensure \|Arg\| is mutable and bound using std::move\(\)\.\""]
Daniel Chenga09cb602017-08-30 13:46:19287
288void WontCompile() {
289 std::unique_ptr<int> x;
290 BindOnce(&TakesMoveOnly, x);
291}
292
293#elif defined(NCTEST_BIND_MOVEONLY_TYPE_BY_VALUE) // [r"Bound argument \|i\| is move-only but will be forwarded by copy\. Ensure \|Arg\| is bound using base::Passed\(\), not std::move\(\)."]
294
295void WontCompile() {
296 std::unique_ptr<int> x;
297 Bind(&TakesMoveOnly, x);
298}
299
300#elif defined(NCTEST_BIND_MOVEONLY_TYPE_WITH_STDMOVE) // [r"Bound argument \|i\| is move-only but will be forwarded by copy\. Ensure \|Arg\| is bound using base::Passed\(\), not std::move\(\)."]
301
302void WontCompile() {
303 std::unique_ptr<int> x;
304 Bind(&TakesMoveOnly, std::move(x));
305}
306
307
[email protected]81814bce2011-09-10 03:03:00308#endif
309
310} // namespace base