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 | |
ajwong@chromium.org | 505386d | 2011-09-10 12:03:00 +0900 | [diff] [blame] | 8 | #include "base/bind.h" |
avi | a6a6a68 | 2015-12-27 07:15:14 +0900 | [diff] [blame] | 9 | #include "base/callback.h" |
| 10 | #include "base/macros.h" |
ajwong@chromium.org | c9c79af | 2011-11-22 04:23:44 +0900 | [diff] [blame] | 11 | #include "base/memory/ref_counted.h" |
ajwong@chromium.org | 505386d | 2011-09-10 12:03:00 +0900 | [diff] [blame] | 12 | |
| 13 | namespace base { |
| 14 | |
| 15 | // 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] | 16 | // helper function declarations will generate unused definition warnings. |
ajwong@chromium.org | 505386d | 2011-09-10 12:03:00 +0900 | [diff] [blame] | 17 | |
| 18 | static const int kParentValue = 1; |
| 19 | static const int kChildValue = 2; |
| 20 | |
| 21 | class NoRef { |
| 22 | public: |
| 23 | void VoidMethod0() {} |
| 24 | void VoidConstMethod0() const {} |
| 25 | int IntMethod0() { return 1; } |
| 26 | }; |
| 27 | |
ajwong@chromium.org | c9c79af | 2011-11-22 04:23:44 +0900 | [diff] [blame] | 28 | class HasRef : public NoRef, public base::RefCounted<HasRef> { |
ajwong@chromium.org | 505386d | 2011-09-10 12:03:00 +0900 | [diff] [blame] | 29 | }; |
| 30 | |
| 31 | class Parent { |
| 32 | public: |
tzik | 260fab5 | 2015-12-19 18:18:46 +0900 | [diff] [blame] | 33 | void AddRef() const {} |
| 34 | void Release() const {} |
ajwong@chromium.org | 505386d | 2011-09-10 12:03:00 +0900 | [diff] [blame] | 35 | virtual void VirtualSet() { value = kParentValue; } |
| 36 | void NonVirtualSet() { value = kParentValue; } |
| 37 | int value; |
| 38 | }; |
| 39 | |
| 40 | class Child : public Parent { |
| 41 | public: |
| 42 | virtual void VirtualSet() { value = kChildValue; } |
| 43 | void NonVirtualSet() { value = kChildValue; } |
| 44 | }; |
| 45 | |
| 46 | class NoRefParent { |
| 47 | public: |
| 48 | virtual void VirtualSet() { value = kParentValue; } |
| 49 | void NonVirtualSet() { value = kParentValue; } |
| 50 | int value; |
| 51 | }; |
| 52 | |
| 53 | class NoRefChild : public NoRefParent { |
| 54 | virtual void VirtualSet() { value = kChildValue; } |
| 55 | void NonVirtualSet() { value = kChildValue; } |
| 56 | }; |
| 57 | |
| 58 | template <typename T> |
| 59 | T PolymorphicIdentity(T t) { |
| 60 | return t; |
| 61 | } |
| 62 | |
| 63 | int UnwrapParentRef(Parent& p) { |
| 64 | return p.value; |
| 65 | } |
| 66 | |
| 67 | template <typename T> |
| 68 | void VoidPolymorphic1(T t) { |
| 69 | } |
| 70 | |
tzik | fa7381d | 2016-05-12 08:05:05 +0900 | [diff] [blame] | 71 | #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] | 72 | |
| 73 | // Method bound to const-object. |
| 74 | // |
| 75 | // Only const methods should be allowed to work with const objects. |
| 76 | void WontCompile() { |
| 77 | HasRef has_ref; |
| 78 | const HasRef* const_has_ref_ptr_ = &has_ref; |
tzik | 260fab5 | 2015-12-19 18:18:46 +0900 | [diff] [blame] | 79 | Callback<void()> method_to_const_cb = |
ajwong@chromium.org | 505386d | 2011-09-10 12:03:00 +0900 | [diff] [blame] | 80 | Bind(&HasRef::VoidMethod0, const_has_ref_ptr_); |
| 81 | method_to_const_cb.Run(); |
| 82 | } |
| 83 | |
dcheng | c7fd832 | 2014-11-04 06:29:30 +0900 | [diff] [blame] | 84 | #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] | 85 | |
| 86 | // Method bound to non-refcounted object. |
| 87 | // |
| 88 | // We require refcounts unless you have Unretained(). |
| 89 | void WontCompile() { |
| 90 | NoRef no_ref; |
tzik | 260fab5 | 2015-12-19 18:18:46 +0900 | [diff] [blame] | 91 | Callback<void()> no_ref_cb = |
ajwong@chromium.org | 505386d | 2011-09-10 12:03:00 +0900 | [diff] [blame] | 92 | Bind(&NoRef::VoidMethod0, &no_ref); |
| 93 | no_ref_cb.Run(); |
| 94 | } |
| 95 | |
dcheng | c7fd832 | 2014-11-04 06:29:30 +0900 | [diff] [blame] | 96 | #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] | 97 | |
| 98 | // Const Method bound to non-refcounted object. |
| 99 | // |
| 100 | // We require refcounts unless you have Unretained(). |
| 101 | void WontCompile() { |
| 102 | NoRef no_ref; |
tzik | 260fab5 | 2015-12-19 18:18:46 +0900 | [diff] [blame] | 103 | Callback<void()> no_ref_const_cb = |
ajwong@chromium.org | 505386d | 2011-09-10 12:03:00 +0900 | [diff] [blame] | 104 | Bind(&NoRef::VoidConstMethod0, &no_ref); |
| 105 | no_ref_const_cb.Run(); |
| 106 | } |
| 107 | |
tzik | 1c6525c | 2016-02-16 12:46:07 +0900 | [diff] [blame] | 108 | #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] | 109 | |
| 110 | // Const argument used with non-const pointer parameter of same type. |
| 111 | // |
| 112 | // This is just a const-correctness check. |
| 113 | void WontCompile() { |
| 114 | const NoRef* const_no_ref_ptr; |
tzik | 260fab5 | 2015-12-19 18:18:46 +0900 | [diff] [blame] | 115 | Callback<NoRef*()> pointer_same_cb = |
ajwong@chromium.org | 505386d | 2011-09-10 12:03:00 +0900 | [diff] [blame] | 116 | Bind(&PolymorphicIdentity<NoRef*>, const_no_ref_ptr); |
| 117 | pointer_same_cb.Run(); |
| 118 | } |
| 119 | |
tzik | 1c6525c | 2016-02-16 12:46:07 +0900 | [diff] [blame] | 120 | #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] | 121 | |
| 122 | // Const argument used with non-const pointer parameter of super type. |
| 123 | // |
| 124 | // This is just a const-correctness check. |
| 125 | void WontCompile() { |
| 126 | const NoRefChild* const_child_ptr; |
tzik | 260fab5 | 2015-12-19 18:18:46 +0900 | [diff] [blame] | 127 | Callback<NoRefParent*()> pointer_super_cb = |
ajwong@chromium.org | 505386d | 2011-09-10 12:03:00 +0900 | [diff] [blame] | 128 | Bind(&PolymorphicIdentity<NoRefParent*>, const_child_ptr); |
| 129 | pointer_super_cb.Run(); |
| 130 | } |
| 131 | |
dcheng | c7fd832 | 2014-11-04 06:29:30 +0900 | [diff] [blame] | 132 | #elif defined(DISABLED_NCTEST_DISALLOW_NON_CONST_REF_PARAM) // [r"fatal error: no member named 'AddRef' in 'base::NoRef'"] |
| 133 | // TODO(dcheng): I think there's a type safety promotion issue here where we can |
| 134 | // pass a const ref to a non const-ref function, or vice versa accidentally. Or |
| 135 | // we make a copy accidentally. Check. |
ajwong@chromium.org | 505386d | 2011-09-10 12:03:00 +0900 | [diff] [blame] | 136 | |
| 137 | // Functions with reference parameters, unsupported. |
| 138 | // |
| 139 | // First, non-const reference parameters are disallowed by the Google |
| 140 | // style guide. Second, since we are doing argument forwarding it becomes |
| 141 | // very tricky to avoid copies, maintain const correctness, and not |
| 142 | // accidentally have the function be modifying a temporary, or a copy. |
| 143 | void WontCompile() { |
| 144 | Parent p; |
| 145 | Callback<int(Parent&)> ref_arg_cb = Bind(&UnwrapParentRef); |
| 146 | ref_arg_cb.Run(p); |
| 147 | } |
| 148 | |
tzik | 587f17f | 2015-12-11 15:39:06 +0900 | [diff] [blame] | 149 | #elif defined(NCTEST_DISALLOW_BIND_TO_NON_CONST_REF_PARAM) // [r"fatal error: static_assert failed \"do not bind functions with nonconst ref\""] |
ajwong@chromium.org | 505386d | 2011-09-10 12:03:00 +0900 | [diff] [blame] | 150 | |
| 151 | // Binding functions with reference parameters, unsupported. |
| 152 | // |
| 153 | // See comment in NCTEST_DISALLOW_NON_CONST_REF_PARAM |
| 154 | void WontCompile() { |
| 155 | Parent p; |
tzik | 260fab5 | 2015-12-19 18:18:46 +0900 | [diff] [blame] | 156 | Callback<int()> ref_cb = Bind(&UnwrapParentRef, p); |
ajwong@chromium.org | 505386d | 2011-09-10 12:03:00 +0900 | [diff] [blame] | 157 | ref_cb.Run(); |
| 158 | } |
| 159 | |
tzik | 587f17f | 2015-12-11 15:39:06 +0900 | [diff] [blame] | 160 | #elif defined(NCTEST_NO_IMPLICIT_ARRAY_PTR_CONVERSION) // [r"fatal error: static_assert failed \"first bound argument to method cannot be array\""] |
ajwong@chromium.org | 505386d | 2011-09-10 12:03:00 +0900 | [diff] [blame] | 161 | |
| 162 | // A method should not be bindable with an array of objects. |
| 163 | // |
| 164 | // This is likely not wanted behavior. We specifically check for it though |
| 165 | // because it is possible, depending on how you implement prebinding, to |
| 166 | // implicitly convert an array type to a pointer type. |
| 167 | void WontCompile() { |
| 168 | HasRef p[10]; |
tzik | 260fab5 | 2015-12-19 18:18:46 +0900 | [diff] [blame] | 169 | Callback<void()> method_bound_to_array_cb = |
ajwong@chromium.org | 505386d | 2011-09-10 12:03:00 +0900 | [diff] [blame] | 170 | Bind(&HasRef::VoidMethod0, p); |
| 171 | method_bound_to_array_cb.Run(); |
| 172 | } |
| 173 | |
tzik | 587f17f | 2015-12-11 15:39:06 +0900 | [diff] [blame] | 174 | #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] | 175 | |
| 176 | // Refcounted types should not be bound as a raw pointer. |
| 177 | void WontCompile() { |
| 178 | HasRef for_raw_ptr; |
ajwong@chromium.org | c9c79af | 2011-11-22 04:23:44 +0900 | [diff] [blame] | 179 | int a; |
tzik | 260fab5 | 2015-12-19 18:18:46 +0900 | [diff] [blame] | 180 | Callback<void()> ref_count_as_raw_ptr_a = |
ajwong@chromium.org | c9c79af | 2011-11-22 04:23:44 +0900 | [diff] [blame] | 181 | Bind(&VoidPolymorphic1<int*>, &a); |
tzik | 260fab5 | 2015-12-19 18:18:46 +0900 | [diff] [blame] | 182 | Callback<void()> ref_count_as_raw_ptr = |
ajwong@chromium.org | 505386d | 2011-09-10 12:03:00 +0900 | [diff] [blame] | 183 | Bind(&VoidPolymorphic1<HasRef*>, &for_raw_ptr); |
| 184 | } |
| 185 | |
tzik | 587f17f | 2015-12-11 15:39:06 +0900 | [diff] [blame] | 186 | #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] | 187 | |
| 188 | // WeakPtrs cannot be bound to methods with return types. |
| 189 | void WontCompile() { |
| 190 | NoRef no_ref; |
| 191 | WeakPtrFactory<NoRef> weak_factory(&no_ref); |
tzik | 260fab5 | 2015-12-19 18:18:46 +0900 | [diff] [blame] | 192 | Callback<int()> weak_ptr_with_non_void_return_type = |
ajwong@chromium.org | 505386d | 2011-09-10 12:03:00 +0900 | [diff] [blame] | 193 | Bind(&NoRef::IntMethod0, weak_factory.GetWeakPtr()); |
| 194 | weak_ptr_with_non_void_return_type.Run(); |
| 195 | } |
| 196 | |
tzik | 817518d | 2016-02-12 22:54:37 +0900 | [diff] [blame] | 197 | #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] | 198 | |
| 199 | // Bind result cannot be assigned to Callbacks with a mismatching type. |
| 200 | void WontCompile() { |
| 201 | Closure callback_mismatches_bind_type = Bind(&VoidPolymorphic1<int>); |
| 202 | } |
| 203 | |
| 204 | #endif |
| 205 | |
| 206 | } // namespace base |