Skip to content

More progress on SE-0025 ('private' and 'fileprivate') #3755

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 4 commits into from
Jul 26, 2016
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
13 changes: 8 additions & 5 deletions lib/Sema/CodeSynthesis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<ParamDecl*, 8> params;
Expand Down Expand Up @@ -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.
Expand Down
5 changes: 3 additions & 2 deletions lib/Sema/DerivedConformanceEquatableHashable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
Expand Down
3 changes: 2 additions & 1 deletion lib/Sema/DerivedConformanceRawRepresentable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions lib/Sema/DerivedConformances.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down
42 changes: 26 additions & 16 deletions lib/Sema/TypeCheckDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<ParamDecl>(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);
Expand All @@ -1371,7 +1383,7 @@ void TypeChecker::computeAccessibility(ValueDecl *D) {
validateAccessibility(generic);
Accessibility access = Accessibility::Internal;
if (isa<ProtocolDecl>(generic))
access = generic->getFormalAccess();
access = std::max(access, generic->getFormalAccess());
D->setAccessibility(access);
break;
}
Expand Down Expand Up @@ -3522,9 +3534,7 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
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.
Expand Down Expand Up @@ -5785,12 +5795,7 @@ class DeclChecker : public DeclVisitor<DeclChecker> {


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
Expand All @@ -5811,6 +5816,7 @@ class DeclChecker : public DeclVisitor<DeclChecker> {

// 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.
Expand Down Expand Up @@ -6522,7 +6528,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;
}

Expand All @@ -6543,7 +6550,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;
}
}
Expand Down Expand Up @@ -6905,7 +6913,8 @@ void TypeChecker::validateAccessibility(ValueDecl *D) {
auto assocType = cast<AssociatedTypeDecl>(D);
auto prot = assocType->getProtocol();
validateAccessibility(prot);
assocType->setAccessibility(prot->getFormalAccess());
assocType->setAccessibility(std::max(prot->getFormalAccess(),
Accessibility::Internal));
break;
}

Expand All @@ -6928,7 +6937,8 @@ void TypeChecker::validateAccessibility(ValueDecl *D) {
} else {
auto container = cast<NominalTypeDecl>(D->getDeclContext());
validateAccessibility(container);
D->setAccessibility(container->getFormalAccess());
D->setAccessibility(std::max(container->getFormalAccess(),
Accessibility::Internal));
}
break;
}
Expand Down
1 change: 1 addition & 0 deletions lib/Sema/TypeCheckGeneric.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
9 changes: 8 additions & 1 deletion lib/Sema/TypeCheckProtocol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
6 changes: 4 additions & 2 deletions lib/Serialization/Deserialization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3036,7 +3036,8 @@ Decl *ModuleFile::getDecl(DeclID DID, Optional<DeclContext *> ForcedContext) {
elem->setInterfaceType(interfaceType);
if (isImplicit)
elem->setImplicit();
elem->setAccessibility(cast<EnumDecl>(DC)->getFormalAccess());
elem->setAccessibility(std::max(cast<EnumDecl>(DC)->getFormalAccess(),
Accessibility::Internal));

break;
}
Expand Down Expand Up @@ -3203,7 +3204,8 @@ Decl *ModuleFile::getDecl(DeclID DID, Optional<DeclContext *> ForcedContext) {
/*selfpat*/nullptr, DC);
declOrOffset = dtor;

dtor->setAccessibility(cast<ClassDecl>(DC)->getFormalAccess());
dtor->setAccessibility(std::max(cast<ClassDecl>(DC)->getFormalAccess(),
Accessibility::Internal));
auto *selfParams = readParameterList();
selfParams->get(0)->setImplicit(); // self is implicit.

Expand Down
4 changes: 2 additions & 2 deletions stdlib/public/SDK/Dispatch/Data.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand All @@ -38,7 +38,7 @@ public struct DispatchData : RandomAccessCollection, _ObjectiveCBridgeable {
}
}

private var __wrapped: __DispatchData
fileprivate var __wrapped: __DispatchData

/// Initialize a `Data` with copied memory content.
///
Expand Down
2 changes: 1 addition & 1 deletion stdlib/public/SDK/Dispatch/Queue.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions stdlib/public/SDK/Dispatch/Time.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion stdlib/public/SDK/Foundation/Boxing.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<MutableType : NSObject where MutableType : NSCopying> {
private var _pointer : MutableType
fileprivate var _pointer : MutableType

init(reference : MutableType) {
_pointer = reference.copy() as! MutableType
Expand Down
2 changes: 1 addition & 1 deletion stdlib/public/SDK/Foundation/Calendar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion stdlib/public/SDK/Foundation/CharacterSet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion stdlib/public/SDK/Foundation/Data.swift
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public struct Data : ReferenceConvertible, CustomStringConvertible, Equatable, H
/// A custom deallocator.
case custom((UnsafeMutablePointer<UInt8>, Int) -> Void)

private var _deallocator : ((UnsafeMutablePointer<Void>, Int) -> Void)? {
fileprivate var _deallocator : ((UnsafeMutablePointer<Void>, Int) -> Void)? {
switch self {
case .virtualMemory:
return { __NSDataInvokeDeallocatorVM($0, $1) }
Expand Down
2 changes: 1 addition & 1 deletion stdlib/public/SDK/Foundation/Date.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion stdlib/public/SDK/Foundation/DateComponents.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
4 changes: 2 additions & 2 deletions stdlib/public/SDK/Foundation/IndexPath.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
Expand All @@ -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)
}
Expand Down
Loading