Swift 4.1 incompatible issue

Recently I have updated my project to support Swift 4.1, and I found an incompatible issue, caused a crash.
The previous version code looks like this:

public enum XMLSubscriptResult {
    ///  some other implementation ....

    public var xmlList:[XML]? {
        do {
            return try getXMLList()
        } catch {
            return nil
        }
    }
    
    public func getXMLList() throws -> [XML] {
        switch self {
        case .null(let error):
            throw XMLError.subscriptFailue(error)
        case .string(_, let path):
            throw XMLError.subscriptFailue("can not get list from attribute, from keyChain: \(path)")
        case .xml(let xml, _): return [xml]
        case .array(let xmls, _): return xmls
        }
    }
}

the reason caused the crash is because in attribute getter called a method that throws an error. After I change the attribute getter as below:

    public var xmlList:[XML]? {
        switch self {
        case .null(_):
            return nil
        case .string(_, _):
            return nil
        case .xml(let xml, _): return [xml]
        case .array(let xmls, _): return xmls
        }
    }

the error is gone ~

How did this ever work before? :flushed:

I would think having a do/try inside a computed property would be fine.

I see nothing wrong with your original code. Please file a bug report at https://bugs.swift.org with a self-contained project.

Hilariously my eyes saw a get {…} set {…} on the property. Wow, I need some sleep :flushed:

BTW return try? getXMLList() would be a nice obfuscating one liner :smiley:

I have reported to [SR-7347] Swift 4.1 Code runtime error with 'try' (it works for Swift 4.0) · Issue #49895 · apple/swift · GitHub
I have attached a playground file too.

1 Like

Does this still cause a crash for anyone? The code doesn't compile anymore, but Xcode doesn't crash for me in current versions.