Skip to content

Commit 970073a

Browse files
authored
Promote a bunch of Swift 3 compatibility access warnings to errors. (#10086)
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 cb877fd commit 970073a

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
@@ -1194,7 +1194,8 @@ ERROR(pattern_type_access,none,
11941194
"%select{%select{variable|constant}0|property}1 "
11951195
"%select{must be declared %select{"
11961196
"%select{private|fileprivate|internal|%error|%error}3|private or fileprivate}4"
1197-
"|cannot be declared %select{%error|fileprivate|internal|public|open}3}2 "
1197+
"|cannot be declared "
1198+
"%select{in this context|fileprivate|internal|public|open}3}2 "
11981199
"because its type uses "
11991200
"%select{a private|a fileprivate|an internal|%error|%error}5 type",
12001201
(bool, bool, bool, Accessibility, bool, Accessibility))
@@ -1209,7 +1210,8 @@ ERROR(pattern_type_access_inferred,none,
12091210
"%select{%select{variable|constant}0|property}1 "
12101211
"%select{must be declared %select{"
12111212
"%select{private|fileprivate|internal|%error|%error}3|private or fileprivate}4"
1212-
"|cannot be declared %select{%error|fileprivate|internal|public|open}3}2 "
1213+
"|cannot be declared "
1214+
"%select{in this context|fileprivate|internal|public|open}3}2 "
12131215
"because its type %6 uses "
12141216
"%select{a private|a fileprivate|an internal|%error|%error}5 type",
12151217
(bool, bool, bool, Accessibility, bool, Accessibility, Type))
@@ -1261,14 +1263,16 @@ ERROR(unsupported_nested_protocol,none,
12611263
ERROR(type_alias_underlying_type_access,none,
12621264
"type alias %select{must be declared %select{"
12631265
"%select{private|fileprivate|internal|%error|%error}2|private or fileprivate}3"
1264-
"|cannot be declared %select{%error|fileprivate|internal|public|open}1}0 "
1266+
"|cannot be declared "
1267+
"%select{in this context|fileprivate|internal|public|open}1}0 "
12651268
"because its underlying type uses "
12661269
"%select{a private|a fileprivate|an internal|%error|%error}2 type",
12671270
(bool, Accessibility, Accessibility, bool))
12681271
WARNING(type_alias_underlying_type_access_warn,none,
12691272
"type alias %select{should be declared "
12701273
"%select{private|fileprivate|internal|%error|%error}2"
1271-
"|should not be declared %select{%error|fileprivate|internal|public|open}1}0 "
1274+
"|should not be declared "
1275+
"%select{in this context|fileprivate|internal|public|open}1}0 "
12721276
"because its underlying type uses "
12731277
"%select{a private|a fileprivate|an internal|%error|%error}2 type",
12741278
(bool, Accessibility, Accessibility, bool))
@@ -1277,7 +1281,8 @@ WARNING(type_alias_underlying_type_access_warn,none,
12771281
ERROR(subscript_type_access,none,
12781282
"subscript %select{must be declared "
12791283
"%select{private|fileprivate|internal|%error|%error}1"
1280-
"|cannot be declared %select{%error|fileprivate|internal|public|open}1}0 "
1284+
"|cannot be declared "
1285+
"%select{in this context|fileprivate|internal|public|open}1}0 "
12811286
"because its %select{index|element type}3 uses "
12821287
"%select{a private|a fileprivate|an internal|%error|%error}2 type",
12831288
(bool, Accessibility, Accessibility, bool))
@@ -1294,7 +1299,8 @@ ERROR(function_type_access,none,
12941299
"%select{function|method|initializer}4 "
12951300
"%select{must be declared %select{"
12961301
"%select{private|fileprivate|internal|%error|%error}1|private or fileprivate}2"
1297-
"|cannot be declared %select{%error|fileprivate|internal|public|open}1}0 "
1302+
"|cannot be declared "
1303+
"%select{in this context|fileprivate|internal|public|open}1}0 "
12981304
"because its %select{parameter|result}5 uses "
12991305
"%select{a private|a fileprivate|an internal|%error|%error}3 type",
13001306
(bool, Accessibility, bool, Accessibility, unsigned, bool))
@@ -1452,8 +1458,9 @@ ERROR(protocol_refine_access,none,
14521458
"%select{protocol must be declared %select{"
14531459
"%select{private|fileprivate|internal|%error|%error}2"
14541460
"|private or fileprivate}3 because it refines"
1455-
"|%select{%error|fileprivate|internal|public|%error}1 protocol cannot "
1456-
"refine}0 %select{a private|a fileprivate|an internal|%error|%error}2 protocol",
1461+
"|%select{in this context|fileprivate|internal|public|%error}1 "
1462+
"protocol cannot refine}0 "
1463+
"%select{a private|a fileprivate|an internal|%error|%error}2 protocol",
14571464
(bool, Accessibility, Accessibility, bool))
14581465
WARNING(protocol_refine_access_warn,none,
14591466
"%select{protocol should be declared "
@@ -1492,7 +1499,8 @@ NOTE(default_associated_type_req_fail,none,
14921499
(Type, DeclName, Type, Type, bool))
14931500
ERROR(associated_type_access,none,
14941501
"associated type in "
1495-
"%select{%error|a fileprivate|an internal|a public|%error}0 protocol uses "
1502+
"%select{a private|a fileprivate|an internal|a public|%error}0 protocol "
1503+
"uses "
14961504
"%select{a private|a fileprivate|an internal|%error|%error}1 type in its "
14971505
"%select{default definition|requirement}2 ",
14981506
(Accessibility, Accessibility, unsigned))
@@ -1722,7 +1730,8 @@ WARNING(typealias_override_associated_type,none,
17221730
ERROR(generic_param_access,none,
17231731
"%0 %select{must be declared %select{"
17241732
"%select{private|fileprivate|internal|%error|%error}3|private or fileprivate}4"
1725-
"|cannot be declared %select{%error|fileprivate|internal|public|open}2}1 "
1733+
"|cannot be declared "
1734+
"%select{in this context|fileprivate|internal|public|open}2}1 "
17261735
"because its generic %select{parameter|requirement}5 uses "
17271736
"%select{a private|a fileprivate|an internal|%error|%error}3 type",
17281737
(DescriptiveDeclKind, bool, Accessibility, Accessibility, bool, bool))
@@ -1944,7 +1953,7 @@ WARNING(class_inherits_anyobject,none,
19441953

19451954
// Enums
19461955
ERROR(enum_case_access,none,
1947-
"enum case in %select{%error|a fileprivate|an internal|a public|%error}0 enum "
1956+
"enum case in %select{a private|a fileprivate|an internal|a public|%error}0 enum "
19481957
"uses %select{a private|a fileprivate|an internal|%error|%error}1 type",
19491958
(Accessibility, Accessibility))
19501959
WARNING(enum_case_access_warn,none,
@@ -1975,7 +1984,8 @@ NOTE(enum_declares_rawrep_with_raw_type,none,
19751984
ERROR(enum_raw_type_access,none,
19761985
"enum %select{must be declared %select{"
19771986
"%select{private|fileprivate|internal|%error|%error}2|private or fileprivate}3"
1978-
"|cannot be declared %select{%error|fileprivate|internal|public|open}1}0 "
1987+
"|cannot be declared "
1988+
"%select{in this context|fileprivate|internal|public|open}1}0 "
19791989
"because its raw type uses "
19801990
"%select{a private|a fileprivate|an internal|%error|%error}2 type",
19811991
(bool, Accessibility, Accessibility, bool))
@@ -3026,7 +3036,8 @@ ERROR(bool_intrinsics_not_found,none,
30263036
ERROR(class_super_access,none,
30273037
"class %select{must be declared %select{"
30283038
"%select{private|fileprivate|internal|%error|%error}2|private or fileprivate}3"
3029-
"|cannot be declared %select{%error|fileprivate|internal|public|open}1}0 "
3039+
"|cannot be declared "
3040+
"%select{in this context|fileprivate|internal|public|open}1}0 "
30303041
"because its superclass is "
30313042
"%select{private|fileprivate|internal|%error|%error}2",
30323043
(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)