Skip to content

Commit

Permalink
Support swift-syntax from 600.0.0-latest (#223)
Browse files Browse the repository at this point in the history
* 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.

* wip
  • Loading branch information
stephencelis committed Jun 12, 2024
1 parent 76b98de commit 13de2b8
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 22 deletions.
12 changes: 6 additions & 6 deletions Dependencies.xcworkspace/xcshareddata/swiftpm/Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
},
{
Expand Down Expand Up @@ -68,17 +68,17 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/pointfreeco/swift-macro-testing",
"state" : {
"revision" : "5c4a1b9d7c23cd5c08ea50677d8e89080365cb00",
"version" : "0.4.0"
"revision" : "851c8b6bde2000d8051dc9aca1efee04dcc37411",
"version" : "0.4.1"
}
},
{
"identity" : "swift-snapshot-testing",
"kind" : "remoteSourceControl",
"location" : "https://github.com/pointfreeco/swift-snapshot-testing",
"state" : {
"revision" : "625ccca8570773dd84a34ee51a81aa2bc5a4f97a",
"version" : "1.16.0"
"revision" : "8ddd519780452729c6634ad6bd0d2595938e9ea3",
"version" : "1.16.1"
}
},
{
Expand Down
2 changes: 1 addition & 1 deletion [email protected]
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down
27 changes: 20 additions & 7 deletions Sources/DependenciesMacrosPlugin/DependencyClientMacro.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<D: DeclGroupSyntax, M: DeclSyntaxProtocol, C: MacroExpansionContext>(
of node: AttributeSyntax,
Expand All @@ -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
{
Expand Down Expand Up @@ -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(
Expand Down
25 changes: 18 additions & 7 deletions Sources/DependenciesMacrosPlugin/DependencyEndpointMacro.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<D: DeclSyntaxProtocol, C: MacroExpansionContext>(
of node: AttributeSyntax,
Expand Down Expand Up @@ -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)")
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
}

Expand Down
15 changes: 14 additions & 1 deletion Sources/DependenciesMacrosPlugin/Support.swift
Original file line number Diff line number Diff line change
@@ -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: SyntaxProtocol>(_ node: Node?) {
if let node {
Expand Down Expand Up @@ -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
}
}

0 comments on commit 13de2b8

Please sign in to comment.