Skip to content

Annotate Foundation overlay with __shared/__owned #19143

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
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
2 changes: 1 addition & 1 deletion stdlib/public/SDK/Foundation/AffineTransform.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public struct AffineTransform : ReferenceConvertible, Hashable, CustomStringConv
self.tY = tY
}

fileprivate init(reference: NSAffineTransform) {
fileprivate init(reference: __shared NSAffineTransform) {
m11 = reference.transformStruct.m11
m12 = reference.transformStruct.m12
m21 = reference.transformStruct.m21
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 @@ -19,7 +19,7 @@ internal final class _MutableHandle<MutableType : NSObject>
where MutableType : NSCopying {
fileprivate var _pointer : MutableType

init(reference : MutableType) {
init(reference : __shared MutableType) {
_pointer = reference.copy() as! MutableType
}

Expand Down
4 changes: 2 additions & 2 deletions stdlib/public/SDK/Foundation/Calendar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public struct Calendar : Hashable, Equatable, ReferenceConvertible, _MutableBoxi
/// Returns a new Calendar.
///
/// - parameter identifier: The kind of calendar to use.
public init(identifier: Identifier) {
public init(identifier: __shared Identifier) {
let result = __NSCalendarCreate(Calendar._toNSCalendarIdentifier(identifier))
_handle = _MutableHandle(adoptingReference: result as! NSCalendar)
_autoupdating = false
Expand All @@ -106,7 +106,7 @@ public struct Calendar : Hashable, Equatable, ReferenceConvertible, _MutableBoxi
// MARK: -
// MARK: Bridging

fileprivate init(reference : NSCalendar) {
fileprivate init(reference : __shared NSCalendar) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one should be owned, no? Since you're immediately going to store it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Although it's not a public entry point anyway, so it doesn't really matter.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_MutableHandle makes a copy of the reference given to it so we never store the original — I've propagated __shared out to every init(reference:) which uses this, per @eeckstein's recommendation.

_handle = _MutableHandle(reference: reference)
if __NSCalendarIsAutoupdating(reference) {
_autoupdating = true
Expand Down
10 changes: 5 additions & 5 deletions stdlib/public/SDK/Foundation/CharacterSet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -390,23 +390,23 @@ public struct CharacterSet : ReferenceConvertible, Equatable, Hashable, SetAlgeb
/// Initialize with the characters in the given string.
///
/// - parameter string: The string content to inspect for characters.
public init(charactersIn string: String) {
public init(charactersIn string: __shared String) {
_storage = _CharacterSetStorage(immutableReference: CFCharacterSetCreateWithCharactersInString(nil, string as CFString))
}

/// Initialize with a bitmap representation.
///
/// This method is useful for creating a character set object with data from a file or other external data source.
/// - parameter data: The bitmap representation.
public init(bitmapRepresentation data: Data) {
public init(bitmapRepresentation data: __shared Data) {
_storage = _CharacterSetStorage(immutableReference: CFCharacterSetCreateWithBitmapRepresentation(nil, data as CFData))
}

/// Initialize with the contents of a file.
///
/// Returns `nil` if there was an error reading the file.
/// - parameter file: The file to read.
public init?(contentsOfFile file: String) {
public init?(contentsOfFile file: __shared String) {
do {
let data = try Data(contentsOf: URL(fileURLWithPath: file), options: .mappedIfSafe)
_storage = _CharacterSetStorage(immutableReference: CFCharacterSetCreateWithBitmapRepresentation(nil, data as CFData))
Expand All @@ -415,7 +415,7 @@ public struct CharacterSet : ReferenceConvertible, Equatable, Hashable, SetAlgeb
}
}

fileprivate init(_bridged characterSet: NSCharacterSet) {
fileprivate init(_bridged characterSet: __shared NSCharacterSet) {
_storage = _CharacterSetStorage(immutableReference: characterSet.copy() as! CFCharacterSet)
}

Expand All @@ -427,7 +427,7 @@ public struct CharacterSet : ReferenceConvertible, Equatable, Hashable, SetAlgeb
_storage = _uncopiedStorage
}

fileprivate init(_builtIn: CFCharacterSetPredefinedSet) {
fileprivate init(_builtIn: __shared CFCharacterSetPredefinedSet) {
_storage = _CharacterSetStorage(immutableReference: CFCharacterSetGetPredefined(_builtIn))
}

Expand Down
8 changes: 4 additions & 4 deletions stdlib/public/SDK/Foundation/Data.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1178,7 +1178,7 @@ public struct Data : ReferenceConvertible, Equatable, Hashable, RandomAccessColl
/// - parameter url: The `URL` to read.
/// - parameter options: Options for the read operation. Default value is `[]`.
/// - throws: An error in the Cocoa domain, if `url` cannot be read.
public init(contentsOf url: URL, options: Data.ReadingOptions = []) throws {
public init(contentsOf url: __shared URL, options: Data.ReadingOptions = []) throws {
let d = try NSData(contentsOf: url, options: ReadingOptions(rawValue: options.rawValue))
_backing = _DataStorage(immutableReference: d, offset: 0)
_sliceRange = 0..<d.length
Expand All @@ -1189,7 +1189,7 @@ public struct Data : ReferenceConvertible, Equatable, Hashable, RandomAccessColl
/// Returns nil when the input is not recognized as valid Base-64.
/// - parameter base64String: The string to parse.
/// - parameter options: Encoding options. Default value is `[]`.
public init?(base64Encoded base64String: String, options: Data.Base64DecodingOptions = []) {
public init?(base64Encoded base64String: __shared String, options: Data.Base64DecodingOptions = []) {
if let d = NSData(base64Encoded: base64String, options: Base64DecodingOptions(rawValue: options.rawValue)) {
_backing = _DataStorage(immutableReference: d, offset: 0)
_sliceRange = 0..<d.length
Expand All @@ -1204,7 +1204,7 @@ public struct Data : ReferenceConvertible, Equatable, Hashable, RandomAccessColl
///
/// - parameter base64Data: Base-64, UTF-8 encoded input data.
/// - parameter options: Decoding options. Default value is `[]`.
public init?(base64Encoded base64Data: Data, options: Data.Base64DecodingOptions = []) {
public init?(base64Encoded base64Data: __shared Data, options: Data.Base64DecodingOptions = []) {
if let d = NSData(base64Encoded: base64Data, options: Base64DecodingOptions(rawValue: options.rawValue)) {
_backing = _DataStorage(immutableReference: d, offset: 0)
_sliceRange = 0..<d.length
Expand All @@ -1220,7 +1220,7 @@ public struct Data : ReferenceConvertible, Equatable, Hashable, RandomAccessColl
/// If the resulting value is mutated, then `Data` will invoke the `mutableCopy()` function on the reference to copy the contents. You may customize the behavior of that function if you wish to return a specialized mutable subclass.
///
/// - parameter reference: The instance of `NSData` that you wish to wrap. This instance will be copied by `struct Data`.
public init(referencing reference: NSData) {
public init(referencing reference: __shared NSData) {
#if DEPLOYMENT_RUNTIME_SWIFT
let providesConcreteBacking = reference._providesConcreteBacking()
#else
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 @@ -269,7 +269,7 @@ public struct DateComponents : ReferenceConvertible, Hashable, Equatable, _Mutab

// MARK: - Bridging Helpers

fileprivate init(reference: NSDateComponents) {
fileprivate init(reference: __shared NSDateComponents) {
_handle = _MutableHandle(reference: reference)
}

Expand Down
2 changes: 1 addition & 1 deletion stdlib/public/SDK/Foundation/Decimal.swift
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,7 @@ extension Decimal {
}

extension Decimal : CustomStringConvertible {
public init?(string: String, locale: Locale? = nil) {
public init?(string: __shared String, locale: __shared Locale? = nil) {
let scan = Scanner(string: string)
var theDecimal = Decimal()
scan.locale = locale
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
}
}

mutating func append(contentsOf other: [Int]) {
mutating func append(contentsOf other: __owned [Int]) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this probably isn't necessary since it's just [Int]

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, Array of Int is not a trivial type, i.e. involves reference counting of the array buffer.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, but IndexPath doesn't gain anything by taking the array owned, since it's not going to be able to use that buffer for storage, and the type isn't generic.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right, assuming that the case where self is empty and size of other > 2 is not the common case, it's better to pass other as guaranteed.
(I misread airspeedswift's comment: I though he meant that it's not worth annotating because of the parameter type).

switch self {
case .empty:
switch other.count {
Expand Down Expand Up @@ -684,7 +684,7 @@ public struct IndexPath : ReferenceConvertible, Equatable, Hashable, MutableColl

// MARK: - Bridging Helpers

fileprivate init(nsIndexPath: ReferenceType) {
fileprivate init(nsIndexPath: __shared ReferenceType) {
let count = nsIndexPath.length
if count == 0 {
_indexes = []
Expand Down
2 changes: 1 addition & 1 deletion stdlib/public/SDK/Foundation/IndexSet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -661,7 +661,7 @@ public struct IndexSet : ReferenceConvertible, Equatable, BidirectionalCollectio
return _handle.reference
}

fileprivate init(reference: NSIndexSet) {
fileprivate init(reference: __shared NSIndexSet) {
_handle = _MutablePairHandle(reference)
}
}
Expand Down
8 changes: 4 additions & 4 deletions stdlib/public/SDK/Foundation/JSONEncoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ fileprivate struct _JSONEncodingStorage {
return array
}

fileprivate mutating func push(container: NSObject) {
fileprivate mutating func push(container: __owned NSObject) {
self.containers.append(container)
}

Expand Down Expand Up @@ -961,7 +961,7 @@ fileprivate class _JSONReferencingEncoder : _JSONEncoder {

/// Initializes `self` by referencing the given dictionary container in the given encoder.
fileprivate init(referencing encoder: _JSONEncoder,
key: CodingKey, convertedKey: CodingKey, wrapping dictionary: NSMutableDictionary) {
key: CodingKey, convertedKey: __shared CodingKey, wrapping dictionary: NSMutableDictionary) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see why only the one parameter changed here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This parameter is not stored; all others are. Based on your comments, it sounds to me like I've misunderstood something critical here. Let me start an email thread so we can sort this out.

self.encoder = encoder
self.reference = .dictionary(dictionary, convertedKey.stringValue)
super.init(options: encoder.options, codingPath: encoder.codingPath)
Expand Down Expand Up @@ -1273,7 +1273,7 @@ fileprivate struct _JSONDecodingStorage {
return self.containers.last!
}

fileprivate mutating func push(container: Any) {
fileprivate mutating func push(container: __owned Any) {
self.containers.append(container)
}

Expand Down Expand Up @@ -1616,7 +1616,7 @@ fileprivate struct _JSONKeyedDecodingContainer<K : CodingKey> : KeyedDecodingCon
return _JSONUnkeyedDecodingContainer(referencing: self.decoder, wrapping: array)
}

private func _superDecoder(forKey key: CodingKey) throws -> Decoder {
private func _superDecoder(forKey key: __owned CodingKey) throws -> Decoder {
self.decoder.codingPath.append(key)
defer { self.decoder.codingPath.removeLast() }

Expand Down
2 changes: 1 addition & 1 deletion stdlib/public/SDK/Foundation/Locale.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public struct Locale : Hashable, Equatable, ReferenceConvertible {
_autoupdating = false
}

fileprivate init(reference: NSLocale) {
fileprivate init(reference: __shared NSLocale) {
_wrapped = reference.copy() as! NSLocale
if __NSLocaleIsAutoupdating(reference) {
_autoupdating = true
Expand Down
4 changes: 2 additions & 2 deletions stdlib/public/SDK/Foundation/NSArray.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ extension Array : _ObjectiveCBridgeable {
///
/// The provided `NSArray` will be copied to ensure that the copy can
/// not be mutated by other code.
internal init(_cocoaArray: NSArray) {
internal init(_cocoaArray: __shared NSArray) {
assert(_isBridgedVerbatimToObjectiveC(Element.self),
"Array can be backed by NSArray only when the element type can be bridged verbatim to Objective-C")
// FIXME: We would like to call CFArrayCreateCopy() to avoid doing an
Expand Down Expand Up @@ -153,7 +153,7 @@ extension NSArray {
/// Discussion: After an immutable array has been initialized in
/// this way, it cannot be modified.
@nonobjc
public convenience init(array anArray: NSArray) {
public convenience init(array anArray: __shared NSArray) {
self.init(array: anArray as Array)
}
}
Expand Down
6 changes: 3 additions & 3 deletions stdlib/public/SDK/Foundation/NSDictionary.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ extension Dictionary {
///
/// The provided `NSDictionary` will be copied to ensure that the copy can
/// not be mutated by other code.
public init(_cocoaDictionary: _NSDictionary) {
public init(_cocoaDictionary: __shared _NSDictionary) {
assert(
_isBridgedVerbatimToObjectiveC(Key.self) &&
_isBridgedVerbatimToObjectiveC(Value.self),
Expand Down Expand Up @@ -165,7 +165,7 @@ extension NSDictionary : Sequence {
return nil
}

internal init(_ _dict: NSDictionary) {
internal init(_ _dict: __shared NSDictionary) {
_fastIterator = NSFastEnumerationIterator(_dict)
}
}
Expand Down Expand Up @@ -219,7 +219,7 @@ extension NSDictionary {
/// than the original receiver--containing the keys and values
/// found in `otherDictionary`.
@objc(_swiftInitWithDictionary_NSDictionary:)
public convenience init(dictionary otherDictionary: NSDictionary) {
public convenience init(dictionary otherDictionary: __shared NSDictionary) {
// FIXME(performance)(compiler limitation): we actually want to do just
// `self = otherDictionary.copy()`, but Swift does not have factory
// initializers right now.
Expand Down
4 changes: 2 additions & 2 deletions stdlib/public/SDK/Foundation/NSError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ extension CFError : Error {
public protocol _ObjectiveCBridgeableError : Error {
/// Produce a value of the error type corresponding to the given NSError,
/// or return nil if it cannot be bridged.
init?(_bridgedNSError: NSError)
init?(_bridgedNSError: __shared NSError)
}

/// A hook for the runtime to use _ObjectiveCBridgeableError in order to
Expand Down Expand Up @@ -398,7 +398,7 @@ extension _BridgedNSError {
extension _BridgedNSError where Self.RawValue: FixedWidthInteger {
public var _code: Int { return Int(rawValue) }

public init?(_bridgedNSError: NSError) {
public init?(_bridgedNSError: __shared NSError) {
if _bridgedNSError.domain != Self._nsErrorDomain {
return nil
}
Expand Down
2 changes: 1 addition & 1 deletion stdlib/public/SDK/Foundation/NSExpression.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
extension NSExpression {
// + (NSExpression *) expressionWithFormat:(NSString *)expressionFormat, ...;
public
convenience init(format expressionFormat: String, _ args: CVarArg...) {
convenience init(format expressionFormat: __shared String, _ args: CVarArg...) {
let va_args = getVaList(args)
self.init(format: expressionFormat, arguments: va_args)
}
Expand Down
Loading