Skip to content

Commit 40fe88f

Browse files
authored
Merge pull request #1532 from kimdv/kimdv/remove-ComputedLocation
2 parents 3e3bd6a + 0f1666e commit 40fe88f

File tree

6 files changed

+30
-97
lines changed

6 files changed

+30
-97
lines changed

Sources/SwiftCompilerPluginMessageHandling/PluginMacroExpansionContext.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,8 @@ class SourceManager {
160160
return SourceLocation(
161161
// NOTE: IUO because 'localLocation' is created by a location converter
162162
// which guarantees non-nil line/column.
163-
line: localLocation.line! + lineOffset,
164-
column: localLocation.column! + columnOffset,
163+
line: localLocation.line + lineOffset,
164+
column: localLocation.column + columnOffset,
165165
offset: localLocation.offset + positionOffset,
166166
file: file
167167
)

Sources/SwiftDiagnostics/DiagnosticsFormatter.swift

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -112,32 +112,28 @@ public struct DiagnosticsFormatter {
112112
return nil
113113
}
114114

115-
let startLoc = highlight.startLocation(converter: slc, afterLeadingTrivia: true);
116-
guard let startLine = startLoc.line else {
117-
return nil
118-
}
115+
let startLoc = highlight.startLocation(converter: slc, afterLeadingTrivia: true)
116+
let startLine = startLoc.line
119117

120118
// Find the starting column.
121119
let startColumn: Int
122120
if startLine < lineNumber {
123121
startColumn = 1
124-
} else if startLine == lineNumber, let column = startLoc.column {
125-
startColumn = column
122+
} else if startLine == lineNumber {
123+
startColumn = startLoc.column
126124
} else {
127125
return nil
128126
}
129127

130128
// Find the ending column.
131129
let endLoc = highlight.endLocation(converter: slc, afterTrailingTrivia: false)
132-
guard let endLine = endLoc.line else {
133-
return nil
134-
}
130+
let endLine = endLoc.line
135131

136132
let endColumn: Int
137133
if endLine > lineNumber {
138134
endColumn = annotatedLine.sourceString.count
139-
} else if endLine == lineNumber, let column = endLoc.column {
140-
endColumn = column
135+
} else if endLine == lineNumber {
136+
endColumn = endLoc.column
141137
} else {
142138
return nil
143139
}
@@ -283,9 +279,9 @@ public struct DiagnosticsFormatter {
283279
annotatedSource.append("\n")
284280
}
285281

286-
let columnsWithDiagnostics = Set(annotatedLine.diagnostics.map { $0.location(converter: slc).column ?? 0 })
282+
let columnsWithDiagnostics = Set(annotatedLine.diagnostics.map { $0.location(converter: slc).column })
287283
let diagsPerColumn = Dictionary(grouping: annotatedLine.diagnostics) { diag in
288-
diag.location(converter: slc).column ?? 0
284+
diag.location(converter: slc).column
289285
}.sorted { lhs, rhs in
290286
lhs.key > rhs.key
291287
}

Sources/SwiftSyntax/SourceLocation.swift

Lines changed: 8 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -10,83 +10,32 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13-
/// Represent the user-facing part of SourceLocation that can be calculated
14-
/// on demand.
15-
struct ComputedLocation: Hashable, Codable, CustomDebugStringConvertible {
16-
/// The line in the file where this location resides. 1-based.
17-
let line: Int
18-
19-
/// The UTF-8 byte offset from the beginning of the line where this location
20-
/// resides. 1-based.
21-
let column: Int
22-
23-
/// The file in which this location resides.
24-
let file: String
25-
26-
var debugDescription: String {
27-
// Print file name?
28-
return "\(line):\(column)"
29-
}
30-
31-
init(line: Int, column: Int, file: String) {
32-
self.line = line
33-
self.column = column
34-
self.file = file
35-
}
36-
init(offset: Int, using converter: SourceLocationConverter) {
37-
let loc = converter.location(for: AbsolutePosition(utf8Offset: offset))
38-
precondition(loc.offset == offset)
39-
self.line = loc.line!
40-
self.column = loc.column!
41-
self.file = loc.file!
42-
}
43-
}
44-
4513
/// Represents a source location in a Swift file.
4614
public struct SourceLocation: Hashable, Codable, CustomDebugStringConvertible {
4715

48-
/// Line and column that can be computed on demand.
49-
private var compLoc: ComputedLocation?
50-
5116
/// The UTF-8 byte offset into the file where this location resides.
5217
public let offset: Int
5318

5419
/// The line in the file where this location resides. 1-based.
55-
public var line: Int? {
56-
return compLoc?.line
57-
}
20+
public var line: Int
5821

5922
/// The UTF-8 byte offset from the beginning of the line where this location
6023
/// resides. 1-based.
61-
public var column: Int? {
62-
return compLoc?.column
63-
}
24+
public let column: Int
6425

6526
/// The file in which this location resides.
66-
public var file: String? {
67-
return compLoc?.file
68-
}
27+
public let file: String
6928

7029
public var debugDescription: String {
71-
guard let compLoc = compLoc else {
72-
return "\(offset)"
73-
}
74-
return compLoc.debugDescription
30+
// Print file name?
31+
return "\(line):\(column)"
7532
}
7633

7734
public init(line: Int, column: Int, offset: Int, file: String) {
35+
self.line = line
7836
self.offset = offset
79-
self.compLoc = ComputedLocation(line: line, column: column, file: file)
80-
}
81-
82-
/// Initialize SourceLocation with a utf8 offset.
83-
/// If a SourceLocationConverter is given, line, column and file will be populated;
84-
/// otherwise they will be nil.
85-
public init(offset: Int, converter: SourceLocationConverter? = nil) {
86-
self.offset = offset
87-
if let converter = converter {
88-
self.compLoc = ComputedLocation(offset: offset, using: converter)
89-
}
37+
self.column = column
38+
self.file = file
9039
}
9140
}
9241

Sources/SwiftSyntaxMacros/MacroExpansionContext.swift

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -134,18 +134,14 @@ extension MacroExpansionContext {
134134
at position: PositionInSyntaxNode,
135135
filePathMode: SourceLocationFilePathMode
136136
) -> AbstractSourceLocation? {
137-
guard let sourceLoc: SourceLocation = location(of: node, at: position, filePathMode: filePathMode),
138-
let file = sourceLoc.file,
139-
let line = sourceLoc.line,
140-
let column = sourceLoc.column
141-
else {
137+
guard let sourceLoc: SourceLocation = location(of: node, at: position, filePathMode: filePathMode) else {
142138
return nil
143139
}
144140

145141
return AbstractSourceLocation(
146-
file: "\(literal: file)",
147-
line: "\(literal: line)",
148-
column: "\(literal: column)"
142+
file: "\(literal: sourceLoc.file)",
143+
line: "\(literal: sourceLoc.line)",
144+
column: "\(literal: sourceLoc.column)"
149145
)
150146
}
151147

Tests/SwiftParserTest/Assertions.swift

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -336,16 +336,8 @@ func assertLocation<T: SyntaxProtocol>(
336336
let locationConverter = SourceLocationConverter(file: "", source: tree.description)
337337
let actualLocation = location
338338
let expectedLocation = locationConverter.location(for: AbsolutePosition(utf8Offset: markerLoc))
339-
if let actualLine = actualLocation.line,
340-
let actualColumn = actualLocation.column,
341-
let expectedLine = expectedLocation.line,
342-
let expectedColumn = expectedLocation.column
343-
{
344-
if actualLine != expectedLine || actualColumn != expectedColumn {
345-
XCTFail("Expected location \(expectedLine):\(expectedColumn) but got \(actualLine):\(actualColumn)", file: file, line: line)
346-
}
347-
} else {
348-
XCTFail("Failed to resolve diagnostic location to line/column", file: file, line: line)
339+
if actualLocation.line != expectedLocation.line || actualLocation.column != expectedLocation.column {
340+
XCTFail("Expected location \(expectedLocation.line):\(expectedLocation.column) but got \(actualLocation.line):\(actualLocation.column)", file: file, line: line)
349341
}
350342
} else {
351343
XCTFail("Did not find marker \(locationMarker) in the source code", file: file, line: line)

Tests/SwiftSyntaxTest/AbsolutePositionTests.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ public class AbsolutePositionTests: XCTestCase {
155155
XCTAssertEqual(startLoc.line, 8)
156156
XCTAssertEqual(startLoc.column, 1)
157157
XCTAssertEqual(
158-
converter.position(ofLine: startLoc.line!, column: startLoc.column!),
158+
converter.position(ofLine: startLoc.line, column: startLoc.column),
159159
secondReturnStmt.positionAfterSkippingLeadingTrivia
160160
)
161161

@@ -167,7 +167,7 @@ public class AbsolutePositionTests: XCTestCase {
167167
XCTAssertEqual(startLocBeforeTrivia.line, 6)
168168
XCTAssertEqual(startLocBeforeTrivia.column, 1)
169169
XCTAssertEqual(
170-
converter.position(ofLine: startLocBeforeTrivia.line!, column: startLocBeforeTrivia.column!),
170+
converter.position(ofLine: startLocBeforeTrivia.line, column: startLocBeforeTrivia.column),
171171
secondReturnStmt.position
172172
)
173173

@@ -183,9 +183,9 @@ public class AbsolutePositionTests: XCTestCase {
183183
XCTAssertEqual(endLocAfterTrivia.line, 11)
184184
XCTAssertEqual(endLocAfterTrivia.column, 1)
185185

186-
XCTAssertTrue(converter.isValid(line: startLoc.line!, column: startLoc.column!))
187-
XCTAssertFalse(converter.isValid(line: startLoc.line!, column: startLoc.column! + 50))
188-
XCTAssertFalse(converter.isValid(line: 0, column: startLoc.column!))
186+
XCTAssertTrue(converter.isValid(line: startLoc.line, column: startLoc.column))
187+
XCTAssertFalse(converter.isValid(line: startLoc.line, column: startLoc.column + 50))
188+
XCTAssertFalse(converter.isValid(line: 0, column: startLoc.column))
189189
XCTAssertTrue(converter.isValid(position: secondReturnStmt.position))
190190
XCTAssertFalse(converter.isValid(position: secondReturnStmt.position + SourceLength(utf8Length: 100)))
191191
}

0 commit comments

Comments
 (0)