swiftlang / swift

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

[SR-12270] SIL memory lifetime failure with throwing initializer #54698

Open ChristopherRogers opened 4 years ago

ChristopherRogers commented 4 years ago
Previous ID SR-12270
Radar rdar://problem/59830255
Original Reporter @ChristopherRogers
Type Bug

Attachment: Download

Environment 2020-03-21 Swift 5.2 development toolchain
Additional Detail from JIRA | | | |------------------|-----------------| |Votes | 0 | |Component/s | Compiler | |Labels | Bug | |Assignee | None | |Priority | Medium | md5: d9dbe5f63cbfb51b53aea9f00a9a7801

Issue Description:

Building the code below using the Swift 5.2 development toolchain, I get the following error.

SIL memory lifetime failure in @$s8SR_122706ClassAC7bStringACSgSS_tcfc: memory is not initialized, but should
memory location:   %2 = alloc_stack $ClassA, let, name "self"      // users: %17, %16, %5, %4, %20, %36, %21, %39
at instruction:   %36 = load [take] %2 : $*ClassA                 // users: %38, %37
import Foundation

class ClassA: NSObject {
    private let b: ClassB

    @objc // Uncomment either one of these for workaround
    init(_ b: ClassB) {
        self.b = b
    }

    @objc // Uncomment either one of these for workaround
    convenience init?(bString: String) {
        do {
            // ... or use the following two lines instead.
//            let b = try ClassB(bString)
//            self.init(b)
            try self.init(ClassB(bString))
        }
        catch {
            return nil
        }
    }
}

class ClassB: NSObject {
    init(_ string: String) throws {}
}
hborla commented 4 years ago

@swift-ci create

eeckstein commented 4 years ago

@ChristopherRogers is it possible that you attach a whole project with which we can reproduce the problem?

ChristopherRogers commented 4 years ago

I was able to reduce a test case. I've updated the description above to include it.

eeckstein commented 4 years ago

Perfect, thanks!
I could reproduce the issue.