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// This is a "No Compile Test" suite.
6// http://dev.chromium.org/developers/testing/no-compile-tests
7
8#include "base/memory/weak_ptr.h"
9
10namespace base {
11
12struct Producer : SupportsWeakPtr<Producer> {};
13struct DerivedProducer : Producer {};
14struct OtherDerivedProducer : Producer {};
15struct MultiplyDerivedProducer : Producer,
16                                 SupportsWeakPtr<MultiplyDerivedProducer> {};
17struct Unrelated {};
18struct DerivedUnrelated : Unrelated {};
19
20#if defined(NCTEST_AUTO_DOWNCAST)  // [r"fatal error: cannot initialize a member subobject of type 'base::DerivedProducer \*' with an lvalue of type 'base::Producer \*const'"]
21
22void WontCompile() {
23  Producer f;
24  WeakPtr<Producer> ptr = f.AsWeakPtr();
25  WeakPtr<DerivedProducer> derived_ptr = ptr;
26}
27
28#elif defined(NCTEST_STATIC_DOWNCAST)  // [r"fatal error: cannot initialize a member subobject of type 'base::DerivedProducer \*' with an lvalue of type 'base::Producer \*const'"]
29
30void WontCompile() {
31  Producer f;
32  WeakPtr<Producer> ptr = f.AsWeakPtr();
33  WeakPtr<DerivedProducer> derived_ptr =
34      static_cast<WeakPtr<DerivedProducer> >(ptr);
35}
36
37#elif defined(NCTEST_AUTO_REF_DOWNCAST)  // [r"fatal error: non-const lvalue reference to type 'WeakPtr<base::DerivedProducer>' cannot bind to a value of unrelated type 'WeakPtr<base::Producer>'"]
38
39void WontCompile() {
40  Producer f;
41  WeakPtr<Producer> ptr = f.AsWeakPtr();
42  WeakPtr<DerivedProducer>& derived_ptr = ptr;
43}
44
45#elif defined(NCTEST_STATIC_REF_DOWNCAST)  // [r"fatal error: non-const lvalue reference to type 'WeakPtr<base::DerivedProducer>' cannot bind to a value of unrelated type 'WeakPtr<base::Producer>'"]
46
47void WontCompile() {
48  Producer f;
49  WeakPtr<Producer> ptr = f.AsWeakPtr();
50  WeakPtr<DerivedProducer>& derived_ptr =
51      static_cast<WeakPtr<DerivedProducer>&>(ptr);
52}
53
54#elif defined(NCTEST_STATIC_ASWEAKPTR_DOWNCAST)  // [r"no matching function"]
55
56void WontCompile() {
57  Producer f;
58  WeakPtr<DerivedProducer> ptr =
59      SupportsWeakPtr<Producer>::StaticAsWeakPtr<DerivedProducer>(&f);
60}
61
62#elif defined(NCTEST_UNSAFE_HELPER_DOWNCAST)  // [r"fatal error: cannot initialize a member subobject of type 'base::DerivedProducer \*' with an lvalue of type 'base::Producer \*const'"]
63
64void WontCompile() {
65  Producer f;
66  WeakPtr<DerivedProducer> ptr = AsWeakPtr(&f);
67}
68
69#elif defined(NCTEST_UNSAFE_INSTANTIATED_HELPER_DOWNCAST)  // [r"no matching function"]
70
71void WontCompile() {
72  Producer f;
73  WeakPtr<DerivedProducer> ptr = AsWeakPtr<DerivedProducer>(&f);
74}
75
76#elif defined(NCTEST_UNSAFE_WRONG_INSANTIATED_HELPER_DOWNCAST)  // [r"fatal error: cannot initialize a member subobject of type 'base::DerivedProducer \*' with an lvalue of type 'base::Producer \*const'"]
77
78void WontCompile() {
79  Producer f; 
80  WeakPtr<DerivedProducer> ptr = AsWeakPtr<Producer>(&f);
81}
82
83#elif defined(NCTEST_UNSAFE_HELPER_CAST)  // [r"fatal error: cannot initialize a member subobject of type 'base::OtherDerivedProducer \*' with an lvalue of type 'base::DerivedProducer \*const'"]
84
85void WontCompile() {
86  DerivedProducer f;
87  WeakPtr<OtherDerivedProducer> ptr = AsWeakPtr(&f);
88}
89
90#elif defined(NCTEST_UNSAFE_INSTANTIATED_HELPER_SIDECAST)  // [r"fatal error: no matching function for call to 'AsWeakPtr'"]
91
92void WontCompile() {
93  DerivedProducer f;
94  WeakPtr<OtherDerivedProducer> ptr = AsWeakPtr<OtherDerivedProducer>(&f);
95}
96
97#elif defined(NCTEST_UNSAFE_WRONG_INSTANTIATED_HELPER_SIDECAST)  // [r"fatal error: cannot initialize a member subobject of type 'base::OtherDerivedProducer \*' with an lvalue of type 'base::DerivedProducer \*const'"]
98
99void WontCompile() {
100  DerivedProducer f;
101  WeakPtr<OtherDerivedProducer> ptr = AsWeakPtr<DerivedProducer>(&f);
102}
103
104#elif defined(NCTEST_UNRELATED_HELPER)  // [r"fatal error: cannot initialize a member subobject of type 'base::Unrelated \*' with an lvalue of type 'base::DerivedProducer \*const'"]
105
106void WontCompile() {
107  DerivedProducer f;
108  WeakPtr<Unrelated> ptr = AsWeakPtr(&f);
109}
110
111#elif defined(NCTEST_UNRELATED_INSTANTIATED_HELPER)  // [r"no matching function"]
112
113void WontCompile() {
114  DerivedProducer f;
115  WeakPtr<Unrelated> ptr = AsWeakPtr<Unrelated>(&f);
116}
117
118#elif defined(NCTEST_COMPLETELY_UNRELATED_HELPER)  // [r"fatal error: static_assert failed \"AsWeakPtr argument must inherit from SupportsWeakPtr\""]
119
120void WontCompile() {
121  Unrelated f;
122  WeakPtr<Unrelated> ptr = AsWeakPtr(&f);
123}
124
125#elif defined(NCTEST_DERIVED_COMPLETELY_UNRELATED_HELPER)  // [r"fatal error: static_assert failed \"AsWeakPtr argument must inherit from SupportsWeakPtr\""]
126
127void WontCompile() {
128  DerivedUnrelated f;
129  WeakPtr<Unrelated> ptr = AsWeakPtr(&f);
130}
131
132#elif defined(NCTEST_AMBIGUOUS_ANCESTORS)  // [r"fatal error: ambiguous conversion from derived class 'base::MultiplyDerivedProducer' to base class 'base::internal::SupportsWeakPtrBase':"]
133
134void WontCompile() {
135  MultiplyDerivedProducer f;
136  WeakPtr<MultiplyDerivedProducer> ptr = AsWeakPtr(&f);
137}
138
139#endif
140
141}
142