From bb91911846bcc0d396cf6b3548d97edfe2a7e9e4 Mon Sep 17 00:00:00 2001 From: Stephen Celis Date: Wed, 12 Jun 2024 09:55:29 -0700 Subject: [PATCH 1/2] Support swift-syntax from 600.0.0-latest The Xcode 16 beta generates macro projects using these swift-syntax snapshots. Luckily things seem to be backwards compatible, so we can expand our supported range. --- .../xcshareddata/swiftpm/Package.resolved | 4 ++-- Package@swift-5.9.swift | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Dependencies.xcworkspace/xcshareddata/swiftpm/Package.resolved b/Dependencies.xcworkspace/xcshareddata/swiftpm/Package.resolved index 356c6c59..9f376e6d 100644 --- a/Dependencies.xcworkspace/xcshareddata/swiftpm/Package.resolved +++ b/Dependencies.xcworkspace/xcshareddata/swiftpm/Package.resolved @@ -14,8 +14,8 @@ "kind" : "remoteSourceControl", "location" : "https://github.com/apple/swift-argument-parser", "state" : { - "revision" : "46989693916f56d1186bd59ac15124caef896560", - "version" : "1.3.1" + "revision" : "0fbc8848e389af3bb55c182bc19ca9d5dc2f255b", + "version" : "1.4.0" } }, { diff --git a/Package@swift-5.9.swift b/Package@swift-5.9.swift index 81139cbb..5a3ff2a6 100644 --- a/Package@swift-5.9.swift +++ b/Package@swift-5.9.swift @@ -22,7 +22,7 @@ let package = Package( ), ], dependencies: [ - .package(url: "https://github.com/apple/swift-syntax", "509.0.0"..<"511.0.0"), + .package(url: "https://github.com/apple/swift-syntax", "509.0.0"..<"601.0.0"), .package(url: "https://github.com/google/swift-benchmark", from: "0.1.0"), .package(url: "https://github.com/pointfreeco/combine-schedulers", from: "1.0.0"), .package(url: "https://github.com/pointfreeco/swift-clocks", from: "1.0.0"), From cb5195b7d499a8c0f1a874a4403e81bf918108b5 Mon Sep 17 00:00:00 2001 From: Stephen Celis Date: Wed, 12 Jun 2024 11:46:53 -0700 Subject: [PATCH 2/2] wip --- .../xcshareddata/swiftpm/Package.resolved | 8 +++--- .../DependencyClientMacro.swift | 27 ++++++++++++++----- .../DependencyEndpointMacro.swift | 25 ++++++++++++----- .../DependenciesMacrosPlugin/Support.swift | 15 ++++++++++- 4 files changed, 56 insertions(+), 19 deletions(-) diff --git a/Dependencies.xcworkspace/xcshareddata/swiftpm/Package.resolved b/Dependencies.xcworkspace/xcshareddata/swiftpm/Package.resolved index 9f376e6d..99565fde 100644 --- a/Dependencies.xcworkspace/xcshareddata/swiftpm/Package.resolved +++ b/Dependencies.xcworkspace/xcshareddata/swiftpm/Package.resolved @@ -68,8 +68,8 @@ "kind" : "remoteSourceControl", "location" : "https://github.com/pointfreeco/swift-macro-testing", "state" : { - "revision" : "5c4a1b9d7c23cd5c08ea50677d8e89080365cb00", - "version" : "0.4.0" + "revision" : "851c8b6bde2000d8051dc9aca1efee04dcc37411", + "version" : "0.4.1" } }, { @@ -77,8 +77,8 @@ "kind" : "remoteSourceControl", "location" : "https://github.com/pointfreeco/swift-snapshot-testing", "state" : { - "revision" : "625ccca8570773dd84a34ee51a81aa2bc5a4f97a", - "version" : "1.16.0" + "revision" : "8ddd519780452729c6634ad6bd0d2595938e9ea3", + "version" : "1.16.1" } }, { diff --git a/Sources/DependenciesMacrosPlugin/DependencyClientMacro.swift b/Sources/DependenciesMacrosPlugin/DependencyClientMacro.swift index 9400435b..942aa6b3 100644 --- a/Sources/DependenciesMacrosPlugin/DependencyClientMacro.swift +++ b/Sources/DependenciesMacrosPlugin/DependencyClientMacro.swift @@ -2,9 +2,12 @@ import SwiftDiagnostics import SwiftOperators import SwiftSyntax import SwiftSyntaxBuilder -import SwiftSyntaxMacroExpansion import SwiftSyntaxMacros +#if !canImport(SwiftSyntax600) + import SwiftSyntaxMacroExpansion +#endif + public enum DependencyClientMacro: MemberAttributeMacro, MemberMacro { public static func expansion( of node: AttributeSyntax, @@ -30,7 +33,7 @@ public enum DependencyClientMacro: MemberAttributeMacro, MemberMacro { if let initializer = binding.initializer { guard try !initializer.diagnose(node, context: context).earlyOut else { return [] } - } else if functionType.effectSpecifiers?.throwsSpecifier == nil, + } else if functionType.effectSpecifiers?.hasThrowsClause != true, !functionType.isVoid, !functionType.isOptional { @@ -173,12 +176,22 @@ public enum DependencyClientMacro: MemberAttributeMacro, MemberMacro { ) binding.typeAnnotation?.type = TypeSyntax(attributedTypeSyntax) } else if let typeSyntax = type.as(FunctionTypeSyntax.self) { - binding.typeAnnotation?.type = TypeSyntax( - AttributedTypeSyntax( - attributes: [.attribute("@escaping").with(\.trailingTrivia, .space)], - baseType: typeSyntax + #if canImport(SwiftSyntax600) + binding.typeAnnotation?.type = TypeSyntax( + AttributedTypeSyntax( + specifiers: [], + attributes: [.attribute("@escaping").with(\.trailingTrivia, .space)], + baseType: typeSyntax + ) ) - ) + #else + binding.typeAnnotation?.type = TypeSyntax( + AttributedTypeSyntax( + attributes: [.attribute("@escaping").with(\.trailingTrivia, .space)], + baseType: typeSyntax + ) + ) + #endif } else if binding.typeAnnotation == nil { binding.pattern.trailingTrivia = "" binding.typeAnnotation = TypeAnnotationSyntax( diff --git a/Sources/DependenciesMacrosPlugin/DependencyEndpointMacro.swift b/Sources/DependenciesMacrosPlugin/DependencyEndpointMacro.swift index 44a330dc..7cb9b283 100644 --- a/Sources/DependenciesMacrosPlugin/DependencyEndpointMacro.swift +++ b/Sources/DependenciesMacrosPlugin/DependencyEndpointMacro.swift @@ -3,9 +3,12 @@ import SwiftOperators import SwiftParser import SwiftSyntax import SwiftSyntaxBuilder -import SwiftSyntaxMacroExpansion import SwiftSyntaxMacros +#if !canImport(SwiftSyntax600) + import SwiftSyntaxMacroExpansion +#endif + public enum DependencyEndpointMacro: AccessorMacro, PeerMacro { public static func expansion( of node: AttributeSyntax, @@ -98,7 +101,7 @@ public enum DependencyEndpointMacro: AccessorMacro, PeerMacro { unimplementedDefault = closure } else { unimplementedDefault = functionType.unimplementedDefault - if functionType.effectSpecifiers?.throwsSpecifier != nil { + if functionType.effectSpecifiers?.hasThrowsClause == true { unimplementedDefault.statements.append( """ throw DependenciesMacros.Unimplemented("\(raw: unescapedIdentifier)") @@ -134,7 +137,7 @@ public enum DependencyEndpointMacro: AccessorMacro, PeerMacro { .with(\.leadingTrivia, .newline) } var effectSpecifiers = "" - if functionType.effectSpecifiers?.throwsSpecifier != nil { + if functionType.effectSpecifiers?.hasThrowsClause == true { effectSpecifiers.append("try ") } if functionType.effectSpecifiers?.asyncSpecifier != nil { @@ -370,10 +373,18 @@ extension TupleTypeElementSyntax { } fileprivate var isInout: Bool { - self.type - .as(AttributedTypeSyntax.self)? - .specifier? - .tokenKind == .keyword(.inout) + #if canImport(SwiftSyntax600) + self.type + .as(AttributedTypeSyntax.self)? + .specifiers.contains( + where: { $0.as(SimpleTypeSpecifierSyntax.self)?.specifier.tokenKind == .keyword(.inout) } + ) == true + #else + self.type + .as(AttributedTypeSyntax.self)? + .specifier? + .tokenKind == .keyword(.inout) + #endif } } diff --git a/Sources/DependenciesMacrosPlugin/Support.swift b/Sources/DependenciesMacrosPlugin/Support.swift index 13bef324..28bb6653 100644 --- a/Sources/DependenciesMacrosPlugin/Support.swift +++ b/Sources/DependenciesMacrosPlugin/Support.swift @@ -1,9 +1,12 @@ import SwiftDiagnostics import SwiftSyntax import SwiftSyntaxBuilder -import SwiftSyntaxMacroExpansion import SwiftSyntaxMacros +#if !canImport(SwiftSyntax600) + import SwiftSyntaxMacroExpansion +#endif + extension SyntaxStringInterpolation { mutating func appendInterpolation(_ node: Node?) { if let node { @@ -232,3 +235,13 @@ extension Array where Element == String { self.flatMap { [$0, "\(module).\($0)"] } } } + +extension TypeEffectSpecifiersSyntax { + var hasThrowsClause: Bool { + #if canImport(SwiftSyntax600) + throwsClause != nil + #else + throwsSpecifier != nil + #endif + } +}