From a965e22ae46b98db7f2035687d586ecc9eddcb02 Mon Sep 17 00:00:00 2001 From: Jordan Rose Date: Mon, 25 Jul 2016 15:58:07 -0700 Subject: [PATCH 1/4] Convert the Dispatch and Foundation overlays to 'fileprivate'. Similar to the work in apple/swift-corelibs-foundation#445. Groundwork for SE-0025 ('private' and 'fileprivate'). No intended functionality change. --- stdlib/public/SDK/Dispatch/Data.swift | 4 +-- stdlib/public/SDK/Dispatch/Queue.swift | 2 +- stdlib/public/SDK/Dispatch/Time.swift | 4 +-- stdlib/public/SDK/Foundation/Boxing.swift | 2 +- stdlib/public/SDK/Foundation/Calendar.swift | 2 +- .../public/SDK/Foundation/CharacterSet.swift | 2 +- stdlib/public/SDK/Foundation/Data.swift | 2 +- stdlib/public/SDK/Foundation/Date.swift | 2 +- .../SDK/Foundation/DateComponents.swift | 2 +- stdlib/public/SDK/Foundation/IndexPath.swift | 4 +-- stdlib/public/SDK/Foundation/IndexSet.swift | 26 +++++++++---------- stdlib/public/SDK/Foundation/Locale.swift | 4 +-- .../SDK/Foundation/PersonNameComponents.swift | 2 +- stdlib/public/SDK/Foundation/TimeZone.swift | 4 +-- stdlib/public/SDK/Foundation/URL.swift | 10 +++---- .../public/SDK/Foundation/URLComponents.swift | 8 +++--- stdlib/public/SDK/Foundation/URLRequest.swift | 2 +- stdlib/public/SDK/Foundation/UUID.swift | 4 +-- stdlib/public/SDK/Foundation/UserInfo.swift | 4 +-- 19 files changed, 45 insertions(+), 45 deletions(-) diff --git a/stdlib/public/SDK/Dispatch/Data.swift b/stdlib/public/SDK/Dispatch/Data.swift index 4593d6a3f7390..2ad7b78e60b4c 100644 --- a/stdlib/public/SDK/Dispatch/Data.swift +++ b/stdlib/public/SDK/Dispatch/Data.swift @@ -29,7 +29,7 @@ public struct DispatchData : RandomAccessCollection, _ObjectiveCBridgeable { /// A custom deallocator case custom(DispatchQueue?, @convention(block) () -> Void) - private var _deallocator: (DispatchQueue?, @convention(block) () -> Void) { + fileprivate var _deallocator: (DispatchQueue?, @convention(block) () -> Void) { switch self { case .free: return (nil, _dispatch_data_destructor_free()) case .unmap: return (nil, _dispatch_data_destructor_munmap()) @@ -38,7 +38,7 @@ public struct DispatchData : RandomAccessCollection, _ObjectiveCBridgeable { } } - private var __wrapped: __DispatchData + fileprivate var __wrapped: __DispatchData /// Initialize a `Data` with copied memory content. /// diff --git a/stdlib/public/SDK/Dispatch/Queue.swift b/stdlib/public/SDK/Dispatch/Queue.swift index cb03e4acfaa7e..b4130a4857949 100644 --- a/stdlib/public/SDK/Dispatch/Queue.swift +++ b/stdlib/public/SDK/Dispatch/Queue.swift @@ -33,7 +33,7 @@ public extension DispatchQueue { @available(OSX 10.12, iOS 10.0, tvOS 10.0, watchOS 3.0, *) public static let initiallyInactive = Attributes(rawValue: 1<<2) - private func _attr() -> __OS_dispatch_queue_attr? { + fileprivate func _attr() -> __OS_dispatch_queue_attr? { var attr: __OS_dispatch_queue_attr? = nil if self.contains(.concurrent) { diff --git a/stdlib/public/SDK/Dispatch/Time.swift b/stdlib/public/SDK/Dispatch/Time.swift index 2b31a771ddd12..357ff5a50b0ac 100644 --- a/stdlib/public/SDK/Dispatch/Time.swift +++ b/stdlib/public/SDK/Dispatch/Time.swift @@ -20,7 +20,7 @@ public struct DispatchTime : Comparable { public static let distantFuture = DispatchTime(rawValue: ~0) - private init(rawValue: dispatch_time_t) { + fileprivate init(rawValue: dispatch_time_t) { self.rawValue = rawValue } @@ -60,7 +60,7 @@ public struct DispatchWallTime : Comparable { public static let distantFuture = DispatchWallTime(rawValue: ~0) - private init(rawValue: dispatch_time_t) { + fileprivate init(rawValue: dispatch_time_t) { self.rawValue = rawValue } diff --git a/stdlib/public/SDK/Foundation/Boxing.swift b/stdlib/public/SDK/Foundation/Boxing.swift index 844f9dba092b2..a4542c9a51cb6 100644 --- a/stdlib/public/SDK/Foundation/Boxing.swift +++ b/stdlib/public/SDK/Foundation/Boxing.swift @@ -16,7 +16,7 @@ /// /// Note: This assumes that the result of calling copy() is mutable. The documentation says that classes which do not have a mutable/immutable distinction should just adopt NSCopying instead of NSMutableCopying. internal final class _MutableHandle { - private var _pointer : MutableType + fileprivate var _pointer : MutableType init(reference : MutableType) { _pointer = reference.copy() as! MutableType diff --git a/stdlib/public/SDK/Foundation/Calendar.swift b/stdlib/public/SDK/Foundation/Calendar.swift index af16d204ebc97..1f33e93d6c6fa 100644 --- a/stdlib/public/SDK/Foundation/Calendar.swift +++ b/stdlib/public/SDK/Foundation/Calendar.swift @@ -115,7 +115,7 @@ public struct Calendar : CustomStringConvertible, CustomDebugStringConvertible, // MARK: - // MARK: Bridging - private init(reference : NSCalendar) { + fileprivate init(reference : NSCalendar) { _handle = _MutableHandle(reference: reference) if __NSCalendarIsAutoupdating(reference) { _autoupdating = true diff --git a/stdlib/public/SDK/Foundation/CharacterSet.swift b/stdlib/public/SDK/Foundation/CharacterSet.swift index 1e9cfa623df40..05f8881548927 100644 --- a/stdlib/public/SDK/Foundation/CharacterSet.swift +++ b/stdlib/public/SDK/Foundation/CharacterSet.swift @@ -82,7 +82,7 @@ public struct CharacterSet : ReferenceConvertible, Equatable, Hashable, SetAlgeb // MARK: Init methods - private init(_bridged characterSet: NSCharacterSet) { + fileprivate init(_bridged characterSet: NSCharacterSet) { // We must copy the input because it might be mutable; just like storing a value type in ObjC _wrapped = _SwiftNSCharacterSet(immutableObject: characterSet.copy() as AnyObject) } diff --git a/stdlib/public/SDK/Foundation/Data.swift b/stdlib/public/SDK/Foundation/Data.swift index d7152447bc96c..dc44f6113df37 100644 --- a/stdlib/public/SDK/Foundation/Data.swift +++ b/stdlib/public/SDK/Foundation/Data.swift @@ -103,7 +103,7 @@ public struct Data : ReferenceConvertible, CustomStringConvertible, Equatable, H /// A custom deallocator. case custom((UnsafeMutablePointer, Int) -> Void) - private var _deallocator : ((UnsafeMutablePointer, Int) -> Void)? { + fileprivate var _deallocator : ((UnsafeMutablePointer, Int) -> Void)? { switch self { case .virtualMemory: return { __NSDataInvokeDeallocatorVM($0, $1) } diff --git a/stdlib/public/SDK/Foundation/Date.swift b/stdlib/public/SDK/Foundation/Date.swift index bbae2906ef024..ff7ec6ccacb27 100644 --- a/stdlib/public/SDK/Foundation/Date.swift +++ b/stdlib/public/SDK/Foundation/Date.swift @@ -21,7 +21,7 @@ import CoreFoundation public struct Date : ReferenceConvertible, Comparable, Equatable, CustomStringConvertible { public typealias ReferenceType = NSDate - private var _time : TimeInterval + fileprivate var _time : TimeInterval /// The number of seconds from 1 January 1970 to the reference date, 1 January 2001. public static let timeIntervalBetween1970AndReferenceDate : TimeInterval = 978307200.0 diff --git a/stdlib/public/SDK/Foundation/DateComponents.swift b/stdlib/public/SDK/Foundation/DateComponents.swift index ac8c92fa2f562..ecb0621745e2c 100644 --- a/stdlib/public/SDK/Foundation/DateComponents.swift +++ b/stdlib/public/SDK/Foundation/DateComponents.swift @@ -277,7 +277,7 @@ public struct DateComponents : ReferenceConvertible, Hashable, Equatable, _Mutab // MARK: - Bridging Helpers - private init(reference: NSDateComponents) { + fileprivate init(reference: NSDateComponents) { _handle = _MutableHandle(reference: reference) } diff --git a/stdlib/public/SDK/Foundation/IndexPath.swift b/stdlib/public/SDK/Foundation/IndexPath.swift index c6388b8730b97..3323ebdfb9b93 100644 --- a/stdlib/public/SDK/Foundation/IndexPath.swift +++ b/stdlib/public/SDK/Foundation/IndexPath.swift @@ -156,7 +156,7 @@ public struct IndexPath : ReferenceConvertible, Equatable, Hashable, MutableColl // MARK: - Bridging Helpers - private init(nsIndexPath: ReferenceType) { + fileprivate init(nsIndexPath: ReferenceType) { let count = nsIndexPath.length if count == 0 { _indexes = [] @@ -171,7 +171,7 @@ public struct IndexPath : ReferenceConvertible, Equatable, Hashable, MutableColl } } - private func makeReference() -> ReferenceType { + fileprivate func makeReference() -> ReferenceType { return _indexes.withUnsafeBufferPointer { return ReferenceType(indexes: $0.baseAddress, length: $0.count) } diff --git a/stdlib/public/SDK/Foundation/IndexSet.swift b/stdlib/public/SDK/Foundation/IndexSet.swift index 03f897eae3dad..730ab423e0887 100644 --- a/stdlib/public/SDK/Foundation/IndexSet.swift +++ b/stdlib/public/SDK/Foundation/IndexSet.swift @@ -101,12 +101,12 @@ public struct IndexSet : ReferenceConvertible, Equatable, BidirectionalCollectio public let startIndex : Index public let endIndex : Index - private var indexSet : IndexSet + fileprivate var indexSet : IndexSet // Range of element values private var intersectingRange : Range? - private init(indexSet : IndexSet, intersecting range : Range?) { + fileprivate init(indexSet : IndexSet, intersecting range : Range?) { self.indexSet = indexSet self.intersectingRange = range @@ -170,12 +170,12 @@ public struct IndexSet : ReferenceConvertible, Equatable, BidirectionalCollectio /// The mechanism for accessing the integers stored in an IndexSet. public struct Index : CustomStringConvertible, Comparable { - private var value : IndexSet.Element - private var extent : Range - private var rangeIndex : Int - private let rangeCount : Int + fileprivate var value : IndexSet.Element + fileprivate var extent : Range + fileprivate var rangeIndex : Int + fileprivate let rangeCount : Int - private init(value: Int, extent: Range, rangeIndex: Int, rangeCount: Int) { + fileprivate init(value: Int, extent: Range, rangeIndex: Int, rangeCount: Int) { self.value = value self.extent = extent self.rangeCount = rangeCount @@ -190,7 +190,7 @@ public struct IndexSet : ReferenceConvertible, Equatable, BidirectionalCollectio public typealias ReferenceType = NSIndexSet public typealias Element = Int - private var _handle: _MutablePairHandle + fileprivate var _handle: _MutablePairHandle /// Initialize an `IndexSet` with a range of integers. public init(integersIn range: Range) { @@ -768,11 +768,11 @@ public struct IndexSet : ReferenceConvertible, Equatable, BidirectionalCollectio // MARK: - Bridging Support - private var reference: NSIndexSet { + fileprivate var reference: NSIndexSet { return _handle.reference } - private init(reference: NSIndexSet) { + fileprivate init(reference: NSIndexSet) { _handle = _MutablePairHandle(reference) } } @@ -788,7 +788,7 @@ private struct IndexSetBoundaryIterator : IteratorProtocol { private var i1UsedLower : Bool private var i2UsedLower : Bool - private init(_ is1 : IndexSet, _ is2 : IndexSet) { + fileprivate init(_ is1 : IndexSet, _ is2 : IndexSet) { i1 = is1.rangeView.makeIterator() i2 = is2.rangeView.makeIterator() @@ -800,7 +800,7 @@ private struct IndexSetBoundaryIterator : IteratorProtocol { i2UsedLower = false } - private mutating func next() -> Element? { + fileprivate mutating func next() -> Element? { if i1Range == nil && i2Range == nil { return nil } @@ -900,7 +900,7 @@ private enum _MutablePair { /// /// a.k.a. Box private final class _MutablePairHandle { - private var _pointer: _MutablePair + fileprivate var _pointer: _MutablePair /// Initialize with an immutable reference instance. /// diff --git a/stdlib/public/SDK/Foundation/Locale.swift b/stdlib/public/SDK/Foundation/Locale.swift index 691a434a7bb4b..b92c3b61880f9 100644 --- a/stdlib/public/SDK/Foundation/Locale.swift +++ b/stdlib/public/SDK/Foundation/Locale.swift @@ -34,7 +34,7 @@ public struct Locale : CustomStringConvertible, CustomDebugStringConvertible, Ha public typealias LanguageDirection = NSLocale.LanguageDirection - private var _wrapped : NSLocale + fileprivate var _wrapped : NSLocale private var _autoupdating : Bool /// Returns a locale which tracks the user's current preferences. @@ -61,7 +61,7 @@ public struct Locale : CustomStringConvertible, CustomDebugStringConvertible, Ha _autoupdating = false } - private init(reference: NSLocale) { + fileprivate init(reference: NSLocale) { _wrapped = reference.copy() as! NSLocale if __NSLocaleIsAutoupdating(reference) { _autoupdating = true diff --git a/stdlib/public/SDK/Foundation/PersonNameComponents.swift b/stdlib/public/SDK/Foundation/PersonNameComponents.swift index dd76d4d149686..5aae334427369 100644 --- a/stdlib/public/SDK/Foundation/PersonNameComponents.swift +++ b/stdlib/public/SDK/Foundation/PersonNameComponents.swift @@ -21,7 +21,7 @@ public struct PersonNameComponents : ReferenceConvertible, Hashable, Equatable, _handle = _MutableHandle(adoptingReference: NSPersonNameComponents()) } - private init(reference: NSPersonNameComponents) { + fileprivate init(reference: NSPersonNameComponents) { _handle = _MutableHandle(reference: reference) } diff --git a/stdlib/public/SDK/Foundation/TimeZone.swift b/stdlib/public/SDK/Foundation/TimeZone.swift index 28572fece282a..eede5266c51df 100644 --- a/stdlib/public/SDK/Foundation/TimeZone.swift +++ b/stdlib/public/SDK/Foundation/TimeZone.swift @@ -33,7 +33,7 @@ internal func __NSTimeZoneCurrent() -> NSTimeZone public struct TimeZone : CustomStringConvertible, CustomDebugStringConvertible, Hashable, Equatable, ReferenceConvertible { public typealias ReferenceType = NSTimeZone - private var _wrapped : NSTimeZone + fileprivate var _wrapped : NSTimeZone private var _autoupdating : Bool /// The time zone currently used by the system. @@ -100,7 +100,7 @@ public struct TimeZone : CustomStringConvertible, CustomDebugStringConvertible, } } - private init(reference: NSTimeZone) { + fileprivate init(reference: NSTimeZone) { if __NSTimeZoneIsAutoupdating(reference) { // we can't copy this or we lose its auto-ness (27048257) // fortunately it's immutable diff --git a/stdlib/public/SDK/Foundation/URL.swift b/stdlib/public/SDK/Foundation/URL.swift index 8d37e084db4e2..aa2c61e32380f 100644 --- a/stdlib/public/SDK/Foundation/URL.swift +++ b/stdlib/public/SDK/Foundation/URL.swift @@ -36,15 +36,15 @@ public struct URLThumbnailSizeKey : RawRepresentable, Hashable { As a convenience, volume resource values can be requested from any file system URL. The value returned will reflect the property value for the volume on which the resource is located. */ public struct URLResourceValues { - private var _values: [URLResourceKey: Any] - private var _keys: Set + fileprivate var _values: [URLResourceKey: Any] + fileprivate var _keys: Set public init() { _values = [:] _keys = [] } - private init(keys: Set, values: [URLResourceKey: Any]) { + fileprivate init(keys: Set, values: [URLResourceKey: Any]) { _values = values _keys = keys } @@ -477,7 +477,7 @@ public struct URLResourceValues { */ public struct URL : ReferenceConvertible, CustomStringConvertible, Equatable { public typealias ReferenceType = NSURL - private var _url : NSURL + fileprivate var _url : NSURL public typealias BookmarkResolutionOptions = NSURL.BookmarkResolutionOptions public typealias BookmarkCreationOptions = NSURL.BookmarkCreationOptions @@ -1130,7 +1130,7 @@ public struct URL : ReferenceConvertible, CustomStringConvertible, Equatable { } } - private init(reference: NSURL) { + fileprivate init(reference: NSURL) { _url = URL._converted(from: reference).copy() as! NSURL } diff --git a/stdlib/public/SDK/Foundation/URLComponents.swift b/stdlib/public/SDK/Foundation/URLComponents.swift index 8236217a51da0..110d95bfc5c7a 100644 --- a/stdlib/public/SDK/Foundation/URLComponents.swift +++ b/stdlib/public/SDK/Foundation/URLComponents.swift @@ -305,7 +305,7 @@ public struct URLComponents : ReferenceConvertible, Hashable, CustomStringConver // MARK: - Bridging - private init(reference: NSURLComponents) { + fileprivate init(reference: NSURLComponents) { _handle = _MutableHandle(reference: reference) } @@ -349,14 +349,14 @@ extension URLComponents : _ObjectiveCBridgeable { public struct URLQueryItem : ReferenceConvertible, Hashable, Equatable, CustomStringConvertible { public typealias ReferenceType = NSURLQueryItem - private var _queryItem : NSURLQueryItem + fileprivate var _queryItem : NSURLQueryItem public init(name: String, value: String?) { _queryItem = NSURLQueryItem(name: name, value: value) } - private init(reference: NSURLQueryItem) { _queryItem = reference.copy() as! NSURLQueryItem } - private var reference : NSURLQueryItem { return _queryItem } + fileprivate init(reference: NSURLQueryItem) { _queryItem = reference.copy() as! NSURLQueryItem } + fileprivate var reference : NSURLQueryItem { return _queryItem } public var name : String { get { return _queryItem.name } diff --git a/stdlib/public/SDK/Foundation/URLRequest.swift b/stdlib/public/SDK/Foundation/URLRequest.swift index ab08b4d47cf6a..d292a58564965 100644 --- a/stdlib/public/SDK/Foundation/URLRequest.swift +++ b/stdlib/public/SDK/Foundation/URLRequest.swift @@ -41,7 +41,7 @@ public struct URLRequest : ReferenceConvertible, CustomStringConvertible, Equata _handle = _MutableHandle(adoptingReference: NSMutableURLRequest(url: url, cachePolicy: cachePolicy, timeoutInterval: timeoutInterval)) } - private init(_bridged request: NSURLRequest) { + fileprivate init(_bridged request: NSURLRequest) { _handle = _MutableHandle(reference: request.mutableCopy() as! NSMutableURLRequest) } diff --git a/stdlib/public/SDK/Foundation/UUID.swift b/stdlib/public/SDK/Foundation/UUID.swift index fb3bebf6435b5..5ab083c25013d 100644 --- a/stdlib/public/SDK/Foundation/UUID.swift +++ b/stdlib/public/SDK/Foundation/UUID.swift @@ -27,7 +27,7 @@ public struct UUID : ReferenceConvertible, Hashable, Equatable, CustomStringConv } } - private init(reference: NSUUID) { + fileprivate init(reference: NSUUID) { var bytes: uuid_t = (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) withUnsafeMutablePointer(&bytes) { reference.getBytes(unsafeBitCast($0, to: UnsafeMutablePointer.self)) @@ -79,7 +79,7 @@ public struct UUID : ReferenceConvertible, Hashable, Equatable, CustomStringConv // MARK: - Bridging Support - private var reference: NSUUID { + fileprivate var reference: NSUUID { var bytes = uuid return withUnsafePointer(&bytes) { return NSUUID(uuidBytes: unsafeBitCast($0, to: UnsafePointer.self)) diff --git a/stdlib/public/SDK/Foundation/UserInfo.swift b/stdlib/public/SDK/Foundation/UserInfo.swift index f6800a110e9a2..a9f8b8ed5fbd5 100644 --- a/stdlib/public/SDK/Foundation/UserInfo.swift +++ b/stdlib/public/SDK/Foundation/UserInfo.swift @@ -46,8 +46,8 @@ internal class _NSUserInfoDictionaryKeyEnumerator : NSEnumerator { } internal class _NSUserInfoDictionary : NSMutableDictionary { - private var _objc = [NSObject : AnyObject]() - private var _swift = [NSObject : Any]() + fileprivate var _objc = [NSObject : AnyObject]() + fileprivate var _swift = [NSObject : Any]() /// Note: these two init methods are the only proper method of construction and must be marked as "convenience" and call super.init() because the overlay has required init methods in extensions private convenience init(objc: [NSObject : AnyObject], swift: [NSObject : Any]) { From c70a5a5d678f24f83f9eeb4ffb71fc224ac43472 Mon Sep 17 00:00:00 2001 From: Jordan Rose Date: Mon, 25 Jul 2016 17:52:21 -0700 Subject: [PATCH 2/4] Default access for many synthesized members to 'internal'. More detail: some members are intended to have the same the access as their containing types. This doesn't fly for SE-0025 'private', which would limit the members to only being accessed from lexically within the type decl, instead anywhere the type itself can be seen. Instead, follow the rule for user-written members---internal by default---and then raise the access level to 'public' if necessary. This affects: - enum cases - deinitializers - protocol requirements - generic parameters - implicit initializers - inherited initializers - derived conformance members - synthesized typealiases for associated types --- lib/Sema/CodeSynthesis.cpp | 13 +++-- .../DerivedConformanceEquatableHashable.cpp | 5 +- .../DerivedConformanceRawRepresentable.cpp | 3 +- lib/Sema/DerivedConformances.cpp | 5 +- lib/Sema/TypeCheckDecl.cpp | 26 ++++----- lib/Sema/TypeCheckGeneric.cpp | 1 + lib/Sema/TypeCheckProtocol.cpp | 9 ++- lib/Serialization/Deserialization.cpp | 6 +- .../complete_override_access_control.swift | 16 +++--- test/attr/accessibility_print.swift | 55 +++++++++++++++---- 10 files changed, 93 insertions(+), 46 deletions(-) diff --git a/lib/Sema/CodeSynthesis.cpp b/lib/Sema/CodeSynthesis.cpp index 0ebcf0a767a5f..2820c569f31ff 100644 --- a/lib/Sema/CodeSynthesis.cpp +++ b/lib/Sema/CodeSynthesis.cpp @@ -1917,9 +1917,9 @@ ConstructorDecl *swift::createImplicitConstructor(TypeChecker &tc, ImplicitConstructorKind ICK) { ASTContext &context = tc.Context; SourceLoc Loc = decl->getLoc(); - Accessibility accessLevel = decl->getFormalAccess(); - if (!decl->hasClangNode()) - accessLevel = std::min(accessLevel, Accessibility::Internal); + auto accessLevel = Accessibility::Internal; + if (decl->hasClangNode()) + accessLevel = std::max(accessLevel, decl->getFormalAccess()); // Determine the parameter type of the implicit constructor. SmallVector params; @@ -2114,8 +2114,11 @@ swift::createDesignatedInitOverride(TypeChecker &tc, /*GenericParams=*/nullptr, classDecl); ctor->setImplicit(); - ctor->setAccessibility(std::min(classDecl->getFormalAccess(), - superclassCtor->getFormalAccess())); + + Accessibility access = classDecl->getFormalAccess(); + access = std::max(access, Accessibility::Internal); + access = std::min(access, superclassCtor->getFormalAccess()); + ctor->setAccessibility(access); // Make sure the constructor is only as available as its superclass's // constructor. diff --git a/lib/Sema/DerivedConformanceEquatableHashable.cpp b/lib/Sema/DerivedConformanceEquatableHashable.cpp index 29343f483a1e4..4ee22b100bbe0 100644 --- a/lib/Sema/DerivedConformanceEquatableHashable.cpp +++ b/lib/Sema/DerivedConformanceEquatableHashable.cpp @@ -417,7 +417,8 @@ deriveHashable_enum_hashValue(TypeChecker &tc, Decl *parentDecl, interfaceType = FunctionType::get(selfType, methodType); getterDecl->setInterfaceType(interfaceType); - getterDecl->setAccessibility(enumDecl->getFormalAccess()); + getterDecl->setAccessibility(std::max(Accessibility::Internal, + enumDecl->getFormalAccess())); // If the enum was not imported, the derived conformance is either from the // enum itself or an extension, in which case we will emit the declaration @@ -433,7 +434,7 @@ deriveHashable_enum_hashValue(TypeChecker &tc, Decl *parentDecl, hashValueDecl->setImplicit(); hashValueDecl->makeComputed(SourceLoc(), getterDecl, nullptr, nullptr, SourceLoc()); - hashValueDecl->setAccessibility(enumDecl->getFormalAccess()); + hashValueDecl->setAccessibility(getterDecl->getFormalAccess()); Pattern *hashValuePat = new (C) NamedPattern(hashValueDecl, /*implicit*/true); hashValuePat->setType(intType); diff --git a/lib/Sema/DerivedConformanceRawRepresentable.cpp b/lib/Sema/DerivedConformanceRawRepresentable.cpp index b3d1704b00f34..31cf1136d412c 100644 --- a/lib/Sema/DerivedConformanceRawRepresentable.cpp +++ b/lib/Sema/DerivedConformanceRawRepresentable.cpp @@ -336,7 +336,8 @@ static ConstructorDecl *deriveRawRepresentable_init(TypeChecker &tc, } initDecl->setInterfaceType(allocIfaceType); initDecl->setInitializerInterfaceType(initIfaceType); - initDecl->setAccessibility(enumDecl->getFormalAccess()); + initDecl->setAccessibility(std::max(Accessibility::Internal, + enumDecl->getFormalAccess())); // If the enum was not imported, the derived conformance is either from the // enum itself or an extension, in which case we will emit the declaration diff --git a/lib/Sema/DerivedConformances.cpp b/lib/Sema/DerivedConformances.cpp index 14ffab7e67b98..8c8b53b369e0c 100644 --- a/lib/Sema/DerivedConformances.cpp +++ b/lib/Sema/DerivedConformances.cpp @@ -151,7 +151,8 @@ FuncDecl *DerivedConformance::declareDerivedPropertyGetter(TypeChecker &tc, } else interfaceType = type; getterDecl->setInterfaceType(interfaceType); - getterDecl->setAccessibility(typeDecl->getFormalAccess()); + getterDecl->setAccessibility(std::max(typeDecl->getFormalAccess(), + Accessibility::Internal)); // If the enum was not imported, the derived conformance is either from the // enum itself or an extension, in which case we will emit the declaration @@ -181,7 +182,7 @@ DerivedConformance::declareDerivedReadOnlyProperty(TypeChecker &tc, propDecl->setImplicit(); propDecl->makeComputed(SourceLoc(), getterDecl, nullptr, nullptr, SourceLoc()); - propDecl->setAccessibility(typeDecl->getFormalAccess()); + propDecl->setAccessibility(getterDecl->getFormalAccess()); propDecl->setInterfaceType(propertyInterfaceType); Pattern *propPat = new (C) NamedPattern(propDecl, /*implicit*/ true); diff --git a/lib/Sema/TypeCheckDecl.cpp b/lib/Sema/TypeCheckDecl.cpp index 173c768f95dd2..cf764b9e66e13 100644 --- a/lib/Sema/TypeCheckDecl.cpp +++ b/lib/Sema/TypeCheckDecl.cpp @@ -1371,7 +1371,7 @@ void TypeChecker::computeAccessibility(ValueDecl *D) { validateAccessibility(generic); Accessibility access = Accessibility::Internal; if (isa(generic)) - access = generic->getFormalAccess(); + access = std::max(access, generic->getFormalAccess()); D->setAccessibility(access); break; } @@ -3522,9 +3522,7 @@ class DeclChecker : public DeclVisitor { assocType->setIsBeingTypeChecked(); TC.checkDeclAttributesEarly(assocType); - if (!assocType->hasAccessibility()) - assocType->setAccessibility(assocType->getProtocol()->getFormalAccess()); - + TC.validateAccessibility(assocType); TC.checkInheritanceClause(assocType); // Check the default definition, if there is one. @@ -5785,12 +5783,7 @@ class DeclChecker : public DeclVisitor { TC.checkDeclAttributesEarly(EED); - - EnumDecl *ED = EED->getParentEnum(); - - if (!EED->hasAccessibility()) - EED->setAccessibility(ED->getFormalAccess()); - + TC.validateAccessibility(EED); EED->setIsBeingTypeChecked(); // Only attempt to validate the argument type or raw value if the element @@ -5811,6 +5804,7 @@ class DeclChecker : public DeclVisitor { // If we have a raw value, make sure there's a raw type as well. if (auto *rawValue = EED->getRawValueExpr()) { + EnumDecl *ED = EED->getParentEnum(); if (!ED->hasRawType()) { TC.diagnose(rawValue->getLoc(),diag::enum_raw_value_without_raw_type); // Recover by setting the raw type as this element's type. @@ -6522,7 +6516,8 @@ void TypeChecker::validateDecl(ValueDecl *D, bool resolveTypeParams) { if (!assocType->hasType()) assocType->computeType(); if (!typeParam->hasAccessibility()) - typeParam->setAccessibility(nominal->getFormalAccess()); + typeParam->setAccessibility(std::max(nominal->getFormalAccess(), + Accessibility::Internal)); break; } @@ -6543,7 +6538,8 @@ void TypeChecker::validateDecl(ValueDecl *D, bool resolveTypeParams) { if (!assocType->hasType()) assocType->computeType(); if (!typeParam->hasAccessibility()) - typeParam->setAccessibility(fn->getFormalAccess()); + typeParam->setAccessibility(std::max(fn->getFormalAccess(), + Accessibility::Internal)); break; } } @@ -6905,7 +6901,8 @@ void TypeChecker::validateAccessibility(ValueDecl *D) { auto assocType = cast(D); auto prot = assocType->getProtocol(); validateAccessibility(prot); - assocType->setAccessibility(prot->getFormalAccess()); + assocType->setAccessibility(std::max(prot->getFormalAccess(), + Accessibility::Internal)); break; } @@ -6928,7 +6925,8 @@ void TypeChecker::validateAccessibility(ValueDecl *D) { } else { auto container = cast(D->getDeclContext()); validateAccessibility(container); - D->setAccessibility(container->getFormalAccess()); + D->setAccessibility(std::max(container->getFormalAccess(), + Accessibility::Internal)); } break; } diff --git a/lib/Sema/TypeCheckGeneric.cpp b/lib/Sema/TypeCheckGeneric.cpp index c159b8896308c..4528de77676f5 100644 --- a/lib/Sema/TypeCheckGeneric.cpp +++ b/lib/Sema/TypeCheckGeneric.cpp @@ -801,6 +801,7 @@ void TypeChecker::finalizeGenericParamList(ArchetypeBuilder &builder, access = nominal->getFormalAccess(); else access = Accessibility::Internal; + access = std::max(access, Accessibility::Internal); // Wire up the archetypes. for (auto GP : *genericParams) { diff --git a/lib/Sema/TypeCheckProtocol.cpp b/lib/Sema/TypeCheckProtocol.cpp index 9f0dc5ae0b617..85eeebd5351eb 100644 --- a/lib/Sema/TypeCheckProtocol.cpp +++ b/lib/Sema/TypeCheckProtocol.cpp @@ -1960,7 +1960,14 @@ void ConformanceChecker::recordTypeWitness(AssociatedTypeDecl *assocType, // Inject the typealias into the nominal decl that conforms to the protocol. if (auto nominal = DC->getAsNominalTypeOrNominalTypeExtensionContext()) { TC.computeAccessibility(nominal); - aliasDecl->setAccessibility(nominal->getFormalAccess()); + // FIXME: Ideally this would use the protocol's access too---that is, + // a typealias added for an internal protocol shouldn't need to be + // public---but that can be problematic if the same type conforms to two + // protocols with different access levels. + Accessibility aliasAccess = nominal->getFormalAccess(); + aliasAccess = std::max(aliasAccess, Accessibility::Internal); + aliasDecl->setAccessibility(aliasAccess); + if (nominal == DC) { nominal->addMember(aliasDecl); } else { diff --git a/lib/Serialization/Deserialization.cpp b/lib/Serialization/Deserialization.cpp index 0f06599e12420..ad28dc5ebbcaf 100644 --- a/lib/Serialization/Deserialization.cpp +++ b/lib/Serialization/Deserialization.cpp @@ -3036,7 +3036,8 @@ Decl *ModuleFile::getDecl(DeclID DID, Optional ForcedContext) { elem->setInterfaceType(interfaceType); if (isImplicit) elem->setImplicit(); - elem->setAccessibility(cast(DC)->getFormalAccess()); + elem->setAccessibility(std::max(cast(DC)->getFormalAccess(), + Accessibility::Internal)); break; } @@ -3203,7 +3204,8 @@ Decl *ModuleFile::getDecl(DeclID DID, Optional ForcedContext) { /*selfpat*/nullptr, DC); declOrOffset = dtor; - dtor->setAccessibility(cast(DC)->getFormalAccess()); + dtor->setAccessibility(std::max(cast(DC)->getFormalAccess(), + Accessibility::Internal)); auto *selfParams = readParameterList(); selfParams->get(0)->setImplicit(); // self is implicit. diff --git a/test/IDE/complete_override_access_control.swift b/test/IDE/complete_override_access_control.swift index fd1a669afe920..166d7d9e71835 100644 --- a/test/IDE/complete_override_access_control.swift +++ b/test/IDE/complete_override_access_control.swift @@ -112,16 +112,16 @@ public class TestPublicABC : ProtocolAPrivate, ProtocolBInternal, ProtocolCPubli // TEST_INTERNAL_ABC: Begin completions, 15 items // TEST_INTERNAL_ABC-DAG: Decl[Constructor]/Super: init(fromProtocolA: TagPA) {|}{{; name=.+$}} -// TEST_INTERNAL_ABC-DAG: Decl[InstanceMethod]/Super: private func protoAFunc(x: TagPA) {|}{{; name=.+$}} -// TEST_INTERNAL_ABC-DAG: Decl[InstanceMethod]/Super: private func protoAFuncOptional(x: TagPA) {|}{{; name=.+$}} +// TEST_INTERNAL_ABC-DAG: Decl[InstanceMethod]/Super: func protoAFunc(x: TagPA) {|}{{; name=.+$}} +// TEST_INTERNAL_ABC-DAG: Decl[InstanceMethod]/Super: func protoAFuncOptional(x: TagPA) {|}{{; name=.+$}} // TEST_INTERNAL_ABC-DAG: Decl[Constructor]/Super: init(fromProtocolB: TagPB) {|}{{; name=.+$}} // TEST_INTERNAL_ABC-DAG: Decl[InstanceMethod]/Super: func protoBFunc(x: TagPB) {|}{{; name=.+$}} // TEST_INTERNAL_ABC-DAG: Decl[InstanceMethod]/Super: func protoBFuncOptional(x: TagPB) {|}{{; name=.+$}} // TEST_INTERNAL_ABC-DAG: Decl[Constructor]/Super: init(fromProtocolC: TagPC) {|}{{; name=.+$}} // TEST_INTERNAL_ABC-DAG: Decl[InstanceMethod]/Super: func protoCFunc(x: TagPC) {|}{{; name=.+$}} // TEST_INTERNAL_ABC-DAG: Decl[InstanceMethod]/Super: func protoCFuncOptional(x: TagPC) {|}{{; name=.+$}} -// TEST_INTERNAL_ABC-DAG: Decl[InstanceVar]/Super: private var protoAVarRW: TagPA -// TEST_INTERNAL_ABC-DAG: Decl[InstanceVar]/Super: private var protoAVarRO: TagPA +// TEST_INTERNAL_ABC-DAG: Decl[InstanceVar]/Super: var protoAVarRW: TagPA +// TEST_INTERNAL_ABC-DAG: Decl[InstanceVar]/Super: var protoAVarRO: TagPA // TEST_INTERNAL_ABC-DAG: Decl[InstanceVar]/Super: var protoBVarRW: TagPB // TEST_INTERNAL_ABC-DAG: Decl[InstanceVar]/Super: var protoBVarRO: TagPB // TEST_INTERNAL_ABC-DAG: Decl[InstanceVar]/Super: var protoCVarRW: TagPC @@ -130,16 +130,16 @@ public class TestPublicABC : ProtocolAPrivate, ProtocolBInternal, ProtocolCPubli // TEST_PUBLIC_ABC: Begin completions, 15 items // TEST_PUBLIC_ABC-DAG: Decl[Constructor]/Super: init(fromProtocolA: TagPA) {|}{{; name=.+$}} -// TEST_PUBLIC_ABC-DAG: Decl[InstanceMethod]/Super: private func protoAFunc(x: TagPA) {|}{{; name=.+$}} -// TEST_PUBLIC_ABC-DAG: Decl[InstanceMethod]/Super: private func protoAFuncOptional(x: TagPA) {|}{{; name=.+$}} +// TEST_PUBLIC_ABC-DAG: Decl[InstanceMethod]/Super: func protoAFunc(x: TagPA) {|}{{; name=.+$}} +// TEST_PUBLIC_ABC-DAG: Decl[InstanceMethod]/Super: func protoAFuncOptional(x: TagPA) {|}{{; name=.+$}} // TEST_PUBLIC_ABC-DAG: Decl[Constructor]/Super: init(fromProtocolB: TagPB) {|}{{; name=.+$}} // TEST_PUBLIC_ABC-DAG: Decl[InstanceMethod]/Super: func protoBFunc(x: TagPB) {|}{{; name=.+$}} // TEST_PUBLIC_ABC-DAG: Decl[InstanceMethod]/Super: func protoBFuncOptional(x: TagPB) {|}{{; name=.+$}} // TEST_PUBLIC_ABC-DAG: Decl[Constructor]/Super: init(fromProtocolC: TagPC) {|}{{; name=.+$}} // TEST_PUBLIC_ABC-DAG: Decl[InstanceMethod]/Super: public func protoCFunc(x: TagPC) {|}{{; name=.+$}} // TEST_PUBLIC_ABC-DAG: Decl[InstanceMethod]/Super: public func protoCFuncOptional(x: TagPC) {|}{{; name=.+$}} -// TEST_PUBLIC_ABC-DAG: Decl[InstanceVar]/Super: private var protoAVarRW: TagPA -// TEST_PUBLIC_ABC-DAG: Decl[InstanceVar]/Super: private var protoAVarRO: TagPA +// TEST_PUBLIC_ABC-DAG: Decl[InstanceVar]/Super: var protoAVarRW: TagPA +// TEST_PUBLIC_ABC-DAG: Decl[InstanceVar]/Super: var protoAVarRO: TagPA // TEST_PUBLIC_ABC-DAG: Decl[InstanceVar]/Super: var protoBVarRW: TagPB // TEST_PUBLIC_ABC-DAG: Decl[InstanceVar]/Super: var protoBVarRO: TagPB // TEST_PUBLIC_ABC-DAG: Decl[InstanceVar]/Super: public var protoCVarRW: TagPC diff --git a/test/attr/accessibility_print.swift b/test/attr/accessibility_print.swift index d29e0ec3f32f2..608833220c017 100644 --- a/test/attr/accessibility_print.swift +++ b/test/attr/accessibility_print.swift @@ -29,8 +29,8 @@ struct BA_DefaultStruct { private struct BB_PrivateStruct { // CHECK: internal var x var x = 0 - // CHECK: private init(x: Int) - // CHECK: private init() + // CHECK: internal init(x: Int) + // CHECK: internal init() } // CHECK: {{^[}]}} // CHECK-LABEL: internal{{(\*/)?}} struct BC_InternalStruct { @@ -60,8 +60,8 @@ public struct BE_PublicStructPrivateMembers { fileprivate struct BF_FilePrivateStruct { // CHECK: {{^}} internal var x var x = 0 - // CHECK: {{^}} fileprivate init(x: Int) - // CHECK: {{^}} fileprivate init() + // CHECK: {{^}} internal init(x: Int) + // CHECK: {{^}} internal init() } // CHECK: {{^[}]}} @@ -69,7 +69,7 @@ fileprivate struct BF_FilePrivateStruct { private class CA_PrivateClass { // CHECK: {{^}} deinit deinit {} - // CHECK: private init() + // CHECK: internal init() } // CHECK: {{^[}]}} // CHECK-LABEL: internal{{(\*/)?}} class CB_InternalClass @@ -94,7 +94,7 @@ private enum DA_PrivateEnum { case Foo, Bar // CHECK: internal init() init() { self = .Foo } - // CHECK: private var hashValue + // CHECK: internal var hashValue } // CHECK: {{^[}]}} // CHECK-LABEL: internal{{(\*/)?}} enum DB_InternalEnum { @@ -122,9 +122,9 @@ public enum DC_PublicEnum { private protocol EA_PrivateProtocol { // CHECK: {{^}} associatedtype Foo associatedtype Foo - // CHECK: private var Bar + // CHECK: internal var Bar var Bar: Int { get } - // CHECK: private func baz() + // CHECK: internal func baz() func baz() } // CHECK: {{^[}]}} @@ -325,19 +325,52 @@ extension HC_PrivateProtocol where Assoc == HC_PrivateStruct { func constrained() {} } // CHECK: {{^[}]}} +public protocol IA_PublicAssocTypeProto { + associatedtype PublicValue + var publicValue: PublicValue { get } +} +fileprivate protocol IB_FilePrivateAssocTypeProto { + associatedtype FilePrivateValue + var filePrivateValue: FilePrivateValue { get } +} +// CHECK-LABEL: public{{(\*/)?}} class IC_PublicAssocTypeImpl : IA_PublicAssocTypeProto, IB_FilePrivateAssocTypeProto { +public class IC_PublicAssocTypeImpl: IA_PublicAssocTypeProto, IB_FilePrivateAssocTypeProto { + public var publicValue: Int = 0 + public var filePrivateValue: Int = 0 + // CHECK-DAG: {{^}} public typealias PublicValue + // CHECK-DAG: {{^}} public typealias FilePrivateValue +} // CHECK: {{^[}]}} + +// CHECK-LABEL: private{{(\*/)?}} class ID_PrivateAssocTypeImpl : IA_PublicAssocTypeProto, IB_FilePrivateAssocTypeProto { +private class ID_PrivateAssocTypeImpl: IA_PublicAssocTypeProto, IB_FilePrivateAssocTypeProto { + public var publicValue: Int = 0 + public var filePrivateValue: Int = 0 + // CHECK-DAG: {{^}} internal typealias PublicValue + // CHECK-DAG: {{^}} internal typealias FilePrivateValue +} // CHECK: {{^[}]}} + // CHECK-LABEL: class MultipleAttributes { class MultipleAttributes { // CHECK: {{^}} final {{(/\*)?private(\*/)?}} func foo() final private func foo() {} -} +} // CHECK: {{^[}]}} // CHECK-LABEL: public{{(\*/)?}} class PublicInitBase { public class PublicInitBase { // CHECK: {{^}} {{(/\*)?public(\*/)?}} init() public init() {} -} + // CHECK: {{^}} {{(/\*)?fileprivate(\*/)?}} init(other: PublicInitBase) + fileprivate init(other: PublicInitBase) {} +} // CHECK: {{^[}]}} // CHECK-LABEL: public{{(\*/)?}} class PublicInitInheritor : PublicInitBase { public class PublicInitInheritor : PublicInitBase { // CHECK: {{^}} public init() -} + // CHECK: {{^}} fileprivate init(other: PublicInitBase) +} // CHECK: {{^[}]}} + +// CHECK-LABEL: {{(/\*)?private(\*/)?}} class PublicInitPrivateInheritor : PublicInitBase { +private class PublicInitPrivateInheritor : PublicInitBase { + // CHECK: {{^}} internal init() + // CHECK: {{^}} fileprivate init(other: PublicInitBase) +} // CHECK: {{^[}]}} From e4118f19c8bf5676c750d6c15fa46cad41e4f4a3 Mon Sep 17 00:00:00 2001 From: Jordan Rose Date: Mon, 25 Jul 2016 17:54:14 -0700 Subject: [PATCH 3/4] Port tests to 'fileprivate'. Similar to apple/swift#3753. Groundwork for SE-0025 ('private' and 'fileprivate'). No intended functionality change. --- test/1_stdlib/Runtime.swift.gyb | 2 +- test/DebugInfo/typealias.swift | 4 ++-- test/IRGen/method_linkage.swift | 12 +++++------ test/Inputs/ObjCBridging/Appliances.swift | 2 +- .../Inputs/testability_helper.swift | 2 +- .../Inputs/mangling_private_helper.swift | 2 +- test/SILGen/objc_extensions.swift | 2 +- .../Inputs/devirt_access_other_module.swift | 2 +- test/SILOptimizer/dead_func_init_method.sil | 2 +- test/SILOptimizer/devirt_access.swift | 6 +++--- .../devirt_access_other_module.swift | 6 +++--- test/SILOptimizer/let_properties_opts.swift | 20 +++++++++---------- .../let_properties_opts_runtime.swift | 2 +- 13 files changed, 32 insertions(+), 32 deletions(-) diff --git a/test/1_stdlib/Runtime.swift.gyb b/test/1_stdlib/Runtime.swift.gyb index 1c137b4740f29..d30da591778b4 100644 --- a/test/1_stdlib/Runtime.swift.gyb +++ b/test/1_stdlib/Runtime.swift.gyb @@ -287,7 +287,7 @@ extension Int { var description: String { return "abc" } } - private class PrivateExtensionClassConformsToP2 : P2 { + fileprivate class PrivateExtensionClassConformsToP2 : P2 { var description: String { return "def" } } } diff --git a/test/DebugInfo/typealias.swift b/test/DebugInfo/typealias.swift index 812c6ed1763a6..7ab2b6f99e5ec 100644 --- a/test/DebugInfo/typealias.swift +++ b/test/DebugInfo/typealias.swift @@ -8,8 +8,8 @@ class DWARF { typealias DIEOffset = UInt32 // CHECK-DAG: ![[VOID:.*]] = !DICompositeType({{.*}}identifier: "_TtT_" // CHECK-DAG: ![[PRIVATETYPE:.*]] = !DIDerivedType(tag: DW_TAG_typedef, name: "_TtaC9typealias5DWARFP{{.+}}11PrivateType",{{.*}} line: [[@LINE+1]], baseType: ![[VOID]]) - private typealias PrivateType = () - private static func usePrivateType() -> PrivateType { return () } + fileprivate typealias PrivateType = () + fileprivate static func usePrivateType() -> PrivateType { return () } } func main () { diff --git a/test/IRGen/method_linkage.swift b/test/IRGen/method_linkage.swift index edb6d4f1dbe60..39054dd7c2660 100644 --- a/test/IRGen/method_linkage.swift +++ b/test/IRGen/method_linkage.swift @@ -7,37 +7,37 @@ class Base { // CHECK: define hidden void @_TFC14method_linkage4Base{{.*}}3foofT_T_ @inline(never) - private func foo() { + fileprivate func foo() { } // CHECK: define internal void @_TFC14method_linkage4Base{{.*}}3barfT_T_ @inline(never) - private final func bar() { + fileprivate final func bar() { } // CHECK: define hidden void @_TFC14method_linkage4Base{{.*}}5otherfT_T_ @inline(never) - private func other() { + fileprivate func other() { } } class Derived : Base { // CHECK: define hidden void @_TFC14method_linkage7Derived{{.*}}3foofT_T_ @inline(never) - private final override func foo() { + fileprivate final override func foo() { } } extension Base { // CHECK: define internal void @_TFC14method_linkage4Base{{.*}}7extfuncfT_T_ @inline(never) - private func extfunc() { + fileprivate func extfunc() { } } public class PublicClass { // CHECK: define{{( protected)?}} void @_TFC14method_linkage11PublicClass{{.*}}4pfoofT_T_ @inline(never) - private func pfoo() { + fileprivate func pfoo() { } // CHECK: define{{( protected)?}} void @_TFC14method_linkage11PublicClass4pbarfT_T_ diff --git a/test/Inputs/ObjCBridging/Appliances.swift b/test/Inputs/ObjCBridging/Appliances.swift index e45d6092af2f6..89ca952d19707 100644 --- a/test/Inputs/ObjCBridging/Appliances.swift +++ b/test/Inputs/ObjCBridging/Appliances.swift @@ -35,7 +35,7 @@ extension Refrigerator : _ObjectiveCBridgeable { } public struct ManufacturerInfo { - private var impl: APPManufacturerInfo + fileprivate var impl: APPManufacturerInfo public var value: DataType { return impl.value } diff --git a/test/Interpreter/Inputs/testability_helper.swift b/test/Interpreter/Inputs/testability_helper.swift index a0d3c0ffeab4c..53d8ecaf69e5d 100644 --- a/test/Interpreter/Inputs/testability_helper.swift +++ b/test/Interpreter/Inputs/testability_helper.swift @@ -9,7 +9,7 @@ internal class Base : CustomStringConvertible { var description: String { return "instance \(id)" } - private func privateFn() -> String { + fileprivate func privateFn() -> String { return "private \(id)" } func callPrivate() -> String { diff --git a/test/SILGen/Inputs/mangling_private_helper.swift b/test/SILGen/Inputs/mangling_private_helper.swift index a50fdfc46ba58..4acd5df101f1b 100644 --- a/test/SILGen/Inputs/mangling_private_helper.swift +++ b/test/SILGen/Inputs/mangling_private_helper.swift @@ -1,5 +1,5 @@ public class Base { - private func privateMethod() {} + fileprivate func privateMethod() {} } // Demonstrate the need for a vtable entry for privateMethod(). diff --git a/test/SILGen/objc_extensions.swift b/test/SILGen/objc_extensions.swift index d743879681732..f121a0f6c22f5 100644 --- a/test/SILGen/objc_extensions.swift +++ b/test/SILGen/objc_extensions.swift @@ -60,7 +60,7 @@ extension SubSub { // SR-1025 extension Base { - private static var x = 1 + fileprivate static var x = 1 } // CHECK-LABEL: sil hidden @_TF15objc_extensions19testStaticVarAccessFT_T_ diff --git a/test/SILOptimizer/Inputs/devirt_access_other_module.swift b/test/SILOptimizer/Inputs/devirt_access_other_module.swift index dc72453b5b5d9..4507fc449d90b 100644 --- a/test/SILOptimizer/Inputs/devirt_access_other_module.swift +++ b/test/SILOptimizer/Inputs/devirt_access_other_module.swift @@ -1,5 +1,5 @@ public class ExternalClass { - private func foo() {} + fileprivate func foo() {} } public func getExternalClass() -> ExternalClass { diff --git a/test/SILOptimizer/dead_func_init_method.sil b/test/SILOptimizer/dead_func_init_method.sil index 06e5829594a6f..6d2dc2f805c93 100644 --- a/test/SILOptimizer/dead_func_init_method.sil +++ b/test/SILOptimizer/dead_func_init_method.sil @@ -9,7 +9,7 @@ import Swift import SwiftShims private class Base { - private init() + init() } private class Derived : Base { diff --git a/test/SILOptimizer/devirt_access.swift b/test/SILOptimizer/devirt_access.swift index 28a26fbbe0332..a3c55529c6711 100644 --- a/test/SILOptimizer/devirt_access.swift +++ b/test/SILOptimizer/devirt_access.swift @@ -40,7 +40,7 @@ func testInternalPrivate() { class LocalInternalClass { - private func foo() {} + fileprivate func foo() {} func bar() {} } @@ -72,8 +72,8 @@ func testLocalPrivate() { private class PrivateClass { - private func foo() {} - private func bar() {} + fileprivate func foo() {} + fileprivate func bar() {} } private class LocalPrivateSubclass : PrivateClass { diff --git a/test/SILOptimizer/devirt_access_other_module.swift b/test/SILOptimizer/devirt_access_other_module.swift index e1da65b725ad9..dea6075041c29 100644 --- a/test/SILOptimizer/devirt_access_other_module.swift +++ b/test/SILOptimizer/devirt_access_other_module.swift @@ -8,7 +8,7 @@ // rdar://21408247 public class ExternalClass { - private func foo() {} + fileprivate func foo() {} } public func getExternalClass() -> ExternalClass { @@ -32,9 +32,9 @@ public func getExternalClass() -> ExternalClass { } private class PrivateSubclass : ExternalClass { - override private func foo() {} + override fileprivate func foo() {} } internal class InternalSubclass : ExternalClass { - override private func foo() {} + override fileprivate func foo() {} } diff --git a/test/SILOptimizer/let_properties_opts.swift b/test/SILOptimizer/let_properties_opts.swift index 8181196aab7f9..d485d2b700e42 100644 --- a/test/SILOptimizer/let_properties_opts.swift +++ b/test/SILOptimizer/let_properties_opts.swift @@ -3,14 +3,14 @@ // Test propagation of non-static let properties with compile-time constant values. -// TODO: Once this optimization can remove the propagated private/internal let properties or +// TODO: Once this optimization can remove the propagated fileprivate/internal let properties or // mark them as ones without a storage, new tests should be added here to check for this // functionality. // FIXME: This test is written in Swift instead of SIL, because there are some problems // with SIL deserialization (rdar://22636911) -// Check that initializers do not contain a code to initialize private or +// Check that initializers do not contain a code to initialize fileprivate or // internal (if used with WMO) properties, because their values are propagated into // their uses and they cannot be accessed from other modules. Therefore the // initialization code could be removed. @@ -36,7 +36,7 @@ // CHECK-WMO: ref_element_addr %{{[0-9]+}} : $Foo, #Foo.Prop3 // CHECK-WMO: return -// Check that initializers do not contain a code to initialize private properties, +// Check that initializers do not contain a code to initialize fileprivate properties, // because their values are propagated into their uses and they cannot be accessed // from other modules. Therefore the initialization code could be removed. // Specifically, the initialization code for Prop2 can be removed. @@ -58,7 +58,7 @@ public class Foo { public let Prop0: Int32 = 1 let Prop1: Int32 = 1 + 4/2 + 8 - private let Prop2: Int32 = 3*7 + fileprivate let Prop2: Int32 = 3*7 internal let Prop3: Int32 = 4*8 public init(i:Int32) {} public init(i:Int64) {} @@ -66,7 +66,7 @@ public class Foo { public class Foo1 { let Prop1: Int32 - private let Prop2: Int32 = 3*7 + fileprivate let Prop2: Int32 = 3*7 internal let Prop3: Int32 = 4*8 public init(i:Int32) { Prop1 = 11 @@ -80,7 +80,7 @@ public class Foo1 { public struct Boo { public let Prop0: Int32 = 1 let Prop1: Int32 = 1 + 4/2 + 8 - private let Prop2: Int32 = 3*7 + fileprivate let Prop2: Int32 = 3*7 internal let Prop3: Int32 = 4*8 public init(i:Int32) {} public init(i:Int64) {} @@ -104,7 +104,7 @@ struct Boo3 { //public let Prop0: Int32 let Prop1: Int32 - private let Prop2: Int32 + fileprivate let Prop2: Int32 internal let Prop3: Int32 @inline(__always) @@ -149,12 +149,12 @@ public struct StructWithPublicAndInternalLetProperties { } // The initializer of this struct cannot be defined elsewhere, -// because it contains a private stored property, which is +// because it contains a fileprivate stored property, which is // impossible to initialize outside of this file. public struct StructWithPublicAndInternalAndPrivateLetProperties { public let Prop0: Int32 internal let Prop1: Int32 - private let Prop2: Int32 + fileprivate let Prop2: Int32 init(_ v: Int32, _ u: Int32) { Prop0 = 10 @@ -326,7 +326,7 @@ public func testStructPropertyAccessibility(_ b: StructWithPublicAndInternalLetP } // Properties can be initialized only in this file, because one of the -// properties is private. +// properties is fileprivate. // Therefore their values are known and can be propagated. // CHECK: sil @_TF19let_properties_opts31testStructPropertyAccessibilityFVS_50StructWithPublicAndInternalAndPrivateLetPropertiesVs5Int32 diff --git a/test/SILOptimizer/let_properties_opts_runtime.swift b/test/SILOptimizer/let_properties_opts_runtime.swift index bd3e8358a8ddb..74f6f3c759c69 100644 --- a/test/SILOptimizer/let_properties_opts_runtime.swift +++ b/test/SILOptimizer/let_properties_opts_runtime.swift @@ -56,7 +56,7 @@ struct Boo3 { //public let Prop0: Int32 let Prop1: Int32 - private let Prop2: Int32 + fileprivate let Prop2: Int32 internal let Prop3: Int32 @inline(__always) From ebdee214640f2a8c7d65f003a0bfff49e8f7b2b7 Mon Sep 17 00:00:00 2001 From: Jordan Rose Date: Mon, 25 Jul 2016 17:55:25 -0700 Subject: [PATCH 4/4] Handle top-level vars' and closure params' access specially. Parameters are normally given 'private' access, because they can only be referred to within the body of the owning function. However, single-expression closures allow a parameter to appear in a constraint system in the containing context. Mark closure parameters as 'fileprivate' instead. Similarly, 'private' at the top level is normally equivalent to 'fileprivate', but not for a decl that appears within top-level imperative code, which has a TopLevelCodeDecl context. This currently only happens for bindings in a top-level 'guard' statement; mark these variables and constants as 'fileprivate' as well. More progress on SE-0025 ('private' and 'fileprivate'). --- lib/Sema/TypeCheckDecl.cpp | 16 ++++++++++++++-- test/expr/capture/top-level-guard.swift | 2 +- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/lib/Sema/TypeCheckDecl.cpp b/lib/Sema/TypeCheckDecl.cpp index cf764b9e66e13..db3eccc06dbce 100644 --- a/lib/Sema/TypeCheckDecl.cpp +++ b/lib/Sema/TypeCheckDecl.cpp @@ -1354,10 +1354,22 @@ void TypeChecker::computeAccessibility(ValueDecl *D) { if (!D->hasAccessibility()) { DeclContext *DC = D->getDeclContext(); switch (DC->getContextKind()) { - case DeclContextKind::SerializedLocal: + case DeclContextKind::TopLevelCodeDecl: + // Variables declared in a top-level 'guard' statement can be accessed in + // later top-level code. + D->setAccessibility(Accessibility::FilePrivate); + break; case DeclContextKind::AbstractClosureExpr: + if (isa(D)) { + // Closure parameters may need to be accessible to the enclosing + // context, for single-expression closures. + D->setAccessibility(Accessibility::FilePrivate); + } else { + D->setAccessibility(Accessibility::Private); + } + break; + case DeclContextKind::SerializedLocal: case DeclContextKind::Initializer: - case DeclContextKind::TopLevelCodeDecl: case DeclContextKind::AbstractFunctionDecl: case DeclContextKind::SubscriptDecl: D->setAccessibility(Accessibility::Private); diff --git a/test/expr/capture/top-level-guard.swift b/test/expr/capture/top-level-guard.swift index 352230344a9ed..e68bbc04930d9 100644 --- a/test/expr/capture/top-level-guard.swift +++ b/test/expr/capture/top-level-guard.swift @@ -39,7 +39,7 @@ let closureCapture: () -> Void = { [x] in } // CHECK-LABEL: (defer_stmt -// CHECK-NEXT: (func_decl implicit "$defer()" type='() -> ()' access=private captures=(x) +// CHECK-NEXT: (func_decl implicit "$defer()" type='() -> ()' access=fileprivate captures=(x) defer { _ = x }