Skip to content

Confusing designated initializer rules #80311

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
AdamCmiel opened this issue Mar 26, 2025 · 1 comment · May be fixed by #80334
Open

Confusing designated initializer rules #80311

AdamCmiel opened this issue Mar 26, 2025 · 1 comment · May be fixed by #80334
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. triage needed This issue needs more specific labels

Comments

@AdamCmiel
Copy link

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 AdamCmiel added bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. triage needed This issue needs more specific labels labels Mar 26, 2025
@AdamCmiel
Copy link
Author

Possibly a dupe of #65316

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. triage needed This issue needs more specific labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant