ajwong@chromium.org | 505386d | 2011-09-10 12:03:00 +0900 | [diff] [blame] | 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. |
| 4 | |
scheib | 5616e12 | 2015-10-20 04:29:55 +0900 | [diff] [blame] | 5 | // This is a "No Compile Test" suite. |
| 6 | // http://dev.chromium.org/developers/testing/no-compile-tests |
| 7 | |
dcheng | b78a84f | 2016-09-24 14:05:57 +0900 | [diff] [blame] | 8 | #include <utility> |
| 9 | |
ajwong@chromium.org | 505386d | 2011-09-10 12:03:00 +0900 | [diff] [blame] | 10 | #include "base/bind.h" |
avi | a6a6a68 | 2015-12-27 07:15:14 +0900 | [diff] [blame] | 11 | #include "base/callback.h" |
| 12 | #include "base/macros.h" |
ajwong@chromium.org | c9c79af | 2011-11-22 04:23:44 +0900 | [diff] [blame] | 13 | #include "base/memory/ref_counted.h" |
ajwong@chromium.org | 505386d | 2011-09-10 12:03:00 +0900 | [diff] [blame] | 14 | |
| 15 | namespace base { |
| 16 | |
| 17 | // Do not put everything inside an anonymous namespace. If you do, many of the |
viettrungluu@chromium.org | d6f0292 | 2014-02-22 01:24:13 +0900 | [diff] [blame] | 18 | // helper function declarations will generate unused definition warnings. |
ajwong@chromium.org | 505386d | 2011-09-10 12:03:00 +0900 | [diff] [blame] | 19 | |
| 20 | static const int kParentValue = 1; |
| 21 | static const int kChildValue = 2; |
| 22 | |
| 23 | class NoRef { |
| 24 | public: |
| 25 | void VoidMethod0() {} |
| 26 | void VoidConstMethod0() const {} |
| 27 | int IntMethod0() { return 1; } |
| 28 | }; |
| 29 | |
ajwong@chromium.org | c9c79af | 2011-11-22 04:23:44 +0900 | [diff] [blame] | 30 | class HasRef : public NoRef, public base::RefCounted<HasRef> { |
ajwong@chromium.org | 505386d | 2011-09-10 12:03:00 +0900 | [diff] [blame] | 31 | }; |
| 32 | |
| 33 | class Parent { |
| 34 | public: |
tzik | 260fab5 | 2015-12-19 18:18:46 +0900 | [diff] [blame] | 35 | void AddRef() const {} |
| 36 | void Release() const {} |
ajwong@chromium.org | 505386d | 2011-09-10 12:03:00 +0900 | [diff] [blame] | 37 | virtual void VirtualSet() { value = kParentValue; } |
| 38 | void NonVirtualSet() { value = kParentValue; } |
| 39 | int value; |
| 40 | }; |
| 41 | |
| 42 | class Child : public Parent { |
| 43 | public: |
| 44 | virtual void VirtualSet() { value = kChildValue; } |
| 45 | void NonVirtualSet() { value = kChildValue; } |
| 46 | }; |
| 47 | |
| 48 | class NoRefParent { |
| 49 | public: |
| 50 | virtual void VirtualSet() { value = kParentValue; } |
| 51 | void NonVirtualSet() { value = kParentValue; } |
| 52 | int value; |
| 53 | }; |
| 54 | |
| 55 | class NoRefChild : public NoRefParent { |
| 56 | virtual void VirtualSet() { value = kChildValue; } |
| 57 | void NonVirtualSet() { value = kChildValue; } |
| 58 | }; |
| 59 | |
| 60 | template <typename T> |
| 61 | T PolymorphicIdentity(T t) { |
| 62 | return t; |
| 63 | } |
| 64 | |
| 65 | int UnwrapParentRef(Parent& p) { |
| 66 | return p.value; |
| 67 | } |
| 68 | |
| 69 | template <typename T> |
| 70 | void VoidPolymorphic1(T t) { |
| 71 | } |
| 72 | |
tzik | fa7381d | 2016-05-12 08:05:05 +0900 | [diff] [blame] | 73 | #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.org | 505386d | 2011-09-10 12:03:00 +0900 | [diff] [blame] | 74 | |
| 75 | // Method bound to const-object. |
| 76 | // |
| 77 | // Only const methods should be allowed to work with const objects. |
| 78 | void WontCompile() { |
| 79 | HasRef has_ref; |
| 80 | const HasRef* const_has_ref_ptr_ = &has_ref; |
tzik | 260fab5 | 2015-12-19 18:18:46 +0900 | [diff] [blame] | 81 | Callback<void()> method_to_const_cb = |
ajwong@chromium.org | 505386d | 2011-09-10 12:03:00 +0900 | [diff] [blame] | 82 | Bind(&HasRef::VoidMethod0, const_has_ref_ptr_); |
| 83 | method_to_const_cb.Run(); |
| 84 | } |
| 85 | |
dcheng | c7fd832 | 2014-11-04 06:29:30 +0900 | [diff] [blame] | 86 | #elif defined(NCTEST_METHOD_BIND_NEEDS_REFCOUNTED_OBJECT) // [r"fatal error: no member named 'AddRef' in 'base::NoRef'"] |
ajwong@chromium.org | 505386d | 2011-09-10 12:03:00 +0900 | [diff] [blame] | 87 | |
| 88 | // Method bound to non-refcounted object. |
| 89 | // |
| 90 | // We require refcounts unless you have Unretained(). |
| 91 | void WontCompile() { |
| 92 | NoRef no_ref; |
tzik | 260fab5 | 2015-12-19 18:18:46 +0900 | [diff] [blame] | 93 | Callback<void()> no_ref_cb = |
ajwong@chromium.org | 505386d | 2011-09-10 12:03:00 +0900 | [diff] [blame] | 94 | Bind(&NoRef::VoidMethod0, &no_ref); |
| 95 | no_ref_cb.Run(); |
| 96 | } |
| 97 | |
dcheng | c7fd832 | 2014-11-04 06:29:30 +0900 | [diff] [blame] | 98 | #elif defined(NCTEST_CONST_METHOD_NEEDS_REFCOUNTED_OBJECT) // [r"fatal error: no member named 'AddRef' in 'base::NoRef'"] |
ajwong@chromium.org | 505386d | 2011-09-10 12:03:00 +0900 | [diff] [blame] | 99 | |
| 100 | // Const Method bound to non-refcounted object. |
| 101 | // |
| 102 | // We require refcounts unless you have Unretained(). |
| 103 | void WontCompile() { |
| 104 | NoRef no_ref; |
tzik | 260fab5 | 2015-12-19 18:18:46 +0900 | [diff] [blame] | 105 | Callback<void()> no_ref_const_cb = |
ajwong@chromium.org | 505386d | 2011-09-10 12:03:00 +0900 | [diff] [blame] | 106 | Bind(&NoRef::VoidConstMethod0, &no_ref); |
| 107 | no_ref_const_cb.Run(); |
| 108 | } |
| 109 | |
tzik | 1c6525c | 2016-02-16 12:46:07 +0900 | [diff] [blame] | 110 | #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.org | 505386d | 2011-09-10 12:03:00 +0900 | [diff] [blame] | 111 | |
| 112 | // Const argument used with non-const pointer parameter of same type. |
| 113 | // |
| 114 | // This is just a const-correctness check. |
| 115 | void WontCompile() { |
| 116 | const NoRef* const_no_ref_ptr; |
tzik | 260fab5 | 2015-12-19 18:18:46 +0900 | [diff] [blame] | 117 | Callback<NoRef*()> pointer_same_cb = |
ajwong@chromium.org | 505386d | 2011-09-10 12:03:00 +0900 | [diff] [blame] | 118 | Bind(&PolymorphicIdentity<NoRef*>, const_no_ref_ptr); |
| 119 | pointer_same_cb.Run(); |
| 120 | } |
| 121 | |
tzik | 1c6525c | 2016-02-16 12:46:07 +0900 | [diff] [blame] | 122 | #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.org | 505386d | 2011-09-10 12:03:00 +0900 | [diff] [blame] | 123 | |
| 124 | // Const argument used with non-const pointer parameter of super type. |
| 125 | // |
| 126 | // This is just a const-correctness check. |
| 127 | void WontCompile() { |
| 128 | const NoRefChild* const_child_ptr; |
tzik | 260fab5 | 2015-12-19 18:18:46 +0900 | [diff] [blame] | 129 | Callback<NoRefParent*()> pointer_super_cb = |
ajwong@chromium.org | 505386d | 2011-09-10 12:03:00 +0900 | [diff] [blame] | 130 | Bind(&PolymorphicIdentity<NoRefParent*>, const_child_ptr); |
| 131 | pointer_super_cb.Run(); |
| 132 | } |
| 133 | |
dcheng | c7fd832 | 2014-11-04 06:29:30 +0900 | [diff] [blame] | 134 | #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.org | 505386d | 2011-09-10 12:03:00 +0900 | [diff] [blame] | 138 | |
| 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. |
| 145 | void WontCompile() { |
| 146 | Parent p; |
| 147 | Callback<int(Parent&)> ref_arg_cb = Bind(&UnwrapParentRef); |
| 148 | ref_arg_cb.Run(p); |
| 149 | } |
| 150 | |
tzik | a84047c | 2016-06-25 18:59:34 +0900 | [diff] [blame] | 151 | #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.org | 505386d | 2011-09-10 12:03:00 +0900 | [diff] [blame] | 152 | |
| 153 | // Binding functions with reference parameters, unsupported. |
| 154 | // |
| 155 | // See comment in NCTEST_DISALLOW_NON_CONST_REF_PARAM |
| 156 | void WontCompile() { |
| 157 | Parent p; |
tzik | 260fab5 | 2015-12-19 18:18:46 +0900 | [diff] [blame] | 158 | Callback<int()> ref_cb = Bind(&UnwrapParentRef, p); |
ajwong@chromium.org | 505386d | 2011-09-10 12:03:00 +0900 | [diff] [blame] | 159 | ref_cb.Run(); |
| 160 | } |
| 161 | |
tzik | 9f27e1f | 2016-07-01 14:54:12 +0900 | [diff] [blame] | 162 | #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.org | 505386d | 2011-09-10 12:03:00 +0900 | [diff] [blame] | 163 | |
| 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. |
| 169 | void WontCompile() { |
| 170 | HasRef p[10]; |
tzik | 260fab5 | 2015-12-19 18:18:46 +0900 | [diff] [blame] | 171 | Callback<void()> method_bound_to_array_cb = |
ajwong@chromium.org | 505386d | 2011-09-10 12:03:00 +0900 | [diff] [blame] | 172 | Bind(&HasRef::VoidMethod0, p); |
| 173 | method_bound_to_array_cb.Run(); |
| 174 | } |
| 175 | |
tzik | 9f27e1f | 2016-07-01 14:54:12 +0900 | [diff] [blame] | 176 | #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.org | 505386d | 2011-09-10 12:03:00 +0900 | [diff] [blame] | 177 | |
| 178 | // Refcounted types should not be bound as a raw pointer. |
| 179 | void WontCompile() { |
| 180 | HasRef for_raw_ptr; |
ajwong@chromium.org | c9c79af | 2011-11-22 04:23:44 +0900 | [diff] [blame] | 181 | int a; |
tzik | 260fab5 | 2015-12-19 18:18:46 +0900 | [diff] [blame] | 182 | Callback<void()> ref_count_as_raw_ptr_a = |
ajwong@chromium.org | c9c79af | 2011-11-22 04:23:44 +0900 | [diff] [blame] | 183 | Bind(&VoidPolymorphic1<int*>, &a); |
tzik | 260fab5 | 2015-12-19 18:18:46 +0900 | [diff] [blame] | 184 | Callback<void()> ref_count_as_raw_ptr = |
ajwong@chromium.org | 505386d | 2011-09-10 12:03:00 +0900 | [diff] [blame] | 185 | Bind(&VoidPolymorphic1<HasRef*>, &for_raw_ptr); |
| 186 | } |
| 187 | |
tzik | 587f17f | 2015-12-11 15:39:06 +0900 | [diff] [blame] | 188 | #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.org | 505386d | 2011-09-10 12:03:00 +0900 | [diff] [blame] | 189 | |
| 190 | // WeakPtrs cannot be bound to methods with return types. |
| 191 | void WontCompile() { |
| 192 | NoRef no_ref; |
| 193 | WeakPtrFactory<NoRef> weak_factory(&no_ref); |
tzik | 260fab5 | 2015-12-19 18:18:46 +0900 | [diff] [blame] | 194 | Callback<int()> weak_ptr_with_non_void_return_type = |
ajwong@chromium.org | 505386d | 2011-09-10 12:03:00 +0900 | [diff] [blame] | 195 | Bind(&NoRef::IntMethod0, weak_factory.GetWeakPtr()); |
| 196 | weak_ptr_with_non_void_return_type.Run(); |
| 197 | } |
| 198 | |
tzik | 817518d | 2016-02-12 22:54:37 +0900 | [diff] [blame] | 199 | #elif defined(NCTEST_DISALLOW_ASSIGN_DIFFERENT_TYPES) // [r"fatal error: no viable conversion from 'Callback<MakeUnboundRunType<void \(\*\)\(int\)>>' to 'Callback<void \(\)>'"] |
ajwong@chromium.org | 505386d | 2011-09-10 12:03:00 +0900 | [diff] [blame] | 200 | |
| 201 | // Bind result cannot be assigned to Callbacks with a mismatching type. |
| 202 | void WontCompile() { |
| 203 | Closure callback_mismatches_bind_type = Bind(&VoidPolymorphic1<int>); |
| 204 | } |
| 205 | |
wychen | 317e8c3 | 2016-12-23 16:08:44 +0900 | [diff] [blame] | 206 | #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>'"] |
tzik | 31d3fae | 2016-07-08 18:42:38 +0900 | [diff] [blame] | 207 | |
| 208 | void WontCompile() { |
wychen | 6625f1c | 2017-02-25 09:14:32 +0900 | [diff] [blame] | 209 | int i = 0, j = 0; |
| 210 | Bind([i,&j]() {j = i;}); |
tzik | 31d3fae | 2016-07-08 18:42:38 +0900 | [diff] [blame] | 211 | } |
| 212 | |
dcheng | b78a84f | 2016-09-24 14:05:57 +0900 | [diff] [blame] | 213 | #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 | |
| 215 | void WontCompile() { |
wychen | 3c94c6b | 2016-11-17 03:17:15 +0900 | [diff] [blame] | 216 | OnceClosure cb = BindOnce([] {}); |
| 217 | OnceClosure cb2 = BindOnce(std::move(cb)); |
dcheng | b78a84f | 2016-09-24 14:05:57 +0900 | [diff] [blame] | 218 | } |
| 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 | |
| 222 | void WontCompile() { |
| 223 | Closure cb = Bind([] {}); |
| 224 | Closure cb2 = Bind(cb); |
| 225 | } |
| 226 | |
dcheng | 397a22d | 2016-12-21 08:56:59 +0900 | [diff] [blame] | 227 | #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\(\)\.\""] |
dcheng | 83887ee | 2016-11-22 16:46:06 +0900 | [diff] [blame] | 228 | |
| 229 | void WontCompile() { |
| 230 | OnceClosure cb = Bind([] {}); |
| 231 | cb.Run(); |
| 232 | } |
| 233 | |
dcheng | 397a22d | 2016-12-21 08:56:59 +0900 | [diff] [blame] | 234 | #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 | |
| 236 | void 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 | |
| 243 | void WontCompile() { |
| 244 | const OnceClosure cb = Bind([] {}); |
| 245 | std::move(cb).Run(); |
| 246 | } |
| 247 | |
ajwong@chromium.org | 505386d | 2011-09-10 12:03:00 +0900 | [diff] [blame] | 248 | #endif |
| 249 | |
| 250 | } // namespace base |