1 // Copyright (c) 2012 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 5 #include "base/memory/weak_ptr.h" 6 7 namespace base { 8 9 struct Producer : SupportsWeakPtr<Producer> {}; 10 struct DerivedProducer : Producer {}; 11 struct OtherDerivedProducer : Producer {}; 12 struct MultiplyDerivedProducer : Producer, 13 SupportsWeakPtr<MultiplyDerivedProducer> {}; 14 struct Unrelated {}; 15 struct DerivedUnrelated : Unrelated {}; 16 17 #if defined(NCTEST_AUTO_DOWNCAST) // [r"invalid conversion from"] 18 19 void WontCompile() { 20 Producer f; 21 WeakPtr<Producer> ptr = f.AsWeakPtr(); 22 WeakPtr<DerivedProducer> derived_ptr = ptr; 23 } 24 25 #elif defined(NCTEST_STATIC_DOWNCAST) // [r"invalid conversion from"] 26 27 void WontCompile() { 28 Producer f; 29 WeakPtr<Producer> ptr = f.AsWeakPtr(); 30 WeakPtr<DerivedProducer> derived_ptr = 31 static_cast<WeakPtr<DerivedProducer> >(ptr); 32 } 33 34 #elif defined(NCTEST_AUTO_REF_DOWNCAST) // [r"invalid initialization of reference"] 35 36 void WontCompile() { 37 Producer f; 38 WeakPtr<Producer> ptr = f.AsWeakPtr(); 39 WeakPtr<DerivedProducer>& derived_ptr = ptr; 40 } 41 42 #elif defined(NCTEST_STATIC_REF_DOWNCAST) // [r"invalid static_cast"] 43 44 void WontCompile() { 45 Producer f; 46 WeakPtr<Producer> ptr = f.AsWeakPtr(); 47 WeakPtr<DerivedProducer>& derived_ptr = 48 static_cast<WeakPtr<DerivedProducer>&>(ptr); 49 } 50 51 #elif defined(NCTEST_STATIC_ASWEAKPTR_DOWNCAST) // [r"no matching function"] 52 53 void WontCompile() { 54 Producer f; 55 WeakPtr<DerivedProducer> ptr = 56 SupportsWeakPtr<Producer>::StaticAsWeakPtr<DerivedProducer>(&f); 57 } 58 59 #elif defined(NCTEST_UNSAFE_HELPER_DOWNCAST) // [r"invalid conversion from"] 60 61 void WontCompile() { 62 Producer f; 63 WeakPtr<DerivedProducer> ptr = AsWeakPtr(&f); 64 } 65 66 #elif defined(NCTEST_UNSAFE_INSTANTIATED_HELPER_DOWNCAST) // [r"no matching function"] 67 68 void WontCompile() { 69 Producer f; 70 WeakPtr<DerivedProducer> ptr = AsWeakPtr<DerivedProducer>(&f); 71 } 72 73 #elif defined(NCTEST_UNSAFE_WRONG_INSANTIATED_HELPER_DOWNCAST) // [r"invalid conversion from"] 74 75 void WontCompile() { 76 Producer f; 77 WeakPtr<DerivedProducer> ptr = AsWeakPtr<Producer>(&f); 78 } 79 80 #elif defined(NCTEST_UNSAFE_HELPER_CAST) // [r"cannot convert"] 81 82 void WontCompile() { 83 DerivedProducer f; 84 WeakPtr<OtherDerivedProducer> ptr = AsWeakPtr(&f); 85 } 86 87 #elif defined(NCTEST_UNSAFE_INSTANTIATED_HELPER_SIDECAST) // [r"no matching function"] 88 89 void WontCompile() { 90 DerivedProducer f; 91 WeakPtr<OtherDerivedProducer> ptr = AsWeakPtr<OtherDerivedProducer>(&f); 92 } 93 94 #elif defined(NCTEST_UNSAFE_WRONG_INSTANTIATED_HELPER_SIDECAST) // [r"cannot convert"] 95 96 void WontCompile() { 97 DerivedProducer f; 98 WeakPtr<OtherDerivedProducer> ptr = AsWeakPtr<DerivedProducer>(&f); 99 } 100 101 #elif defined(NCTEST_UNRELATED_HELPER) // [r"cannot convert"] 102 103 void WontCompile() { 104 DerivedProducer f; 105 WeakPtr<Unrelated> ptr = AsWeakPtr(&f); 106 } 107 108 #elif defined(NCTEST_UNRELATED_INSTANTIATED_HELPER) // [r"no matching function"] 109 110 void WontCompile() { 111 DerivedProducer f; 112 WeakPtr<Unrelated> ptr = AsWeakPtr<Unrelated>(&f); 113 } 114 115 #elif defined(NCTEST_COMPLETELY_UNRELATED_HELPER) // [r"size of array is negative"] 116 117 void WontCompile() { 118 Unrelated f; 119 WeakPtr<Unrelated> ptr = AsWeakPtr(&f); 120 } 121 122 #elif defined(NCTEST_DERIVED_COMPLETELY_UNRELATED_HELPER) // [r"size of array is negative"] 123 124 void WontCompile() { 125 DerivedUnrelated f; 126 WeakPtr<Unrelated> ptr = AsWeakPtr(&f); 127 } 128 129 #elif defined(NCTEST_AMBIGUOUS_ANCESTORS) // [r"ambiguous base"] 130 131 void WontCompile() { 132 MultiplyDerivedProducer f; 133 WeakPtr<MultiplyDerivedProducer> ptr = AsWeakPtr(&f); 134 } 135 136 #endif 137 138 } 139