Skip to content

Commit

Permalink
feat(Swift 6.0): Prepare package for swift update
Browse files Browse the repository at this point in the history
  • Loading branch information
maximkrouk committed Sep 28, 2024
1 parent 69d543d commit 475e181
Show file tree
Hide file tree
Showing 15 changed files with 173 additions and 31 deletions.
20 changes: 10 additions & 10 deletions .github/package.xcworkspace/xcshareddata/swiftpm/Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,37 @@
{
"identity" : "swift-macro-testing",
"kind" : "remoteSourceControl",
"location" : "https://github.com/pointfreeco/swift-macro-testing.git",
"location" : "https://github.com/pointfreeco/swift-macro-testing",
"state" : {
"revision" : "10dcef36314ddfea6f60442169b0b320204cbd35",
"version" : "0.2.2"
"revision" : "20c1a8f3b624fb5d1503eadcaa84743050c350f4",
"version" : "0.5.2"
}
},
{
"identity" : "swift-macro-toolkit",
"kind" : "remoteSourceControl",
"location" : "https://github.com/stackotter/swift-macro-toolkit.git",
"state" : {
"revision" : "106daeb38eb3f52b1540aed981fc63fa22274576",
"version" : "0.3.1"
"revision" : "2eded9c9a98cba42d70fb179d516231fffcff1e4",
"version" : "0.5.0"
}
},
{
"identity" : "swift-snapshot-testing",
"kind" : "remoteSourceControl",
"location" : "https://github.com/pointfreeco/swift-snapshot-testing",
"state" : {
"revision" : "8e68404f641300bfd0e37d478683bb275926760c",
"version" : "1.15.2"
"revision" : "7b0bbbae90c41f848f90ac7b4df6c4f50068256d",
"version" : "1.17.5"
}
},
{
"identity" : "swift-syntax",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-syntax.git",
"location" : "https://github.com/swiftlang/swift-syntax",
"state" : {
"revision" : "74203046135342e4a4a627476dd6caf8b28fe11b",
"version" : "509.0.0"
"revision" : "2bc86522d115234d1f588efe2bcb4ce4be8f8b82",
"version" : "510.0.3"
}
}
],
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ DerivedData/
.swiftpm/configuration/registries.json
.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata
.netrc
Package.resolved
2 changes: 1 addition & 1 deletion .spi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ version: 1
builder:
configs:
- documentation_targets: [Interception]
swift_version: 5.9
swift_version: 5.10
18 changes: 9 additions & 9 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,35 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/pointfreeco/swift-macro-testing.git",
"state" : {
"revision" : "10dcef36314ddfea6f60442169b0b320204cbd35",
"version" : "0.2.2"
"revision" : "20c1a8f3b624fb5d1503eadcaa84743050c350f4",
"version" : "0.5.2"
}
},
{
"identity" : "swift-macro-toolkit",
"kind" : "remoteSourceControl",
"location" : "https://github.com/stackotter/swift-macro-toolkit.git",
"state" : {
"revision" : "106daeb38eb3f52b1540aed981fc63fa22274576",
"version" : "0.3.1"
"revision" : "2eded9c9a98cba42d70fb179d516231fffcff1e4",
"version" : "0.5.0"
}
},
{
"identity" : "swift-snapshot-testing",
"kind" : "remoteSourceControl",
"location" : "https://github.com/pointfreeco/swift-snapshot-testing",
"state" : {
"revision" : "8e68404f641300bfd0e37d478683bb275926760c",
"version" : "1.15.2"
"revision" : "7b0bbbae90c41f848f90ac7b4df6c4f50068256d",
"version" : "1.17.5"
}
},
{
"identity" : "swift-syntax",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-syntax.git",
"location" : "https://github.com/swiftlang/swift-syntax",
"state" : {
"revision" : "74203046135342e4a4a627476dd6caf8b28fe11b",
"version" : "509.0.0"
"revision" : "2bc86522d115234d1f588efe2bcb4ce4be8f8b82",
"version" : "510.0.3"
}
}
],
Expand Down
17 changes: 13 additions & 4 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// swift-tools-version: 5.9
// swift-tools-version:5.10

import PackageDescription
import CompilerPluginSupport
Expand All @@ -10,7 +10,7 @@ let package = Package(
.macOS(.v10_15),
.tvOS(.v13),
.macCatalyst(.v13),
.watchOS(.v6)
.watchOS(.v6),
],
products: [
.library(
Expand Down Expand Up @@ -42,11 +42,11 @@ let package = Package(
dependencies: [
.package(
url: "https://github.com/stackotter/swift-macro-toolkit.git",
.upToNextMinor(from: "0.3.0")
.upToNextMinor(from: "0.5.0")
),
.package(
url: "https://github.com/pointfreeco/swift-macro-testing.git",
.upToNextMinor(from: "0.2.2")
.upToNextMinor(from: "0.5.0")
)
],
targets: [
Expand Down Expand Up @@ -109,3 +109,12 @@ let package = Package(
),
]
)

#if compiler(>=6)
for target in package.targets where target.type != .system && target.type != .test {
target.swiftSettings = target.swiftSettings ?? []
target.swiftSettings?.append(contentsOf: [
.enableUpcomingFeature("InferSendableFromCaptures")
])
}
#endif
120 changes: 120 additions & 0 deletions [email protected]
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
// swift-tools-version:6.0

import PackageDescription
import CompilerPluginSupport

let package = Package(
name: "swift-interception",
platforms: [
.iOS(.v13),
.macOS(.v10_15),
.tvOS(.v13),
.macCatalyst(.v13),
.watchOS(.v6),
],
products: [
.library(
name: "_InterceptionCustomSelectors",
type: .static,
targets: ["_InterceptionCustomSelectors"]
),
.library(
name: "_InterceptionMacros",
type: .static,
targets: ["_InterceptionMacros"]
),
.library(
name: "_InterceptionUtils",
type: .static,
targets: ["_InterceptionUtils"]
),
.library(
name: "Interception",
type: .static,
targets: ["Interception"]
),
.library(
name: "InterceptionMacros",
type: .static,
targets: ["InterceptionMacros"]
),
],
dependencies: [
.package(
url: "https://github.com/stackotter/swift-macro-toolkit.git",
.upToNextMinor(from: "0.5.0")
),
.package(
url: "https://github.com/pointfreeco/swift-macro-testing.git",
.upToNextMinor(from: "0.5.2")
)
],
targets: [
.target(
name: "_InterceptionMacros",
dependencies: [
.target(name: "InterceptionMacrosPlugin"),
.target(name: "_InterceptionCustomSelectors")
]
),
.target(name: "_InterceptionCustomSelectors"),
.target(name: "_InterceptionUtilsObjc"),
.target(
name: "_InterceptionUtils",
dependencies: [
.target(name: "_InterceptionUtilsObjc"),
]
),
.target(
name: "Interception",
dependencies: [
.target(name: "_InterceptionCustomSelectors"),
.target(name: "_InterceptionUtils"),
]
),
.target(
name: "InterceptionMacros",
dependencies: [
.target(name: "_InterceptionMacros"),
.target(name: "Interception")
]
),
.macro(
name: "InterceptionMacrosPlugin",
dependencies: [
.product(
name: "MacroToolkit",
package: "swift-macro-toolkit"
)
]
),
.testTarget(
name: "InterceptionMacrosPluginTests",
dependencies: [
.target(name: "InterceptionMacrosPlugin"),
.product(name: "MacroTesting", package: "swift-macro-testing"),
]
),
.testTarget(
name: "InterceptionTests",
dependencies: [
.target(name: "Interception"),
]
),
.testTarget(
name: "InterceptionMacrosTests",
dependencies: [
.target(name: "InterceptionMacros"),
]
),
],
swiftLanguageModes: [.v6]
)

for target in package.targets where target.type == .system || target.type == .test {
target.swiftSettings?.append(contentsOf: [
.swiftLanguageMode(.v5),
.enableExperimentalFeature("StrictConcurrency"),
.enableUpcomingFeature("InferSendableFromCaptures"),
])
}
5 changes: 5 additions & 0 deletions Sources/Interception/NSObject+Interception.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,18 @@ import _InterceptionCustomSelectors

/// Whether the runtime subclass has already been prepared for method
/// interception.
nonisolated(unsafe)
private let interceptedKey = AssociationKey(default: false)

/// Holds the method signature cache of the runtime subclass.
nonisolated(unsafe)
private let signatureCacheKey = AssociationKey<SignatureCache>()

/// Holds the method selector cache of the runtime subclass.
nonisolated(unsafe)
private let selectorCacheKey = AssociationKey<SelectorCache>()

nonisolated(unsafe)
internal let noImplementation: IMP = unsafeBitCast(Int(0), to: IMP.self)

extension NSObject {
Expand Down Expand Up @@ -327,6 +331,7 @@ private func setupMethodSignatureCaching(_ realClass: AnyClass, _ signatureCache

@_spi(Internals)
public struct SwiftInterceptionDefaultInterceptionHandlerKey: Hashable {
nonisolated(unsafe)
public static let shared: Self = .init()

private init() {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public struct MethodSelectorMacro: ExpressionMacro {
of node: Node,
in context: Context
) -> ExprSyntax {
guard let arg = node.argumentList.first.map(\.expression)
guard let arg = node.arguments.first.map(\.expression)
else { fatalError("compiler bug: the macro does not have any arguments") }

return """
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public struct PropertySelectorMacro: ExpressionMacro {
of node: Node,
in context: Context
) -> ExprSyntax {
guard let arg = node.argumentList.first.map(\.expression)
guard let arg = node.arguments.first.map(\.expression)
else { fatalError("compiler bug: the macro does not have any arguments") }

return """
Expand Down
7 changes: 6 additions & 1 deletion Sources/_InterceptionUtils/ObjC+Constants.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,15 @@ public enum ObjCSelector {

// Method encoding of the unavailable selectors.
public enum ObjCMethodEncoding {
nonisolated(unsafe)
public static let forwardInvocation = extract("v@:@")

nonisolated(unsafe)
public static let methodSignatureForSelector = extract("v@::")

nonisolated(unsafe)
public static let getClass = extract("#@:")

private static func extract(_ string: StaticString) -> UnsafePointer<CChar> {
return UnsafeRawPointer(string.utf8Start).assumingMemoryBound(to: CChar.self)
}
Expand Down
2 changes: 2 additions & 0 deletions Sources/_InterceptionUtils/ObjC+RuntimeSubclassing.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ import Combine
import Foundation

/// Whether the runtime subclass has already been swizzled.
nonisolated(unsafe)
private let runtimeSubclassedKey = AssociationKey(default: false)

/// A known combine-interception runtime subclass of the instance. `nil` if the runtime subclass
/// has not been requested for the instance before.
nonisolated(unsafe)
private let knownRuntimeSubclassKey = AssociationKey<AnyClass?>(default: nil)

extension NSObject {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import InterceptionMacrosPlugin
final class MethodSelectorTests: XCTestCase {
override func invokeTest() {
withMacroTesting(
isRecording: false,
record: false,
macros: [
"methodSelector": MethodSelectorMacro.self
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import InterceptionMacrosPlugin
final class PropertySelectorTests: XCTestCase {
override func invokeTest() {
withMacroTesting(
isRecording: false,
record: false,
macros: [
"propertySelector": PropertySelectorMacro.self
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ final class InterceptionMacrosTests: XCTestCase {
}
}

fileprivate class Object: NSObject {
fileprivate final class Object: NSObject, Sendable {
@discardableResult
@objc dynamic
func zero() -> Int { 0 }
Expand Down
2 changes: 1 addition & 1 deletion Tests/InterceptionTests/InterceptionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ final class InterceptionTests: XCTestCase {
}
}

fileprivate class Object: NSObject {
fileprivate final class Object: NSObject, Sendable {
@discardableResult
@objc dynamic
func zero() -> Int { 0 }
Expand Down

0 comments on commit 475e181

Please sign in to comment.