Skip to content

Commit c0e8ef3

Browse files
committed
Update package with changes from recent ABI stable releases
This brings the contents of this package up to date with versions of the ABI stable `System` module that shipped in recent iOS/macOS releases. Most changes only touch internal implementation details, but some ABI-related fixes also affect the public API. There are no source-incompatible changes. - ABI stability fixes (un-deprecate CModeT, don't use PlatformChar in inlinable contexts that need to deploy to iOS 14) - New utilities: `_RawBuffer`, `Array<String>._withCStringArray`, `_withOptionalUnsafePointerOrNull`, `_withStackBuffer`, `system_memset` - Prefer using explicit optionals to implicitly-unwrapped ones - Add support for wildcard matching in mocking tests - A bunch of new availability annotations. (I expect there will be more of these coming in future PRs.)
1 parent 1d5e35a commit c0e8ef3

21 files changed

+523
-46
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
.DS_Store
22
/.build
33
/Packages
4-
/*.xcodeproj
4+
swift-system.xcodeproj
55
xcuserdata/
66
.*.sw?

Sources/System/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ add_library(SystemPackage
1616
PlatformString.swift
1717
SystemString.swift
1818
Util.swift
19+
Util+StringArray.swift
1920
UtilConsumers.swift)
2021
set_target_properties(SystemPackage PROPERTIES
2122
INTERFACE_INCLUDE_DIRECTORIES ${CMAKE_Swift_MODULE_DIRECTORY})
@@ -32,6 +33,7 @@ target_sources(SystemPackage PRIVATE
3233
Internals/Constants.swift
3334
Internals/Exports.swift
3435
Internals/Mocking.swift
36+
Internals/RawBuffer.swift
3537
Internals/Syscalls.swift
3638
Internals/WindowsSyscallAdapters.swift)
3739
target_link_libraries(SystemPackage PUBLIC

Sources/System/FileDescriptor.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ public struct FileDescriptor: RawRepresentable, Hashable, Codable {
2525
public init(rawValue: CInt) { self.rawValue = rawValue }
2626
}
2727

28-
// Standard file descriptors
28+
// Standard file descriptors.
29+
/*System 0.0.1, @available(macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0, *)*/
2930
extension FileDescriptor {
3031
/// The standard input file descriptor, with a numeric value of 0.
3132
@_alwaysEmitIntoClient

Sources/System/FileOperations.swift

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ extension FileDescriptor {
3131
permissions: FilePermissions? = nil,
3232
retryOnInterrupt: Bool = true
3333
) throws -> FileDescriptor {
34-
try path.withPlatformString {
34+
try path.withCString {
3535
try FileDescriptor.open(
3636
$0, mode, options: options, permissions: permissions, retryOnInterrupt: retryOnInterrupt)
3737
}
@@ -53,7 +53,7 @@ extension FileDescriptor {
5353
/// The corresponding C function is `open`.
5454
@_alwaysEmitIntoClient
5555
public static func open(
56-
_ path: UnsafePointer<CInterop.PlatformChar>,
56+
_ path: UnsafePointer<CChar>,
5757
_ mode: FileDescriptor.AccessMode,
5858
options: FileDescriptor.OpenOptions = FileDescriptor.OpenOptions(),
5959
permissions: FilePermissions? = nil,
@@ -66,7 +66,7 @@ extension FileDescriptor {
6666

6767
@usableFromInline
6868
internal static func _open(
69-
_ path: UnsafePointer<CInterop.PlatformChar>,
69+
_ path: UnsafePointer<CChar>,
7070
_ mode: FileDescriptor.AccessMode,
7171
options: FileDescriptor.OpenOptions,
7272
permissions: FilePermissions?,
@@ -308,7 +308,10 @@ extension FileDescriptor {
308308
buffer,
309309
retryOnInterrupt: retryOnInterrupt)
310310
}
311+
}
311312

313+
/*System 0.0.2, @available(macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0, *)*/
314+
extension FileDescriptor {
312315
/// Duplicate this file descriptor and return the newly created copy.
313316
///
314317
/// - Parameters:
@@ -385,7 +388,7 @@ extension FileDescriptor {
385388
public static func pipe() throws -> (readEnd: FileDescriptor, writeEnd: FileDescriptor) {
386389
try _pipe().get()
387390
}
388-
391+
389392
/*System 1.1.0, @available(macOS 9999, iOS 9999, watchOS 9999, tvOS 9999, *)*/
390393
@usableFromInline
391394
internal static func _pipe() -> Result<(readEnd: FileDescriptor, writeEnd: FileDescriptor), Errno> {

Sources/System/FilePath/FilePathComponentView.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ extension FilePath {
4040
}
4141
}
4242

43-
#if SYSTEM_PACKAGE
43+
#if SYSTEM_PACKAGE // Workaround for a __consuming getter bug in Swift 5.3.3
4444
/// View the non-root components that make up this path.
4545
public var components: ComponentView {
4646
get { ComponentView(self) }
@@ -202,6 +202,7 @@ extension FilePath {
202202

203203
// MARK: - Internals
204204

205+
/*System 0.0.2, @available(macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0, *)*/
205206
extension FilePath.ComponentView: _PathSlice {
206207
internal var _range: Range<SystemString.Index> {
207208
_start ..< _path._storage.endIndex
@@ -214,6 +215,7 @@ extension FilePath.ComponentView: _PathSlice {
214215

215216
// MARK: - Invariants
216217

218+
/*System 0.0.2, @available(macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0, *)*/
217219
extension FilePath.ComponentView {
218220
internal func _invariantCheck() {
219221
#if DEBUG

Sources/System/FilePath/FilePathComponents.swift

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ extension FilePath.Component {
9898
}
9999
}
100100

101+
/*System 0.0.2, @available(macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0, *)*/
101102
extension FilePath.Root {
102103
// TODO: Windows analysis APIs
103104
}
@@ -183,15 +184,18 @@ extension _PathSlice {
183184
internal var _storage: SystemString { _path._storage }
184185
}
185186

187+
/*System 0.0.2, @available(macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0, *)*/
186188
extension FilePath.Component: _PathSlice {
187189
}
190+
/*System 0.0.2, @available(macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0, *)*/
188191
extension FilePath.Root: _PathSlice {
189192
internal var _range: Range<SystemString.Index> {
190193
(..<_rootEnd).relative(to: _path._storage)
191194
}
192195
}
196+
197+
/*System 0.0.1, @available(macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0, *)*/
193198
extension FilePath: _PlatformStringable {
194-
@usableFromInline
195199
func _withPlatformString<Result>(_ body: (UnsafePointer<CInterop.PlatformChar>) throws -> Result) rethrows -> Result {
196200
try _storage.withPlatformString(body)
197201
}
@@ -202,6 +206,7 @@ extension FilePath: _PlatformStringable {
202206

203207
}
204208

209+
/*System 0.0.2, @available(macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0, *)*/
205210
extension FilePath.Component {
206211
// The index of the `.` denoting an extension
207212
internal func _extensionIndex() -> SystemString.Index? {
@@ -230,6 +235,7 @@ internal func _makeExtension(_ ext: String) -> SystemString {
230235
return result
231236
}
232237

238+
/*System 0.0.2, @available(macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0, *)*/
233239
extension FilePath.Component {
234240
internal init?(_ str: SystemString) {
235241
// FIXME: explicit null root? Or something else?
@@ -242,6 +248,7 @@ extension FilePath.Component {
242248
}
243249
}
244250

251+
/*System 0.0.2, @available(macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0, *)*/
245252
extension FilePath.Root {
246253
internal init?(_ str: SystemString) {
247254
// FIXME: explicit null root? Or something else?
@@ -256,6 +263,7 @@ extension FilePath.Root {
256263

257264
// MARK: - Invariants
258265

266+
/*System 0.0.2, @available(macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0, *)*/
259267
extension FilePath.Component {
260268
// TODO: ensure this all gets easily optimized away in release...
261269
internal func _invariantCheck() {
@@ -267,6 +275,8 @@ extension FilePath.Component {
267275
#endif // DEBUG
268276
}
269277
}
278+
279+
/*System 0.0.2, @available(macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0, *)*/
270280
extension FilePath.Root {
271281
internal func _invariantCheck() {
272282
#if DEBUG

Sources/System/FilePath/FilePathParsing.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ extension SystemString {
111111
}
112112
}
113113

114+
/*System 0.0.1, @available(macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0, *)*/
114115
extension FilePath {
115116
internal mutating func _removeTrailingSeparator() {
116117
_storage._removeTrailingSeparator()
@@ -191,6 +192,7 @@ extension SystemString {
191192
}
192193
}
193194

195+
/*System 0.0.1, @available(macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0, *)*/
194196
extension FilePath {
195197
internal var _relativeStart: SystemString.Index {
196198
_storage._relativePathStart
@@ -202,6 +204,7 @@ extension FilePath {
202204

203205
// Parse separators
204206

207+
/*System 0.0.1, @available(macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0, *)*/
205208
extension FilePath {
206209
internal typealias _Index = SystemString.Index
207210

@@ -265,6 +268,7 @@ extension FilePath {
265268
}
266269
}
267270

271+
/*System 0.0.2, @available(macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0, *)*/
268272
extension FilePath.ComponentView {
269273
// TODO: Store this...
270274
internal var _relativeStart: SystemString.Index {
@@ -289,6 +293,7 @@ extension SystemString {
289293
}
290294
}
291295

296+
/*System 0.0.2, @available(macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0, *)*/
292297
extension FilePath.Root {
293298
// Asserting self is a root, returns whether this is an
294299
// absolute root.
@@ -313,6 +318,7 @@ extension FilePath.Root {
313318
}
314319
}
315320

321+
/*System 0.0.1, @available(macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0, *)*/
316322
extension FilePath {
317323
internal var _portableDescription: String {
318324
guard _windowsPaths else { return description }
@@ -331,6 +337,7 @@ internal var _windowsPaths: Bool {
331337
#endif
332338
}
333339

340+
/*System 0.0.1, @available(macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0, *)*/
334341
extension FilePath {
335342
// Whether we should add a separator when doing an append
336343
internal var _needsSeparatorForAppend: Bool {
@@ -358,6 +365,7 @@ extension FilePath {
358365
}
359366

360367
// MARK: - Invariants
368+
/*System 0.0.1, @available(macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0, *)*/
361369
extension FilePath {
362370
internal func _invariantCheck() {
363371
#if DEBUG

Sources/System/FilePath/FilePathString.swift

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ extension FilePath {
2020
self.init(_platformString: platformString)
2121
}
2222

23+
#if !os(Windows) // Availability workaround
2324
/// Calls the given closure with a pointer to the contents of the file path,
2425
/// represented as a null-terminated platform string.
2526
///
@@ -37,12 +38,28 @@ extension FilePath {
3738
_ body: (UnsafePointer<CInterop.PlatformChar>) throws -> Result
3839
) rethrows -> Result {
3940
// For backwards deployment, call withCString if available.
40-
#if !os(Windows)
4141
return try withCString(body)
42-
#else
42+
}
43+
#else
44+
/// Calls the given closure with a pointer to the contents of the file path,
45+
/// represented as a null-terminated platform string.
46+
///
47+
/// - Parameter body: A closure with a pointer parameter
48+
/// that points to a null-terminated platform string.
49+
/// If `body` has a return value,
50+
/// that value is also used as the return value for this method.
51+
/// - Returns: The return value, if any, of the `body` closure parameter.
52+
///
53+
/// The pointer passed as an argument to `body` is valid
54+
/// only during the execution of this method.
55+
/// Don't try to store the pointer for later use.
56+
public func withPlatformString<Result>(
57+
_ body: (UnsafePointer<CInterop.PlatformChar>) throws -> Result
58+
) rethrows -> Result {
59+
// For backwards deployment, call withCString if available.
4360
return try _withPlatformString(body)
44-
#endif
4561
}
62+
#endif
4663
}
4764

4865
/*System 0.0.2, @available(macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0, *)*/

Sources/System/FilePermissions.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,14 @@
2121
public struct FilePermissions: OptionSet, Hashable, Codable {
2222
/// The raw C file permissions.
2323
@_alwaysEmitIntoClient
24-
public let rawValue: CInterop.Mode
24+
public let rawValue: CModeT
2525

2626
/// Create a strongly-typed file permission from a raw C value.
2727
@_alwaysEmitIntoClient
28-
public init(rawValue: CInterop.Mode) { self.rawValue = rawValue }
28+
public init(rawValue: CModeT) { self.rawValue = rawValue }
2929

3030
@_alwaysEmitIntoClient
31-
private init(_ raw: CInterop.Mode) { self.init(rawValue: raw) }
31+
private init(_ raw: CModeT) { self.init(rawValue: raw) }
3232

3333
/// Indicates that other users have read-only permission.
3434
@_alwaysEmitIntoClient
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
This source file is part of the Swift System open source project
3+
4+
Copyright (c) 2021 Apple Inc. and the Swift System project authors
5+
Licensed under Apache License v2.0 with Runtime Library Exception
6+
7+
See https://swift.org/LICENSE.txt for license information
8+
*/
9+
10+
extension String {
11+
internal init(
12+
_unsafeUninitializedCapacity capacity: Int,
13+
initializingUTF8With body: (UnsafeMutableBufferPointer<UInt8>) throws -> Int
14+
) rethrows {
15+
if #available(macOS 11, iOS 14.0, watchOS 7.0, tvOS 14.0, *) {
16+
self = try String(
17+
unsafeUninitializedCapacity: capacity,
18+
initializingUTF8With: body)
19+
return
20+
}
21+
22+
let array = try Array<UInt8>(
23+
unsafeUninitializedCapacity: capacity
24+
) { buffer, count in
25+
count = try body(buffer)
26+
}
27+
self = String(decoding: array, as: UTF8.self)
28+
}
29+
}

0 commit comments

Comments
 (0)