swift icon indicating copy to clipboard operation
swift copied to clipboard

Confusing designated initializer rules

Open AdamCmiel opened this issue 9 months ago • 1 comments

Description

Pretty sure we're breaking rule #1 for initializer delegation here: https://docs.swift.org/swift-book/documentation/the-swift-programming-language/initialization/#Initializer-Delegation-for-Class-Types

If a derived class has a let property that's initialized before calling it's super's convenience initializer, that call is allowed. If the let property is rewritten as a var or removed, the appropriate super init delegation to a designated initializer diagnostic is emitted.

Reproduction

class Base {
    public init(name: String) {
        print(name)
    }

    convenience init(number: Int) {
        self.init(name: "\(number)")
    }
}

class DerivedLetPasses: Base {
    let string: String
    init() {
        self.string = "foo"
        super.init(number: 3) // compilation succeeds
    }
}

class DerivedVarFails: Base {
    var string: String
    init() {
        self.string = "foo"
        super.init(number: 3) // error: Must call a designated initializer of the superclass 'Base'
    }
}

class DerivedNoExtraProperties: Base {
    init() {
        super.init(number: 3) // error: Must call a designated initializer of the superclass 'Base'
    }
}

Expected behavior

I believe that diagnostic should be emitted for all three derived classes, this would be consistent with the docs.

Environment

Xcode version 16.2 (16C5032a)

Additional information

No response

AdamCmiel avatar Mar 26 '25 17:03 AdamCmiel

Possibly a dupe of #65316

AdamCmiel avatar Mar 26 '25 17:03 AdamCmiel