blob: e7f2bd8702e36f57bf7b717b105ac8469a1ce036 [file] [log] [blame]
ajwong@chromium.org505386d2011-09-10 12:03:00 +09001// 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
scheib5616e122015-10-20 04:29:55 +09005// This is a "No Compile Test" suite.
6// http://dev.chromium.org/developers/testing/no-compile-tests
7
dchengb78a84f2016-09-24 14:05:57 +09008#include <utility>
9
ajwong@chromium.org505386d2011-09-10 12:03:00 +090010#include "base/bind.h"
avia6a6a682015-12-27 07:15:14 +090011#include "base/callback.h"
12#include "base/macros.h"
ajwong@chromium.orgc9c79af2011-11-22 04:23:44 +090013#include "base/memory/ref_counted.h"
ajwong@chromium.org505386d2011-09-10 12:03:00 +090014
15namespace base {
16
17// Do not put everything inside an anonymous namespace. If you do, many of the
viettrungluu@chromium.orgd6f02922014-02-22 01:24:13 +090018// helper function declarations will generate unused definition warnings.
ajwong@chromium.org505386d2011-09-10 12:03:00 +090019
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
ajwong@chromium.orgc9c79af2011-11-22 04:23:44 +090030class HasRef : public NoRef, public base::RefCounted<HasRef> {
ajwong@chromium.org505386d2011-09-10 12:03:00 +090031};
32
33class Parent {
34 public:
tzik260fab52015-12-19 18:18:46 +090035 void AddRef() const {}
36 void Release() const {}
ajwong@chromium.org505386d2011-09-10 12:03:00 +090037 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
tzikfa7381d2016-05-12 08:05:05 +090073#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"]
ajwong@chromium.org505386d2011-09-10 12:03:00 +090074
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;
tzik260fab52015-12-19 18:18:46 +090081 Callback<void()> method_to_const_cb =
ajwong@chromium.org505386d2011-09-10 12:03:00 +090082 Bind(&HasRef::VoidMethod0, const_has_ref_ptr_);
83 method_to_const_cb.Run();
84}
85
dchengc7fd8322014-11-04 06:29:30 +090086#elif defined(NCTEST_METHOD_BIND_NEEDS_REFCOUNTED_OBJECT) // [r"fatal error: no member named 'AddRef' in 'base::NoRef'"]
ajwong@chromium.org505386d2011-09-10 12:03:00 +090087
88// Method bound to non-refcounted object.
89//
90// We require refcounts unless you have Unretained().
91void WontCompile() {
92 NoRef no_ref;
tzik260fab52015-12-19 18:18:46 +090093 Callback<void()> no_ref_cb =
ajwong@chromium.org505386d2011-09-10 12:03:00 +090094 Bind(&NoRef::VoidMethod0, &no_ref);
95 no_ref_cb.Run();
96}
97
dchengc7fd8322014-11-04 06:29:30 +090098#elif defined(NCTEST_CONST_METHOD_NEEDS_REFCOUNTED_OBJECT) // [r"fatal error: no member named 'AddRef' in 'base::NoRef'"]
ajwong@chromium.org505386d2011-09-10 12:03:00 +090099
100// Const Method bound to non-refcounted object.
101//
102// We require refcounts unless you have Unretained().
103void WontCompile() {
104 NoRef no_ref;
tzik260fab52015-12-19 18:18:46 +0900105 Callback<void()> no_ref_const_cb =
ajwong@chromium.org505386d2011-09-10 12:03:00 +0900106 Bind(&NoRef::VoidConstMethod0, &no_ref);
107 no_ref_const_cb.Run();
108}
109
tzik1c6525c2016-02-16 12:46:07 +0900110#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'"]
ajwong@chromium.org505386d2011-09-10 12:03:00 +0900111
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;
tzik260fab52015-12-19 18:18:46 +0900117 Callback<NoRef*()> pointer_same_cb =
ajwong@chromium.org505386d2011-09-10 12:03:00 +0900118 Bind(&PolymorphicIdentity<NoRef*>, const_no_ref_ptr);
119 pointer_same_cb.Run();
120}
121
tzik1c6525c2016-02-16 12:46:07 +0900122#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'"]
ajwong@chromium.org505386d2011-09-10 12:03:00 +0900123
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;
tzik260fab52015-12-19 18:18:46 +0900129 Callback<NoRefParent*()> pointer_super_cb =
ajwong@chromium.org505386d2011-09-10 12:03:00 +0900130 Bind(&PolymorphicIdentity<NoRefParent*>, const_child_ptr);
131 pointer_super_cb.Run();
132}
133
dchengc7fd8322014-11-04 06:29:30 +0900134#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.
ajwong@chromium.org505386d2011-09-10 12:03:00 +0900138
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
tzika84047c2016-06-25 18:59:34 +0900151#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"]
ajwong@chromium.org505386d2011-09-10 12:03:00 +0900152
153// Binding functions with reference parameters, unsupported.
154//
155// See comment in NCTEST_DISALLOW_NON_CONST_REF_PARAM
156void WontCompile() {
157 Parent p;
tzik260fab52015-12-19 18:18:46 +0900158 Callback<int()> ref_cb = Bind(&UnwrapParentRef, p);
ajwong@chromium.org505386d2011-09-10 12:03:00 +0900159 ref_cb.Run();
160}
161
tzik9f27e1f2016-07-01 14:54:12 +0900162#elif defined(NCTEST_NO_IMPLICIT_ARRAY_PTR_CONVERSION) // [r"fatal error: static_assert failed \"First bound argument to a method cannot be an array.\""]
ajwong@chromium.org505386d2011-09-10 12:03:00 +0900163
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];
tzik260fab52015-12-19 18:18:46 +0900171 Callback<void()> method_bound_to_array_cb =
ajwong@chromium.org505386d2011-09-10 12:03:00 +0900172 Bind(&HasRef::VoidMethod0, p);
173 method_bound_to_array_cb.Run();
174}
175
tzik9f27e1f2016-07-01 14:54:12 +0900176#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.\""]
ajwong@chromium.org505386d2011-09-10 12:03:00 +0900177
178// Refcounted types should not be bound as a raw pointer.
179void WontCompile() {
180 HasRef for_raw_ptr;
ajwong@chromium.orgc9c79af2011-11-22 04:23:44 +0900181 int a;
tzik260fab52015-12-19 18:18:46 +0900182 Callback<void()> ref_count_as_raw_ptr_a =
ajwong@chromium.orgc9c79af2011-11-22 04:23:44 +0900183 Bind(&VoidPolymorphic1<int*>, &a);
tzik260fab52015-12-19 18:18:46 +0900184 Callback<void()> ref_count_as_raw_ptr =
ajwong@chromium.org505386d2011-09-10 12:03:00 +0900185 Bind(&VoidPolymorphic1<HasRef*>, &for_raw_ptr);
186}
187
tzik587f17f2015-12-11 15:39:06 +0900188#elif defined(NCTEST_WEAKPTR_BIND_MUST_RETURN_VOID) // [r"fatal error: static_assert failed \"weak_ptrs can only bind to methods without return values\""]
ajwong@chromium.org505386d2011-09-10 12:03:00 +0900189
190// WeakPtrs cannot be bound to methods with return types.
191void WontCompile() {
192 NoRef no_ref;
193 WeakPtrFactory<NoRef> weak_factory(&no_ref);
tzik260fab52015-12-19 18:18:46 +0900194 Callback<int()> weak_ptr_with_non_void_return_type =
ajwong@chromium.org505386d2011-09-10 12:03:00 +0900195 Bind(&NoRef::IntMethod0, weak_factory.GetWeakPtr());
196 weak_ptr_with_non_void_return_type.Run();
197}
198
tzik817518d2016-02-12 22:54:37 +0900199#elif defined(NCTEST_DISALLOW_ASSIGN_DIFFERENT_TYPES) // [r"fatal error: no viable conversion from 'Callback<MakeUnboundRunType<void \(\*\)\(int\)>>' to 'Callback<void \(\)>'"]
ajwong@chromium.org505386d2011-09-10 12:03:00 +0900200
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
wychen317e8c32016-12-23 16:08:44 +0900206#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>'"]
tzik31d3fae2016-07-08 18:42:38 +0900207
208void WontCompile() {
wychen6625f1c2017-02-25 09:14:32 +0900209 int i = 0, j = 0;
210 Bind([i,&j]() {j = i;});
tzik31d3fae2016-07-08 18:42:38 +0900211}
212
dchengb78a84f2016-09-24 14:05:57 +0900213#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() {
wychen3c94c6b2016-11-17 03:17:15 +0900216 OnceClosure cb = BindOnce([] {});
217 OnceClosure cb2 = BindOnce(std::move(cb));
dchengb78a84f2016-09-24 14:05:57 +0900218}
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
dcheng397a22d2016-12-21 08:56:59 +0900227#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\(\)\.\""]
dcheng83887ee2016-11-22 16:46:06 +0900228
229void WontCompile() {
230 OnceClosure cb = Bind([] {});
231 cb.Run();
232}
233
dcheng397a22d2016-12-21 08:56:59 +0900234#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
ajwong@chromium.org505386d2011-09-10 12:03:00 +0900248#endif
249
250} // namespace base