From b5698798d1c372544478ca55c4777e9ef1feab7e Mon Sep 17 00:00:00 2001 From: Kim de Vos Date: Thu, 13 Apr 2023 16:16:25 +0200 Subject: [PATCH] Remove force unwrapping for source location Companion of https://github.com/apple/swift-syntax/pull/1532 --- lib/ASTGen/Sources/ASTGen/PluginHost.swift | 4 ++-- test/Macros/Inputs/syntax_macro_definitions.swift | 8 +++----- 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/lib/ASTGen/Sources/ASTGen/PluginHost.swift b/lib/ASTGen/Sources/ASTGen/PluginHost.swift index bd89fa9444177..edf6e010c0a81 100644 --- a/lib/ASTGen/Sources/ASTGen/PluginHost.swift +++ b/lib/ASTGen/Sources/ASTGen/PluginHost.swift @@ -352,7 +352,7 @@ extension PluginMessage.Syntax { fileID: fileID, fileName: fileName, offset: loc.offset, - line: loc.line!, - column: loc.column!)) + line: loc.line, + column: loc.column)) } } diff --git a/test/Macros/Inputs/syntax_macro_definitions.swift b/test/Macros/Inputs/syntax_macro_definitions.swift index 5d54bd5918e47..e3fe7857c572f 100644 --- a/test/Macros/Inputs/syntax_macro_definitions.swift +++ b/test/Macros/Inputs/syntax_macro_definitions.swift @@ -38,13 +38,11 @@ public struct FileIDMacro: ExpressionMacro { of macro: Node, in context: Context ) throws -> ExprSyntax { - guard let sourceLoc = context.location(of: macro), - let fileID = sourceLoc.file - else { + guard let sourceLoc = context.location(of: macro) else { throw CustomError.message("can't find location for macro") } - let fileLiteral: ExprSyntax = "\(literal: fileID)" + let fileLiteral: ExprSyntax = "\(literal: sourceLoc.file)" return fileLiteral.with(\.leadingTrivia, macro.leadingTrivia) } } @@ -724,7 +722,7 @@ public enum LeftHandOperandFinderMacro: ExpressionMacro { fatalError("missing source location information") } - print("Source range for LHS is \(lhsStartLoc.file!): \(lhsStartLoc.line!):\(lhsStartLoc.column!)-\(lhsEndLoc.line!):\(lhsEndLoc.column!)") + print("Source range for LHS is \(lhsStartLoc.file): \(lhsStartLoc.line):\(lhsStartLoc.column)-\(lhsEndLoc.line):\(lhsEndLoc.column)") return .visitChildren }