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