Skip to content

Conversation

@vliegeois
Copy link

Null init(from decoder: Decoder): explicitly call self.init() if container.decodeNil() is true

Otherwise, the code fails to compile using swift 6.2.1 compiler from swift.org in release mode.
Strangely, the code compiles fine on debug mode or using swift compiler provided by apple within Xcode.

To demonstrate the issue, here is a demo code that fails to compile for all compiler:

public struct MyStruct {
    public init() {}
    
    public init (_ val: Bool) {
        if val {
            self.init()
        } else {
        }
    }
}

'self.init' isn't called on all paths before returning from initializer

But, if self.init() is replaced by self = .init(), which is similar to the code in init(from decoder: Decoder) for Null structure, the code compiles fine on apple swift version. However, this trigger an issue (see attached file) with swift compiler from swift.org, at least in release mode.

I think, it would make more sense that the code fails so that both self.init() and self = .init() codes produce the same error.

So the demo code should be written:

public struct MyStruct {
    public init() {}
    
    public init (_ val: Bool) {
        if val {
            self.init()
        } else {
            self.init()
        }
    }
}

I propose to do the same in Null init(from decoder: Decoder) function.

Even if the code seems valid without that for some compilers, it make sense to init self on both path, so if let container = container as? AnySingleValueBSONDecodingContainer succeed or and the else clause.

Regards

Vincent

…tainer.decodeNil() is true

For some reasons it fails with swift 6.2.1 compiler from swift.org in release mode
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant