blob: e6b253480873f97009e535a492d1470636b73b4a [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
tzik4435e8042016-05-11 23:05:0573#if defined(NCTEST_METHOD_ON_CONST_OBJECT) // [r"fatal error: binding value of type 'const base::HasRef' to reference to type 'base::NoRef' drops 'const' qualifier"]
[email protected]81814bce2011-09-10 03:03:0074
75// Method bound to const-object.
76//
77// Only const methods should be allowed to work with const objects.
78void WontCompile() {
79 HasRef has_ref;
80 const HasRef* const_has_ref_ptr_ = &has_ref;
tzik3bc7779b2015-12-19 09:18:4681 Callback<void()> method_to_const_cb =
[email protected]81814bce2011-09-10 03:03:0082 Bind(&HasRef::VoidMethod0, const_has_ref_ptr_);
83 method_to_const_cb.Run();
84}
85
dchengb760d722014-11-03 21:29:3086#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:0087
88// Method bound to non-refcounted object.
89//
90// We require refcounts unless you have Unretained().
91void WontCompile() {
92 NoRef no_ref;
tzik3bc7779b2015-12-19 09:18:4693 Callback<void()> no_ref_cb =
[email protected]81814bce2011-09-10 03:03:0094 Bind(&NoRef::VoidMethod0, &no_ref);
95 no_ref_cb.Run();
96}
97
dchengb760d722014-11-03 21:29:3098#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:0099
100// Const Method bound to non-refcounted object.
101//
102// We require refcounts unless you have Unretained().
103void WontCompile() {
104 NoRef no_ref;
tzik3bc7779b2015-12-19 09:18:46105 Callback<void()> no_ref_const_cb =
[email protected]81814bce2011-09-10 03:03:00106 Bind(&NoRef::VoidConstMethod0, &no_ref);
107 no_ref_const_cb.Run();
108}
109
tzik0d811a902016-02-16 03:46:07110#elif defined(NCTEST_CONST_POINTER) // [r"fatal error: cannot initialize a parameter of type 'base::NoRef \*' with an lvalue of type 'const base::NoRef \*const'"]
[email protected]81814bce2011-09-10 03:03:00111
112// Const argument used with non-const pointer parameter of same type.
113//
114// This is just a const-correctness check.
115void WontCompile() {
116 const NoRef* const_no_ref_ptr;
tzik3bc7779b2015-12-19 09:18:46117 Callback<NoRef*()> pointer_same_cb =
[email protected]81814bce2011-09-10 03:03:00118 Bind(&PolymorphicIdentity<NoRef*>, const_no_ref_ptr);
119 pointer_same_cb.Run();
120}
121
tzik0d811a902016-02-16 03:46:07122#elif defined(NCTEST_CONST_POINTER_SUBTYPE) // [r"fatal error: cannot initialize a parameter of type 'base::NoRefParent \*' with an lvalue of type 'const base::NoRefChild \*const'"]
[email protected]81814bce2011-09-10 03:03:00123
124// Const argument used with non-const pointer parameter of super type.
125//
126// This is just a const-correctness check.
127void WontCompile() {
128 const NoRefChild* const_child_ptr;
tzik3bc7779b2015-12-19 09:18:46129 Callback<NoRefParent*()> pointer_super_cb =
[email protected]81814bce2011-09-10 03:03:00130 Bind(&PolymorphicIdentity<NoRefParent*>, const_child_ptr);
131 pointer_super_cb.Run();
132}
133
dchengb760d722014-11-03 21:29:30134#elif defined(DISABLED_NCTEST_DISALLOW_NON_CONST_REF_PARAM) // [r"fatal error: no member named 'AddRef' in 'base::NoRef'"]
135// TODO(dcheng): I think there's a type safety promotion issue here where we can
136// pass a const ref to a non const-ref function, or vice versa accidentally. Or
137// we make a copy accidentally. Check.
[email protected]81814bce2011-09-10 03:03:00138
139// Functions with reference parameters, unsupported.
140//
141// First, non-const reference parameters are disallowed by the Google
142// style guide. Second, since we are doing argument forwarding it becomes
143// very tricky to avoid copies, maintain const correctness, and not
144// accidentally have the function be modifying a temporary, or a copy.
145void WontCompile() {
146 Parent p;
147 Callback<int(Parent&)> ref_arg_cb = Bind(&UnwrapParentRef);
148 ref_arg_cb.Run(p);
149}
150
tzik9921447d2016-06-25 09:59:34151#elif defined(NCTEST_DISALLOW_BIND_TO_NON_CONST_REF_PARAM) // [r"fatal error: binding value of type 'const base::Parent' to reference to type 'base::Parent' drops 'const' qualifier"]
[email protected]81814bce2011-09-10 03:03:00152
153// Binding functions with reference parameters, unsupported.
154//
155// See comment in NCTEST_DISALLOW_NON_CONST_REF_PARAM
156void WontCompile() {
157 Parent p;
tzik3bc7779b2015-12-19 09:18:46158 Callback<int()> ref_cb = Bind(&UnwrapParentRef, p);
[email protected]81814bce2011-09-10 03:03:00159 ref_cb.Run();
160}
161
tzik99de02b2016-07-01 05:54:12162#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:00163
164// A method should not be bindable with an array of objects.
165//
166// This is likely not wanted behavior. We specifically check for it though
167// because it is possible, depending on how you implement prebinding, to
168// implicitly convert an array type to a pointer type.
169void WontCompile() {
170 HasRef p[10];
tzik3bc7779b2015-12-19 09:18:46171 Callback<void()> method_bound_to_array_cb =
[email protected]81814bce2011-09-10 03:03:00172 Bind(&HasRef::VoidMethod0, p);
173 method_bound_to_array_cb.Run();
174}
175
tzik99de02b2016-07-01 05:54:12176#elif defined(NCTEST_NO_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:00177
178// Refcounted types should not be bound as a raw pointer.
179void WontCompile() {
180 HasRef for_raw_ptr;
[email protected]7296f2762011-11-21 19:23:44181 int a;
tzik3bc7779b2015-12-19 09:18:46182 Callback<void()> ref_count_as_raw_ptr_a =
[email protected]7296f2762011-11-21 19:23:44183 Bind(&VoidPolymorphic1<int*>, &a);
tzik3bc7779b2015-12-19 09:18:46184 Callback<void()> ref_count_as_raw_ptr =
[email protected]81814bce2011-09-10 03:03:00185 Bind(&VoidPolymorphic1<HasRef*>, &for_raw_ptr);
186}
187
tzik096369a2015-12-11 06:39:06188#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:00189
190// WeakPtrs cannot be bound to methods with return types.
191void WontCompile() {
192 NoRef no_ref;
193 WeakPtrFactory<NoRef> weak_factory(&no_ref);
tzik3bc7779b2015-12-19 09:18:46194 Callback<int()> weak_ptr_with_non_void_return_type =
[email protected]81814bce2011-09-10 03:03:00195 Bind(&NoRef::IntMethod0, weak_factory.GetWeakPtr());
196 weak_ptr_with_non_void_return_type.Run();
197}
198
tzikdaa4f5a2016-02-12 13:54:37199#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:00200
201// Bind result cannot be assigned to Callbacks with a mismatching type.
202void WontCompile() {
203 Closure callback_mismatches_bind_type = Bind(&VoidPolymorphic1<int>);
204}
205
wychen58b75f592016-12-23 07:08:44206#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:38207
208void WontCompile() {
209 int i = 0;
210 Bind([i]() {});
211}
212
dcheng172b6ad2016-09-24 05:05:57213#elif defined(NCTEST_DISALLOW_BINDING_ONCE_CALLBACK_WITH_NO_ARGS) // [r"static_assert failed \"Attempting to bind a base::Callback with no additional arguments: save a heap allocation and use the original base::Callback object\""]
214
215void WontCompile() {
wychenb2e1f31c2016-11-16 18:17:15216 OnceClosure cb = BindOnce([] {});
217 OnceClosure cb2 = BindOnce(std::move(cb));
dcheng172b6ad2016-09-24 05:05:57218}
219
220#elif defined(NCTEST_DISALLOW_BINDING_REPEATING_CALLBACK_WITH_NO_ARGS) // [r"static_assert failed \"Attempting to bind a base::Callback with no additional arguments: save a heap allocation and use the original base::Callback object\""]
221
222void WontCompile() {
223 Closure cb = Bind([] {});
224 Closure cb2 = Bind(cb);
225}
226
dcheng3f2fd66e2016-12-20 23:56:59227#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:06228
229void WontCompile() {
230 OnceClosure cb = Bind([] {});
231 cb.Run();
232}
233
dcheng3f2fd66e2016-12-20 23:56:59234#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\(\)\.\""]
235
236void WontCompile() {
237 const OnceClosure cb = Bind([] {});
238 cb.Run();
239}
240
241#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\(\)\.\""]
242
243void WontCompile() {
244 const OnceClosure cb = Bind([] {});
245 std::move(cb).Run();
246}
247
[email protected]81814bce2011-09-10 03:03:00248#endif
249
250} // namespace base