Skip to content

Allow 'public' classes to have 'internal' required initializers #14775

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

Merged
merged 1 commit into from
Feb 26, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ CHANGELOG
Swift 5.0
---------

* Public classes may now have internal `required` initializers. The rule for
`required` initializers is that they must be available everywhere the class
can be subclassed, but previously we said that `required` initializers on
public classes needed to be public themselves. (This limitation is a holdover
from before the introduction of the open/public distinction in Swift 3.)

* C macros containing casts are no longer imported to Swift if the type in the
cast is unavailable or deprecated, or produces some other diagnostic when
referenced. (These macros were already only imported under very limited
Expand Down
4 changes: 3 additions & 1 deletion include/swift/AST/DiagnosticsSema.def
Original file line number Diff line number Diff line change
Expand Up @@ -3176,7 +3176,9 @@ NOTE(required_initializer_here,none,
"'required' initializer is declared in superclass here", ())

ERROR(required_initializer_not_accessible,none,
"'required' initializer must be as accessible as its enclosing type", ())
"'required' initializer must be accessible wherever class %0 can be "
"subclassed",
(DeclName))
ERROR(required_initializer_missing_keyword,none,
"'required' modifier must be present on all overrides of a required "
"initializer", ())
Expand Down
20 changes: 15 additions & 5 deletions lib/Sema/TypeCheckDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7290,13 +7290,23 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
if (CD->isRequired()) {
if (auto nominal = CD->getDeclContext()
->getAsNominalTypeOrNominalTypeExtensionContext()) {
auto requiredAccess = std::min(nominal->getFormalAccess(),
AccessLevel::Public);
if (requiredAccess == AccessLevel::Private)
AccessLevel requiredAccess;
switch (nominal->getFormalAccess()) {
case AccessLevel::Open:
requiredAccess = AccessLevel::Public;
break;
case AccessLevel::Public:
case AccessLevel::Internal:
requiredAccess = AccessLevel::Internal;
break;
case AccessLevel::FilePrivate:
case AccessLevel::Private:
requiredAccess = AccessLevel::FilePrivate;
break;
}
if (CD->getFormalAccess() < requiredAccess) {
auto diag = TC.diagnose(CD,
diag::required_initializer_not_accessible);
auto diag = TC.diagnose(CD, diag::required_initializer_not_accessible,
nominal->getFullName());
fixItAccess(diag, CD, requiredAccess);
}
}
Expand Down
12 changes: 8 additions & 4 deletions test/Compatibility/accessibility.swift
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ internal extension Base {
}

public class PublicSub: Base {
required init() {} // expected-error {{'required' initializer must be as accessible as its enclosing type}} {{12-12=public }}
private required init() {} // expected-error {{'required' initializer must be accessible wherever class 'PublicSub' can be subclassed}} {{3-10=internal}}
override func foo() {} // expected-error {{overriding instance method must be as accessible as the declaration it overrides}} {{12-12=public }}
override var bar: Int { // expected-error {{overriding var must be as accessible as the declaration it overrides}} {{12-12=public }}
get { return 0 }
Expand All @@ -174,8 +174,12 @@ public class PublicSub: Base {
override subscript () -> () { return () } // expected-error {{overriding subscript must be as accessible as the declaration it overrides}} {{12-12=public }}
}

public class PublicSubGood: Base {
required init() {} // okay
}

internal class InternalSub: Base {
required private init() {} // expected-error {{'required' initializer must be as accessible as its enclosing type}} {{12-19=internal}}
required private init() {} // expected-error {{'required' initializer must be accessible wherever class 'InternalSub' can be subclassed}} {{12-19=internal}}
private override func foo() {} // expected-error {{overriding instance method must be as accessible as its enclosing type}} {{3-10=internal}}
private override var bar: Int { // expected-error {{overriding var must be as accessible as its enclosing type}} {{3-10=internal}}
get { return 0 }
Expand Down Expand Up @@ -207,7 +211,7 @@ internal class InternalSubPrivateSet: Base {
}

fileprivate class FilePrivateSub: Base {
required private init() {} // expected-error {{'required' initializer must be as accessible as its enclosing type}} {{12-19=fileprivate}}
required private init() {} // expected-error {{'required' initializer must be accessible wherever class 'FilePrivateSub' can be subclassed}} {{12-19=fileprivate}}
private override func foo() {} // expected-error {{overriding instance method must be as accessible as its enclosing type}} {{3-10=fileprivate}}
private override var bar: Int { // expected-error {{overriding var must be as accessible as its enclosing type}} {{3-10=fileprivate}}
get { return 0 }
Expand Down Expand Up @@ -249,7 +253,7 @@ fileprivate class FilePrivateSubPrivateSet: Base {
}

private class PrivateSub: Base {
required private init() {} // expected-error {{'required' initializer must be as accessible as its enclosing type}} {{12-19=fileprivate}}
required private init() {} // expected-error {{'required' initializer must be accessible wherever class 'PrivateSub' can be subclassed}} {{12-19=fileprivate}}
private override func foo() {} // expected-error {{overriding instance method must be as accessible as its enclosing type}} {{3-10=fileprivate}}
private override var bar: Int { // expected-error {{overriding var must be as accessible as its enclosing type}} {{3-10=fileprivate}}
get { return 0 }
Expand Down
12 changes: 8 additions & 4 deletions test/Sema/accessibility.swift
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ internal extension Base {
}

public class PublicSub: Base {
required init() {} // expected-error {{'required' initializer must be as accessible as its enclosing type}} {{12-12=public }}
private required init() {} // expected-error {{'required' initializer must be accessible wherever class 'PublicSub' can be subclassed}} {{3-10=internal}}
override func foo() {} // expected-error {{overriding instance method must be as accessible as the declaration it overrides}} {{12-12=public }}
override var bar: Int { // expected-error {{overriding var must be as accessible as the declaration it overrides}} {{12-12=public }}
get { return 0 }
Expand All @@ -174,8 +174,12 @@ public class PublicSub: Base {
override subscript () -> () { return () } // expected-error {{overriding subscript must be as accessible as the declaration it overrides}} {{12-12=public }}
}

public class PublicSubGood: Base {
required init() {} // okay
}

internal class InternalSub: Base {
required private init() {} // expected-error {{'required' initializer must be as accessible as its enclosing type}} {{12-19=internal}}
required private init() {} // expected-error {{'required' initializer must be accessible wherever class 'InternalSub' can be subclassed}} {{12-19=internal}}
private override func foo() {} // expected-error {{overriding instance method must be as accessible as its enclosing type}} {{3-10=internal}}
private override var bar: Int { // expected-error {{overriding var must be as accessible as its enclosing type}} {{3-10=internal}}
get { return 0 }
Expand Down Expand Up @@ -207,7 +211,7 @@ internal class InternalSubPrivateSet: Base {
}

fileprivate class FilePrivateSub: Base {
required private init() {} // expected-error {{'required' initializer must be as accessible as its enclosing type}} {{12-19=fileprivate}}
required private init() {} // expected-error {{'required' initializer must be accessible wherever class 'FilePrivateSub' can be subclassed}} {{12-19=fileprivate}}
private override func foo() {} // expected-error {{overriding instance method must be as accessible as its enclosing type}} {{3-10=fileprivate}}
private override var bar: Int { // expected-error {{overriding var must be as accessible as its enclosing type}} {{3-10=fileprivate}}
get { return 0 }
Expand Down Expand Up @@ -249,7 +253,7 @@ fileprivate class FilePrivateSubPrivateSet: Base {
}

private class PrivateSub: Base {
required private init() {} // expected-error {{'required' initializer must be as accessible as its enclosing type}} {{12-19=fileprivate}}
required private init() {} // expected-error {{'required' initializer must be accessible wherever class 'PrivateSub' can be subclassed}} {{12-19=fileprivate}}
private override func foo() {} // expected-error {{overriding instance method must be as accessible as its enclosing type}} {{3-10=fileprivate}}
private override var bar: Int { // expected-error {{overriding var must be as accessible as its enclosing type}} {{3-10=fileprivate}}
get { return 0 }
Expand Down