Clang should error when passing a non-__autoreleasing pointer to a parameter expecting an __autoreleasing pointer

Originator:heath.borders
Number:rdar://25489844 Date Originated:01-Apr-2016 09:19 AM
Status:Open Resolved:
Product:Developer Tools Product Version:Version 7.3 (7D175)
Classification:Serious Bug Reproducible:Always
 
Summary:
When a __block variable is passed to a parameter expecting an __autoreleasing pointer, ARC doesn’t retain that variable after assignment. This leads to the variable unexpectedly being released at the end of the autorelease pool. Instead, there should be an error emitted saying that the variable isn’t __autoreleasing as expected.

Steps to Reproduce:
Compile the code below:

void (^blockWithOutPointerThatDispatchesLater)(NSObject * __autoreleasing *,
                                               dispatch_block_t) = ^(NSObject * __autoreleasing * outPointer,
                                                                     dispatch_block_t block) {
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW,
                                 (int64_t)(1 * NSEC_PER_SEC)),
                   dispatch_get_main_queue(),
                   block);
    *outPointer = [NSObject new];
};

{
    NSObject * __block blockVar2;
    blockWithOutPointerThatDispatchesLater(&blockVar2,
                                           ^{
                                               NSLog(@"blockVar2: %@",
                                                     blockVar2);
                                           });
    // prints nil
}

Expected Results:
Clang should emit an error when we pass &blockVar2 to blockWithOutPointerThatDispatchesLater because the developer expects that blockVar2 would be retained.

Actual Results:
No error is emitted. blockVar2 is not retained, and the NSLog prints nil.

Regression:
Describe circumstances where the problem occurs or does not occur, such as software versions and/or hardware configurations.

Notes:
Provide additional information, such as references to related problems, workarounds and relevant attachments.

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!