xref: /aosp_15_r20/external/clang/test/SemaObjC/arc-unavailable-for-weakref.m (revision 67e74705e28f6214e480b399dd47ea732279e315)
1*67e74705SXin Li// RUN: %clang_cc1 -triple x86_64-apple-darwin11 -fobjc-runtime-has-weak -fsyntax-only -fobjc-arc -verify -Wno-objc-root-class %s
2*67e74705SXin Li// rdar://9693477
3*67e74705SXin Li
4*67e74705SXin Li__attribute__((objc_arc_weak_reference_unavailable))
5*67e74705SXin Li@interface NSOptOut1072  // expected-note {{class is declared here}}
6*67e74705SXin Li@end
7*67e74705SXin Li
8*67e74705SXin Li@interface sub : NSOptOut1072 @end // expected-note 2 {{class is declared here}}
9*67e74705SXin Li
10*67e74705SXin Liint main() {
11*67e74705SXin Li  __weak sub *w2; // expected-error {{class is incompatible with __weak references}}
12*67e74705SXin Li
13*67e74705SXin Li  __weak NSOptOut1072 *ns1; // expected-error {{class is incompatible with __weak references}}
14*67e74705SXin Li
15*67e74705SXin Li  id obj;
16*67e74705SXin Li
17*67e74705SXin Li  ns1 = (__weak sub *)obj; // expected-error {{assignment of a weak-unavailable object to a __weak object}} \
18*67e74705SXin Li                           // expected-error {{class is incompatible with __weak references}} \
19*67e74705SXin Li                           // expected-error {{explicit ownership qualifier on cast result has no effect}}
20*67e74705SXin Li}
21*67e74705SXin Li
22*67e74705SXin Li// rdar://9732636
23*67e74705SXin Li__attribute__((objc_arc_weak_reference_unavailable))
24*67e74705SXin Li@interface NOWEAK
25*67e74705SXin Li+ (id) new;
26*67e74705SXin Li@end
27*67e74705SXin Li
28*67e74705SXin LiNOWEAK * Test1() {
29*67e74705SXin Li  NOWEAK * strong1 = [NOWEAK new];
30*67e74705SXin Li  __weak id weak1;
31*67e74705SXin Li  weak1 = strong1; // expected-error {{assignment of a weak-unavailable object to a __weak object}}
32*67e74705SXin Li
33*67e74705SXin Li  __weak id weak2 = strong1; // expected-error {{assignment of a weak-unavailable object to a __weak object}}
34*67e74705SXin Li  return (__weak id)strong1; // expected-error {{cast of weak-unavailable object of type 'NOWEAK *' to a __weak object of type '__weak id'}} \
35*67e74705SXin Li                             // expected-error {{explicit ownership qualifier on cast result has no effect}}
36*67e74705SXin Li}
37*67e74705SXin Li
38*67e74705SXin Li@protocol P @end
39*67e74705SXin Li@protocol P1 @end
40*67e74705SXin Li
41*67e74705SXin LiNOWEAK<P, P1> * Test2() {
42*67e74705SXin Li  NOWEAK<P, P1> * strong1 = 0;
43*67e74705SXin Li  __weak id<P> weak1;
44*67e74705SXin Li  weak1 = strong1; // expected-error {{assignment of a weak-unavailable object to a __weak object}}
45*67e74705SXin Li
46*67e74705SXin Li  __weak id<P> weak2 = strong1; // expected-error {{assignment of a weak-unavailable object to a __weak object}}
47*67e74705SXin Li  return (__weak id<P>)strong1; // expected-error {{cast of weak-unavailable object of type 'NOWEAK<P,P1> *' to a __weak object of type '__weak id<P>'}} \
48*67e74705SXin Li                                // expected-error {{explicit ownership qualifier on cast result has no effect}}
49*67e74705SXin Li}
50*67e74705SXin Li
51*67e74705SXin Li// rdar://10535245
52*67e74705SXin Li__attribute__((objc_arc_weak_reference_unavailable))
53*67e74705SXin Li@interface NSFont
54*67e74705SXin Li@end
55*67e74705SXin Li
56*67e74705SXin Li@interface I
57*67e74705SXin Li{
58*67e74705SXin Li}
59*67e74705SXin Li@property (weak) NSFont *font; // expected-error {{synthesizing __weak instance variable of type 'NSFont *', which does not support weak references}}
60*67e74705SXin Li@end
61*67e74705SXin Li
62*67e74705SXin Li@implementation I // expected-note {{when implemented by class I}}
63*67e74705SXin Li@synthesize font = _font;
64*67e74705SXin Li@end
65*67e74705SXin Li
66*67e74705SXin Li// rdar://13676793
67*67e74705SXin Li@protocol MyProtocol
68*67e74705SXin Li@property (weak) NSFont *font; // expected-error {{synthesizing __weak instance variable of type 'NSFont *', which does not support weak references}}
69*67e74705SXin Li@end
70*67e74705SXin Li
71*67e74705SXin Li@interface I1 <MyProtocol>
72*67e74705SXin Li@end
73*67e74705SXin Li
74*67e74705SXin Li@implementation I1 // expected-note {{when implemented by class I1}}
75*67e74705SXin Li@synthesize font = _font;
76*67e74705SXin Li@end
77*67e74705SXin Li
78*67e74705SXin Li@interface Super
79*67e74705SXin Li@property (weak) NSFont *font;  // expected-error {{synthesizing __weak instance variable of type 'NSFont *', which does not support weak references}}
80*67e74705SXin Li@end
81*67e74705SXin Li
82*67e74705SXin Li
83*67e74705SXin Li@interface I2 : Super
84*67e74705SXin Li@end
85*67e74705SXin Li
86*67e74705SXin Li@implementation I2 // expected-note {{when implemented by class I2}}
87*67e74705SXin Li@synthesize font = _font;
88*67e74705SXin Li@end
89*67e74705SXin Li
90*67e74705SXin Li__attribute__((objc_arc_weak_reference_unavailable(1)))	// expected-error {{'objc_arc_weak_reference_unavailable' attribute takes no arguments}}
91*67e74705SXin Li@interface I3
92*67e74705SXin Li@end
93*67e74705SXin Li
94*67e74705SXin Liint I4 __attribute__((objc_arc_weak_reference_unavailable)); // expected-error {{'objc_arc_weak_reference_unavailable' attribute only applies to Objective-C interfaces}}
95