1// Copyright 2011 The Chromium Authors 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#define FORCE_UNRETAINED_COMPLETENESS_CHECKS_FOR_TESTS 1 9 10#include <utility> 11 12#include "base/functional/bind.h" 13#include "base/functional/callback.h" 14#include "base/functional/disallow_unretained.h" 15#include "base/memory/ref_counted.h" 16#include "base/memory/raw_ptr.h" 17#include "base/memory/raw_ref.h" 18#include "base/test/bind.h" 19 20namespace base { 21 22// Do not put everything inside an anonymous namespace. If you do, many of the 23// helper function declarations will generate unused definition warnings. 24 25static const int kParentValue = 1; 26static const int kChildValue = 2; 27 28class NoRef { 29 public: 30 void VoidMethod0() {} 31 void VoidConstMethod0() const {} 32 int IntMethod0() { return 1; } 33}; 34 35class HasRef : public NoRef, public base::RefCounted<HasRef> { 36}; 37 38class Parent { 39 public: 40 void AddRef() const {} 41 void Release() const {} 42 virtual void VirtualSet() { value = kParentValue; } 43 void NonVirtualSet() { value = kParentValue; } 44 int value; 45}; 46 47class Child : public Parent { 48 public: 49 virtual void VirtualSet() { value = kChildValue; } 50 void NonVirtualSet() { value = kChildValue; } 51}; 52 53class NoRefParent { 54 public: 55 virtual void VirtualSet() { value = kParentValue; } 56 void NonVirtualSet() { value = kParentValue; } 57 int value; 58}; 59 60class NoRefChild : public NoRefParent { 61 virtual void VirtualSet() { value = kChildValue; } 62 void NonVirtualSet() { value = kChildValue; } 63}; 64 65template <typename T> 66T PolymorphicIdentity(T t) { 67 return t; 68} 69 70int UnwrapParentRef(Parent& p) { 71 return p.value; 72} 73 74template <typename T> 75void VoidPolymorphic1(T t) { 76} 77 78void TakesMoveOnly(std::unique_ptr<int>) { 79} 80 81void TakesIntRef(int& ref) {} 82 83struct NonEmptyFunctor { 84 int x; 85 void operator()() const {} 86}; 87 88class Incomplete; 89void PassIncompleteByPtr(Incomplete*) {} 90 91class Dangerous { 92 public: 93 void Method() {} 94 95 DISALLOW_UNRETAINED(); 96}; 97 98void PassDangerousByPtr(Dangerous*) {} 99void PassDangerousByConstRef(const Dangerous&) {} 100void PassDangerousByMutableRef(Dangerous&) {} 101 102 103#if defined(NCTEST_METHOD_ON_CONST_OBJECT) // [r"Type mismatch between bound argument and bound functor's parameter\."] 104 105// Method bound to const-object. 106// 107// Only const methods should be allowed to work with const objects. 108void WontCompile() { 109 HasRef has_ref; 110 const HasRef* const_has_ref_ptr_ = &has_ref; 111 RepeatingCallback<void()> method_to_const_cb = 112 BindRepeating(&HasRef::VoidMethod0, const_has_ref_ptr_); 113 method_to_const_cb.Run(); 114} 115 116#elif defined(NCTEST_METHOD_BIND_NEEDS_REFCOUNTED_OBJECT) // [r"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."] 117 118 119// Method bound to non-refcounted object. 120// 121// We require refcounts unless you have Unretained(). 122void WontCompile() { 123 NoRef no_ref; 124 RepeatingCallback<void()> no_ref_cb = 125 BindRepeating(&NoRef::VoidMethod0, &no_ref); 126 no_ref_cb.Run(); 127} 128 129#elif defined(NCTEST_CONST_METHOD_BIND_NEEDS_REFCOUNTED_OBJECT) // [r"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."] 130 131// Const Method bound to non-refcounted object. 132// 133// We require refcounts unless you have Unretained(). 134void WontCompile() { 135 NoRef no_ref; 136 RepeatingCallback<void()> no_ref_const_cb = 137 BindRepeating(&NoRef::VoidConstMethod0, &no_ref); 138 no_ref_const_cb.Run(); 139} 140 141#elif defined(NCTEST_METHOD_BIND_RAW_PTR_RECEIVER_NEEDS_REFCOUNTED_OBJECT) // [r"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."] 142 143 144// Method bound to non-refcounted object. 145// 146// We require refcounts unless you have Unretained(). 147void WontCompile() { 148 NoRef no_ref; 149 raw_ptr<NoRef> rawptr(&no_ref); 150 RepeatingCallback<void()> no_ref_cb = 151 BindRepeating(&NoRef::VoidMethod0, rawptr); 152 no_ref_cb.Run(); 153} 154 155#elif defined(NCTEST_CONST_METHOD_BIND_RAW_PTR_RECEIVER_NEEDS_REFCOUNTED_OBJECT) // [r"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."] 156 157// Const Method bound to non-refcounted object. 158// 159// We require refcounts unless you have Unretained(). 160void WontCompile() { 161 NoRef no_ref; 162 raw_ptr<NoRef> rawptr(&no_ref); 163 RepeatingCallback<void()> no_ref_const_cb = 164 BindRepeating(&NoRef::VoidConstMethod0, rawptr); 165 no_ref_const_cb.Run(); 166} 167 168#elif defined(NCTEST_METHOD_BIND_REF_WRAPPER_RECEIVER_NON_REFCOUNTED_OBJECT) // [r"Cannot convert `this` argument to address\. Method calls must be bound using a pointer-like `this` argument\."] 169 170// Method bound to non-refcounted object. It fails to compile with 171// std::reference_wrapper. 172void WontCompile() { 173 NoRef no_ref; 174 RepeatingCallback<void()> no_ref_cb = 175 BindRepeating(&NoRef::VoidMethod0, std::cref(no_ref)); 176 no_ref_cb.Run(); 177} 178 179#elif defined(NCTEST_METHOD_BIND_NATIVE_REF_RECEIVER_NON_REFCOUNTED_OBJECT) // [r"Cannot convert `this` argument to address\. Method calls must be bound using a pointer-like `this` argument\."] 180 181// Method bound to non-refcounted object. It fails to compile with 182// a native reference. 183void WontCompile() { 184 NoRef no_ref; 185 RepeatingCallback<void()> no_ref_cb = 186 BindRepeating(&NoRef::VoidMethod0, no_ref); 187 no_ref_cb.Run(); 188} 189 190#elif defined(NCTEST_METHOD_BIND_RAW_REF_RECEIVER_NON_REFCOUNTED_OBJECT) // [r"Receivers may not be raw_ref<T>\."] 191 192// Method bound to non-refcounted object. It fails to compile with 193// a raw_ref. 194void WontCompile() { 195 NoRef no_ref; 196 raw_ref<NoRef> rawref(no_ref); 197 RepeatingCallback<void()> no_ref_cb = 198 BindRepeating(&NoRef::VoidMethod0, rawref); 199 no_ref_cb.Run(); 200} 201 202#elif defined(NCTEST_METHOD_BIND_REF_WRAPPER_RECEIVER_REFCOUNTED_OBJECT) // [r"Cannot convert `this` argument to address\. Method calls must be bound using a pointer-like `this` argument\."] 203 204// Method bound to non-refcounted object. It fails to compile with 205// std::reference_wrapper. 206void WontCompile() { 207 HasRef has_ref; 208 RepeatingCallback<void()> has_ref_cb = 209 BindRepeating(&HasRef::VoidMethod0, std::cref(has_ref)); 210 has_ref_cb.Run(); 211} 212 213#elif defined(NCTEST_METHOD_BIND_NATIVE_REF_RECEIVER_REFCOUNTED_OBJECT) // [r"Cannot convert `this` argument to address\. Method calls must be bound using a pointer-like `this` argument\."] 214 215// Method bound to non-refcounted object. It fails to compile with 216// a native reference. 217void WontCompile() { 218 HasRef has_ref; 219 RepeatingCallback<void()> has_ref_cb = 220 BindRepeating(&HasRef::VoidMethod0, has_ref); 221 has_ref_cb.Run(); 222} 223 224#elif defined(NCTEST_METHOD_BIND_RAW_REF_RECEIVER_REFCOUNTED_OBJECT) // [r"Receivers may not be raw_ref<T>\."] 225 226// Method bound to non-refcounted object. It fails to compile with 227// a raw_ref. 228void WontCompile() { 229 HasRef has_ref; 230 raw_ref<HasRef> rawref(has_ref); 231 RepeatingCallback<void()> has_ref_cb = 232 BindRepeating(&HasRef::VoidMethod0, rawref); 233 has_ref_cb.Run(); 234} 235 236#elif defined(NCTEST_CONST_POINTER) // [r"Type mismatch between bound argument and bound functor's parameter\."] 237// Const argument used with non-const pointer parameter of same type. 238// 239// This is just a const-correctness check. 240void WontCompile() { 241 const NoRef* const_no_ref_ptr; 242 RepeatingCallback<NoRef*()> pointer_same_cb = 243 BindRepeating(&PolymorphicIdentity<NoRef*>, const_no_ref_ptr); 244 pointer_same_cb.Run(); 245} 246 247#elif defined(NCTEST_CONST_POINTER_SUBTYPE) // [r"Type mismatch between bound argument and bound functor's parameter\."] 248 249// Const argument used with non-const pointer parameter of super type. 250// 251// This is just a const-correctness check. 252void WontCompile() { 253 const NoRefChild* const_child_ptr; 254 RepeatingCallback<NoRefParent*()> pointer_super_cb = 255 BindRepeating(&PolymorphicIdentity<NoRefParent*>, const_child_ptr); 256 pointer_super_cb.Run(); 257} 258 259#elif defined(DISABLED_NCTEST_DISALLOW_NON_CONST_REF_PARAM) // [r"no member named 'AddRef' in 'base::NoRef'"] 260// TODO(dcheng): I think there's a type safety promotion issue here where we can 261// pass a const ref to a non const-ref function, or vice versa accidentally. Or 262// we make a copy accidentally. Check. 263 264// Functions with reference parameters, unsupported. 265// 266// First, non-const reference parameters are disallowed by the Google 267// style guide. Second, since we are doing argument forwarding it becomes 268// very tricky to avoid copies, maintain const correctness, and not 269// accidentally have the function be modifying a temporary, or a copy. 270void WontCompile() { 271 Parent p; 272 RepeatingCallback<int(Parent&)> ref_arg_cb = BindRepeating(&UnwrapParentRef); 273 ref_arg_cb.Run(p); 274} 275 276#elif defined(NCTEST_BIND_ONCE_WITH_NON_CONST_REF_PARAM) // [r"Bound argument for non-const reference parameter must be wrapped in std::ref\(\) or base::OwnedRef\(\)."] 277 278// Binding functions with reference parameters requires `std::ref()` or 279// 'base::OwnedRef()`. 280void WontCompile() { 281 int v = 1; 282 auto cb = BindOnce(&TakesIntRef, v); 283} 284 285#elif defined(NCTEST_BIND_REPEATING_WITH_NON_CONST_REF_PARAM) // [r"Bound argument for non-const reference parameter must be wrapped in std::ref\(\) or base::OwnedRef\(\)."] 286 287// Binding functions with reference parameters requires `std::ref()` or 288// 'base::OwnedRef()`. 289void WontCompile() { 290 int v = 1; 291 auto cb = BindRepeating(&TakesIntRef, v); 292} 293 294#elif defined(NCTEST_NON_CONST_REF_PARAM_WRONG_TYPE) // [r"Type mismatch between bound argument and bound functor's parameter."] 295 296// If the argument and parameter types mismatch then the compile error should be 297// the generic type mismatch error. 298void WontCompile() { 299 float f = 1.0f; 300 auto cb = BindOnce(&TakesIntRef, f); 301} 302 303#elif defined(NCTEST_NON_CONST_REF_PARAM_WRONG_TYPE_AND_WRAPPED) // [r"Type mismatch between bound argument and bound functor's parameter."] 304 305// If the argument and parameter types mismatch then the compile error should be 306// the generic type mismatch error even if the argument is wrapped in 307// base::OwnedRef(). 308void WontCompile() { 309 float f = 1.0f; 310 auto cb = BindOnce(&TakesIntRef, base::OwnedRef(f)); 311} 312 313#elif defined(NCTEST_NO_IMPLICIT_ARRAY_PTR_CONVERSION) // [r"First bound argument to a method cannot be an array."] 314 315// A method should not be bindable with an array of objects. 316// 317// This is likely not wanted behavior. We specifically check for it though 318// because it is possible, depending on how you implement prebinding, to 319// implicitly convert an array type to a pointer type. 320void WontCompile() { 321 HasRef p[10]; 322 RepeatingCallback<void()> method_bound_to_array_cb = 323 BindRepeating(&HasRef::VoidMethod0, p); 324 method_bound_to_array_cb.Run(); 325} 326 327#elif defined(NCTEST_NO_RVALUE_RAW_REF_FOR_REFCOUNTED_TYPES) // [r"A parameter is a refcounted type and needs scoped_refptr."] 328 329// Refcounted types should not be bound as a raw pointer. 330void WontCompile() { 331 HasRef has_ref; 332 raw_ref<HasRef> rr(has_ref); 333 int a; 334 raw_ref<int> rr_a(a); 335 RepeatingCallback<void()> ref_count_as_raw_ptr_a = 336 BindRepeating(&VoidPolymorphic1<raw_ref<int>>, rr_a); 337 RepeatingCallback<void()> ref_count_as_raw_ptr = 338 BindRepeating(&VoidPolymorphic1<raw_ref<HasRef>>, rr); 339} 340 341#elif defined(NCTEST_NO_RVALUE_RAW_PTR_FOR_REFCOUNTED_TYPES) // [r"A parameter is a refcounted type and needs scoped_refptr."] 342 343// Refcounted types should not be bound as a raw pointer. 344void WontCompile() { 345 HasRef for_raw_ptr; 346 int a; 347 RepeatingCallback<void()> ref_count_as_raw_ptr_a = 348 BindRepeating(&VoidPolymorphic1<int*>, &a); 349 RepeatingCallback<void()> ref_count_as_raw_ptr = 350 BindRepeating(&VoidPolymorphic1<HasRef*>, &for_raw_ptr); 351} 352 353#elif defined(NCTEST_NO_LVALUE_RAW_PTR_FOR_REFCOUNTED_TYPES) // [r"A parameter is a refcounted type and needs scoped_refptr."] 354 355// Refcounted types should not be bound as a raw pointer. 356void WontCompile() { 357 HasRef* for_raw_ptr = nullptr; 358 RepeatingCallback<void()> ref_count_as_raw_ptr = 359 BindRepeating(&VoidPolymorphic1<HasRef*>, for_raw_ptr); 360} 361 362#elif defined(NCTEST_NO_RVALUE_CONST_RAW_PTR_FOR_REFCOUNTED_TYPES) // [r"A parameter is a refcounted type and needs scoped_refptr."] 363 364// Refcounted types should not be bound as a raw pointer. 365void WontCompile() { 366 const HasRef for_raw_ptr; 367 RepeatingCallback<void()> ref_count_as_raw_ptr = 368 BindRepeating(&VoidPolymorphic1<const HasRef*>, &for_raw_ptr); 369} 370 371#elif defined(NCTEST_NO_LVALUE_CONST_RAW_PTR_FOR_REFCOUNTED_TYPES) // [r"A parameter is a refcounted type and needs scoped_refptr."] 372 373// Refcounted types should not be bound as a raw pointer. 374void WontCompile() { 375 const HasRef* for_raw_ptr = nullptr; 376 RepeatingCallback<void()> ref_count_as_raw_ptr = 377 BindRepeating(&VoidPolymorphic1<const HasRef*>, for_raw_ptr); 378} 379 380#elif defined(NCTEST_WEAKPTR_BIND_MUST_RETURN_VOID) // [r"WeakPtrs can only bind to methods without return values."] 381 382// WeakPtrs cannot be bound to methods with return types. 383void WontCompile() { 384 NoRef no_ref; 385 WeakPtrFactory<NoRef> weak_factory(&no_ref); 386 RepeatingCallback<int()> weak_ptr_with_non_void_return_type = 387 BindRepeating(&NoRef::IntMethod0, weak_factory.GetWeakPtr()); 388 weak_ptr_with_non_void_return_type.Run(); 389} 390 391#elif defined(NCTEST_DISALLOW_ASSIGN_DIFFERENT_TYPES) // [r"no viable conversion from 'RepeatingCallback<UnboundRunType>' to 'RepeatingCallback<void \(\)>'"] 392 393// Bind result cannot be assigned to Callbacks with a mismatching type. 394void WontCompile() { 395 RepeatingClosure callback_mismatches_bind_type = 396 BindRepeating(&VoidPolymorphic1<int>); 397} 398 399#elif defined(NCTEST_DISALLOW_CAPTURING_LAMBDA) // [r"Capturing lambdas and stateful lambdas are intentionally not supported\."] 400 401void WontCompile() { 402 int i = 0, j = 0; 403 BindOnce([i,&j]() {j = i;}); 404} 405 406#elif defined(NCTEST_DISALLOW_ONCECALLBACK_RUN_ON_LVALUE) // [r"OnceCallback::Run\(\) may only be invoked on a non-const rvalue, i\.e\. std::move\(callback\).Run\(\)."] 407 408void WontCompile() { 409 OnceClosure cb = BindOnce([] {}); 410 cb.Run(); 411} 412 413#elif defined(NCTEST_DISALLOW_ONCECALLBACK_RUN_ON_CONST_LVALUE) // [r"OnceCallback::Run\(\) may only be invoked on a non-const rvalue, i\.e\. std::move\(callback\).Run\(\)."] 414 415void WontCompile() { 416 const OnceClosure cb = BindOnce([] {}); 417 cb.Run(); 418} 419 420#elif defined(NCTEST_DISALLOW_ONCECALLBACK_RUN_ON_CONST_RVALUE) // [r"OnceCallback::Run\(\) may only be invoked on a non-const rvalue, i\.e\. std::move\(callback\).Run\(\)."] 421 422void WontCompile() { 423 const OnceClosure cb = BindOnce([] {}); 424 std::move(cb).Run(); 425} 426 427#elif defined(NCTEST_DISALLOW_BIND_ONCECALLBACK) // [r"BindRepeating\(\) cannot bind OnceCallback. Use BindOnce\(\) with std::move\(\)\."] 428 429void WontCompile() { 430 BindRepeating(BindOnce([](int) {}), 42); 431} 432 433#elif defined(NCTEST_DISALLOW_BINDONCE_LVALUE_ONCECALLBACK) // [r"BindOnce\(\) requires non-const rvalue for OnceCallback binding, i\.e\. base::BindOnce\(std::move\(callback\)\)\."] 434void WontCompile() { 435 auto cb = BindOnce([](int) {}); 436 BindOnce(cb, 42); 437} 438 439#elif defined(NCTEST_DISALLOW_BINDONCE_RVALUE_CONST_ONCECALLBACK) // [r"BindOnce\(\) requires non-const rvalue for OnceCallback binding, i\.e\. base::BindOnce\(std::move\(callback\)\)\."] 440 441void WontCompile() { 442 const auto cb = BindOnce([](int) {}); 443 BindOnce(std::move(cb), 42); 444} 445 446#elif defined(NCTEST_BINDONCE_MOVEONLY_TYPE_BY_VALUE) // [r"Attempting to bind a move-only type\. Use std::move\(\) to transfer ownership to the created callback\."] 447 448void WontCompile() { 449 std::unique_ptr<int> x; 450 BindOnce(&TakesMoveOnly, x); 451} 452 453#elif defined(NCTEST_BIND_MOVEONLY_TYPE_BY_VALUE) // [r"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\."] 454 455void WontCompile() { 456 std::unique_ptr<int> x; 457 BindRepeating(&TakesMoveOnly, x); 458} 459 460#elif defined(NCTEST_BIND_MOVEONLY_TYPE_WITH_STDMOVE) // [r"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\."] 461 462void WontCompile() { 463 std::unique_ptr<int> x; 464 BindRepeating(&TakesMoveOnly, std::move(x)); 465} 466 467#elif defined(NCTEST_BIND_NON_EMPTY_FUNCTOR) // [r"Capturing lambdas and stateful lambdas are intentionally not supported\."] 468 469void WontCompile() { 470 BindRepeating(NonEmptyFunctor()); 471} 472 473#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\)\)\."] 474void WontCompile() { 475 int foo = 42; 476 auto mutable_lambda = [&]() mutable {}; 477 BindLambdaForTesting(mutable_lambda); 478} 479 480#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\)\)\."] 481 482void WontCompile() { 483 int foo = 42; 484 const auto mutable_lambda = [&]() mutable {}; 485 BindLambdaForTesting(std::move(mutable_lambda)); 486} 487 488#elif defined(NCTEST_BIND_UNCOPYABLE_AND_UNMOVABLE_TYPE) // [r"Cannot capture argument: is the argument copyable or movable\?"] 489 490void WontCompile() { 491 struct UncopyableUnmovable { 492 UncopyableUnmovable() = default; 493 UncopyableUnmovable(const UncopyableUnmovable&) = delete; 494 UncopyableUnmovable& operator=(const UncopyableUnmovable&) = delete; 495 }; 496 497 UncopyableUnmovable u; 498 BindOnce([] (const UncopyableUnmovable&) {}, u); 499} 500 501#elif defined(NCTEST_BIND_ONCE_WITH_PASSED) // [r"Use std::move\(\) instead of base::Passed\(\) with base::BindOnce\(\)\."] 502 503void WontCompile() { 504 std::unique_ptr<int> x; 505 BindOnce([] (std::unique_ptr<int>) {}, Passed(&x)); 506} 507 508#elif defined(NCTEST_BIND_ONCE_WITH_ADDRESS_OF_OVERLOADED_FUNCTION) // [r"reference to overloaded function could not be resolved; did you mean to call it\?"] 509 510void F(int); 511void F(float); 512 513void WontCompile() { 514 BindOnce(&F, 1, 2, 3); 515} 516 517#elif defined(NCTEST_BIND_REPEATING_WITH_ADDRESS_OF_OVERLOADED_FUNCTION) // [r"reference to overloaded function could not be resolved; did you mean to call it\?"] 518 519void F(int); 520void F(float); 521 522void WontCompile() { 523 BindRepeating(&F, 1, 2, 3); 524} 525 526#elif defined(NCTEST_UNRETAINED_WITH_INCOMPLETE_TYPE) // [r"T must be fully defined\."] 527 528void WontCompile(Incomplete* incomplete) { 529 BindOnce(&PassIncompleteByPtr, base::Unretained(incomplete)); 530} 531 532#elif defined(NCTEST_RAW_POINTER_WITH_INCOMPLETE_TYPE) // [r"T must be fully defined\."] 533 534void WontCompile(Incomplete* incomplete) { 535 BindOnce(&PassIncompleteByPtr, incomplete); 536} 537 538#elif defined(NCTEST_UNRETAINED_WITH_DISALLOWED_TYPE) // [r"Callback cannot capture an unprotected C\+\+ pointer since this type is annotated with DISALLOW_UNRETAINED\(\)\."] 539 540void WontCompile() { 541 Dangerous dangerous; 542 BindOnce(&Dangerous::Method, base::Unretained(&dangerous)); 543} 544 545#elif defined(NCTEST_RAW_POINTER_WITH_DISALLOWED_TYPE) // [r"Callback cannot capture an unprotected C\+\+ pointer since this type is annotated with DISALLOW_UNRETAINED\(\)\."] 546 547void WontCompile() { 548 Dangerous dangerous; 549 BindOnce(&PassDangerousByPtr, &dangerous); 550} 551 552#elif defined(NCTEST_RAW_CONST_REFERENCE_WITH_DISALLOWED_TYPE) // [r"Callback cannot capture an unprotected C\+\+ reference since this type is annotated with DISALLOW_UNRETAINED\(\)\."] 553 554void WontCompile() { 555 Dangerous dangerous; 556 BindOnce(&PassDangerousByConstRef, std::cref(dangerous)); 557} 558 559#elif defined(NCTEST_RAW_MUTABLE_REFERENCE_WITH_DISALLOWED_TYPE) // [r"Callback cannot capture an unprotected C\+\+ reference since this type is annotated with DISALLOW_UNRETAINED\(\)\."] 560 561void WontCompile() { 562 Dangerous dangerous; 563 BindOnce(&PassDangerousByMutableRef, std::ref(dangerous)); 564} 565 566#elif defined(NCTEST_UNSAFE_DANLING_WITHOUT_RAW_PTR_RECEIVER) // [r"base::UnsafeDangling\(\) pointers must be received by functors with MayBeDangling<T> as parameter\."] 567 568void PassIntPtr(int *ptr) {} 569 570void WontCompile() { 571 int val; 572 BindOnce(&PassIntPtr, UnsafeDangling(&val)); 573} 574 575#elif defined(NCTEST_UNSAFE_DANLING_WITH_DIFFERENT_PTR_TRAITS) // [r"MayBeDangling<T> parameter must receive the same RawPtrTraits as the one passed to the corresponding base::UnsafeDangling\(\) call\."] 576 577void PassIntPtr(MayBeDangling<int> ptr) {} 578 579void WontCompile() { 580 int val; 581 BindOnce(&PassIntPtr, UnsafeDangling<int, RawPtrTraits::kDummyForTest>(&val)); 582} 583 584#elif defined(NCTEST_UNSAFE_DANLING_UNTRIAGED_WITH_RAW_PTR_RECEIVER) // [r"base::Bind\(\) target functor has a parameter of type raw_ptr<T>."] 585 586void PassIntRawPtr(raw_ptr<int> ptr) {} 587 588void WontCompile() { 589 int val; 590 BindOnce(&PassIntRawPtr, UnsafeDanglingUntriaged(&val)); 591} 592 593#endif 594 595} // namespace base 596