Clang/Foundation: Key type of NSMutableDictionary is not checked properly

Originator:daniel
Number:rdar://27473065 Date Originated:21-Jul-2016 06:37 PM
Status:Open Resolved:
Product:Developer Tools Product Version:Xcode-beta 3 (8S174q)
Classification:Serious Bug Reproducible:Always
 
Summary:
Although Foundation/NSMutableDictionary.h declares -setObject:forKey: and -setObject:forKeyedSubscript: with a type signature that requires the KeyType to conform to NSCopying, this rich type information is lost at the call site:
Regardless of the dictionary’s actual KeyType, anything that conforms to NSCopying is accepted without warning by the compiler.

Steps to Reproduce:
Compile the code between the triple backticks using Xcode or command line clang:
```
#import <Foundation/Foundation.h>

@interface YUNOWarn : NSObject <NSCopying> @end

@implementation YUNOWarn

+ (void)pointlessMethod {
    NSMutableDictionary<NSString *, NSNumber *> *canary = [NSMutableDictionary dictionary];
    YUNOWarn *obviouslyInvalidKey = [[YUNOWarn alloc] init];

    canary[obviouslyInvalidKey] = @1234;
    [canary setObject:@1234 forKey:obviouslyInvalidKey];
}

- (id)copyWithZone:(NSZone *)zone {
    return self;
}

@end
```

Expected Results:
There are two warnings because of a type–mismatch for the key in line 11 and 12.

Actual Results:
This code compiles without a warning.

Regression:
??

Notes:

Comments


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!