Skip to content

[5.8] Adjustments because SwiftSyntax cherry-picks most API changes from main to release/5.8 #63258

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/ASTGen/Sources/ASTGen/Decls.swift
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ extension ASTGenVisitor {

let loc = self.base.advanced(by: node.position.utf8Offset).raw
let isStateic = false // TODO: compute this
let isLet = node.letOrVarKeyword.tokenKind == .letKeyword
let isLet = node.letOrVarKeyword.tokenKind == .keyword(.let)

// TODO: don't drop "initializer" on the floor.
return .decl(
Expand Down
2 changes: 1 addition & 1 deletion lib/ASTGen/Sources/ASTGen/Exprs.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ extension ASTGenVisitor {
let tupleElement = TupleExprElementSyntax(
label: nil, colon: nil, expression: ExprSyntax(trailingClosure), trailingComma: nil)

return visit(node.addArgument(tupleElement).withTrailingClosure(nil))
return visit(node.addArgument(tupleElement).with(\.trailingClosure, nil))
}

let args = visit(node.argumentList).rawValue
Expand Down
2 changes: 1 addition & 1 deletion lib/ASTGen/Sources/ASTGen/Literals.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ extension ASTGenVisitor {

public func visit(_ node: BooleanLiteralExprSyntax) -> ASTNode {
let loc = self.base.advanced(by: node.position.utf8Offset).raw
let value = node.booleanLiteral == .trueKeyword()
let value = node.booleanLiteral == .keyword(.true)
return .expr(SwiftBooleanLiteralExpr_create(ctx, value, loc))
}

Expand Down
4 changes: 2 additions & 2 deletions lib/ASTGen/Sources/ASTGen/Macros.swift
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ func evaluateMacro(
}

// Emit diagnostics accumulated in the context.
let macroName = parentExpansion.macro.withoutTrivia().description
let macroName = parentExpansion.macro.trimmedDescription
for diag in context.diagnostics {
emitDiagnostic(
diagEnginePtr: diagEnginePtr,
Expand All @@ -186,7 +186,7 @@ func evaluateMacro(
)
}

var evaluatedSyntaxStr = evaluatedSyntax.withoutTrivia().description
var evaluatedSyntaxStr = evaluatedSyntax.trimmedDescription
evaluatedSyntaxStr.withUTF8 { utf8 in
let evaluatedResultPtr = UnsafeMutablePointer<UInt8>.allocate(capacity: utf8.count + 1)
if let baseAddress = utf8.baseAddress {
Expand Down
8 changes: 4 additions & 4 deletions lib/ASTGen/Sources/ASTGen/Types.swift
Original file line number Diff line number Diff line change
Expand Up @@ -145,12 +145,12 @@ extension ASTGenVisitor {
let lParenLoc = self.base.advanced(by: node.leftParen.position.utf8Offset).raw
let rParenLoc = self.base.advanced(by: node.rightParen.position.utf8Offset).raw
let args = TupleTypeRepr_create(self.ctx, elements, lParenLoc, rParenLoc)
let asyncLoc = node.asyncKeyword.map { self.base.advanced(by: $0.position.utf8Offset).raw }
let throwsLoc = node.throwsOrRethrowsKeyword.map {
let asyncLoc = node.effectSpecifiers?.asyncSpecifier.map { self.base.advanced(by: $0.position.utf8Offset).raw }
let throwsLoc = node.effectSpecifiers?.throwsSpecifier.map {
self.base.advanced(by: $0.position.utf8Offset).raw
}
let arrowLoc = self.base.advanced(by: node.arrow.position.utf8Offset).raw
let retTy = visit(node.returnType).rawValue
let arrowLoc = self.base.advanced(by: node.output.arrow.position.utf8Offset).raw
let retTy = visit(node.output.returnType).rawValue
return .type(FunctionTypeRepr_create(self.ctx, args, asyncLoc, throwsLoc, arrowLoc, retTy))
}
}
Expand Down
28 changes: 15 additions & 13 deletions test/Macros/Inputs/syntax_macro_definitions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ private func replaceFirstLabel(
}

return tuple.replacing(
childAt: 0, with: firstElement.withLabel(.identifier(newLabel)))
childAt: 0, with: firstElement.with(\.label, .identifier(newLabel)))
}

public struct ColorLiteralMacro: ExpressionMacro {
Expand All @@ -26,7 +26,7 @@ public struct ColorLiteralMacro: ExpressionMacro {
)
let initSyntax: ExprSyntax = ".init(\(argList))"
if let leadingTrivia = macro.leadingTrivia {
return initSyntax.withLeadingTrivia(leadingTrivia)
return initSyntax.with(\.leadingTrivia, leadingTrivia)
}
return initSyntax
}
Expand All @@ -36,9 +36,9 @@ public struct FileIDMacro: ExpressionMacro {
public static func expansion(
of macro: MacroExpansionExprSyntax, in context: inout MacroExpansionContext
) -> ExprSyntax {
let fileLiteral: ExprSyntax = #""\#(context.moduleName)/\#(context.fileName)""#
let fileLiteral: ExprSyntax = #""\#(raw: context.moduleName)/\#(raw: context.fileName)""#
if let leadingTrivia = macro.leadingTrivia {
return fileLiteral.withLeadingTrivia(leadingTrivia)
return fileLiteral.with(\.leadingTrivia, leadingTrivia)
}
return fileLiteral
}
Expand Down Expand Up @@ -82,9 +82,9 @@ public struct AddBlocker: ExpressionMacro {
}

// Link the folded argument back into the tree.
var node = node.withArgumentList(node.argumentList.replacing(childAt: 0, with: node.argumentList.first!.withExpression(foldedArgument.as(ExprSyntax.self)!)))
let node = node.with(\.argumentList, node.argumentList.replacing(childAt: 0, with: node.argumentList.first!.with(\.expression, foldedArgument.as(ExprSyntax.self)!)))

class AddVisitor: SyntaxRewriter {
class AddVisitor: SyntaxRewriter {
var diagnostics: [Diagnostic] = []

override func visit(
Expand All @@ -102,8 +102,8 @@ public struct AddBlocker: ExpressionMacro {
severity: .error
),
highlights: [
Syntax(node.leftOperand.withoutTrivia()),
Syntax(node.rightOperand.withoutTrivia())
Syntax(node.leftOperand.with(\.leadingTrivia, []).with(\.trailingTrivia, [])),
Syntax(node.rightOperand.with(\.leadingTrivia, []).with(\.trailingTrivia, []))
],
fixIts: [
FixIt(
Expand All @@ -114,10 +114,10 @@ public struct AddBlocker: ExpressionMacro {
),
changes: [
FixIt.Change.replace(
oldNode: Syntax(binOp.operatorToken.withoutTrivia()),
oldNode: Syntax(binOp.operatorToken.with(\.leadingTrivia, []).with(\.trailingTrivia, [])),
newNode: Syntax(
TokenSyntax(
.spacedBinaryOperator("-"),
.binaryOperator("-"),
presence: .present
)
)
Expand All @@ -129,10 +129,12 @@ public struct AddBlocker: ExpressionMacro {
)

return ExprSyntax(
node.withOperatorOperand(
node.with(
\.operatorOperand,
ExprSyntax(
binOp.withOperatorToken(
binOp.operatorToken.withKind(.spacedBinaryOperator("-"))
binOp.with(
\.operatorToken,
binOp.operatorToken.withKind(.binaryOperator("-"))
)
)
)
Expand Down
2 changes: 1 addition & 1 deletion test/Parse/new_parser_diagnostics.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@
// REQUIRES: asserts

_ = [(Int) -> async throws Int]()
// expected-error@-1{{'async throws' may only occur before '->'}}
// expected-error@-1{{'async throws' must preceed '->'}}
// expected-note@-2{{move 'async throws' in front of '->'}}{{15-21=}} {{21-28=}} {{20-21= }} {{12-12=async }} {{12-12=throws }}