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-14621] Compiler doesn't diagnose capture-before-initialization #56973

Open
swift-ci opened this issue May 11, 2021 · 2 comments
Open

[SR-14621] Compiler doesn't diagnose capture-before-initialization #56973

swift-ci opened this issue May 11, 2021 · 2 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself

Comments

@swift-ci
Copy link
Collaborator

Previous ID SR-14621
Radar rdar://problem/77851872
Original Reporter jasonbobier (JIRA User)
Type Bug
Status Reopened
Resolution
Environment

Xcode 12.5

Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug
Assignee None
Priority Medium

md5: 42011c7b79c4ca46cd8ab5322d9e3add

Issue Description:

This code used to cause an error and no longer does.

var someId = UIApplication.shared.beginBackgroundTask(withName: "Finish Network Tasks") {
    UIApplication.shared.endBackgroundTask(someId)
    someId = .invalid
}

If I mimic the UIKit code and write my own version, it properly causes an error.

open class Test : UIResponder {
    var n: String?
    var h: (() -> Void)?
    
    open func beginBackgroundTask(withName taskName: String?, expirationHandler handler: (() -> Void)? = nil) -> UIBackgroundTaskIdentifier {
        n = taskName
        h = handler
        return UIBackgroundTaskIdentifier(rawValue: 5)
    }
}

let t = Test()
var anotherId = t.beginBackgroundTask(withName: "Finish Network Tasks") {
    print(anotherId)
}

If the beginBackgrountTask happens on a secondary thread this is a potential race condition because the handler is always called on the main thread. This used to produce a capture before initialization error in previous versions of swift.

@typesanitizer
Copy link

Minimal example: (compiles fine on main)

func f(_ k:() -> Int) -> Int { k() }

var x: Int = f {
  x
} 

@typesanitizer
Copy link

@swift-ci create

@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
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself
Projects
None yet
Development

No branches or pull requests

2 participants