Skip to content
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

Subclass compiles without initializing properties #69965

Open
PadraigK opened this issue Nov 17, 2023 · 0 comments · May be fixed by #70219
Open

Subclass compiles without initializing properties #69965

PadraigK opened this issue Nov 17, 2023 · 0 comments · May be fixed by #70219
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. triage needed This issue needs more specific labels

Comments

@PadraigK
Copy link

PadraigK commented Nov 17, 2023

Description

When subclassing an open class from another module, Swift is not warning me if I have neglect to initialize all properties in my subclass. It unexpectedly builds fine but crashes at runtime.

Steps to reproduce

ModuleA:

open class MySuperclass {}

ModuleB:

import ModuleA
class MySubclass: MySuperclass {
    var hi: String
}

hi is never initialized, but this builds.

Expected behavior
I expect an error like: Class 'MySubclass' has no initializers: Stored property 'hi' without initial value prevents synthesized initializers. If I remove the inheritance clause from MySubclass I do get this error.

Environment

  • swift-driver version: 1.87.1 Apple Swift version 5.9 (swiftlang-5.9.0.128.108 clang-1500.0.40.1)
    Target: arm64-apple-macosx14.0
  • Xcode 15.0.1 / 15A507
  • Deployment target: macOS 13
@PadraigK PadraigK added bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. triage needed This issue needs more specific labels labels Nov 17, 2023
DougGregor added a commit to DougGregor/swift that referenced this issue Dec 5, 2023
In cases where a subclass was unable to synthesize any initializers
(for example, because there were missing designated initializers in the
superclass), we were skipping checking of superclass required
initializers. This meant that we would silently accept subclasses that
cannot be initialized directly (that would produce an error), but
could at runtime be initialized via a required initializer... that
didn't account for the subclass.

Fixes the original problem from swiftlang#69965,
but not the most minimal one.
DougGregor added a commit to DougGregor/swift that referenced this issue Dec 5, 2023
…ssing ones

The presence of a superclass from another module that has missing
designated initializers was suppressing the diagnostic complaining
about a class having no initializers. Start complaining about such
classes when the superclass comes from Swift, but downgrade the error
to a warning prior to Swift 6 so we don't break existing code.

Fixes swiftlang#69965.
Catfish-Man pushed a commit to Catfish-Man/swift that referenced this issue Jan 19, 2024
In cases where a subclass was unable to synthesize any initializers
(for example, because there were missing designated initializers in the
superclass), we were skipping checking of superclass required
initializers. This meant that we would silently accept subclasses that
cannot be initialized directly (that would produce an error), but
could at runtime be initialized via a required initializer... that
didn't account for the subclass.

Fixes the original problem from swiftlang#69965,
but not the most minimal one.
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
1 participant