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.
5 // This is a "No Compile Test" suite.
6 // http://dev.chromium.org/developers/testing/no-compile-tests
10 #include "base/bind.h"
11 #include "base/callback.h"
12 #include "base/macros.h"
13 #include "base/memory/ref_counted.h"
14 #include "base/test/bind_test_util.h"
18 // Do not put everything inside an anonymous namespace. If you do, many of the
19 // helper function declarations will generate unused definition warnings.
21 static const int kParentValue = 1;
22 static const int kChildValue = 2;
27 void VoidConstMethod0() const {}
28 int IntMethod0() { return 1; }
31 class HasRef : public NoRef, public base::RefCounted<HasRef> {
36 void AddRef() const {}
37 void Release() const {}
38 virtual void VirtualSet() { value = kParentValue; }
39 void NonVirtualSet() { value = kParentValue; }
43 class Child : public Parent {
45 virtual void VirtualSet() { value = kChildValue; }
46 void NonVirtualSet() { value = kChildValue; }
51 virtual void VirtualSet() { value = kParentValue; }
52 void NonVirtualSet() { value = kParentValue; }
56 class NoRefChild : public NoRefParent {
57 virtual void VirtualSet() { value = kChildValue; }
58 void NonVirtualSet() { value = kChildValue; }
62 T PolymorphicIdentity(T t) {
66 int UnwrapParentRef(Parent& p) {
71 void VoidPolymorphic1(T t) {
74 void TakesMoveOnly(std::unique_ptr<int>) {
77 struct NonEmptyFunctor {
79 void operator()() const {}
82 // TODO(hans): Remove .* and update the static_assert expectations once we roll
83 // past Clang r313315. https://crbug.com/765692.
85 #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\|\.\""]
87 // Method bound to const-object.
89 // Only const methods should be allowed to work with const objects.
92 const HasRef* const_has_ref_ptr_ = &has_ref;
93 Callback<void()> method_to_const_cb =
94 Bind(&HasRef::VoidMethod0, const_has_ref_ptr_);
95 method_to_const_cb.Run();
98 #elif defined(NCTEST_METHOD_BIND_NEEDS_REFCOUNTED_OBJECT) // [r"fatal error: static_assert failed \"Receivers may not be raw pointers\."]
101 // Method bound to non-refcounted object.
103 // We require refcounts unless you have Unretained().
106 Callback<void()> no_ref_cb =
107 Bind(&NoRef::VoidMethod0, &no_ref);
111 #elif defined(NCTEST_CONST_METHOD_NEEDS_REFCOUNTED_OBJECT) // [r"fatal error: static_assert failed \"Receivers may not be raw pointers\."]
113 // Const Method bound to non-refcounted object.
115 // We require refcounts unless you have Unretained().
118 Callback<void()> no_ref_const_cb =
119 Bind(&NoRef::VoidConstMethod0, &no_ref);
120 no_ref_const_cb.Run();
123 #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\|\.\""]
125 // Const argument used with non-const pointer parameter of same type.
127 // This is just a const-correctness check.
129 const NoRef* const_no_ref_ptr;
130 Callback<NoRef*()> pointer_same_cb =
131 Bind(&PolymorphicIdentity<NoRef*>, const_no_ref_ptr);
132 pointer_same_cb.Run();
135 #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\|\.\""]
137 // Const argument used with non-const pointer parameter of super type.
139 // This is just a const-correctness check.
141 const NoRefChild* const_child_ptr;
142 Callback<NoRefParent*()> pointer_super_cb =
143 Bind(&PolymorphicIdentity<NoRefParent*>, const_child_ptr);
144 pointer_super_cb.Run();
147 #elif defined(DISABLED_NCTEST_DISALLOW_NON_CONST_REF_PARAM) // [r"fatal error: no member named 'AddRef' in 'base::NoRef'"]
148 // TODO(dcheng): I think there's a type safety promotion issue here where we can
149 // pass a const ref to a non const-ref function, or vice versa accidentally. Or
150 // we make a copy accidentally. Check.
152 // Functions with reference parameters, unsupported.
154 // First, non-const reference parameters are disallowed by the Google
155 // style guide. Second, since we are doing argument forwarding it becomes
156 // very tricky to avoid copies, maintain const correctness, and not
157 // accidentally have the function be modifying a temporary, or a copy.
160 Callback<int(Parent&)> ref_arg_cb = Bind(&UnwrapParentRef);
164 #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\|\.\""]
166 // Binding functions with reference parameters, unsupported.
168 // See comment in NCTEST_DISALLOW_NON_CONST_REF_PARAM
171 Callback<int()> ref_cb = Bind(&UnwrapParentRef, p);
175 #elif defined(NCTEST_NO_IMPLICIT_ARRAY_PTR_CONVERSION) // [r"fatal error: static_assert failed .*\"First bound argument to a method cannot be an array\.\""]
177 // A method should not be bindable with an array of objects.
179 // This is likely not wanted behavior. We specifically check for it though
180 // because it is possible, depending on how you implement prebinding, to
181 // implicitly convert an array type to a pointer type.
184 Callback<void()> method_bound_to_array_cb =
185 Bind(&HasRef::VoidMethod0, p);
186 method_bound_to_array_cb.Run();
189 #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\.\""]
191 // Refcounted types should not be bound as a raw pointer.
195 Callback<void()> ref_count_as_raw_ptr_a =
196 Bind(&VoidPolymorphic1<int*>, &a);
197 Callback<void()> ref_count_as_raw_ptr =
198 Bind(&VoidPolymorphic1<HasRef*>, &for_raw_ptr);
201 #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\.\""]
203 // Refcounted types should not be bound as a raw pointer.
205 HasRef* for_raw_ptr = nullptr;
206 Callback<void()> ref_count_as_raw_ptr =
207 Bind(&VoidPolymorphic1<HasRef*>, for_raw_ptr);
210 #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\.\""]
212 // Refcounted types should not be bound as a raw pointer.
214 const HasRef for_raw_ptr;
215 Callback<void()> ref_count_as_raw_ptr =
216 Bind(&VoidPolymorphic1<const HasRef*>, &for_raw_ptr);
219 #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\.\""]
221 // Refcounted types should not be bound as a raw pointer.
223 const HasRef* for_raw_ptr = nullptr;
224 Callback<void()> ref_count_as_raw_ptr =
225 Bind(&VoidPolymorphic1<const HasRef*>, for_raw_ptr);
228 #elif defined(NCTEST_WEAKPTR_BIND_MUST_RETURN_VOID) // [r"fatal error: static_assert failed .*\"weak_ptrs can only bind to methods without return values\""]
230 // WeakPtrs cannot be bound to methods with return types.
233 WeakPtrFactory<NoRef> weak_factory(&no_ref);
234 Callback<int()> weak_ptr_with_non_void_return_type =
235 Bind(&NoRef::IntMethod0, weak_factory.GetWeakPtr());
236 weak_ptr_with_non_void_return_type.Run();
239 #elif defined(NCTEST_DISALLOW_ASSIGN_DIFFERENT_TYPES) // [r"fatal error: no viable conversion from 'Callback<MakeUnboundRunType<void \(\*\)\(int\)>>' to 'Callback<void \(\)>'"]
241 // Bind result cannot be assigned to Callbacks with a mismatching type.
243 Closure callback_mismatches_bind_type = Bind(&VoidPolymorphic1<int>);
246 #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>'"]
250 Bind([i,&j]() {j = i;});
253 #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\(\)\.\""]
256 OnceClosure cb = Bind([] {});
260 #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\(\)\.\""]
263 const OnceClosure cb = Bind([] {});
267 #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\(\)\.\""]
270 const OnceClosure cb = Bind([] {});
274 #elif defined(NCTEST_DISALLOW_BIND_ONCECALLBACK) // [r"fatal error: static_assert failed .*\"BindRepeating cannot bind OnceCallback. Use BindOnce with std::move\(\)\.\""]
277 Bind(BindOnce([](int) {}), 42);
280 #elif defined(NCTEST_DISALLOW_BINDONCE_LVALUE_ONCECALLBACK) // [r"fatal error: static_assert failed .*\"BindOnce requires non-const rvalue for OnceCallback binding\."]
282 auto cb = BindOnce([](int) {});
286 #elif defined(NCTEST_DISALLOW_BINDONCE_RVALUE_CONST_ONCECALLBACK) // [r"fatal error: static_assert failed .*\"BindOnce requires non-const rvalue for OnceCallback binding\."]
289 const auto cb = BindOnce([](int) {});
290 BindOnce(std::move(cb), 42);
293 #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\(\)\.\""]
296 std::unique_ptr<int> x;
297 BindOnce(&TakesMoveOnly, x);
300 #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\(\)."]
303 std::unique_ptr<int> x;
304 Bind(&TakesMoveOnly, x);
307 #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\(\)."]
310 std::unique_ptr<int> x;
311 Bind(&TakesMoveOnly, std::move(x));
314 #elif defined(NCTEST_BIND_NON_EMPTY_FUNCTOR) // [r"fatal error: implicit instantiation of undefined template 'base::internal::FunctorTraits<base::NonEmptyFunctor, void>'"]
317 Bind(NonEmptyFunctor());