xref: /aosp_15_r20/external/clang/test/SemaObjCXX/objc-container-subscripting.mm (revision 67e74705e28f6214e480b399dd47ea732279e315)
1*67e74705SXin Li// RUN: %clang_cc1 -fblocks -triple x86_64-apple-darwin11 -fsyntax-only -std=c++11 -verify %s
2*67e74705SXin Li
3*67e74705SXin Li@class NSArray;
4*67e74705SXin Li
5*67e74705SXin Li@interface NSMutableDictionary
6*67e74705SXin Li- (id)objectForKeyedSubscript:(id)key;
7*67e74705SXin Li- (void)setObject:(id)object forKeyedSubscript:(id)key; // expected-note {{passing argument to parameter 'object' here}}
8*67e74705SXin Li@end
9*67e74705SXin Li
10*67e74705SXin Litemplate<typename T, typename U, typename O>
11*67e74705SXin Livoid test_dictionary_subscripts(T base, U key, O obj) {
12*67e74705SXin Li  base[key] = obj; // expected-error {{expected method to write array element not found on object of type 'NSMutableDictionary *'}} \
13*67e74705SXin Li                   // expected-error {{cannot initialize a parameter of type 'id' with an lvalue of type 'int'}}
14*67e74705SXin Li  obj = base[key];  // expected-error {{expected method to read array element not found on object of type 'NSMutableDictionary *'}} \
15*67e74705SXin Li                    // expected-error {{assigning to 'int' from incompatible type 'id'}}
16*67e74705SXin Li
17*67e74705SXin Li}
18*67e74705SXin Li
19*67e74705SXin Litemplate void test_dictionary_subscripts(NSMutableDictionary*, id, NSArray *ns);
20*67e74705SXin Li
21*67e74705SXin Litemplate void test_dictionary_subscripts(NSMutableDictionary*, NSArray *ns, id);
22*67e74705SXin Li
23*67e74705SXin Litemplate void test_dictionary_subscripts(NSMutableDictionary*, int, id); // expected-note {{in instantiation of function template specialization 'test_dictionary_subscripts<NSMutableDictionary *, int, id>' requested here}}
24*67e74705SXin Li
25*67e74705SXin Litemplate void test_dictionary_subscripts(NSMutableDictionary*, id, int); // expected-note {{in instantiation of function template specialization 'test_dictionary_subscripts<NSMutableDictionary *, id, int>' requested here}}
26*67e74705SXin Li
27*67e74705SXin Li
28*67e74705SXin Li@interface NSMutableArray
29*67e74705SXin Li- (id)objectAtIndexedSubscript:(int)index;
30*67e74705SXin Li- (void)setObject:(id)object atIndexedSubscript:(int)index;
31*67e74705SXin Li@end
32*67e74705SXin Li
33*67e74705SXin Litemplate<typename T, typename U, typename O>
34*67e74705SXin Livoid test_array_subscripts(T base, U index, O obj) {
35*67e74705SXin Li  base[index] = obj; // expected-error {{indexing expression is invalid because subscript type 'double' is not an integral or Objective-C pointer type}}
36*67e74705SXin Li  obj = base[index]; // expected-error {{indexing expression is invalid because subscript type 'double' is not an integral or Objective-C pointer type}}
37*67e74705SXin Li}
38*67e74705SXin Li
39*67e74705SXin Litemplate void  test_array_subscripts(NSMutableArray *, int, id);
40*67e74705SXin Litemplate void  test_array_subscripts(NSMutableArray *, short, id);
41*67e74705SXin Lienum E { e };
42*67e74705SXin Li
43*67e74705SXin Litemplate void  test_array_subscripts(NSMutableArray *, E, id);
44*67e74705SXin Li
45*67e74705SXin Litemplate void  test_array_subscripts(NSMutableArray *, double, id); // expected-note {{in instantiation of function template specialization 'test_array_subscripts<NSMutableArray *, double, id>' requested here}}
46*67e74705SXin Li
47*67e74705SXin Litemplate<typename T>
48*67e74705SXin Listruct ConvertibleTo {
49*67e74705SXin Li  operator T();
50*67e74705SXin Li};
51*67e74705SXin Li
52*67e74705SXin Litemplate<typename T>
53*67e74705SXin Listruct ExplicitlyConvertibleTo {
54*67e74705SXin Li  explicit operator T();
55*67e74705SXin Li};
56*67e74705SXin Li
57*67e74705SXin Litemplate<typename T> ConvertibleTo<T> makeConvertible();
58*67e74705SXin Li
59*67e74705SXin Listruct X {
60*67e74705SXin Li  ConvertibleTo<id> x;
61*67e74705SXin Li  ConvertibleTo<id> get();
62*67e74705SXin Li};
63*67e74705SXin Li
64*67e74705SXin LiNSMutableArray *test_array_convertibility(ConvertibleTo<NSMutableArray*> toArray,
65*67e74705SXin Li                         ConvertibleTo<id> toId,
66*67e74705SXin Li                         ConvertibleTo<int (^)(int)> toBlock,
67*67e74705SXin Li                         ConvertibleTo<int> toInt,
68*67e74705SXin Li                         ExplicitlyConvertibleTo<NSMutableArray *> toArrayExplicit) {
69*67e74705SXin Li  id array;
70*67e74705SXin Li
71*67e74705SXin Li  array[1] = toArray;
72*67e74705SXin Li
73*67e74705SXin Li  array[4] = array[1];
74*67e74705SXin Li
75*67e74705SXin Li  toArrayExplicit[2] = toId; // expected-error {{type 'ExplicitlyConvertibleTo<NSMutableArray *>' does not provide a subscript operator}}
76*67e74705SXin Li
77*67e74705SXin Li  return array[toInt];
78*67e74705SXin Li
79*67e74705SXin Li}
80*67e74705SXin Li
81*67e74705SXin Liid test_dict_convertibility(ConvertibleTo<NSMutableDictionary*> toDict,
82*67e74705SXin Li                         ConvertibleTo<id> toId,
83*67e74705SXin Li                         ConvertibleTo<int (^)(int)> toBlock,
84*67e74705SXin Li                         ConvertibleTo<int> toInt,
85*67e74705SXin Li                         ExplicitlyConvertibleTo<NSMutableDictionary *> toDictExplicit) {
86*67e74705SXin Li
87*67e74705SXin Li
88*67e74705SXin Li  NSMutableDictionary *Dict;
89*67e74705SXin Li  id Id;
90*67e74705SXin Li  Dict[toId] = toBlock;
91*67e74705SXin Li
92*67e74705SXin Li  Dict[toBlock] = toBlock;
93*67e74705SXin Li
94*67e74705SXin Li  Dict[toBlock] = Dict[toId] = Dict[toBlock];
95*67e74705SXin Li
96*67e74705SXin Li  Id = toDictExplicit[toId] = Id; // expected-error {{no viable overloaded operator[] for type 'ExplicitlyConvertibleTo<NSMutableDictionary *>'}}
97*67e74705SXin Li
98*67e74705SXin Li  return Dict[toBlock];
99*67e74705SXin Li}
100*67e74705SXin Li
101*67e74705SXin Li
102*67e74705SXin Litemplate<typename ...Args>
103*67e74705SXin Livoid test_bad_variadic_array_subscripting(Args ...args) {
104*67e74705SXin Li  id arr1;
105*67e74705SXin Li  arr1[3] = args; // expected-error {{expression contains unexpanded parameter pack 'args'}}
106*67e74705SXin Li}
107*67e74705SXin Li
108*67e74705SXin Litemplate<typename ...Args>
109*67e74705SXin Livoid test_variadic_array_subscripting(Args ...args) {
110*67e74705SXin Li  id arr[] = {args[3]...}; // which means: {a[3], b[3], c[3]};
111*67e74705SXin Li}
112*67e74705SXin Li
113*67e74705SXin Litemplate void test_variadic_array_subscripting(id arg1, NSMutableArray* arg2, id arg3);
114*67e74705SXin Li
115*67e74705SXin Li@class Key;
116*67e74705SXin Li
117*67e74705SXin Litemplate<typename Index, typename ...Args>
118*67e74705SXin Livoid test_variadic_dictionary_subscripting(Index I, Args ...args) {
119*67e74705SXin Li  id arr[] = {args[I]...}; // which means: {a[3], b[3], c[3]};
120*67e74705SXin Li}
121*67e74705SXin Li
122*67e74705SXin Litemplate void test_variadic_dictionary_subscripting(Key *key, id arg1, NSMutableDictionary* arg2, id arg3);
123*67e74705SXin Li
124*67e74705SXin Litemplate<int N>
125*67e74705SXin Liid get(NSMutableArray *array) {
126*67e74705SXin Li return array[N]; // array[N] should be a value- and instantiation-dependent ObjCSubscriptRefExpr
127*67e74705SXin Li}
128*67e74705SXin Li
129*67e74705SXin Listruct WeirdIndex {
130*67e74705SXin Li   operator int(); // expected-note {{type conversion function declared here}}
131*67e74705SXin Li   operator id(); // expected-note {{type conversion function declared here}}
132*67e74705SXin Li};
133*67e74705SXin Li
134*67e74705SXin Liid FUNC(WeirdIndex w) {
135*67e74705SXin Li  NSMutableArray *array;
136*67e74705SXin Li  return array[w]; // expected-error {{indexing expression is invalid because subscript type 'WeirdIndex' has multiple type conversion functions}}
137*67e74705SXin Li}
138*67e74705SXin Li
139