Skip to content

Commit 84446c8

Browse files
committed
Promote a bunch of Swift 3 compatibility access warnings to errors.
This was done back in Swift 3.0.1 or 3.0.2. Let's finally make them errors in Swift 4! I also fixed up a number of '%error' entries in diagnostics that can actually happen in invalid code. These are the ones that were already present in the warnings.
1 parent 904ef73 commit 84446c8

File tree

3 files changed

+52
-40
lines changed

3 files changed

+52
-40
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1196,7 +1196,8 @@ ERROR(pattern_type_access,none,
11961196
"%select{%select{variable|constant}0|property}1 "
11971197
"%select{must be declared %select{"
11981198
"%select{private|fileprivate|internal|%error|%error}3|private or fileprivate}4"
1199-
"|cannot be declared %select{%error|fileprivate|internal|public|open}3}2 "
1199+
"|cannot be declared "
1200+
"%select{in this context|fileprivate|internal|public|open}3}2 "
12001201
"because its type uses "
12011202
"%select{a private|a fileprivate|an internal|%error|%error}5 type",
12021203
(bool, bool, bool, Accessibility, bool, Accessibility))
@@ -1211,7 +1212,8 @@ ERROR(pattern_type_access_inferred,none,
12111212
"%select{%select{variable|constant}0|property}1 "
12121213
"%select{must be declared %select{"
12131214
"%select{private|fileprivate|internal|%error|%error}3|private or fileprivate}4"
1214-
"|cannot be declared %select{%error|fileprivate|internal|public|open}3}2 "
1215+
"|cannot be declared "
1216+
"%select{in this context|fileprivate|internal|public|open}3}2 "
12151217
"because its type %6 uses "
12161218
"%select{a private|a fileprivate|an internal|%error|%error}5 type",
12171219
(bool, bool, bool, Accessibility, bool, Accessibility, Type))
@@ -1271,14 +1273,16 @@ ERROR(unsupported_nested_protocol,none,
12711273
ERROR(type_alias_underlying_type_access,none,
12721274
"type alias %select{must be declared %select{"
12731275
"%select{private|fileprivate|internal|%error|%error}2|private or fileprivate}3"
1274-
"|cannot be declared %select{%error|fileprivate|internal|public|open}1}0 "
1276+
"|cannot be declared "
1277+
"%select{in this context|fileprivate|internal|public|open}1}0 "
12751278
"because its underlying type uses "
12761279
"%select{a private|a fileprivate|an internal|%error|%error}2 type",
12771280
(bool, Accessibility, Accessibility, bool))
12781281
WARNING(type_alias_underlying_type_access_warn,none,
12791282
"type alias %select{should be declared "
12801283
"%select{private|fileprivate|internal|%error|%error}2"
1281-
"|should not be declared %select{%error|fileprivate|internal|public|open}1}0 "
1284+
"|should not be declared "
1285+
"%select{in this context|fileprivate|internal|public|open}1}0 "
12821286
"because its underlying type uses "
12831287
"%select{a private|a fileprivate|an internal|%error|%error}2 type",
12841288
(bool, Accessibility, Accessibility, bool))
@@ -1287,7 +1291,8 @@ WARNING(type_alias_underlying_type_access_warn,none,
12871291
ERROR(subscript_type_access,none,
12881292
"subscript %select{must be declared "
12891293
"%select{private|fileprivate|internal|%error|%error}1"
1290-
"|cannot be declared %select{%error|fileprivate|internal|public|open}1}0 "
1294+
"|cannot be declared "
1295+
"%select{in this context|fileprivate|internal|public|open}1}0 "
12911296
"because its %select{index|element type}3 uses "
12921297
"%select{a private|a fileprivate|an internal|%error|%error}2 type",
12931298
(bool, Accessibility, Accessibility, bool))
@@ -1304,7 +1309,8 @@ ERROR(function_type_access,none,
13041309
"%select{function|method|initializer}4 "
13051310
"%select{must be declared %select{"
13061311
"%select{private|fileprivate|internal|%error|%error}1|private or fileprivate}2"
1307-
"|cannot be declared %select{%error|fileprivate|internal|public|open}1}0 "
1312+
"|cannot be declared "
1313+
"%select{in this context|fileprivate|internal|public|open}1}0 "
13081314
"because its %select{parameter|result}5 uses "
13091315
"%select{a private|a fileprivate|an internal|%error|%error}3 type",
13101316
(bool, Accessibility, bool, Accessibility, unsigned, bool))
@@ -1462,8 +1468,9 @@ ERROR(protocol_refine_access,none,
14621468
"%select{protocol must be declared %select{"
14631469
"%select{private|fileprivate|internal|%error|%error}2"
14641470
"|private or fileprivate}3 because it refines"
1465-
"|%select{%error|fileprivate|internal|public|%error}1 protocol cannot "
1466-
"refine}0 %select{a private|a fileprivate|an internal|%error|%error}2 protocol",
1471+
"|%select{in this context|fileprivate|internal|public|%error}1 "
1472+
"protocol cannot refine}0 "
1473+
"%select{a private|a fileprivate|an internal|%error|%error}2 protocol",
14671474
(bool, Accessibility, Accessibility, bool))
14681475
WARNING(protocol_refine_access_warn,none,
14691476
"%select{protocol should be declared "
@@ -1502,7 +1509,8 @@ NOTE(default_associated_type_req_fail,none,
15021509
(Type, DeclName, Type, Type, bool))
15031510
ERROR(associated_type_access,none,
15041511
"associated type in "
1505-
"%select{%error|a fileprivate|an internal|a public|%error}0 protocol uses "
1512+
"%select{a private|a fileprivate|an internal|a public|%error}0 protocol "
1513+
"uses "
15061514
"%select{a private|a fileprivate|an internal|%error|%error}1 type in its "
15071515
"%select{default definition|requirement}2 ",
15081516
(Accessibility, Accessibility, unsigned))
@@ -1732,7 +1740,8 @@ WARNING(typealias_override_associated_type,none,
17321740
ERROR(generic_param_access,none,
17331741
"%0 %select{must be declared %select{"
17341742
"%select{private|fileprivate|internal|%error|%error}3|private or fileprivate}4"
1735-
"|cannot be declared %select{%error|fileprivate|internal|public|open}2}1 "
1743+
"|cannot be declared "
1744+
"%select{in this context|fileprivate|internal|public|open}2}1 "
17361745
"because its generic %select{parameter|requirement}5 uses "
17371746
"%select{a private|a fileprivate|an internal|%error|%error}3 type",
17381747
(DescriptiveDeclKind, bool, Accessibility, Accessibility, bool, bool))
@@ -1954,7 +1963,7 @@ WARNING(class_inherits_anyobject,none,
19541963

19551964
// Enums
19561965
ERROR(enum_case_access,none,
1957-
"enum case in %select{%error|a fileprivate|an internal|a public|%error}0 enum "
1966+
"enum case in %select{a private|a fileprivate|an internal|a public|%error}0 enum "
19581967
"uses %select{a private|a fileprivate|an internal|%error|%error}1 type",
19591968
(Accessibility, Accessibility))
19601969
WARNING(enum_case_access_warn,none,
@@ -1985,7 +1994,8 @@ NOTE(enum_declares_rawrep_with_raw_type,none,
19851994
ERROR(enum_raw_type_access,none,
19861995
"enum %select{must be declared %select{"
19871996
"%select{private|fileprivate|internal|%error|%error}2|private or fileprivate}3"
1988-
"|cannot be declared %select{%error|fileprivate|internal|public|open}1}0 "
1997+
"|cannot be declared "
1998+
"%select{in this context|fileprivate|internal|public|open}1}0 "
19891999
"because its raw type uses "
19902000
"%select{a private|a fileprivate|an internal|%error|%error}2 type",
19912001
(bool, Accessibility, Accessibility, bool))
@@ -3032,7 +3042,8 @@ ERROR(bool_intrinsics_not_found,none,
30323042
ERROR(class_super_access,none,
30333043
"class %select{must be declared %select{"
30343044
"%select{private|fileprivate|internal|%error|%error}2|private or fileprivate}3"
3035-
"|cannot be declared %select{%error|fileprivate|internal|public|open}1}0 "
3045+
"|cannot be declared "
3046+
"%select{in this context|fileprivate|internal|public|open}1}0 "
30363047
"because its superclass is "
30373048
"%select{private|fileprivate|internal|%error|%error}2",
30383049
(bool, Accessibility, Accessibility, bool))

lib/Sema/TypeCheckDecl.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1751,11 +1751,12 @@ static void checkTypeAccessibility(
17511751

17521752
AccessScope contextAccessScope = context->getFormalAccessScope();
17531753
checkTypeAccessibilityImpl(TC, TL, contextAccessScope, DC,
1754-
[=](AccessScope requiredAccessScope,
1755-
const TypeRepr *offendingTR,
1756-
DowngradeToWarning downgradeToWarning) {
1754+
[=, &TC](AccessScope requiredAccessScope,
1755+
const TypeRepr *offendingTR,
1756+
DowngradeToWarning downgradeToWarning) {
17571757
if (!contextAccessScope.isPublic() &&
1758-
!isa<ModuleDecl>(contextAccessScope.getDeclContext())) {
1758+
!isa<ModuleDecl>(contextAccessScope.getDeclContext()) &&
1759+
TC.getLangOpts().isSwiftVersion3()) {
17591760
// Swift 3.0.0 mistakenly didn't diagnose any issues when the context
17601761
// access scope represented a private or fileprivate level.
17611762
downgradeToWarning = DowngradeToWarning::Yes;
@@ -1868,10 +1869,10 @@ static void checkGenericParamAccessibility(TypeChecker &TC,
18681869

18691870
// Swift 3.0.0 mistakenly didn't diagnose any issues when the context access
18701871
// scope represented a private or fileprivate level.
1871-
// FIXME: Conditionalize this on Swift 3 mode.
18721872
if (downgradeToWarning == DowngradeToWarning::No) {
18731873
if (!accessScope.isPublic() &&
1874-
!isa<ModuleDecl>(accessScope.getDeclContext())) {
1874+
!isa<ModuleDecl>(accessScope.getDeclContext()) &&
1875+
TC.getLangOpts().isSwiftVersion3()) {
18751876
downgradeToWarning = DowngradeToWarning::Yes;
18761877
}
18771878
}

test/Sema/accessibility_private.swift

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -163,51 +163,51 @@ extension Container {
163163
}
164164
}
165165

166-
// All of these should be errors, but didn't have the correct behavior in Swift
167-
// 3.0GM.
166+
// All of these are errors in Swift 4, but didn't have the correct behavior in
167+
// Swift 3.0GM.
168168
extension Container {
169169
private struct VeryPrivateStruct { // expected-note * {{type declared here}}
170170
private typealias VeryPrivateType = Int // expected-note * {{type declared here}}
171-
var privateVar: VeryPrivateType { fatalError() } // expected-warning {{property should be declared private because its type uses a private type}}
172-
var privateVar2 = VeryPrivateType() // expected-warning {{property should be declared private because its type 'Container.VeryPrivateStruct.VeryPrivateType' (aka 'Int') uses a private type}}
173-
typealias PrivateAlias = VeryPrivateType // expected-warning {{type alias should be declared private because its underlying type uses a private type}}
174-
subscript(_: VeryPrivateType) -> Void { return () } // expected-warning {{subscript should be declared private because its index uses a private type}}
175-
func privateMethod(_: VeryPrivateType) -> Void {} // expected-warning {{method should be declared private because its parameter uses a private type}} {{none}}
176-
enum PrivateRawValue: VeryPrivateType { // expected-warning {{enum should be declared private because its raw type uses a private type}} {{none}}
171+
var privateVar: VeryPrivateType { fatalError() } // expected-error {{property must be declared private because its type uses a private type}}
172+
var privateVar2 = VeryPrivateType() // expected-error {{property must be declared private because its type 'Container.VeryPrivateStruct.VeryPrivateType' (aka 'Int') uses a private type}}
173+
typealias PrivateAlias = VeryPrivateType // expected-error {{type alias must be declared private because its underlying type uses a private type}}
174+
subscript(_: VeryPrivateType) -> Void { return () } // expected-error {{subscript must be declared private because its index uses a private type}}
175+
func privateMethod(_: VeryPrivateType) -> Void {} // expected-error {{method must be declared private because its parameter uses a private type}} {{none}}
176+
enum PrivateRawValue: VeryPrivateType { // expected-error {{enum must be declared private because its raw type uses a private type}} {{none}}
177177
case A
178178
}
179179
enum PrivatePayload {
180-
case A(VeryPrivateType) // expected-warning {{enum case in an internal enum uses a private type}} {{none}}
180+
case A(VeryPrivateType) // expected-error {{enum case in an internal enum uses a private type}} {{none}}
181181
}
182182

183183
private class PrivateInnerClass {} // expected-note * {{declared here}}
184-
class PrivateSuper: PrivateInnerClass {} // expected-warning {{class should be declared private because its superclass is private}} {{none}}
184+
class PrivateSuper: PrivateInnerClass {} // expected-error {{class must be declared private because its superclass is private}} {{none}}
185185
}
186186

187-
fileprivate var privateVar: VeryPrivateStruct { fatalError() } // expected-warning {{property should not be declared fileprivate because its type uses a private type}} {{none}}
188-
fileprivate typealias PrivateAlias = VeryPrivateStruct // expected-warning {{type alias should not be declared fileprivate because its underlying type uses a private type}} {{none}}
189-
fileprivate subscript(_: VeryPrivateStruct) -> Void { return () } // expected-warning {{subscript should not be declared fileprivate because its index uses a private type}} {{none}}
190-
fileprivate func privateMethod(_: VeryPrivateStruct) -> Void {} // expected-warning {{method should not be declared fileprivate because its parameter uses a private type}} {{none}}
191-
fileprivate enum PrivateRawValue: VeryPrivateStruct {} // expected-warning {{enum should not be declared fileprivate because its raw type uses a private type}} {{none}}
187+
fileprivate var privateVar: VeryPrivateStruct { fatalError() } // expected-error {{property cannot be declared fileprivate because its type uses a private type}} {{none}}
188+
fileprivate typealias PrivateAlias = VeryPrivateStruct // expected-error {{type alias cannot be declared fileprivate because its underlying type uses a private type}} {{none}}
189+
fileprivate subscript(_: VeryPrivateStruct) -> Void { return () } // expected-error {{subscript cannot be declared fileprivate because its index uses a private type}} {{none}}
190+
fileprivate func privateMethod(_: VeryPrivateStruct) -> Void {} // expected-error {{method cannot be declared fileprivate because its parameter uses a private type}} {{none}}
191+
fileprivate enum PrivateRawValue: VeryPrivateStruct {} // expected-error {{enum cannot be declared fileprivate because its raw type uses a private type}} {{none}}
192192
// expected-error@-1 {{raw type 'Container.VeryPrivateStruct' is not expressible by any literal}}
193193
// expected-error@-2 {{'Container.PrivateRawValue' declares raw type 'Container.VeryPrivateStruct', but does not conform to RawRepresentable and conformance could not be synthesized}}
194194
// expected-error@-3 {{RawRepresentable conformance cannot be synthesized because raw type 'Container.VeryPrivateStruct' is not Equatable}}
195195
fileprivate enum PrivatePayload {
196-
case A(VeryPrivateStruct) // expected-warning {{enum case in an internal enum uses a private type}} {{none}}
196+
case A(VeryPrivateStruct) // expected-error {{enum case in an internal enum uses a private type}} {{none}}
197197
}
198198

199199
private class PrivateInnerClass {} // expected-note * {{declared here}}
200-
fileprivate class PrivateSuperClass: PrivateInnerClass {} // expected-warning {{class should not be declared fileprivate because its superclass is private}} {{none}}
201-
fileprivate class PrivateGenericUser<T> where T: PrivateInnerClass {} // expected-warning {{generic class should not be declared fileprivate because its generic requirement uses a private type}} {{none}}
200+
fileprivate class PrivateSuperClass: PrivateInnerClass {} // expected-error {{class cannot be declared fileprivate because its superclass is private}} {{none}}
201+
fileprivate class PrivateGenericUser<T> where T: PrivateInnerClass {} // expected-error {{generic class cannot be declared fileprivate because its generic requirement uses a private type}} {{none}}
202202
}
203203

204204
fileprivate struct SR2579 {
205205
private struct Inner {
206206
private struct InnerPrivateType {}
207-
var innerProperty = InnerPrivateType() // expected-warning {{property should be declared private because its type 'SR2579.Inner.InnerPrivateType' uses a private type}}
207+
var innerProperty = InnerPrivateType() // expected-error {{property must be declared private because its type 'SR2579.Inner.InnerPrivateType' uses a private type}}
208208
}
209209
// FIXME: We need better errors when one access violation results in more
210210
// downstream.
211-
private var outerProperty = Inner().innerProperty // expected-warning {{property should not be declared in this context because its type 'SR2579.Inner.InnerPrivateType' uses a private type}}
212-
var outerProperty2 = Inner().innerProperty // expected-warning {{property should be declared private because its type 'SR2579.Inner.InnerPrivateType' uses a private type}}
211+
private var outerProperty = Inner().innerProperty // expected-error {{property cannot be declared in this context because its type 'SR2579.Inner.InnerPrivateType' uses a private type}}
212+
var outerProperty2 = Inner().innerProperty // expected-error {{property must be declared private because its type 'SR2579.Inner.InnerPrivateType' uses a private type}}
213213
}

0 commit comments

Comments
 (0)