Skip to content

Sema: Ban unsound convenience initializer inheritance inside a module #73988

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
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

slavapestov
Copy link
Contributor

@slavapestov slavapestov commented May 29, 2024

We already prevent the user from subclassing a class with an inaccessible
designated initializer from another module,

open class C {
  private init() {}
  public convenience init(x: Int) { self.init() }
}

However, we should ban this within a module too:

class C {
  private init() {}
  convenience init(x: Int) { self.init() }
}

class D: C {}

Today, we don't diagnose anything, and we also don't synthesize the
override, so we just miscompile instead.

We can't synthesize the override though, because this would then
allow calling the superclass's private init from the subclass, which
violates access control.

So the only option is to ban this.

Fixes rdar://68220237.
Fixes #61508.

@slavapestov slavapestov changed the title Sema: Fix convenience initializer inheritance inside a module Sema: Ban unsound convenience initializer inheritance inside a module May 30, 2024
@slavapestov slavapestov marked this pull request as ready for review May 30, 2024 02:31
We already prevent the user from subclassing a class like this from
another module,

    open class C {
      private init() {}
      public convenience init(x: Int) { self.init() }
    }

However, we cannot allow this within a module also:

    class C {
      private init() {}
      convenience init(x: Int) { self.init() }
    }

    class D: C {}

Today, we don't diagnose anything, and we also don't synthesize the
override, so we just miscompile instead.

We can't synthesize the override though, because this would then
allow calling the superclass's private init from the subclass, which
violates access control.

So the only option is to ban this.

Fixes rdar://68220237.
Fixes swiftlang#61508.
@slavapestov
Copy link
Contributor Author

@swift-ci Please smoke test

@slavapestov
Copy link
Contributor Author

@swift-ci Please test source compatibility

@allevato
Copy link
Member

Out of curiosity, is there anything still blocking this fix?

I happened to find my way here after one of our teams put themselves into a similar situation—they refactored a class so that it only contained a private designated initializer, so I suggested making the class final, and then went to Godbolt because I was wondering "what if..."

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.

Automatic initializer inheritance broken for open classes outside of defining module
2 participants