Skip to content

Confusing designated initializer rules #80311

Open
@AdamCmiel

Description

@AdamCmiel

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugA deviation from expected or documented behavior. Also: expected but undesirable behavior.triage neededThis issue needs more specific labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions