xref: /llvm-project/clang/test/SemaObjC/objc-dictionary-literal.m (revision 0f1c1be1968076d6f96f8a7bcc4a15cf195ecd97)
1// RUN: %clang_cc1  -fsyntax-only -verify %s
2// RUN: %clang_cc1  -fsyntax-only -triple i386-apple-macosx10.9.0 -fobjc-runtime=macosx-fragile-10.9.0 -fobjc-subscripting-legacy-runtime -verify %s
3
4#define nil ((void *)0)
5
6void checkNSDictionaryUnavailableDiagnostic(void) {
7  id key;
8  id value;
9  id dict = @{ key : value }; // expected-error {{definition of class NSDictionary must be available to use Objective-C dictionary literals}}
10}
11
12@class NSDictionary; // expected-note {{forward declaration of class here}}
13
14void checkNSDictionaryFDDiagnostic(void) {
15  id key;
16  id value;
17  id dic = @{ key : value }; // expected-error {{definition of class NSDictionary must be available to use Objective-C dictionary literals}}
18}
19
20@interface NSNumber
21+ (NSNumber *)numberWithChar:(char)value;
22+ (NSNumber *)numberWithInt:(int)value;
23@end
24
25@protocol NSCopying @end
26typedef unsigned long NSUInteger;
27typedef long NSInteger;
28
29@interface NSDictionary
30+ (id)dictionaryWithObjects:(const id [])objects forKeys:(const id <NSCopying> [])keys count:(NSUInteger)cnt;
31- (void)setObject:(id)object forKeyedSubscript:(id)key;
32- (id)objectForKeyedSubscript:(id)key;
33@end
34
35@interface NSString<NSCopying>
36@end
37
38@interface NSArray
39- (id)objectAtIndexedSubscript:(NSInteger)index;
40- (void)setObject:(id)object atIndexedSubscript:(NSInteger)index;
41@end
42
43void *pvoid;
44int main(void) {
45	NSDictionary *dict = @{ @"name":@666 };
46        dict[@"name"] = @666;
47
48        dict["name"] = @666; // expected-error {{indexing expression is invalid because subscript type 'char *' is not an Objective-C pointer}}
49
50        [@{@"foo" : @"bar"} objectForKeyedSubscript:nil];
51        (void)@{@"foo" : @"bar"}[nil];
52        [@{@"foo" : @"bar"} objectForKeyedSubscript:pvoid];
53        (void)@{@"foo" : @"bar"}[pvoid];
54
55	[@{@"foo" : @"bar"} setObject:nil forKeyedSubscript:@"gorf"];
56        @{@"foo" : @"bar"}[nil] = @"gorf";
57	[@{@"foo" : @"bar"} setObject:pvoid forKeyedSubscript:@"gorf"];
58        @{@"foo" : @"bar"}[pvoid] = @"gorf";
59
60	return 0;
61}
62
63enum XXXYYYZZZType { XXXYYYZZZTypeAny }; // expected-note {{'XXXYYYZZZTypeAny' declared here}}
64void foo(void) {
65  NSDictionary *d = @{
66    @"A" : @(XXXYYYZZZTypeA), // expected-error {{use of undeclared identifier 'XXXYYYZZZTypeA'; did you mean 'XXXYYYZZZTypeAny'}}
67    @"F" : @(XXXYYYZZZTypeSomethingSomething), // expected-error {{use of undeclared identifier 'XXXYYYZZZTypeSomethingSomething'}}
68  };
69}
70