Skip to content

Commit c5b408f

Browse files
committed
Use presumed file/line number when printing grouped diagnostics
For the primary diagnostic, print the presumed file/line number in the same manner as the Swift compiler does. In the actual code listing, keep the line numbers and code from the actual source file that was parsed, because there's no sensible way to map the source from the `#sourceLocation`-named file (which might not exist).
1 parent 2f5dd64 commit c5b408f

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

Sources/SwiftDiagnostics/GroupedDiagnostics.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ extension GroupedDiagnostics {
207207

208208
// Display file/line/column and diagnostic text for the primary diagnostic.
209209
prefixString =
210-
"\(location.file):\(location.line):\(location.column): \(diagnosticDecorator.decorateDiagnosticMessage(primaryDiag.diagMessage))\n"
210+
"\(location.presumedFile):\(location.presumedLine):\(location.column): \(diagnosticDecorator.decorateDiagnosticMessage(primaryDiag.diagMessage))\n"
211211

212212
// If the primary diagnostic source file is not the same as the root source file, we're pointing into a generated buffer.
213213
// Provide a link back to the original source file where this generated buffer occurred, so it's easy to find if

Tests/SwiftDiagnosticsTest/GroupDiagnosticsFormatterTests.swift

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,31 @@ extension GroupedDiagnostics {
6060
}
6161

6262
final class GroupedDiagnosticsFormatterTests: XCTestCase {
63+
func testSourceLocations() {
64+
var group = GroupedDiagnostics()
65+
66+
// Main source file.
67+
_ = group.addTestFile(
68+
"""
69+
#sourceLocation(file: "other.swift", line: 123)
70+
let pi = 3.14159 x
71+
""",
72+
displayName: "main.swift",
73+
diagnosticDescriptors: []
74+
)
75+
let annotated = DiagnosticsFormatter.annotateSources(in: group)
76+
assertStringsEqualWithDiff(
77+
annotated,
78+
"""
79+
other.swift:123:17: error: consecutive statements on a line must be separated by newline or ';'
80+
1 │ #sourceLocation(file: "other.swift", line: 123)
81+
2 │ let pi = 3.14159 x
82+
│ ╰─ error: consecutive statements on a line must be separated by newline or ';'
83+
84+
"""
85+
)
86+
}
87+
6388
func testGroupingForMacroExpansion() {
6489
var group = GroupedDiagnostics()
6590

0 commit comments

Comments
 (0)