Skip to content

Commit 7b0a0b9

Browse files
committed
Upgrade to Swift 6 - Fix associated warnings
1 parent cd5f690 commit 7b0a0b9

File tree

77 files changed

+828
-753
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

77 files changed

+828
-753
lines changed

Package.swift

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// swift-tools-version: 5.9
1+
// swift-tools-version: 6.0
22
// The swift-tools-version declares the minimum version of Swift required to build this package.
33

44
import PackageDescription
@@ -67,6 +67,7 @@ let interfaceBuildSettings: [CSetting] = [
6767
let swiftBuildSettings: [SwiftSetting] = [
6868
.define("DEPLOYMENT_RUNTIME_SWIFT"),
6969
.define("SWIFT_CORELIBS_FOUNDATION_HAS_THREADS"),
70+
.swiftLanguageVersion(.v6),
7071
.unsafeFlags([
7172
"-Xfrontend",
7273
"-require-explicit-sendable",
@@ -146,7 +147,7 @@ let package = Package(
146147
exclude: [
147148
"CMakeLists.txt"
148149
],
149-
swiftSettings:swiftBuildSettings
150+
swiftSettings: swiftBuildSettings
150151
),
151152
.target(
152153
name: "CoreFoundation",
@@ -207,6 +208,9 @@ let package = Package(
207208
],
208209
exclude: [
209210
"CMakeLists.txt"
211+
],
212+
swiftSettings: [
213+
.swiftLanguageVersion(.v6)
210214
]
211215
),
212216
.executableTarget(
@@ -215,6 +219,9 @@ let package = Package(
215219
"Foundation",
216220
"FoundationXML",
217221
"FoundationNetworking"
222+
],
223+
swiftSettings: [
224+
.swiftLanguageVersion(.v6)
218225
]
219226
),
220227
.target(
@@ -241,7 +248,8 @@ let package = Package(
241248
.copy("Foundation/Resources")
242249
],
243250
swiftSettings: [
244-
.define("NS_FOUNDATION_ALLOWS_TESTABLE_IMPORT")
251+
.define("NS_FOUNDATION_ALLOWS_TESTABLE_IMPORT"),
252+
.swiftLanguageVersion(.v6)
245253
]
246254
),
247255
]

Sources/CoreFoundation/CFPlatform.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1814,6 +1814,7 @@ CF_CROSS_PLATFORM_EXPORT int _CFThreadSetName(_CFThreadRef thread, const char *_
18141814
#endif
18151815
}
18161816

1817+
// `buf` must be null-terminated
18171818
CF_CROSS_PLATFORM_EXPORT int _CFThreadGetName(char *buf, int length) {
18181819
#if SWIFT_CORELIBS_FOUNDATION_HAS_THREADS
18191820
#if TARGET_OS_MAC

Sources/CoreFoundation/include/ForSwiftFoundationOnly.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -334,10 +334,10 @@ struct _NSCFXMLBridgeUntyped {
334334
void *kCFErrorLocalizedDescriptionKey;
335335
};
336336

337-
CF_EXPORT struct _NSCFXMLBridgeStrong __NSCFXMLBridgeStrong;
338-
CF_EXPORT struct _NSCFXMLBridgeUntyped __NSCFXMLBridgeUntyped;
337+
CF_EXPORT struct _NSCFXMLBridgeStrong __NSCFXMLBridgeStrong __attribute__((swift_attr("nonisolated(unsafe)")));
338+
CF_EXPORT struct _NSCFXMLBridgeUntyped __NSCFXMLBridgeUntyped __attribute__((swift_attr("nonisolated(unsafe)")));
339339

340-
CF_EXPORT struct _CFSwiftBridge __CFSwiftBridge;
340+
CF_EXPORT struct _CFSwiftBridge __CFSwiftBridge __attribute__((swift_attr("nonisolated(unsafe)")));
341341

342342
CF_EXPORT void *_Nullable _CFSwiftRetain(void *_Nullable t);
343343
CF_EXPORT void _CFSwiftRelease(void *_Nullable t);
@@ -406,7 +406,7 @@ typedef void *_CFThreadSpecificKey;
406406
#endif
407407

408408
CF_CROSS_PLATFORM_EXPORT Boolean _CFIsMainThread(void);
409-
CF_EXPORT _CFThreadRef _CFMainPThread;
409+
CF_EXPORT _CFThreadRef _CFMainPThread __attribute__((swift_attr("nonisolated(unsafe)")));
410410

411411
CF_EXPORT CFHashCode __CFHashDouble(double d);
412412

Sources/Foundation/Bridging.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ internal final class __SwiftValue : NSObject, NSCopying {
222222
return __SwiftValue(value)
223223
}
224224

225-
public static let null: AnyObject = NSNull()
225+
public static nonisolated(unsafe) let null: AnyObject = NSNull()
226226

227227
override var description: String { String(describing: value) }
228228
}

Sources/Foundation/Bundle.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ open class Bundle: NSObject, @unchecked Sendable {
3434
#endif
3535
}
3636

37-
private static var _mainBundle : Bundle = {
37+
private static let _mainBundle : Bundle = {
3838
return Bundle(cfBundle: CFBundleGetMainBundle())
3939
}()
4040

Sources/Foundation/DateIntervalFormatter.swift

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -83,18 +83,15 @@ internal extension _CFDateIntervalFormatterBoundaryStyle {
8383
// DateIntervalFormatter returns nil and NO for all methods in Formatter.
8484

8585
open class DateIntervalFormatter: Formatter, @unchecked Sendable {
86-
private let _core: AnyObject
87-
private final var core: CFDateIntervalFormatter {
88-
get { unsafeBitCast(_core, to: CFDateIntervalFormatter.self) }
89-
}
86+
private let core: CFDateIntervalFormatter
9087

9188
public override init() {
92-
_core = CFDateIntervalFormatterCreate(nil, nil, kCFDateIntervalFormatterShortStyle, kCFDateIntervalFormatterShortStyle)
89+
core = CFDateIntervalFormatterCreate(nil, nil, kCFDateIntervalFormatterShortStyle, kCFDateIntervalFormatterShortStyle)
9390
super.init()
9491
}
9592

9693
private init(cfFormatter: CFDateIntervalFormatter) {
97-
self._core = cfFormatter
94+
self.core = cfFormatter
9895
super.init()
9996
}
10097

@@ -120,7 +117,7 @@ open class DateIntervalFormatter: Formatter, @unchecked Sendable {
120117
object(of: NSLocale.self, from: coder, forKey: "NS.locale")?._cfObject,
121118
object(of: NSCalendar.self, from: coder, forKey: "NS.calendar")?._cfObject,
122119
object(of: NSTimeZone.self, from: coder, forKey: "NS.timeZone")?._cfObject)
123-
self._core = core
120+
self.core = core
124121

125122
super.init(coder: coder)
126123
}

Sources/Foundation/FileHandle.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -738,31 +738,31 @@ open class FileHandle : NSObject, @unchecked Sendable {
738738

739739
extension FileHandle {
740740

741-
internal static var _stdinFileHandle: FileHandle = {
741+
internal static let _stdinFileHandle: FileHandle = {
742742
return FileHandle(fileDescriptor: STDIN_FILENO, closeOnDealloc: false)
743743
}()
744744

745745
public class var standardInput: FileHandle {
746746
return _stdinFileHandle
747747
}
748748

749-
internal static var _stdoutFileHandle: FileHandle = {
749+
internal static let _stdoutFileHandle: FileHandle = {
750750
return FileHandle(fileDescriptor: STDOUT_FILENO, closeOnDealloc: false)
751751
}()
752752

753753
public class var standardOutput: FileHandle {
754754
return _stdoutFileHandle
755755
}
756756

757-
internal static var _stderrFileHandle: FileHandle = {
757+
internal static let _stderrFileHandle: FileHandle = {
758758
return FileHandle(fileDescriptor: STDERR_FILENO, closeOnDealloc: false)
759759
}()
760760

761761
public class var standardError: FileHandle {
762762
return _stderrFileHandle
763763
}
764764

765-
internal static var _nulldeviceFileHandle: FileHandle = {
765+
internal static let _nulldeviceFileHandle: FileHandle = {
766766
class NullDevice: FileHandle, @unchecked Sendable {
767767
override var availableData: Data {
768768
return Data()

Sources/Foundation/FileManager+POSIX.swift

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ internal func &(left: UInt32, right: mode_t) -> mode_t {
1414
#endif
1515

1616
@_implementationOnly import CoreFoundation
17+
internal import Synchronization
1718

1819
#if os(WASI)
1920
import WASILibc
@@ -27,6 +28,10 @@ internal var O_TRUNC: Int32 { _getConst_O_TRUNC() }
2728
internal var O_WRONLY: Int32 { _getConst_O_WRONLY() }
2829
#endif
2930

31+
#if os(Linux)
32+
fileprivate let previousStatxFailed = Mutex(false)
33+
#endif
34+
3035
@_implementationOnly import CoreFoundation
3136

3237
extension FileManager {
@@ -257,7 +262,8 @@ extension FileManager {
257262
}
258263

259264
return try _fileSystemRepresentation(withPath: path) { fsRep in
260-
if supportsStatx {
265+
let statxPreviouslyFailed = previousStatxFailed.withLock { $0 }
266+
if supportsStatx && !statxPreviouslyFailed {
261267
var statInfo = stat()
262268
var btime = timespec()
263269
let statxErrno = _stat_with_btime(fsRep, &statInfo, &btime)
@@ -266,7 +272,7 @@ extension FileManager {
266272
case EPERM, ENOSYS:
267273
// statx() may be blocked by a security mechanism (eg libseccomp or Docker) even if the kernel verison is new enough. EPERM or ENONSYS may be reported.
268274
// Dont try to use it in future and fallthough to a normal lstat() call.
269-
supportsStatx = false
275+
previousStatxFailed.withLock { $0 = true }
270276
return try _statxFallback(atPath: path, withFileSystemRepresentation: fsRep)
271277

272278
default:

Sources/Foundation/FileManager.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,20 +34,20 @@ internal let NativeFSREncoding = String.Encoding.utf8.rawValue
3434

3535
#if os(Linux)
3636
// statx() is only supported by Linux kernels >= 4.11.0
37-
internal var supportsStatx: Bool = {
37+
internal let supportsStatx: Bool = {
3838
let requiredVersion = OperatingSystemVersion(majorVersion: 4, minorVersion: 11, patchVersion: 0)
3939
return ProcessInfo.processInfo.isOperatingSystemAtLeast(requiredVersion)
4040
}()
4141

4242
// renameat2() is only supported by Linux kernels >= 3.15
43-
internal var kernelSupportsRenameat2: Bool = {
43+
internal let kernelSupportsRenameat2: Bool = {
4444
let requiredVersion = OperatingSystemVersion(majorVersion: 3, minorVersion: 15, patchVersion: 0)
4545
return ProcessInfo.processInfo.isOperatingSystemAtLeast(requiredVersion)
4646
}()
4747
#endif
4848

4949
// For testing only: this facility pins the language used by displayName to the passed-in language.
50-
private var _overriddenDisplayNameLanguages: [String]? = nil
50+
private nonisolated(unsafe) var _overriddenDisplayNameLanguages: [String]? = nil
5151

5252
extension FileManager {
5353

Sources/Foundation/Host.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ open class Host: NSObject {
7070
internal var _names = [String]()
7171
internal var _addresses = [String]()
7272

73-
static internal let _current = Host(currentHostName(), .current)
73+
static internal let _cachedCurrentHostName = currentHostName()
7474

7575
internal init(_ info: String?, _ type: ResolveType) {
7676
_info = info
@@ -110,7 +110,7 @@ open class Host: NSObject {
110110
}
111111

112112
open class func current() -> Host {
113-
return _current
113+
return Host(Self._cachedCurrentHostName, .current)
114114
}
115115

116116
public convenience init(name: String?) {

Sources/Foundation/JSONEncoder.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1218,8 +1218,9 @@ internal struct _JSONKey: CodingKey {
12181218
//===----------------------------------------------------------------------===//
12191219

12201220
// NOTE: This value is implicitly lazy and _must_ be lazy. We're compiled against the latest SDK (w/ ISO8601DateFormatter), but linked against whichever Foundation the user has. ISO8601DateFormatter might not exist, so we better not hit this code path on an older OS.
1221+
// This file is about to be replaced by one from swift-foundation, which uses Sendable FormatStyle
12211222
@available(macOS 10.12, iOS 10.0, watchOS 3.0, tvOS 10.0, *)
1222-
internal var _iso8601Formatter: ISO8601DateFormatter = {
1223+
internal nonisolated(unsafe) let _iso8601Formatter: ISO8601DateFormatter = {
12231224
let formatter = ISO8601DateFormatter()
12241225
formatter.formatOptions = .withInternetDateTime
12251226
return formatter

Sources/Foundation/NSCFCharacterSet.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,8 @@ internal func _CFSwiftCharacterSetCharacterIsMember(_ cset: CFTypeRef, _ ch: Un
9393
}
9494

9595
internal func _CFSwiftCharacterSetMutableCopy(_ cset: CFTypeRef) -> Unmanaged<CFMutableCharacterSet> {
96-
return Unmanaged.passRetained(unsafeBitCast((cset as! NSCharacterSet).mutableCopy(), to: CFMutableCharacterSet.self))
96+
let copy = (cset as! NSCharacterSet).mutableCopy() as! NSMutableCharacterSet
97+
return Unmanaged.passRetained(unsafeDowncast(copy, to: CFMutableCharacterSet.self))
9798
}
9899

99100
internal func _CFSwiftCharacterSetLongCharacterIsMember(_ cset: CFTypeRef, _ ch:UInt32) -> Bool {

Sources/Foundation/NSConcreteValue.swift

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
//
99

1010
@_implementationOnly import CoreFoundation
11+
internal import Synchronization
1112

1213
internal class NSConcreteValue : NSValue, @unchecked Sendable {
1314

@@ -61,22 +62,20 @@ internal class NSConcreteValue : NSValue, @unchecked Sendable {
6162
}
6263
}
6364

64-
private static var _cachedTypeInfo = Dictionary<String, TypeInfo>()
65-
private static var _cachedTypeInfoLock = NSLock()
65+
private static let _cachedTypeInfo = Mutex(Dictionary<String, TypeInfo>())
6666

6767
private var _typeInfo : TypeInfo
6868
private var _storage : UnsafeMutableRawPointer
6969

7070
required init(bytes value: UnsafeRawPointer, objCType type: UnsafePointer<Int8>) {
7171
let spec = String(cString: type)
72-
var typeInfo : TypeInfo? = nil
73-
74-
NSConcreteValue._cachedTypeInfoLock.synchronized {
75-
typeInfo = NSConcreteValue._cachedTypeInfo[spec]
72+
let typeInfo = NSConcreteValue._cachedTypeInfo.withLock {
73+
var typeInfo = $0[spec]
7674
if typeInfo == nil {
7775
typeInfo = TypeInfo(objCType: spec)
78-
NSConcreteValue._cachedTypeInfo[spec] = typeInfo
76+
$0[spec] = typeInfo
7977
}
78+
return typeInfo
8079
}
8180

8281
guard typeInfo != nil else {

Sources/Foundation/NSDateComponents.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
// or quantities of the units.
3030
// When you create a new one of these, all values begin Undefined.
3131

32-
public var NSDateComponentUndefined: Int = Int.max
32+
public let NSDateComponentUndefined: Int = Int.max
3333

3434
@available(*, unavailable)
3535
extension NSDateComponents : Sendable { }

Sources/Foundation/NSDictionary.swift

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -551,19 +551,10 @@ open class NSDictionary : NSObject, NSCopying, NSMutableCopying, NSSecureCoding,
551551
}
552552
}
553553

554-
#if !os(WASI)
555-
if opts.contains(.concurrent) {
556-
DispatchQueue.concurrentPerform(iterations: count, execute: iteration)
557-
} else {
558-
for idx in 0..<count {
559-
iteration(idx)
560-
}
561-
}
562-
#else
554+
// We ignore the concurrent option because it is not possible to make it thread-safe. The block argument is not marked Sendable.
563555
for idx in 0..<count {
564556
iteration(idx)
565557
}
566-
#endif
567558
}
568559
}
569560

@@ -708,9 +699,11 @@ extension NSDictionary : Sequence {
708699
// We implement this as a shim for now. It is legal to call these methods and the behavior of the resulting NSDictionary will match Darwin's; however, the performance characteristics will be unmodified for the returned dictionary vs. a NSMutableDictionary created without a shared key set.
709700
// SR-XXXX.
710701

702+
final internal class SharedKeySetPlaceholder : NSObject, Sendable { }
703+
711704
extension NSDictionary {
712705

713-
static let sharedKeySetPlaceholder = NSObject()
706+
static let sharedKeySetPlaceholder = SharedKeySetPlaceholder()
714707

715708
/* Use this method to create a key set to pass to +dictionaryWithSharedKeySet:.
716709
The keys are copied from the array and must be copyable.

Sources/Foundation/NSError.swift

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import CRT
1919
#endif
2020

2121
@_implementationOnly import CoreFoundation
22+
internal import Synchronization
2223

2324
public typealias NSErrorDomain = NSString
2425

@@ -169,15 +170,15 @@ open class NSError : NSObject, NSCopying, NSSecureCoding, NSCoding, @unchecked S
169170
return userInfo[NSHelpAnchorErrorKey] as? String
170171
}
171172

172-
internal typealias UserInfoProvider = (_ error: Error, _ key: String) -> Any?
173-
internal static var userInfoProviders = [String: UserInfoProvider]()
173+
internal typealias UserInfoProvider = @Sendable (_ error: Error, _ key: String) -> Any?
174+
internal static let userInfoProviders = Mutex<[String: UserInfoProvider]>([:])
174175

175-
open class func setUserInfoValueProvider(forDomain errorDomain: String, provider: (/* @escaping */ (Error, String) -> Any?)?) {
176-
NSError.userInfoProviders[errorDomain] = provider
176+
open class func setUserInfoValueProvider(forDomain errorDomain: String, provider: (@Sendable (Error, String) -> Any?)?) {
177+
NSError.userInfoProviders.withLock { $0[errorDomain] = provider }
177178
}
178179

179-
open class func userInfoValueProvider(forDomain errorDomain: String) -> ((Error, String) -> Any?)? {
180-
return NSError.userInfoProviders[errorDomain]
180+
open class func userInfoValueProvider(forDomain errorDomain: String) -> (@Sendable (Error, String) -> Any?)? {
181+
NSError.userInfoProviders.withLock { $0[errorDomain] }
181182
}
182183

183184
override open var description: String {
@@ -882,7 +883,7 @@ func _convertNSErrorToError(_ error: NSError?) -> Error {
882883
public // COMPILER_INTRINSIC
883884
func _convertErrorToNSError(_ error: Error) -> NSError {
884885
if let object = _extractDynamicValue(error as Any) {
885-
return unsafeBitCast(object, to: NSError.self)
886+
return unsafeDowncast(object, to: NSError.self)
886887
} else {
887888
let domain: String
888889
let code: Int

0 commit comments

Comments
 (0)