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-11043] Crash on swift_getObjectType on usage of [weak self] or [unowned self] in closures after required initialziation in @objc convenience init #53432

Open
swift-ci opened this issue Jun 28, 2019 · 2 comments

Comments

@swift-ci
Copy link
Collaborator

@swift-ci swift-ci commented Jun 28, 2019

Previous ID SR-11043
Radar None
Original Reporter grigorye (JIRA User)
Type Bug

Attachment: Download

Environment

$ swift --version
Apple Swift version 5.0.1 (swiftlang-1001.0.82.4 clang-1001.0.46.5)
Target: x86_64-apple-darwin18.6.0

Reproducible with Xcode 10.2.1. Not reproducible with Swift 4.2.

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

md5: a5490d8dbd341cfc4cbf7da1454a5fc3

Issue Description:

After switching to Swift 5, we faced a crash in our code base that could be minimized down to the sample below.

The sample is tailored for execution in playground or for invocation with "swift xxx.swift". There's also an macOS project demonstrating the same crash (attached) (also available on GitHub). You can make iOS project and crash it with similar code as well.

This does crash:

import Foundation

class Foo {
    
    let foo: Int? = nil
    
    required init() {
    }
    
    @objc convenience init(x: Int) {
        self.init()
        _ = { [weak self] in
            _ = self
        }
    }
}

_ = Foo(x: 0)

Removal of @objc from the convenience initializer makes it not crash/behave as expected.
Removal of [weak self] from the closure makes it not crash/behave as expected.
Removal of property .foo seems to solve the problem as well.
It doesn't crash if you replace weak with unowned in the above example, but I believe that in our real case, it still crashes even with unowned.

Switching to Swift 4.2 or Swift 5.1 (swiftlang-1100.0.43.3 clang-1100.0.26.3) resolves the problem.

Moving the closure creation to a separate method like below makes it behave as expected:

class Foo {
    
    let foo: Int? = nil
    
    required init() {
    }
    
    @objc convenience init(x: Int) {
        self.init()
        self.bar()
    }
    
    private func bar() {
        _ = { [weak self] in
            _ = self
        }
    }
}
@belkadan
Copy link
Contributor

@belkadan belkadan commented Jun 28, 2019

Looks like this is fixed in Swift 5.1 already. @mikeash, does it ring a bell?

@mikeash
Copy link
Contributor

@mikeash mikeash commented Jun 28, 2019

It seems familiar but my search-fu is not strong enough to dig up a radar for it.

@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
Projects
None yet
Development

No branches or pull requests

3 participants