[email protected] | 81814bce | 2011-09-10 03:03:00 | [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 | 0411d8f7 | 2015-10-19 19:29:55 | [diff] [blame] | 5 | // This is a "No Compile Test" suite. |
| 6 | // http://dev.chromium.org/developers/testing/no-compile-tests |
| 7 | |
dcheng | 172b6ad | 2016-09-24 05:05:57 | [diff] [blame] | 8 | #include <utility> |
| 9 | |
[email protected] | 81814bce | 2011-09-10 03:03:00 | [diff] [blame] | 10 | #include "base/bind.h" |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 11 | #include "base/callback.h" |
| 12 | #include "base/macros.h" |
[email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 13 | #include "base/memory/ref_counted.h" |
[email protected] | 81814bce | 2011-09-10 03:03:00 | [diff] [blame] | 14 | |
| 15 | namespace base { |
| 16 | |
| 17 | // Do not put everything inside an anonymous namespace. If you do, many of the |
[email protected] | 5dd6bdf | 2014-02-21 16:24:13 | [diff] [blame] | 18 | // helper function declarations will generate unused definition warnings. |
[email protected] | 81814bce | 2011-09-10 03:03:00 | [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 | |
[email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 30 | class HasRef : public NoRef, public base::RefCounted<HasRef> { |
[email protected] | 81814bce | 2011-09-10 03:03:00 | [diff] [blame] | 31 | }; |
| 32 | |
| 33 | class Parent { |
| 34 | public: |
tzik | 3bc7779b | 2015-12-19 09:18:46 | [diff] [blame] | 35 | void AddRef() const {} |
| 36 | void Release() const {} |
[email protected] | 81814bce | 2011-09-10 03:03:00 | [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 | 4435e804 | 2016-05-11 23:05:05 | [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"] |
[email protected] | 81814bce | 2011-09-10 03:03:00 | [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 | 3bc7779b | 2015-12-19 09:18:46 | [diff] [blame] | 81 | Callback<void()> method_to_const_cb = |
[email protected] | 81814bce | 2011-09-10 03:03:00 | [diff] [blame] | 82 | Bind(&HasRef::VoidMethod0, const_has_ref_ptr_); |
| 83 | method_to_const_cb.Run(); |
| 84 | } |
| 85 | |
dcheng | b760d72 | 2014-11-03 21:29:30 | [diff] [blame] | 86 | #elif defined(NCTEST_METHOD_BIND_NEEDS_REFCOUNTED_OBJECT) // [r"fatal error: no member named 'AddRef' in 'base::NoRef'"] |
[email protected] | 81814bce | 2011-09-10 03:03:00 | [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 | 3bc7779b | 2015-12-19 09:18:46 | [diff] [blame] | 93 | Callback<void()> no_ref_cb = |
[email protected] | 81814bce | 2011-09-10 03:03:00 | [diff] [blame] | 94 | Bind(&NoRef::VoidMethod0, &no_ref); |
| 95 | no_ref_cb.Run(); |
| 96 | } |
| 97 | |
dcheng | b760d72 | 2014-11-03 21:29:30 | [diff] [blame] | 98 | #elif defined(NCTEST_CONST_METHOD_NEEDS_REFCOUNTED_OBJECT) // [r"fatal error: no member named 'AddRef' in 'base::NoRef'"] |
[email protected] | 81814bce | 2011-09-10 03:03:00 | [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 | 3bc7779b | 2015-12-19 09:18:46 | [diff] [blame] | 105 | Callback<void()> no_ref_const_cb = |
[email protected] | 81814bce | 2011-09-10 03:03:00 | [diff] [blame] | 106 | Bind(&NoRef::VoidConstMethod0, &no_ref); |
| 107 | no_ref_const_cb.Run(); |
| 108 | } |
| 109 | |
tzik | 0d811a90 | 2016-02-16 03:46:07 | [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'"] |
[email protected] | 81814bce | 2011-09-10 03:03:00 | [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 | 3bc7779b | 2015-12-19 09:18:46 | [diff] [blame] | 117 | Callback<NoRef*()> pointer_same_cb = |
[email protected] | 81814bce | 2011-09-10 03:03:00 | [diff] [blame] | 118 | Bind(&PolymorphicIdentity<NoRef*>, const_no_ref_ptr); |
| 119 | pointer_same_cb.Run(); |
| 120 | } |
| 121 | |
tzik | 0d811a90 | 2016-02-16 03:46:07 | [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'"] |
[email protected] | 81814bce | 2011-09-10 03:03:00 | [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 | 3bc7779b | 2015-12-19 09:18:46 | [diff] [blame] | 129 | Callback<NoRefParent*()> pointer_super_cb = |
[email protected] | 81814bce | 2011-09-10 03:03:00 | [diff] [blame] | 130 | Bind(&PolymorphicIdentity<NoRefParent*>, const_child_ptr); |
| 131 | pointer_super_cb.Run(); |
| 132 | } |
| 133 | |
dcheng | b760d72 | 2014-11-03 21:29:30 | [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. |
[email protected] | 81814bce | 2011-09-10 03:03:00 | [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 | 9921447d | 2016-06-25 09:59:34 | [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"] |
[email protected] | 81814bce | 2011-09-10 03:03:00 | [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 | 3bc7779b | 2015-12-19 09:18:46 | [diff] [blame] | 158 | Callback<int()> ref_cb = Bind(&UnwrapParentRef, p); |
[email protected] | 81814bce | 2011-09-10 03:03:00 | [diff] [blame] | 159 | ref_cb.Run(); |
| 160 | } |
| 161 | |
tzik | 99de02b | 2016-07-01 05:54:12 | [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.\""] |
[email protected] | 81814bce | 2011-09-10 03:03:00 | [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 | 3bc7779b | 2015-12-19 09:18:46 | [diff] [blame] | 171 | Callback<void()> method_bound_to_array_cb = |
[email protected] | 81814bce | 2011-09-10 03:03:00 | [diff] [blame] | 172 | Bind(&HasRef::VoidMethod0, p); |
| 173 | method_bound_to_array_cb.Run(); |
| 174 | } |
| 175 | |
tzik | 99de02b | 2016-07-01 05:54:12 | [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.\""] |
[email protected] | 81814bce | 2011-09-10 03:03:00 | [diff] [blame] | 177 | |
| 178 | // Refcounted types should not be bound as a raw pointer. |
| 179 | void WontCompile() { |
| 180 | HasRef for_raw_ptr; |
[email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 181 | int a; |
tzik | 3bc7779b | 2015-12-19 09:18:46 | [diff] [blame] | 182 | Callback<void()> ref_count_as_raw_ptr_a = |
[email protected] | 7296f276 | 2011-11-21 19:23:44 | [diff] [blame] | 183 | Bind(&VoidPolymorphic1<int*>, &a); |
tzik | 3bc7779b | 2015-12-19 09:18:46 | [diff] [blame] | 184 | Callback<void()> ref_count_as_raw_ptr = |
[email protected] | 81814bce | 2011-09-10 03:03:00 | [diff] [blame] | 185 | Bind(&VoidPolymorphic1<HasRef*>, &for_raw_ptr); |
| 186 | } |
| 187 | |
tzik | 096369a | 2015-12-11 06:39:06 | [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\""] |
[email protected] | 81814bce | 2011-09-10 03:03:00 | [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 | 3bc7779b | 2015-12-19 09:18:46 | [diff] [blame] | 194 | Callback<int()> weak_ptr_with_non_void_return_type = |
[email protected] | 81814bce | 2011-09-10 03:03:00 | [diff] [blame] | 195 | Bind(&NoRef::IntMethod0, weak_factory.GetWeakPtr()); |
| 196 | weak_ptr_with_non_void_return_type.Run(); |
| 197 | } |
| 198 | |
tzik | daa4f5a | 2016-02-12 13:54:37 | [diff] [blame] | 199 | #elif defined(NCTEST_DISALLOW_ASSIGN_DIFFERENT_TYPES) // [r"fatal error: no viable conversion from 'Callback<MakeUnboundRunType<void \(\*\)\(int\)>>' to 'Callback<void \(\)>'"] |
[email protected] | 81814bce | 2011-09-10 03:03:00 | [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 | 58b75f59 | 2016-12-23 07:08:44 | [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 | c1db7265 | 2016-07-08 09:42:38 | [diff] [blame] | 207 | |
| 208 | void WontCompile() { |
| 209 | int i = 0; |
| 210 | Bind([i]() {}); |
| 211 | } |
| 212 | |
dcheng | 172b6ad | 2016-09-24 05:05:57 | [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 | b2e1f31c | 2016-11-16 18:17:15 | [diff] [blame] | 216 | OnceClosure cb = BindOnce([] {}); |
| 217 | OnceClosure cb2 = BindOnce(std::move(cb)); |
dcheng | 172b6ad | 2016-09-24 05:05:57 | [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 | 3f2fd66e | 2016-12-20 23:56:59 | [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 | 77172b9 | 2016-11-22 07:46:06 | [diff] [blame] | 228 | |
| 229 | void WontCompile() { |
| 230 | OnceClosure cb = Bind([] {}); |
| 231 | cb.Run(); |
| 232 | } |
| 233 | |
dcheng | 3f2fd66e | 2016-12-20 23:56:59 | [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 | |
[email protected] | 81814bce | 2011-09-10 03:03:00 | [diff] [blame] | 248 | #endif |
| 249 | |
| 250 | } // namespace base |