Avi Drissman | e4622aa | 2022-09-08 20:36:06 | [diff] [blame] | 1 | // Copyright 2011 The Chromium Authors |
Hans Wennborg | e5d14a8 | 2019-01-14 11:31:24 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | // This is a "No Compile Test" suite. |
| 6 | // http://dev.chromium.org/developers/testing/no-compile-tests |
| 7 | |
| 8 | #include <utility> |
| 9 | |
| 10 | #include "base/bind.h" |
| 11 | #include "base/callback.h" |
Hans Wennborg | e5d14a8 | 2019-01-14 11:31:24 | [diff] [blame] | 12 | #include "base/memory/ref_counted.h" |
danakj | 92061c1 | 2022-07-28 23:53:50 | [diff] [blame] | 13 | #include "base/memory/raw_ptr.h" |
| 14 | #include "base/memory/raw_ref.h" |
Guido Urdaneta | ef4e9194 | 2020-11-09 15:06:24 | [diff] [blame] | 15 | #include "base/test/bind.h" |
Hans Wennborg | e5d14a8 | 2019-01-14 11:31:24 | [diff] [blame] | 16 | |
| 17 | namespace base { |
| 18 | |
| 19 | // Do not put everything inside an anonymous namespace. If you do, many of the |
| 20 | // helper function declarations will generate unused definition warnings. |
| 21 | |
| 22 | static const int kParentValue = 1; |
| 23 | static const int kChildValue = 2; |
| 24 | |
| 25 | class NoRef { |
| 26 | public: |
| 27 | void VoidMethod0() {} |
| 28 | void VoidConstMethod0() const {} |
| 29 | int IntMethod0() { return 1; } |
| 30 | }; |
| 31 | |
| 32 | class HasRef : public NoRef, public base::RefCounted<HasRef> { |
| 33 | }; |
| 34 | |
| 35 | class Parent { |
| 36 | public: |
| 37 | void AddRef() const {} |
| 38 | void Release() const {} |
| 39 | virtual void VirtualSet() { value = kParentValue; } |
| 40 | void NonVirtualSet() { value = kParentValue; } |
| 41 | int value; |
| 42 | }; |
| 43 | |
| 44 | class Child : public Parent { |
| 45 | public: |
| 46 | virtual void VirtualSet() { value = kChildValue; } |
| 47 | void NonVirtualSet() { value = kChildValue; } |
| 48 | }; |
| 49 | |
| 50 | class NoRefParent { |
| 51 | public: |
| 52 | virtual void VirtualSet() { value = kParentValue; } |
| 53 | void NonVirtualSet() { value = kParentValue; } |
| 54 | int value; |
| 55 | }; |
| 56 | |
| 57 | class NoRefChild : public NoRefParent { |
| 58 | virtual void VirtualSet() { value = kChildValue; } |
| 59 | void NonVirtualSet() { value = kChildValue; } |
| 60 | }; |
| 61 | |
| 62 | template <typename T> |
| 63 | T PolymorphicIdentity(T t) { |
| 64 | return t; |
| 65 | } |
| 66 | |
| 67 | int UnwrapParentRef(Parent& p) { |
| 68 | return p.value; |
| 69 | } |
| 70 | |
| 71 | template <typename T> |
| 72 | void VoidPolymorphic1(T t) { |
| 73 | } |
| 74 | |
| 75 | void TakesMoveOnly(std::unique_ptr<int>) { |
| 76 | } |
| 77 | |
kylechar | 72e6f78 | 2021-03-17 17:43:38 | [diff] [blame] | 78 | void TakesIntRef(int& ref) {} |
| 79 | |
Hans Wennborg | e5d14a8 | 2019-01-14 11:31:24 | [diff] [blame] | 80 | struct NonEmptyFunctor { |
| 81 | int x; |
| 82 | void operator()() const {} |
| 83 | }; |
| 84 | |
Nico Weber | 318a073d | 2022-08-02 18:55:22 | [diff] [blame] | 85 | #if defined(NCTEST_METHOD_ON_CONST_OBJECT) // [r"static assertion failed.+?BindArgument<0>::ForwardedAs<.+?>::ToParamWithType<.+?>::kCanBeForwardedToBoundFunctor.+?Type mismatch between bound argument and bound functor's parameter\."] |
Daniel Cheng | 9a85adb | 2020-10-30 21:57:51 | [diff] [blame] | 86 | |
Hans Wennborg | e5d14a8 | 2019-01-14 11:31:24 | [diff] [blame] | 87 | // Method bound to const-object. |
| 88 | // |
| 89 | // Only const methods should be allowed to work with const objects. |
| 90 | void WontCompile() { |
| 91 | HasRef has_ref; |
| 92 | const HasRef* const_has_ref_ptr_ = &has_ref; |
kylechar | 650caf0 | 2019-07-17 03:25:41 | [diff] [blame] | 93 | RepeatingCallback<void()> method_to_const_cb = |
| 94 | BindRepeating(&HasRef::VoidMethod0, const_has_ref_ptr_); |
Hans Wennborg | e5d14a8 | 2019-01-14 11:31:24 | [diff] [blame] | 95 | method_to_const_cb.Run(); |
| 96 | } |
| 97 | |
Nico Weber | 318a073d | 2022-08-02 18:55:22 | [diff] [blame] | 98 | #elif defined(NCTEST_METHOD_BIND_NEEDS_REFCOUNTED_OBJECT) // [r"fatal error: static assertion failed due to requirement '!IsPointerV<base::NoRef \*> \|\| 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."] |
Hans Wennborg | e5d14a8 | 2019-01-14 11:31:24 | [diff] [blame] | 99 | |
| 100 | |
| 101 | // Method bound to non-refcounted object. |
| 102 | // |
| 103 | // We require refcounts unless you have Unretained(). |
| 104 | void WontCompile() { |
| 105 | NoRef no_ref; |
kylechar | 650caf0 | 2019-07-17 03:25:41 | [diff] [blame] | 106 | RepeatingCallback<void()> no_ref_cb = |
| 107 | BindRepeating(&NoRef::VoidMethod0, &no_ref); |
Hans Wennborg | e5d14a8 | 2019-01-14 11:31:24 | [diff] [blame] | 108 | no_ref_cb.Run(); |
| 109 | } |
| 110 | |
Nico Weber | 318a073d | 2022-08-02 18:55:22 | [diff] [blame] | 111 | #elif defined(NCTEST_CONST_METHOD_BIND_NEEDS_REFCOUNTED_OBJECT) // [r"fatal error: static assertion failed due to requirement '!IsPointerV<base::NoRef \*> \|\| 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."] |
Hans Wennborg | e5d14a8 | 2019-01-14 11:31:24 | [diff] [blame] | 112 | |
| 113 | // Const Method bound to non-refcounted object. |
| 114 | // |
| 115 | // We require refcounts unless you have Unretained(). |
| 116 | void WontCompile() { |
| 117 | NoRef no_ref; |
kylechar | 650caf0 | 2019-07-17 03:25:41 | [diff] [blame] | 118 | RepeatingCallback<void()> no_ref_const_cb = |
| 119 | BindRepeating(&NoRef::VoidConstMethod0, &no_ref); |
Hans Wennborg | e5d14a8 | 2019-01-14 11:31:24 | [diff] [blame] | 120 | no_ref_const_cb.Run(); |
| 121 | } |
| 122 | |
Nico Weber | 318a073d | 2022-08-02 18:55:22 | [diff] [blame] | 123 | #elif defined(NCTEST_METHOD_BIND_RAW_PTR_RECEIVER_NEEDS_REFCOUNTED_OBJECT) // [r"fatal error: static assertion failed due to requirement '!IsPointerV<base::raw_ptr<base::NoRef, [^>]+>?>> \|\| 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."] |
Daniel Cheng | 00c072a | 2022-06-11 18:50:37 | [diff] [blame] | 124 | |
| 125 | |
| 126 | // Method bound to non-refcounted object. |
| 127 | // |
| 128 | // We require refcounts unless you have Unretained(). |
| 129 | void WontCompile() { |
| 130 | NoRef no_ref; |
| 131 | raw_ptr<NoRef> rawptr(&no_ref); |
| 132 | RepeatingCallback<void()> no_ref_cb = |
| 133 | BindRepeating(&NoRef::VoidMethod0, rawptr); |
| 134 | no_ref_cb.Run(); |
| 135 | } |
| 136 | |
Nico Weber | 318a073d | 2022-08-02 18:55:22 | [diff] [blame] | 137 | #elif defined(NCTEST_CONST_METHOD_BIND_RAW_PTR_RECEIVER_NEEDS_REFCOUNTED_OBJECT) // [r"fatal error: static assertion failed due to requirement '!IsPointerV<base::raw_ptr<base::NoRef, [^>]+>?>> \|\| 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."] |
Daniel Cheng | 00c072a | 2022-06-11 18:50:37 | [diff] [blame] | 138 | |
| 139 | // Const Method bound to non-refcounted object. |
| 140 | // |
| 141 | // We require refcounts unless you have Unretained(). |
| 142 | void WontCompile() { |
| 143 | NoRef no_ref; |
| 144 | raw_ptr<NoRef> rawptr(&no_ref); |
| 145 | RepeatingCallback<void()> no_ref_const_cb = |
| 146 | BindRepeating(&NoRef::VoidConstMethod0, rawptr); |
| 147 | no_ref_const_cb.Run(); |
| 148 | } |
| 149 | |
danakj | 92061c1 | 2022-07-28 23:53:50 | [diff] [blame] | 150 | #elif defined(NCTEST_METHOD_BIND_REF_WRAPPER_RECEIVER_NON_REFCOUNTED_OBJECT) // [r"fatal error: indirection requires pointer operand"] |
| 151 | |
| 152 | // Method bound to non-refcounted object. It fails to compile with |
| 153 | // std::reference_wrapper. |
| 154 | void WontCompile() { |
| 155 | NoRef no_ref; |
| 156 | RepeatingCallback<void()> no_ref_cb = |
| 157 | BindRepeating(&NoRef::VoidMethod0, std::cref(no_ref)); |
| 158 | no_ref_cb.Run(); |
| 159 | } |
| 160 | |
| 161 | #elif defined(NCTEST_METHOD_BIND_NATIVE_REF_RECEIVER_NON_REFCOUNTED_OBJECT) // [r"fatal error: indirection requires pointer operand"] |
| 162 | |
| 163 | // Method bound to non-refcounted object. It fails to compile with |
| 164 | // a native reference. |
| 165 | void WontCompile() { |
| 166 | NoRef no_ref; |
| 167 | RepeatingCallback<void()> no_ref_cb = |
| 168 | BindRepeating(&NoRef::VoidMethod0, no_ref); |
| 169 | no_ref_cb.Run(); |
| 170 | } |
| 171 | |
| 172 | #elif defined(NCTEST_METHOD_BIND_RAW_REF_RECEIVER_NON_REFCOUNTED_OBJECT) // [r"fatal error: .*Receivers may not be raw_ref<T>\."] |
| 173 | |
| 174 | // Method bound to non-refcounted object. It fails to compile with |
| 175 | // a raw_ref. |
| 176 | void WontCompile() { |
| 177 | NoRef no_ref; |
| 178 | raw_ref<NoRef> rawref(no_ref); |
| 179 | RepeatingCallback<void()> no_ref_cb = |
| 180 | BindRepeating(&NoRef::VoidMethod0, rawref); |
| 181 | no_ref_cb.Run(); |
| 182 | } |
| 183 | |
| 184 | #elif defined(NCTEST_METHOD_BIND_REF_WRAPPER_RECEIVER_REFCOUNTED_OBJECT) // [r"fatal error: indirection requires pointer operand"] |
| 185 | |
| 186 | // Method bound to non-refcounted object. It fails to compile with |
| 187 | // std::reference_wrapper. |
| 188 | void WontCompile() { |
| 189 | HasRef has_ref; |
| 190 | RepeatingCallback<void()> has_ref_cb = |
| 191 | BindRepeating(&HasRef::VoidMethod0, std::cref(has_ref)); |
| 192 | has_ref_cb.Run(); |
| 193 | } |
| 194 | |
| 195 | #elif defined(NCTEST_METHOD_BIND_NATIVE_REF_RECEIVER_REFCOUNTED_OBJECT) // [r"fatal error: indirection requires pointer operand"] |
| 196 | |
| 197 | // Method bound to non-refcounted object. It fails to compile with |
| 198 | // a native reference. |
| 199 | void WontCompile() { |
| 200 | HasRef has_ref; |
| 201 | RepeatingCallback<void()> has_ref_cb = |
| 202 | BindRepeating(&HasRef::VoidMethod0, has_ref); |
| 203 | has_ref_cb.Run(); |
| 204 | } |
| 205 | |
| 206 | #elif defined(NCTEST_METHOD_BIND_RAW_REF_RECEIVER_REFCOUNTED_OBJECT) // [r"fatal error: .*Receivers may not be raw_ref<T>\."] |
| 207 | |
| 208 | // Method bound to non-refcounted object. It fails to compile with |
| 209 | // a raw_ref. |
| 210 | void WontCompile() { |
| 211 | HasRef has_ref; |
| 212 | raw_ref<HasRef> rawref(has_ref); |
| 213 | RepeatingCallback<void()> has_ref_cb = |
| 214 | BindRepeating(&HasRef::VoidMethod0, rawref); |
| 215 | has_ref_cb.Run(); |
| 216 | } |
| 217 | |
Nico Weber | 318a073d | 2022-08-02 18:55:22 | [diff] [blame] | 218 | #elif defined(NCTEST_CONST_POINTER) // [r"static assertion failed.+?BindArgument<0>::ForwardedAs<.+?>::ToParamWithType<.+?>::kCanBeForwardedToBoundFunctor.+?Type mismatch between bound argument and bound functor's parameter\."] |
Hans Wennborg | e5d14a8 | 2019-01-14 11:31:24 | [diff] [blame] | 219 | // Const argument used with non-const pointer parameter of same type. |
| 220 | // |
| 221 | // This is just a const-correctness check. |
| 222 | void WontCompile() { |
| 223 | const NoRef* const_no_ref_ptr; |
kylechar | 650caf0 | 2019-07-17 03:25:41 | [diff] [blame] | 224 | RepeatingCallback<NoRef*()> pointer_same_cb = |
| 225 | BindRepeating(&PolymorphicIdentity<NoRef*>, const_no_ref_ptr); |
Hans Wennborg | e5d14a8 | 2019-01-14 11:31:24 | [diff] [blame] | 226 | pointer_same_cb.Run(); |
| 227 | } |
| 228 | |
Nico Weber | 318a073d | 2022-08-02 18:55:22 | [diff] [blame] | 229 | #elif defined(NCTEST_CONST_POINTER_SUBTYPE) // [r"static assertion failed.+?BindArgument<0>::ForwardedAs<.+?>::ToParamWithType<.+?>::kCanBeForwardedToBoundFunctor.+?Type mismatch between bound argument and bound functor's parameter\."] |
Hans Wennborg | e5d14a8 | 2019-01-14 11:31:24 | [diff] [blame] | 230 | |
| 231 | // Const argument used with non-const pointer parameter of super type. |
| 232 | // |
| 233 | // This is just a const-correctness check. |
| 234 | void WontCompile() { |
| 235 | const NoRefChild* const_child_ptr; |
kylechar | 650caf0 | 2019-07-17 03:25:41 | [diff] [blame] | 236 | RepeatingCallback<NoRefParent*()> pointer_super_cb = |
| 237 | BindRepeating(&PolymorphicIdentity<NoRefParent*>, const_child_ptr); |
Hans Wennborg | e5d14a8 | 2019-01-14 11:31:24 | [diff] [blame] | 238 | pointer_super_cb.Run(); |
| 239 | } |
| 240 | |
| 241 | #elif defined(DISABLED_NCTEST_DISALLOW_NON_CONST_REF_PARAM) // [r"fatal error: no member named 'AddRef' in 'base::NoRef'"] |
| 242 | // TODO(dcheng): I think there's a type safety promotion issue here where we can |
| 243 | // pass a const ref to a non const-ref function, or vice versa accidentally. Or |
| 244 | // we make a copy accidentally. Check. |
| 245 | |
| 246 | // Functions with reference parameters, unsupported. |
| 247 | // |
| 248 | // First, non-const reference parameters are disallowed by the Google |
| 249 | // style guide. Second, since we are doing argument forwarding it becomes |
| 250 | // very tricky to avoid copies, maintain const correctness, and not |
| 251 | // accidentally have the function be modifying a temporary, or a copy. |
| 252 | void WontCompile() { |
| 253 | Parent p; |
kylechar | 650caf0 | 2019-07-17 03:25:41 | [diff] [blame] | 254 | RepeatingCallback<int(Parent&)> ref_arg_cb = BindRepeating(&UnwrapParentRef); |
Hans Wennborg | e5d14a8 | 2019-01-14 11:31:24 | [diff] [blame] | 255 | ref_arg_cb.Run(p); |
| 256 | } |
| 257 | |
Nico Weber | 318a073d | 2022-08-02 18:55:22 | [diff] [blame] | 258 | #elif defined(NCTEST_BIND_ONCE_WITH_NON_CONST_REF_PARAM) // [r"static assertion 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\(\)."] |
Hans Wennborg | e5d14a8 | 2019-01-14 11:31:24 | [diff] [blame] | 259 | |
kylechar | 72e6f78 | 2021-03-17 17:43:38 | [diff] [blame] | 260 | // Binding functions with reference parameters requires `std::ref()` or |
| 261 | // 'base::OwnedRef()`. |
Hans Wennborg | e5d14a8 | 2019-01-14 11:31:24 | [diff] [blame] | 262 | void WontCompile() { |
kylechar | 72e6f78 | 2021-03-17 17:43:38 | [diff] [blame] | 263 | int v = 1; |
| 264 | auto cb = BindOnce(&TakesIntRef, v); |
| 265 | } |
| 266 | |
Nico Weber | 318a073d | 2022-08-02 18:55:22 | [diff] [blame] | 267 | #elif defined(NCTEST_BIND_REPEATING_WITH_NON_CONST_REF_PARAM) // [r"static assertion 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\(\)."] |
kylechar | 72e6f78 | 2021-03-17 17:43:38 | [diff] [blame] | 268 | |
| 269 | // Binding functions with reference parameters requires `std::ref()` or |
| 270 | // 'base::OwnedRef()`. |
| 271 | void WontCompile() { |
| 272 | int v = 1; |
| 273 | auto cb = BindRepeating(&TakesIntRef, v); |
| 274 | } |
| 275 | |
Nico Weber | 318a073d | 2022-08-02 18:55:22 | [diff] [blame] | 276 | #elif defined(NCTEST_NON_CONST_REF_PARAM_WRONG_TYPE) // [r"static assertion failed due to requirement 'BindArgument<0>::ForwardedAs<.+?>::ToParamWithType<.+?>::kCanBeForwardedToBoundFunctor': Type mismatch between bound argument and bound functor's parameter."] |
kylechar | 72e6f78 | 2021-03-17 17:43:38 | [diff] [blame] | 277 | |
| 278 | // If the argument and parameter types mismatch then the compile error should be |
| 279 | // the generic type mismatch error. |
| 280 | void WontCompile() { |
| 281 | float f = 1.0f; |
| 282 | auto cb = BindOnce(&TakesIntRef, f); |
| 283 | } |
| 284 | |
Nico Weber | 318a073d | 2022-08-02 18:55:22 | [diff] [blame] | 285 | #elif defined(NCTEST_NON_CONST_REF_PARAM_WRONG_TYPE_AND_WRAPPED) // [r"static assertion failed due to requirement 'BindArgument<0>::ForwardedAs<.+?>::ToParamWithType<.+?>::kCanBeForwardedToBoundFunctor': Type mismatch between bound argument and bound functor's parameter."] |
kylechar | 72e6f78 | 2021-03-17 17:43:38 | [diff] [blame] | 286 | |
| 287 | // If the argument and parameter types mismatch then the compile error should be |
| 288 | // the generic type mismatch error even if the argument is wrapped in |
| 289 | // base::OwnedRef(). |
| 290 | void WontCompile() { |
| 291 | float f = 1.0f; |
| 292 | auto cb = BindOnce(&TakesIntRef, base::OwnedRef(f)); |
Hans Wennborg | e5d14a8 | 2019-01-14 11:31:24 | [diff] [blame] | 293 | } |
| 294 | |
Nico Weber | 318a073d | 2022-08-02 18:55:22 | [diff] [blame] | 295 | #elif defined(NCTEST_NO_IMPLICIT_ARRAY_PTR_CONVERSION) // [r"fatal error: static assertion failed due to requirement '!std::is_array_v<base::HasRef\[10\]>': First bound argument to a method cannot be an array."] |
Hans Wennborg | e5d14a8 | 2019-01-14 11:31:24 | [diff] [blame] | 296 | |
| 297 | // A method should not be bindable with an array of objects. |
| 298 | // |
| 299 | // This is likely not wanted behavior. We specifically check for it though |
| 300 | // because it is possible, depending on how you implement prebinding, to |
| 301 | // implicitly convert an array type to a pointer type. |
| 302 | void WontCompile() { |
| 303 | HasRef p[10]; |
kylechar | 650caf0 | 2019-07-17 03:25:41 | [diff] [blame] | 304 | RepeatingCallback<void()> method_bound_to_array_cb = |
| 305 | BindRepeating(&HasRef::VoidMethod0, p); |
Hans Wennborg | e5d14a8 | 2019-01-14 11:31:24 | [diff] [blame] | 306 | method_bound_to_array_cb.Run(); |
| 307 | } |
| 308 | |
Nico Weber | 318a073d | 2022-08-02 18:55:22 | [diff] [blame] | 309 | #elif defined(NCTEST_NO_RVALUE_RAW_REF_FOR_REFCOUNTED_TYPES) // [r"fatal error: static assertion failed due to requirement '!HasRefCountedTypeAsRawPtr<.*raw_ref<base::HasRef,.*: A parameter is a refcounted type and needs scoped_refptr."] |
danakj | 92061c1 | 2022-07-28 23:53:50 | [diff] [blame] | 310 | |
| 311 | // Refcounted types should not be bound as a raw pointer. |
| 312 | void WontCompile() { |
| 313 | HasRef has_ref; |
| 314 | raw_ref<HasRef> rr(has_ref); |
| 315 | int a; |
| 316 | raw_ref<int> rr_a(a); |
| 317 | RepeatingCallback<void()> ref_count_as_raw_ptr_a = |
| 318 | BindRepeating(&VoidPolymorphic1<raw_ref<int>>, rr_a); |
| 319 | RepeatingCallback<void()> ref_count_as_raw_ptr = |
| 320 | BindRepeating(&VoidPolymorphic1<raw_ref<HasRef>>, rr); |
| 321 | } |
| 322 | |
Nico Weber | 318a073d | 2022-08-02 18:55:22 | [diff] [blame] | 323 | #elif defined(NCTEST_NO_RVALUE_RAW_PTR_FOR_REFCOUNTED_TYPES) // [r"fatal error: static assertion failed due to requirement '!HasRefCountedTypeAsRawPtr<base::HasRef \*>::value': A parameter is a refcounted type and needs scoped_refptr."] |
Hans Wennborg | e5d14a8 | 2019-01-14 11:31:24 | [diff] [blame] | 324 | |
| 325 | // Refcounted types should not be bound as a raw pointer. |
| 326 | void WontCompile() { |
| 327 | HasRef for_raw_ptr; |
| 328 | int a; |
kylechar | 650caf0 | 2019-07-17 03:25:41 | [diff] [blame] | 329 | RepeatingCallback<void()> ref_count_as_raw_ptr_a = |
| 330 | BindRepeating(&VoidPolymorphic1<int*>, &a); |
| 331 | RepeatingCallback<void()> ref_count_as_raw_ptr = |
| 332 | BindRepeating(&VoidPolymorphic1<HasRef*>, &for_raw_ptr); |
Hans Wennborg | e5d14a8 | 2019-01-14 11:31:24 | [diff] [blame] | 333 | } |
| 334 | |
Nico Weber | 318a073d | 2022-08-02 18:55:22 | [diff] [blame] | 335 | #elif defined(NCTEST_NO_LVALUE_RAW_PTR_FOR_REFCOUNTED_TYPES) // [r"fatal error: static assertion failed due to requirement '!HasRefCountedTypeAsRawPtr<base::HasRef \*>::value': A parameter is a refcounted type and needs scoped_refptr."] |
Hans Wennborg | e5d14a8 | 2019-01-14 11:31:24 | [diff] [blame] | 336 | |
| 337 | // Refcounted types should not be bound as a raw pointer. |
| 338 | void WontCompile() { |
| 339 | HasRef* for_raw_ptr = nullptr; |
kylechar | 650caf0 | 2019-07-17 03:25:41 | [diff] [blame] | 340 | RepeatingCallback<void()> ref_count_as_raw_ptr = |
| 341 | BindRepeating(&VoidPolymorphic1<HasRef*>, for_raw_ptr); |
Hans Wennborg | e5d14a8 | 2019-01-14 11:31:24 | [diff] [blame] | 342 | } |
| 343 | |
Nico Weber | 318a073d | 2022-08-02 18:55:22 | [diff] [blame] | 344 | #elif defined(NCTEST_NO_RVALUE_CONST_RAW_PTR_FOR_REFCOUNTED_TYPES) // [r"fatal error: static assertion failed due to requirement '!HasRefCountedTypeAsRawPtr<const base::HasRef \*>::value': A parameter is a refcounted type and needs scoped_refptr."] |
Hans Wennborg | e5d14a8 | 2019-01-14 11:31:24 | [diff] [blame] | 345 | |
| 346 | // Refcounted types should not be bound as a raw pointer. |
| 347 | void WontCompile() { |
| 348 | const HasRef for_raw_ptr; |
kylechar | 650caf0 | 2019-07-17 03:25:41 | [diff] [blame] | 349 | RepeatingCallback<void()> ref_count_as_raw_ptr = |
| 350 | BindRepeating(&VoidPolymorphic1<const HasRef*>, &for_raw_ptr); |
Hans Wennborg | e5d14a8 | 2019-01-14 11:31:24 | [diff] [blame] | 351 | } |
| 352 | |
Nico Weber | 318a073d | 2022-08-02 18:55:22 | [diff] [blame] | 353 | #elif defined(NCTEST_NO_LVALUE_CONST_RAW_PTR_FOR_REFCOUNTED_TYPES) // [r"fatal error: static assertion failed due to requirement '!HasRefCountedTypeAsRawPtr<const base::HasRef \*>::value': A parameter is a refcounted type and needs scoped_refptr."] |
Hans Wennborg | e5d14a8 | 2019-01-14 11:31:24 | [diff] [blame] | 354 | |
| 355 | // Refcounted types should not be bound as a raw pointer. |
| 356 | void WontCompile() { |
| 357 | const HasRef* for_raw_ptr = nullptr; |
kylechar | 650caf0 | 2019-07-17 03:25:41 | [diff] [blame] | 358 | RepeatingCallback<void()> ref_count_as_raw_ptr = |
| 359 | BindRepeating(&VoidPolymorphic1<const HasRef*>, for_raw_ptr); |
Hans Wennborg | e5d14a8 | 2019-01-14 11:31:24 | [diff] [blame] | 360 | } |
| 361 | |
Nico Weber | 318a073d | 2022-08-02 18:55:22 | [diff] [blame] | 362 | #elif defined(NCTEST_WEAKPTR_BIND_MUST_RETURN_VOID) // [r"fatal error: static assertion failed due to requirement 'std::is_void_v<int>': weak_ptrs can only bind to methods without return values"] |
Hans Wennborg | e5d14a8 | 2019-01-14 11:31:24 | [diff] [blame] | 363 | |
| 364 | // WeakPtrs cannot be bound to methods with return types. |
| 365 | void WontCompile() { |
| 366 | NoRef no_ref; |
| 367 | WeakPtrFactory<NoRef> weak_factory(&no_ref); |
kylechar | 650caf0 | 2019-07-17 03:25:41 | [diff] [blame] | 368 | RepeatingCallback<int()> weak_ptr_with_non_void_return_type = |
| 369 | BindRepeating(&NoRef::IntMethod0, weak_factory.GetWeakPtr()); |
Hans Wennborg | e5d14a8 | 2019-01-14 11:31:24 | [diff] [blame] | 370 | weak_ptr_with_non_void_return_type.Run(); |
| 371 | } |
| 372 | |
Daniel Cheng | cfb972d6 | 2021-03-04 13:18:04 | [diff] [blame] | 373 | #elif defined(NCTEST_DISALLOW_ASSIGN_DIFFERENT_TYPES) // [r"fatal error: no viable conversion from 'RepeatingCallback<internal::MakeUnboundRunType<void \(\*\)\(int\)>>' to 'RepeatingCallback<void \(\)>'"] |
Hans Wennborg | e5d14a8 | 2019-01-14 11:31:24 | [diff] [blame] | 374 | |
| 375 | // Bind result cannot be assigned to Callbacks with a mismatching type. |
| 376 | void WontCompile() { |
kylechar | 650caf0 | 2019-07-17 03:25:41 | [diff] [blame] | 377 | RepeatingClosure callback_mismatches_bind_type = |
| 378 | BindRepeating(&VoidPolymorphic1<int>); |
Hans Wennborg | e5d14a8 | 2019-01-14 11:31:24 | [diff] [blame] | 379 | } |
| 380 | |
Nico Weber | 318a073d | 2022-08-02 18:55:22 | [diff] [blame] | 381 | #elif defined(NCTEST_DISALLOW_CAPTURING_LAMBDA) // [r"static assertion failed due to requirement 'FunctorTraits<\(lambda at [^)]+\), void>::is_stateless': Capturing lambdas and stateful lambdas are intentionally not supported\."] |
Hans Wennborg | e5d14a8 | 2019-01-14 11:31:24 | [diff] [blame] | 382 | |
| 383 | void WontCompile() { |
| 384 | int i = 0, j = 0; |
kylechar | 650caf0 | 2019-07-17 03:25:41 | [diff] [blame] | 385 | BindOnce([i,&j]() {j = i;}); |
Hans Wennborg | e5d14a8 | 2019-01-14 11:31:24 | [diff] [blame] | 386 | } |
| 387 | |
Nico Weber | 318a073d | 2022-08-02 18:55:22 | [diff] [blame] | 388 | #elif defined(NCTEST_DISALLOW_ONCECALLBACK_RUN_ON_LVALUE) // [r"fatal error: static assertion failed due to requirement '!sizeof \(\*this\)': OnceCallback::Run\(\) may only be invoked on a non-const rvalue, i\.e\. std::move\(callback\).Run\(\)."] |
Hans Wennborg | e5d14a8 | 2019-01-14 11:31:24 | [diff] [blame] | 389 | |
| 390 | void WontCompile() { |
kylechar | 650caf0 | 2019-07-17 03:25:41 | [diff] [blame] | 391 | OnceClosure cb = BindOnce([] {}); |
Hans Wennborg | e5d14a8 | 2019-01-14 11:31:24 | [diff] [blame] | 392 | cb.Run(); |
| 393 | } |
| 394 | |
Nico Weber | 318a073d | 2022-08-02 18:55:22 | [diff] [blame] | 395 | #elif defined(NCTEST_DISALLOW_ONCECALLBACK_RUN_ON_CONST_LVALUE) // [r"fatal error: static assertion failed due to requirement '!sizeof \(\*this\)': OnceCallback::Run\(\) may only be invoked on a non-const rvalue, i\.e\. std::move\(callback\).Run\(\)."] |
Hans Wennborg | e5d14a8 | 2019-01-14 11:31:24 | [diff] [blame] | 396 | |
| 397 | void WontCompile() { |
kylechar | 650caf0 | 2019-07-17 03:25:41 | [diff] [blame] | 398 | const OnceClosure cb = BindOnce([] {}); |
Hans Wennborg | e5d14a8 | 2019-01-14 11:31:24 | [diff] [blame] | 399 | cb.Run(); |
| 400 | } |
| 401 | |
Nico Weber | 318a073d | 2022-08-02 18:55:22 | [diff] [blame] | 402 | #elif defined(NCTEST_DISALLOW_ONCECALLBACK_RUN_ON_CONST_RVALUE) // [r"fatal error: static assertion failed due to requirement '!sizeof \(\*this\)': OnceCallback::Run\(\) may only be invoked on a non-const rvalue, i\.e\. std::move\(callback\).Run\(\)."] |
Hans Wennborg | e5d14a8 | 2019-01-14 11:31:24 | [diff] [blame] | 403 | |
| 404 | void WontCompile() { |
kylechar | 650caf0 | 2019-07-17 03:25:41 | [diff] [blame] | 405 | const OnceClosure cb = BindOnce([] {}); |
Hans Wennborg | e5d14a8 | 2019-01-14 11:31:24 | [diff] [blame] | 406 | std::move(cb).Run(); |
| 407 | } |
| 408 | |
Nico Weber | 318a073d | 2022-08-02 18:55:22 | [diff] [blame] | 409 | #elif defined(NCTEST_DISALLOW_BIND_ONCECALLBACK) // [r"fatal error: static assertion failed due to requirement '!base::internal::IsOnceCallback<base::OnceCallback<void \(int\)> ?>\(\)': BindRepeating cannot bind OnceCallback. Use BindOnce with std::move\(\)."] |
Hans Wennborg | e5d14a8 | 2019-01-14 11:31:24 | [diff] [blame] | 410 | |
| 411 | void WontCompile() { |
kylechar | 650caf0 | 2019-07-17 03:25:41 | [diff] [blame] | 412 | BindRepeating(BindOnce([](int) {}), 42); |
Hans Wennborg | e5d14a8 | 2019-01-14 11:31:24 | [diff] [blame] | 413 | } |
| 414 | |
Nico Weber | 318a073d | 2022-08-02 18:55:22 | [diff] [blame] | 415 | #elif defined(NCTEST_DISALLOW_BINDONCE_LVALUE_ONCECALLBACK) // [r"fatal error: static assertion 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))."] |
Hans Wennborg | e5d14a8 | 2019-01-14 11:31:24 | [diff] [blame] | 416 | void WontCompile() { |
| 417 | auto cb = BindOnce([](int) {}); |
| 418 | BindOnce(cb, 42); |
| 419 | } |
| 420 | |
Nico Weber | 318a073d | 2022-08-02 18:55:22 | [diff] [blame] | 421 | #elif defined(NCTEST_DISALLOW_BINDONCE_RVALUE_CONST_ONCECALLBACK) // [r"fatal error: static assertion 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))."] |
Hans Wennborg | e5d14a8 | 2019-01-14 11:31:24 | [diff] [blame] | 422 | |
| 423 | void WontCompile() { |
| 424 | const auto cb = BindOnce([](int) {}); |
| 425 | BindOnce(std::move(cb), 42); |
| 426 | } |
| 427 | |
Nico Weber | 318a073d | 2022-08-02 18:55:22 | [diff] [blame] | 428 | #elif defined(NCTEST_BINDONCE_MOVEONLY_TYPE_BY_VALUE) // [r"static assertion failed.+?BindArgument<0>::BoundAs<.+?>::StoredAs<.+?>::kMoveOnlyTypeMustUseStdMove.+?Attempting to bind a move-only type\. Use std::move\(\) to transfer ownership to the created callback\."] |
Hans Wennborg | e5d14a8 | 2019-01-14 11:31:24 | [diff] [blame] | 429 | |
| 430 | void WontCompile() { |
| 431 | std::unique_ptr<int> x; |
| 432 | BindOnce(&TakesMoveOnly, x); |
| 433 | } |
| 434 | |
Nico Weber | 318a073d | 2022-08-02 18:55:22 | [diff] [blame] | 435 | #elif defined(NCTEST_BIND_MOVEONLY_TYPE_BY_VALUE) // [r"static assertion 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\."] |
Hans Wennborg | e5d14a8 | 2019-01-14 11:31:24 | [diff] [blame] | 436 | |
| 437 | void WontCompile() { |
| 438 | std::unique_ptr<int> x; |
kylechar | 650caf0 | 2019-07-17 03:25:41 | [diff] [blame] | 439 | BindRepeating(&TakesMoveOnly, x); |
Hans Wennborg | e5d14a8 | 2019-01-14 11:31:24 | [diff] [blame] | 440 | } |
| 441 | |
Nico Weber | 318a073d | 2022-08-02 18:55:22 | [diff] [blame] | 442 | #elif defined(NCTEST_BIND_MOVEONLY_TYPE_WITH_STDMOVE) // [r"static assertion 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\."] |
Hans Wennborg | e5d14a8 | 2019-01-14 11:31:24 | [diff] [blame] | 443 | |
| 444 | void WontCompile() { |
| 445 | std::unique_ptr<int> x; |
kylechar | 650caf0 | 2019-07-17 03:25:41 | [diff] [blame] | 446 | BindRepeating(&TakesMoveOnly, std::move(x)); |
Hans Wennborg | e5d14a8 | 2019-01-14 11:31:24 | [diff] [blame] | 447 | } |
| 448 | |
Nico Weber | 318a073d | 2022-08-02 18:55:22 | [diff] [blame] | 449 | #elif defined(NCTEST_BIND_NON_EMPTY_FUNCTOR) // [r"static assertion failed due to requirement 'FunctorTraits<base::NonEmptyFunctor, void>::is_stateless': Capturing lambdas and stateful lambdas are intentionally not supported\."] |
Hans Wennborg | e5d14a8 | 2019-01-14 11:31:24 | [diff] [blame] | 450 | |
| 451 | void WontCompile() { |
kylechar | 650caf0 | 2019-07-17 03:25:41 | [diff] [blame] | 452 | BindRepeating(NonEmptyFunctor()); |
Hans Wennborg | e5d14a8 | 2019-01-14 11:31:24 | [diff] [blame] | 453 | } |
| 454 | |
Jan Wilken Dörrie | 4dc6fad7 | 2019-05-22 07:27:56 | [diff] [blame] | 455 | #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\)\)."] |
| 456 | void WontCompile() { |
| 457 | int foo = 42; |
| 458 | auto mutable_lambda = [&]() mutable {}; |
| 459 | BindLambdaForTesting(mutable_lambda); |
| 460 | } |
| 461 | |
| 462 | #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\)\)."] |
| 463 | |
| 464 | void WontCompile() { |
| 465 | int foo = 42; |
| 466 | const auto mutable_lambda = [&]() mutable {}; |
| 467 | BindLambdaForTesting(std::move(mutable_lambda)); |
| 468 | } |
Daniel Cheng | 9a85adb | 2020-10-30 21:57:51 | [diff] [blame] | 469 | |
Nico Weber | 318a073d | 2022-08-02 18:55:22 | [diff] [blame] | 470 | #elif defined(NCTEST_BIND_UNCOPYABLE_AND_UNMOVABLE_TYPE) // [r"static assertion failed.+?BindArgument<0>::BoundAs<.+?>::StoredAs<.+?>::kBindArgumentCanBeCaptured.+?Cannot capture argument: is the argument copyable or movable\?"] |
Daniel Cheng | 9a85adb | 2020-10-30 21:57:51 | [diff] [blame] | 471 | |
| 472 | void WontCompile() { |
| 473 | struct UncopyableUnmovable { |
| 474 | UncopyableUnmovable() = default; |
| 475 | UncopyableUnmovable(const UncopyableUnmovable&) = delete; |
| 476 | UncopyableUnmovable& operator=(const UncopyableUnmovable&) = delete; |
| 477 | }; |
| 478 | |
| 479 | UncopyableUnmovable u; |
| 480 | BindOnce([] (const UncopyableUnmovable&) {}, u); |
| 481 | } |
| 482 | |
Nico Weber | 318a073d | 2022-08-02 18:55:22 | [diff] [blame] | 483 | #elif defined(NCTEST_BIND_ONCE_WITH_PASSED) // [r"static assertion failed.+?Use std::move\(\) instead of base::Passed\(\) with base::BindOnce\(\)"] |
Daniel Cheng | ad0da8e | 2021-02-22 21:07:22 | [diff] [blame] | 484 | |
| 485 | void WontCompile() { |
| 486 | std::unique_ptr<int> x; |
| 487 | BindOnce([] (std::unique_ptr<int>) {}, Passed(&x)); |
| 488 | } |
| 489 | |
Daniel Cheng | 58ea45b | 2021-09-17 22:55:08 | [diff] [blame] | 490 | #elif defined(NCTEST_BIND_ONCE_WITH_ADDRESS_OF_OVERLOADED_FUNCTION) // [r"fatal error: reference to overloaded function could not be resolved; did you mean to call it\?"] |
| 491 | |
| 492 | void F(int); |
| 493 | void F(float); |
| 494 | |
| 495 | void WontCompile() { |
| 496 | BindOnce(&F, 1, 2, 3); |
| 497 | } |
| 498 | |
| 499 | #elif defined(NCTEST_BIND_REPEATING_WITH_ADDRESS_OF_OVERLOADED_FUNCTION) // [r"fatal error: reference to overloaded function could not be resolved; did you mean to call it\?"] |
| 500 | |
| 501 | void F(int); |
| 502 | void F(float); |
| 503 | |
| 504 | void WontCompile() { |
| 505 | BindRepeating(&F, 1, 2, 3); |
| 506 | } |
| 507 | |
Hans Wennborg | e5d14a8 | 2019-01-14 11:31:24 | [diff] [blame] | 508 | #endif |
| 509 | |
| 510 | } // namespace base |