swiftlang / swift

The Swift Programming Language
https://swift.org
Apache License 2.0
67.6k stars 10.37k forks source link

[SR-2601] Nullability is ignored on objc parameters of type `id` #45206

Open swift-ci opened 8 years ago

swift-ci commented 8 years ago
Previous ID SR-2601
Radar None
Original Reporter fabb (JIRA User)
Type Bug
Environment Xcode 8 GM
Additional Detail from JIRA | | | |------------------|-----------------| |Votes | 0 | |Component/s | Compiler | |Labels | Bug | |Assignee | None | |Priority | Medium | md5: 47291f02d7f3cfebfb7e90591c41d00c

Issue Description:

Repro:

// Swift

// should not compile due to nullability in Problematic.h
// crashes at runtime, as _SwiftValue type is passed to objc
Problematic.problematic(Optional("test"));

// Problematic.h

@interface Problematic : NSObject
+ (void)problematic:(id _Nonnull)param;
@end

// Problematic.m

@implementation Problematic
+ (void)problematic:(id)param {
    NSParameterAssert([param isKindOfClass:[NSString class]]);  
}
@end
swift-ci commented 8 years ago

Comment by Fabian Ehrentraud (JIRA)

example project: https://github.com/fabb/OptionalRuntimeCrash.git

belkadan commented 8 years ago

It's not exactly wrong, because id is imported as Any and Any can hold any value, including optionals. @jckarter is considering some ways to improve the experience here, though, such as a warning when implicitly converting from Optional to Any or AnyObject.

swift-ci commented 8 years ago

Comment by Fabian Ehrentraud (JIRA)

In the mean time I found SE-0140 which would also be a good idea to mitigate the issue.
It's just strange that `nonnull` can be annotated but will be ignored, but I understand the tradeoffs made in SE-0116.
Warnings (treated as errors) would be great.