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.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 void TakesIntRef(int& ref) {}
79 struct NonEmptyFunctor {
81 void operator()() const {}
84 #if defined(NCTEST_METHOD_ON_CONST_OBJECT) // [r"static_assert failed.+?BindArgument<0>::ForwardedAs<.+?>::ToParamWithType<.+?>::kCanBeForwardedToBoundFunctor.+?Type mismatch between bound argument and bound functor's parameter\."]
86 // Method bound to const-object.
88 // Only const methods should be allowed to work with const objects.
91 const HasRef* const_has_ref_ptr_ = &has_ref;
92 RepeatingCallback<void()> method_to_const_cb =
93 BindRepeating(&HasRef::VoidMethod0, const_has_ref_ptr_);
94 method_to_const_cb.Run();
97 #elif defined(NCTEST_METHOD_BIND_NEEDS_REFCOUNTED_OBJECT) // [r"fatal error: static_assert failed due to requirement '!std::is_pointer<base::NoRef *>::value || 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.\""]
100 // Method bound to non-refcounted object.
102 // We require refcounts unless you have Unretained().
105 RepeatingCallback<void()> no_ref_cb =
106 BindRepeating(&NoRef::VoidMethod0, &no_ref);
110 #elif defined(NCTEST_CONST_METHOD_NEEDS_REFCOUNTED_OBJECT) // [r"fatal error: static_assert failed due to requirement '!std::is_pointer<base::NoRef *>::value || 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.\""]
112 // Const Method bound to non-refcounted object.
114 // We require refcounts unless you have Unretained().
117 RepeatingCallback<void()> no_ref_const_cb =
118 BindRepeating(&NoRef::VoidConstMethod0, &no_ref);
119 no_ref_const_cb.Run();
122 #elif defined(NCTEST_CONST_POINTER) // [r"static_assert failed.+?BindArgument<0>::ForwardedAs<.+?>::ToParamWithType<.+?>::kCanBeForwardedToBoundFunctor.+?Type mismatch between bound argument and bound functor's parameter\."]
123 // Const argument used with non-const pointer parameter of same type.
125 // This is just a const-correctness check.
127 const NoRef* const_no_ref_ptr;
128 RepeatingCallback<NoRef*()> pointer_same_cb =
129 BindRepeating(&PolymorphicIdentity<NoRef*>, const_no_ref_ptr);
130 pointer_same_cb.Run();
133 #elif defined(NCTEST_CONST_POINTER_SUBTYPE) // [r"static_assert failed.+?BindArgument<0>::ForwardedAs<.+?>::ToParamWithType<.+?>::kCanBeForwardedToBoundFunctor.+?Type mismatch between bound argument and bound functor's parameter\."]
135 // Const argument used with non-const pointer parameter of super type.
137 // This is just a const-correctness check.
139 const NoRefChild* const_child_ptr;
140 RepeatingCallback<NoRefParent*()> pointer_super_cb =
141 BindRepeating(&PolymorphicIdentity<NoRefParent*>, const_child_ptr);
142 pointer_super_cb.Run();
145 #elif defined(DISABLED_NCTEST_DISALLOW_NON_CONST_REF_PARAM) // [r"fatal error: no member named 'AddRef' in 'base::NoRef'"]
146 // TODO(dcheng): I think there's a type safety promotion issue here where we can
147 // pass a const ref to a non const-ref function, or vice versa accidentally. Or
148 // we make a copy accidentally. Check.
150 // Functions with reference parameters, unsupported.
152 // First, non-const reference parameters are disallowed by the Google
153 // style guide. Second, since we are doing argument forwarding it becomes
154 // very tricky to avoid copies, maintain const correctness, and not
155 // accidentally have the function be modifying a temporary, or a copy.
158 RepeatingCallback<int(Parent&)> ref_arg_cb = BindRepeating(&UnwrapParentRef);
162 #elif defined(NCTEST_BIND_ONCE_WITH_NON_CONST_REF_PARAM) // [r"static_assert 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\(\).\""]
164 // Binding functions with reference parameters requires `std::ref()` or
165 // 'base::OwnedRef()`.
168 auto cb = BindOnce(&TakesIntRef, v);
171 #elif defined(NCTEST_BIND_REPEATING_WITH_NON_CONST_REF_PARAM) // [r"static_assert 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\(\).\""]
173 // Binding functions with reference parameters requires `std::ref()` or
174 // 'base::OwnedRef()`.
177 auto cb = BindRepeating(&TakesIntRef, v);
180 #elif defined(NCTEST_NON_CONST_REF_PARAM_WRONG_TYPE) // [r"static_assert failed due to requirement 'BindArgument<0>::ForwardedAs<.+?>::ToParamWithType<.+?>::kCanBeForwardedToBoundFunctor' \"Type mismatch between bound argument and bound functor's parameter.\""]
182 // If the argument and parameter types mismatch then the compile error should be
183 // the generic type mismatch error.
186 auto cb = BindOnce(&TakesIntRef, f);
189 #elif defined(NCTEST_NON_CONST_REF_PARAM_WRONG_TYPE_AND_WRAPPED) // [r"static_assert failed due to requirement 'BindArgument<0>::ForwardedAs<.+?>::ToParamWithType<.+?>::kCanBeForwardedToBoundFunctor' \"Type mismatch between bound argument and bound functor's parameter.\""]
191 // If the argument and parameter types mismatch then the compile error should be
192 // the generic type mismatch error even if the argument is wrapped in
196 auto cb = BindOnce(&TakesIntRef, base::OwnedRef(f));
199 #elif defined(NCTEST_NO_IMPLICIT_ARRAY_PTR_CONVERSION) // [r"fatal error: static_assert failed due to requirement '!std::is_array<base::HasRef \[10\]>::value' \"First bound argument to a method cannot be an array.\""]
201 // A method should not be bindable with an array of objects.
203 // This is likely not wanted behavior. We specifically check for it though
204 // because it is possible, depending on how you implement prebinding, to
205 // implicitly convert an array type to a pointer type.
208 RepeatingCallback<void()> method_bound_to_array_cb =
209 BindRepeating(&HasRef::VoidMethod0, p);
210 method_bound_to_array_cb.Run();
213 #elif defined(NCTEST_NO_RVALUE_RAW_PTR_FOR_REFCOUNTED_TYPES) // [r"fatal error: static_assert failed due to requirement '!HasRefCountedTypeAsRawPtr<base::HasRef \*>::value' \"A parameter is a refcounted type and needs scoped_refptr.\""]
215 // Refcounted types should not be bound as a raw pointer.
219 RepeatingCallback<void()> ref_count_as_raw_ptr_a =
220 BindRepeating(&VoidPolymorphic1<int*>, &a);
221 RepeatingCallback<void()> ref_count_as_raw_ptr =
222 BindRepeating(&VoidPolymorphic1<HasRef*>, &for_raw_ptr);
225 #elif defined(NCTEST_NO_LVALUE_RAW_PTR_FOR_REFCOUNTED_TYPES) // [r"fatal error: static_assert failed due to requirement '!HasRefCountedTypeAsRawPtr<base::HasRef \*>::value' \"A parameter is a refcounted type and needs scoped_refptr.\""]
227 // Refcounted types should not be bound as a raw pointer.
229 HasRef* for_raw_ptr = nullptr;
230 RepeatingCallback<void()> ref_count_as_raw_ptr =
231 BindRepeating(&VoidPolymorphic1<HasRef*>, for_raw_ptr);
234 #elif defined(NCTEST_NO_RVALUE_CONST_RAW_PTR_FOR_REFCOUNTED_TYPES) // [r"fatal error: static_assert failed due to requirement '!HasRefCountedTypeAsRawPtr<const base::HasRef \*>::value' \"A parameter is a refcounted type and needs scoped_refptr.\""]
236 // Refcounted types should not be bound as a raw pointer.
238 const HasRef for_raw_ptr;
239 RepeatingCallback<void()> ref_count_as_raw_ptr =
240 BindRepeating(&VoidPolymorphic1<const HasRef*>, &for_raw_ptr);
243 #elif defined(NCTEST_NO_LVALUE_CONST_RAW_PTR_FOR_REFCOUNTED_TYPES) // [r"fatal error: static_assert failed due to requirement '!HasRefCountedTypeAsRawPtr<const base::HasRef \*>::value' \"A parameter is a refcounted type and needs scoped_refptr.\""]
245 // Refcounted types should not be bound as a raw pointer.
247 const HasRef* for_raw_ptr = nullptr;
248 RepeatingCallback<void()> ref_count_as_raw_ptr =
249 BindRepeating(&VoidPolymorphic1<const HasRef*>, for_raw_ptr);
252 #elif defined(NCTEST_WEAKPTR_BIND_MUST_RETURN_VOID) // [r"fatal error: static_assert failed due to requirement 'std::is_void<int>::value' \"weak_ptrs can only bind to methods without return values\""]
254 // WeakPtrs cannot be bound to methods with return types.
257 WeakPtrFactory<NoRef> weak_factory(&no_ref);
258 RepeatingCallback<int()> weak_ptr_with_non_void_return_type =
259 BindRepeating(&NoRef::IntMethod0, weak_factory.GetWeakPtr());
260 weak_ptr_with_non_void_return_type.Run();
263 #elif defined(NCTEST_DISALLOW_ASSIGN_DIFFERENT_TYPES) // [r"fatal error: no viable conversion from 'RepeatingCallback<internal::MakeUnboundRunType<void \(\*\)\(int\)>>' to 'RepeatingCallback<void \(\)>'"]
265 // Bind result cannot be assigned to Callbacks with a mismatching type.
267 RepeatingClosure callback_mismatches_bind_type =
268 BindRepeating(&VoidPolymorphic1<int>);
271 #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]+\)>'"]
275 BindOnce([i,&j]() {j = i;});
278 #elif defined(NCTEST_DISALLOW_ONCECALLBACK_RUN_ON_LVALUE) // [r"fatal error: static_assert failed due to requirement '!sizeof \(\*this\)' \"OnceCallback::Run\(\) may only be invoked on a non-const rvalue, i\.e\. std::move\(callback\).Run\(\).\""]
281 OnceClosure cb = BindOnce([] {});
285 #elif defined(NCTEST_DISALLOW_ONCECALLBACK_RUN_ON_CONST_LVALUE) // [r"fatal error: static_assert failed due to requirement '!sizeof \(\*this\)' \"OnceCallback::Run\(\) may only be invoked on a non-const rvalue, i\.e\. std::move\(callback\).Run\(\).\""]
288 const OnceClosure cb = BindOnce([] {});
292 #elif defined(NCTEST_DISALLOW_ONCECALLBACK_RUN_ON_CONST_RVALUE) // [r"fatal error: static_assert failed due to requirement '!sizeof \(\*this\)' \"OnceCallback::Run\(\) may only be invoked on a non-const rvalue, i\.e\. std::move\(callback\).Run\(\).\""]
295 const OnceClosure cb = BindOnce([] {});
299 #elif defined(NCTEST_DISALLOW_BIND_ONCECALLBACK) // [r"fatal error: static_assert failed due to requirement '!base::internal::IsOnceCallback<base::OnceCallback<void \(int\)> ?>\(\)' \"BindRepeating cannot bind OnceCallback. Use BindOnce with std::move\(\).\""]
302 BindRepeating(BindOnce([](int) {}), 42);
305 #elif defined(NCTEST_DISALLOW_BINDONCE_LVALUE_ONCECALLBACK) // [r"fatal error: static_assert 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)).\""]
307 auto cb = BindOnce([](int) {});
311 #elif defined(NCTEST_DISALLOW_BINDONCE_RVALUE_CONST_ONCECALLBACK) // [r"fatal error: static_assert 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)).\""]
314 const auto cb = BindOnce([](int) {});
315 BindOnce(std::move(cb), 42);
318 #elif defined(NCTEST_BINDONCE_MOVEONLY_TYPE_BY_VALUE) // [r"static_assert failed.+?BindArgument<0>::BoundAs<.+?>::StoredAs<.+?>::kMoveOnlyTypeMustUseStdMove.+?Attempting to bind a move-only type\. Use std::move\(\) to transfer ownership to the created callback\."]
321 std::unique_ptr<int> x;
322 BindOnce(&TakesMoveOnly, x);
325 #elif defined(NCTEST_BIND_MOVEONLY_TYPE_BY_VALUE) // [r"static_assert 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\."]
328 std::unique_ptr<int> x;
329 BindRepeating(&TakesMoveOnly, x);
332 #elif defined(NCTEST_BIND_MOVEONLY_TYPE_WITH_STDMOVE) // [r"static_assert 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\."]
335 std::unique_ptr<int> x;
336 BindRepeating(&TakesMoveOnly, std::move(x));
339 #elif defined(NCTEST_BIND_NON_EMPTY_FUNCTOR) // [r"fatal error: implicit instantiation of undefined template 'base::internal::FunctorTraits<base::NonEmptyFunctor>'"]
342 BindRepeating(NonEmptyFunctor());
345 #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\)\)."]
348 auto mutable_lambda = [&]() mutable {};
349 BindLambdaForTesting(mutable_lambda);
352 #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\)\)."]
356 const auto mutable_lambda = [&]() mutable {};
357 BindLambdaForTesting(std::move(mutable_lambda));
360 #elif defined(NCTEST_BIND_UNCOPYABLE_AND_UNMOVABLE_TYPE) // [r"static_assert failed.+?BindArgument<0>::BoundAs<.+?>::StoredAs<.+?>::kBindArgumentCanBeCaptured.+?Cannot capture argument: is the argument copyable or movable\?"]
363 struct UncopyableUnmovable {
364 UncopyableUnmovable() = default;
365 UncopyableUnmovable(const UncopyableUnmovable&) = delete;
366 UncopyableUnmovable& operator=(const UncopyableUnmovable&) = delete;
369 UncopyableUnmovable u;
370 BindOnce([] (const UncopyableUnmovable&) {}, u);
373 #elif defined(NCTEST_BIND_ONCE_WITH_PASSED) // [r"static_assert failed.+?Use std::move\(\) instead of base::Passed\(\) with base::BindOnce\(\)"]
376 std::unique_ptr<int> x;
377 BindOnce([] (std::unique_ptr<int>) {}, Passed(&x));