Skip to content

Promote a bunch of Swift 3 compatibility access warnings to errors. #10086

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
Jun 16, 2017
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
37 changes: 24 additions & 13 deletions include/swift/AST/DiagnosticsSema.def
Original file line number Diff line number Diff line change
Expand Up @@ -1196,7 +1196,8 @@ ERROR(pattern_type_access,none,
"%select{%select{variable|constant}0|property}1 "
"%select{must be declared %select{"
"%select{private|fileprivate|internal|%error|%error}3|private or fileprivate}4"
"|cannot be declared %select{%error|fileprivate|internal|public|open}3}2 "
"|cannot be declared "
"%select{in this context|fileprivate|internal|public|open}3}2 "
"because its type uses "
"%select{a private|a fileprivate|an internal|%error|%error}5 type",
(bool, bool, bool, Accessibility, bool, Accessibility))
Expand All @@ -1211,7 +1212,8 @@ ERROR(pattern_type_access_inferred,none,
"%select{%select{variable|constant}0|property}1 "
"%select{must be declared %select{"
"%select{private|fileprivate|internal|%error|%error}3|private or fileprivate}4"
"|cannot be declared %select{%error|fileprivate|internal|public|open}3}2 "
"|cannot be declared "
"%select{in this context|fileprivate|internal|public|open}3}2 "
"because its type %6 uses "
"%select{a private|a fileprivate|an internal|%error|%error}5 type",
(bool, bool, bool, Accessibility, bool, Accessibility, Type))
Expand Down Expand Up @@ -1271,14 +1273,16 @@ ERROR(unsupported_nested_protocol,none,
ERROR(type_alias_underlying_type_access,none,
"type alias %select{must be declared %select{"
"%select{private|fileprivate|internal|%error|%error}2|private or fileprivate}3"
"|cannot be declared %select{%error|fileprivate|internal|public|open}1}0 "
"|cannot be declared "
"%select{in this context|fileprivate|internal|public|open}1}0 "
"because its underlying type uses "
"%select{a private|a fileprivate|an internal|%error|%error}2 type",
(bool, Accessibility, Accessibility, bool))
WARNING(type_alias_underlying_type_access_warn,none,
"type alias %select{should be declared "
"%select{private|fileprivate|internal|%error|%error}2"
"|should not be declared %select{%error|fileprivate|internal|public|open}1}0 "
"|should not be declared "
"%select{in this context|fileprivate|internal|public|open}1}0 "
"because its underlying type uses "
"%select{a private|a fileprivate|an internal|%error|%error}2 type",
(bool, Accessibility, Accessibility, bool))
Expand All @@ -1287,7 +1291,8 @@ WARNING(type_alias_underlying_type_access_warn,none,
ERROR(subscript_type_access,none,
"subscript %select{must be declared "
"%select{private|fileprivate|internal|%error|%error}1"
"|cannot be declared %select{%error|fileprivate|internal|public|open}1}0 "
"|cannot be declared "
"%select{in this context|fileprivate|internal|public|open}1}0 "
"because its %select{index|element type}3 uses "
"%select{a private|a fileprivate|an internal|%error|%error}2 type",
(bool, Accessibility, Accessibility, bool))
Expand All @@ -1304,7 +1309,8 @@ ERROR(function_type_access,none,
"%select{function|method|initializer}4 "
"%select{must be declared %select{"
"%select{private|fileprivate|internal|%error|%error}1|private or fileprivate}2"
"|cannot be declared %select{%error|fileprivate|internal|public|open}1}0 "
"|cannot be declared "
"%select{in this context|fileprivate|internal|public|open}1}0 "
"because its %select{parameter|result}5 uses "
"%select{a private|a fileprivate|an internal|%error|%error}3 type",
(bool, Accessibility, bool, Accessibility, unsigned, bool))
Expand Down Expand Up @@ -1462,8 +1468,9 @@ ERROR(protocol_refine_access,none,
"%select{protocol must be declared %select{"
"%select{private|fileprivate|internal|%error|%error}2"
"|private or fileprivate}3 because it refines"
"|%select{%error|fileprivate|internal|public|%error}1 protocol cannot "
"refine}0 %select{a private|a fileprivate|an internal|%error|%error}2 protocol",
"|%select{in this context|fileprivate|internal|public|%error}1 "
"protocol cannot refine}0 "
"%select{a private|a fileprivate|an internal|%error|%error}2 protocol",
(bool, Accessibility, Accessibility, bool))
WARNING(protocol_refine_access_warn,none,
"%select{protocol should be declared "
Expand Down Expand Up @@ -1502,7 +1509,8 @@ NOTE(default_associated_type_req_fail,none,
(Type, DeclName, Type, Type, bool))
ERROR(associated_type_access,none,
"associated type in "
"%select{%error|a fileprivate|an internal|a public|%error}0 protocol uses "
"%select{a private|a fileprivate|an internal|a public|%error}0 protocol "
"uses "
"%select{a private|a fileprivate|an internal|%error|%error}1 type in its "
"%select{default definition|requirement}2 ",
(Accessibility, Accessibility, unsigned))
Expand Down Expand Up @@ -1732,7 +1740,8 @@ WARNING(typealias_override_associated_type,none,
ERROR(generic_param_access,none,
"%0 %select{must be declared %select{"
"%select{private|fileprivate|internal|%error|%error}3|private or fileprivate}4"
"|cannot be declared %select{%error|fileprivate|internal|public|open}2}1 "
"|cannot be declared "
"%select{in this context|fileprivate|internal|public|open}2}1 "
"because its generic %select{parameter|requirement}5 uses "
"%select{a private|a fileprivate|an internal|%error|%error}3 type",
(DescriptiveDeclKind, bool, Accessibility, Accessibility, bool, bool))
Expand Down Expand Up @@ -1954,7 +1963,7 @@ WARNING(class_inherits_anyobject,none,

// Enums
ERROR(enum_case_access,none,
"enum case in %select{%error|a fileprivate|an internal|a public|%error}0 enum "
"enum case in %select{a private|a fileprivate|an internal|a public|%error}0 enum "
"uses %select{a private|a fileprivate|an internal|%error|%error}1 type",
(Accessibility, Accessibility))
WARNING(enum_case_access_warn,none,
Expand Down Expand Up @@ -1985,7 +1994,8 @@ NOTE(enum_declares_rawrep_with_raw_type,none,
ERROR(enum_raw_type_access,none,
"enum %select{must be declared %select{"
"%select{private|fileprivate|internal|%error|%error}2|private or fileprivate}3"
"|cannot be declared %select{%error|fileprivate|internal|public|open}1}0 "
"|cannot be declared "
"%select{in this context|fileprivate|internal|public|open}1}0 "
"because its raw type uses "
"%select{a private|a fileprivate|an internal|%error|%error}2 type",
(bool, Accessibility, Accessibility, bool))
Expand Down Expand Up @@ -3032,7 +3042,8 @@ ERROR(bool_intrinsics_not_found,none,
ERROR(class_super_access,none,
"class %select{must be declared %select{"
"%select{private|fileprivate|internal|%error|%error}2|private or fileprivate}3"
"|cannot be declared %select{%error|fileprivate|internal|public|open}1}0 "
"|cannot be declared "
"%select{in this context|fileprivate|internal|public|open}1}0 "
"because its superclass is "
"%select{private|fileprivate|internal|%error|%error}2",
(bool, Accessibility, Accessibility, bool))
Expand Down
13 changes: 7 additions & 6 deletions lib/Sema/TypeCheckDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1751,11 +1751,12 @@ static void checkTypeAccessibility(

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

// Swift 3.0.0 mistakenly didn't diagnose any issues when the context access
// scope represented a private or fileprivate level.
// FIXME: Conditionalize this on Swift 3 mode.
if (downgradeToWarning == DowngradeToWarning::No) {
if (!accessScope.isPublic() &&
!isa<ModuleDecl>(accessScope.getDeclContext())) {
!isa<ModuleDecl>(accessScope.getDeclContext()) &&
TC.getLangOpts().isSwiftVersion3()) {
downgradeToWarning = DowngradeToWarning::Yes;
}
}
Expand Down
42 changes: 21 additions & 21 deletions test/Sema/accessibility_private.swift
Original file line number Diff line number Diff line change
Expand Up @@ -163,51 +163,51 @@ extension Container {
}
}

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

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

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

private class PrivateInnerClass {} // expected-note * {{declared here}}
fileprivate class PrivateSuperClass: PrivateInnerClass {} // expected-warning {{class should not be declared fileprivate because its superclass is private}} {{none}}
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}}
fileprivate class PrivateSuperClass: PrivateInnerClass {} // expected-error {{class cannot be declared fileprivate because its superclass is private}} {{none}}
fileprivate class PrivateGenericUser<T> where T: PrivateInnerClass {} // expected-error {{generic class cannot be declared fileprivate because its generic requirement uses a private type}} {{none}}
}

fileprivate struct SR2579 {
private struct Inner {
private struct InnerPrivateType {}
var innerProperty = InnerPrivateType() // expected-warning {{property should be declared private because its type 'SR2579.Inner.InnerPrivateType' uses a private type}}
var innerProperty = InnerPrivateType() // expected-error {{property must be declared private because its type 'SR2579.Inner.InnerPrivateType' uses a private type}}
}
// FIXME: We need better errors when one access violation results in more
// downstream.
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}}
var outerProperty2 = Inner().innerProperty // expected-warning {{property should be declared private because its type 'SR2579.Inner.InnerPrivateType' uses a private type}}
private var outerProperty = Inner().innerProperty // expected-error {{property cannot be declared in this context because its type 'SR2579.Inner.InnerPrivateType' uses a private type}}
var outerProperty2 = Inner().innerProperty // expected-error {{property must be declared private because its type 'SR2579.Inner.InnerPrivateType' uses a private type}}
}