Passing NULL to NSError** parameter of a Swift method crashes, with no compiler warning

Originator:clindsay
Number:rdar://24498034 Date Originated:02/04/2016
Status:Open Resolved:
Product:iOS SDK Product Version:9.2
Classification:Crash Reproducible:Always
 
Summary:
In Objective-C, it's expected that you can pass in NULL to any NSError** parameter to ignore the error. However, when I try to pass NULL to a Swift method that throws an error, it generates a EXC_BAD_INSTRUCTION  runtime error.

I'm surprised at this behavior. I would expect this to either work, or the compiler to generate a warning when you pass NULL to one of these methods.

Here's what the generated header of the Swift method looks like:

+ (BOOL)throwErrorAndReturnError:(NSError * __nullable * __null_unspecified)error;

If this is not supposed to work, it seems like this should generate NSError * __nullable * __nonnull, so that a compiler warning is generated when you try to pass in a nullable NSError*.

Steps to Reproduce:
Create an empty project with this code (example project attached):

// Thrower.swift
class Thrower: NSObject {
    static func throwError() throws {
        throw NSError(domain: "bla", code: 0, userInfo: nil)
    }
}

...

// AppDelegate.m
BOOL success = [Thrower throwErrorAndReturnError:NULL];


Expected Results:
Either
1. [Thrower throwErrorAndReturnError:] returns NO, and continues running.
or
2. The compiler generates a warning telling you you cannot pass NULL into this method.

Actual Results:
The app crashes with a EXC_BAD_INSTRUCTION error.

Version:
iOS 9.2

Configuration:
Simulator & iPhone 6

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!