You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Allow 'public' classes to have 'internal' required initializers (#14775)
They're already not subclassable publicly, so it's okay for the
initializer to not be available to cross-module clients, just like if
it were non-'required'. This allows constructing a class instance
chosen at runtime /within/ the module without having to expose the
existence of the constructor to everybody.
rdar://problem/22845087
Copy file name to clipboardExpand all lines: test/Compatibility/accessibility.swift
+8-4Lines changed: 8 additions & 4 deletions
Original file line number
Diff line number
Diff line change
@@ -165,7 +165,7 @@ internal extension Base {
165
165
}
166
166
167
167
publicclassPublicSub:Base{
168
-
requiredinit(){} // expected-error {{'required' initializer must be as accessible as its enclosing type}} {{12-12=public }}
168
+
privaterequiredinit(){} // expected-error {{'required' initializer must be accessible wherever class 'PublicSub' can be subclassed}} {{3-10=internal}}
169
169
overridefunc foo(){} // expected-error {{overriding instance method must be as accessible as the declaration it overrides}} {{12-12=public }}
170
170
overridevarbar:Int{ // expected-error {{overriding var must be as accessible as the declaration it overrides}} {{12-12=public }}
171
171
get{return0}
@@ -174,8 +174,12 @@ public class PublicSub: Base {
174
174
override subscript ()->(){return()} // expected-error {{overriding subscript must be as accessible as the declaration it overrides}} {{12-12=public }}
175
175
}
176
176
177
+
publicclassPublicSubGood:Base{
178
+
requiredinit(){} // okay
179
+
}
180
+
177
181
internalclassInternalSub:Base{
178
-
requiredprivateinit(){} // expected-error {{'required' initializer must be as accessible as its enclosing type}} {{12-19=internal}}
182
+
requiredprivateinit(){} // expected-error {{'required' initializer must be accessible wherever class 'InternalSub' can be subclassed}} {{12-19=internal}}
179
183
privateoverridefunc foo(){} // expected-error {{overriding instance method must be as accessible as its enclosing type}} {{3-10=internal}}
180
184
privateoverridevarbar:Int{ // expected-error {{overriding var must be as accessible as its enclosing type}} {{3-10=internal}}
181
185
get{return0}
@@ -207,7 +211,7 @@ internal class InternalSubPrivateSet: Base {
207
211
}
208
212
209
213
fileprivateclassFilePrivateSub:Base{
210
-
requiredprivateinit(){} // expected-error {{'required' initializer must be as accessible as its enclosing type}} {{12-19=fileprivate}}
214
+
requiredprivateinit(){} // expected-error {{'required' initializer must be accessible wherever class 'FilePrivateSub' can be subclassed}} {{12-19=fileprivate}}
211
215
privateoverridefunc foo(){} // expected-error {{overriding instance method must be as accessible as its enclosing type}} {{3-10=fileprivate}}
212
216
privateoverridevarbar:Int{ // expected-error {{overriding var must be as accessible as its enclosing type}} {{3-10=fileprivate}}
213
217
get{return0}
@@ -249,7 +253,7 @@ fileprivate class FilePrivateSubPrivateSet: Base {
249
253
}
250
254
251
255
privateclassPrivateSub:Base{
252
-
requiredprivateinit(){} // expected-error {{'required' initializer must be as accessible as its enclosing type}} {{12-19=fileprivate}}
256
+
requiredprivateinit(){} // expected-error {{'required' initializer must be accessible wherever class 'PrivateSub' can be subclassed}} {{12-19=fileprivate}}
253
257
privateoverridefunc foo(){} // expected-error {{overriding instance method must be as accessible as its enclosing type}} {{3-10=fileprivate}}
254
258
privateoverridevarbar:Int{ // expected-error {{overriding var must be as accessible as its enclosing type}} {{3-10=fileprivate}}
Copy file name to clipboardExpand all lines: test/Sema/accessibility.swift
+8-4Lines changed: 8 additions & 4 deletions
Original file line number
Diff line number
Diff line change
@@ -165,7 +165,7 @@ internal extension Base {
165
165
}
166
166
167
167
publicclassPublicSub:Base{
168
-
requiredinit(){} // expected-error {{'required' initializer must be as accessible as its enclosing type}} {{12-12=public }}
168
+
privaterequiredinit(){} // expected-error {{'required' initializer must be accessible wherever class 'PublicSub' can be subclassed}} {{3-10=internal}}
169
169
overridefunc foo(){} // expected-error {{overriding instance method must be as accessible as the declaration it overrides}} {{12-12=public }}
170
170
overridevarbar:Int{ // expected-error {{overriding var must be as accessible as the declaration it overrides}} {{12-12=public }}
171
171
get{return0}
@@ -174,8 +174,12 @@ public class PublicSub: Base {
174
174
override subscript ()->(){return()} // expected-error {{overriding subscript must be as accessible as the declaration it overrides}} {{12-12=public }}
175
175
}
176
176
177
+
publicclassPublicSubGood:Base{
178
+
requiredinit(){} // okay
179
+
}
180
+
177
181
internalclassInternalSub:Base{
178
-
requiredprivateinit(){} // expected-error {{'required' initializer must be as accessible as its enclosing type}} {{12-19=internal}}
182
+
requiredprivateinit(){} // expected-error {{'required' initializer must be accessible wherever class 'InternalSub' can be subclassed}} {{12-19=internal}}
179
183
privateoverridefunc foo(){} // expected-error {{overriding instance method must be as accessible as its enclosing type}} {{3-10=internal}}
180
184
privateoverridevarbar:Int{ // expected-error {{overriding var must be as accessible as its enclosing type}} {{3-10=internal}}
181
185
get{return0}
@@ -207,7 +211,7 @@ internal class InternalSubPrivateSet: Base {
207
211
}
208
212
209
213
fileprivateclassFilePrivateSub:Base{
210
-
requiredprivateinit(){} // expected-error {{'required' initializer must be as accessible as its enclosing type}} {{12-19=fileprivate}}
214
+
requiredprivateinit(){} // expected-error {{'required' initializer must be accessible wherever class 'FilePrivateSub' can be subclassed}} {{12-19=fileprivate}}
211
215
privateoverridefunc foo(){} // expected-error {{overriding instance method must be as accessible as its enclosing type}} {{3-10=fileprivate}}
212
216
privateoverridevarbar:Int{ // expected-error {{overriding var must be as accessible as its enclosing type}} {{3-10=fileprivate}}
213
217
get{return0}
@@ -249,7 +253,7 @@ fileprivate class FilePrivateSubPrivateSet: Base {
249
253
}
250
254
251
255
privateclassPrivateSub:Base{
252
-
requiredprivateinit(){} // expected-error {{'required' initializer must be as accessible as its enclosing type}} {{12-19=fileprivate}}
256
+
requiredprivateinit(){} // expected-error {{'required' initializer must be accessible wherever class 'PrivateSub' can be subclassed}} {{12-19=fileprivate}}
253
257
privateoverridefunc foo(){} // expected-error {{overriding instance method must be as accessible as its enclosing type}} {{3-10=fileprivate}}
254
258
privateoverridevarbar:Int{ // expected-error {{overriding var must be as accessible as its enclosing type}} {{3-10=fileprivate}}
0 commit comments