Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SR-104] Improve Crash-Safety when Importing Objective-C Code Without Nullability Attributes #42726

Open
swift-ci opened this issue Dec 7, 2015 · 3 comments
Labels
compiler The Swift compiler in itself improvement

Comments

@swift-ci
Copy link
Collaborator

swift-ci commented Dec 7, 2015

Previous ID SR-104
Radar None
Original Reporter fabb (JIRA User)
Type Improvement
Additional Detail from JIRA
Votes 2
Component/s Compiler
Labels Improvement
Assignee None
Priority Medium

md5: effd63d4f04c51c96d0bf6c8684f30fa

relates to:

  • SR-223 Option to assume ObjC return values are always nullable

Issue Description:

Introduction

Objective-C code that does not yet include nullability annotations should generate warnings for unsafe accesses in order to prevent runtime crashes.

Motivation

Swift code accessing Objective-C methods can easily crash. If the Objective-C code does not include nullability attributes, it is brought as Implicitly Unwrapped Optional (IUO) into Swift. If the value of the IUO is accessed, and it is nil at that time, a crash occurrs. These unsafe accesses do not produce compiler warnings and can not be identified by only looking at the according Swift code. So these problematic places are not easy to track down in a code base.

Short example:

// Objective-C  
- (NSString *)giveMeAString { return nil; }  

// Swift  
func thisWillCrash() {  
   let string = someObjectiveCObject.giveMeAString()  
   let length = string.length // crash  
}

Use cases

This affects all uses of Objective-C code from Swift, may it be the System SDK, an integrated Framework, or the same application's code.

Very important would be the use case of an application that is written in Objective-C where Swift code is added. The new Swift code (e.g. in an extension to an Objective-C class) is bound to call a lot of the application's old Objective-C code.

Example:

func someSwiftFuncInAnExtension() {
    myDataModel.someProperty.someMethod()
}

In a call like that it is not immediately obvious if myDataModel , someProperty or someMethod is written in Objective-C and missing nullability annotations. Swift should help with writing safe code and be supportive at gradually migrating an application to Swift.

Proposed solution

Additional warnings shall be added when implicitly unwrapping an IUO that was inferred from a lack of nullability information in the Objective-C API.

Example:

// Objective-C  
- (NSString *)giveMeAString { return nil; }  

// Swift  
func someFunction() {  
   let string = someObjectiveCObject.giveMeAString()  
   let length = string.length // warning  
   let length2 = string!.length // no warning  
   if let unwrappedString = string { // no warning  
       let length3 = unwrappedString.length
   }
}

Detailed design

  • Create 2 separate warnings when implicitly unwrapping an IUO that was inferred the from a lack of nullability information in the Objective-C API - one for "system" and the other for "user" frameworks

  • The warning levels shall be NO , YES and YES_ERROR

  • The warning for "system" frameworks shall default to NO , the one for "user" frameworks to YES

  • Xcode Fix-It in case of a compile error

Impact on existing code

The new warnings will not harm productivity. If a project has "Treat warnings as errors" activated, and there are issues with "user" frameworks, that warning could be selectively deactivated if fixing the errors would take too much time.

@belkadan
Copy link
Contributor

belkadan commented Dec 8, 2015

This should be discussed on swift-evolution.

@swift-ci
Copy link
Collaborator Author

Comment by Fabian Ehrentraud (JIRA)

The issue now has been discussed on swift-evolution.
https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20151207/thread.html#1054

The proposal was rejected by the core team though.
apple/swift-evolution#61

But an alternate solution was created (solution #2 in the proposal) which does not incorporate a language change, but the addition of warnings. The response of the community was very positive.
https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20151207/002029.html

So I'll update this issue with the new solution idea.

@belkadan is there any chance this will make it into Swift 3?

@swift-ci
Copy link
Collaborator Author

swift-ci commented Apr 1, 2016

Comment by Fabian Ehrentraud (JIRA)

implementation might be influenced by SE-0054
https://github.com/apple/swift-evolution/blob/master/proposals/0054-abolish-iuo.md

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler The Swift compiler in itself improvement
Projects
None yet
Development

No branches or pull requests

2 participants