Skip to content

Commit 4e9d7f8

Browse files
Kyle-Yed-ronnqvist
andauthored
Fix duplicated diagnostic issue (#733)
* Fix duplicated diagnostic issue If the range’s source is not matched with the resolver’s source, we should not emit a problem * Add test case for duplicated diagnostic issue * Update Sources/SwiftDocC/Semantics/MarkupReferenceResolver.swift Co-authored-by: David Rönnqvist <[email protected]> * Update Tests/SwiftDocCTests/Semantics/MarkupReferenceResolverTests.swift Co-authored-by: David Rönnqvist <[email protected]> * Update Tests/SwiftDocCTests/Semantics/MarkupReferenceResolverTests.swift Co-authored-by: David Rönnqvist <[email protected]> --------- Co-authored-by: David Rönnqvist <[email protected]>
1 parent 04be889 commit 4e9d7f8

File tree

4 files changed

+154
-1
lines changed

4 files changed

+154
-1
lines changed

Sources/SwiftDocC/Semantics/MarkupReferenceResolver.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,13 @@ struct MarkupReferenceResolver: MarkupRewriter {
8080
return resolved
8181

8282
case .failure(let unresolved, let error):
83+
if let rangeLowerBoundSource = range?.lowerBound.source,
84+
let rangeUpperBoundSource = range?.upperBound.source,
85+
let source = source,
86+
source != rangeLowerBoundSource || source != rangeUpperBoundSource {
87+
return nil
88+
}
89+
8390
if let callback = problemForUnresolvedReference,
8491
let problem = callback(unresolved, source, range, fromSymbolLink, error.message) {
8592
problems.append(problem)

Tests/SwiftDocCTests/Semantics/MarkupReferenceResolverTests.swift

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
This source file is part of the Swift.org open source project
33

4-
Copyright (c) 2022 Apple Inc. and the Swift project authors
4+
Copyright (c) 2022-2023 Apple Inc. and the Swift project authors
55
Licensed under Apache License v2.0 with Runtime Library Exception
66

77
See https://swift.org/LICENSE.txt for license information
@@ -27,4 +27,14 @@ class MarkupReferenceResolverTests: XCTestCase {
2727
_ = resolver.visit(document)
2828
XCTAssertEqual(0, resolver.problems.count)
2929
}
30+
31+
func testDuplicatedDiagnosticForExtensionFile() throws {
32+
let (_, context) = try testBundleAndContext(named: "ExtensionArticleBundle")
33+
// Before #733, symbols with documentation extension files emitted duplicated problems:
34+
// - one with a source location in the in-source documentation comment
35+
// - one with a source location in the documentation extension file.
36+
// The source range was only valid for one of these diagnostics. This resulted in an index out of range crash in DefaultDiagnosticConsoleFormatter when displaying line that caused the problem to the user
37+
XCTAssertEqual(1, context.problems.count)
38+
XCTAssertEqual("Server.md", context.problems.first?.diagnostic.source?.lastPathComponent)
39+
}
3040
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# ``Server``
2+
3+
<!-- The correct signature is ``Server/echo(_:)``-->
4+
Check this awesome function ``Server/echo()``
5+
6+
<!-- Copyright (c) 2023 Apple Inc and the Swift Project authors. All Rights Reserved. -->
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
{
2+
"metadata": {
3+
"formatVersion": { "major": 0, "minor": 6, "patch": 0 },
4+
"generator": "Apple Swift version 5.9 (swiftlang-5.9.0.128.108 clang-1500.0.40.1)"
5+
},
6+
"module": {
7+
"name": "ServerKit",
8+
"platform": {
9+
"architecture": "arm64",
10+
"vendor": "apple",
11+
"operatingSystem": {
12+
"name": "ios",
13+
"minimumVersion": { "major": 12, "minor": 0 }
14+
}
15+
}
16+
},
17+
"symbols": [
18+
{
19+
"kind": {
20+
"identifier": "swift.method",
21+
"displayName": "Instance Method"
22+
},
23+
"identifier": {
24+
"precise": "s:9ServerKit0A0C4echoyySSF",
25+
"interfaceLanguage": "swift"
26+
},
27+
"pathComponents": ["Server", "echo(_:)"],
28+
"names": {
29+
"title": "echo(_:)",
30+
"subHeading": [
31+
{ "kind": "keyword", "spelling": "func" },
32+
{ "kind": "text", "spelling": " " },
33+
{ "kind": "identifier", "spelling": "echo" },
34+
{ "kind": "text", "spelling": "(" },
35+
{
36+
"kind": "typeIdentifier",
37+
"spelling": "String",
38+
"preciseIdentifier": "s:SS"
39+
},
40+
{ "kind": "text", "spelling": ")" }
41+
]
42+
},
43+
"functionSignature": {
44+
"parameters": [
45+
{
46+
"name": "message",
47+
"declarationFragments": [
48+
{ "kind": "identifier", "spelling": "message" },
49+
{ "kind": "text", "spelling": ": " },
50+
{
51+
"kind": "typeIdentifier",
52+
"spelling": "String",
53+
"preciseIdentifier": "s:SS"
54+
}
55+
]
56+
}
57+
],
58+
"returns": [{ "kind": "text", "spelling": "()" }]
59+
},
60+
"declarationFragments": [
61+
{ "kind": "keyword", "spelling": "func" },
62+
{ "kind": "text", "spelling": " " },
63+
{ "kind": "identifier", "spelling": "echo" },
64+
{ "kind": "text", "spelling": "(" },
65+
{ "kind": "externalParam", "spelling": "_" },
66+
{ "kind": "text", "spelling": " " },
67+
{ "kind": "internalParam", "spelling": "message" },
68+
{ "kind": "text", "spelling": ": " },
69+
{
70+
"kind": "typeIdentifier",
71+
"spelling": "String",
72+
"preciseIdentifier": "s:SS"
73+
},
74+
{ "kind": "text", "spelling": ")" }
75+
],
76+
"accessLevel": "public",
77+
"location": {
78+
"uri": "file:///tmp/Server.swift",
79+
"position": { "line": 2, "character": 16 }
80+
}
81+
},
82+
{
83+
"kind": { "identifier": "swift.class", "displayName": "Class" },
84+
"identifier": {
85+
"precise": "s:9ServerKit0A0C",
86+
"interfaceLanguage": "swift"
87+
},
88+
"pathComponents": ["Server"],
89+
"names": {
90+
"title": "Server",
91+
"navigator": [{ "kind": "identifier", "spelling": "Server" }],
92+
"subHeading": [
93+
{ "kind": "keyword", "spelling": "class" },
94+
{ "kind": "text", "spelling": " " },
95+
{ "kind": "identifier", "spelling": "Server" }
96+
]
97+
},
98+
"docComment": {
99+
"uri": "file:///tmp/Server.swift",
100+
"module": "ServerKit",
101+
"lines": [
102+
{
103+
"range": {
104+
"start": { "line": 0, "character": 4 },
105+
"end": { "line": 0, "character": 15 }
106+
},
107+
"text": "Hello World"
108+
}
109+
]
110+
},
111+
"declarationFragments": [
112+
{ "kind": "keyword", "spelling": "class" },
113+
{ "kind": "text", "spelling": " " },
114+
{ "kind": "identifier", "spelling": "Server" }
115+
],
116+
"accessLevel": "public",
117+
"location": {
118+
"uri": "file:///tmp/Server.swift",
119+
"position": { "line": 1, "character": 13 }
120+
}
121+
}
122+
],
123+
"relationships": [
124+
{
125+
"kind": "memberOf",
126+
"source": "s:9ServerKit0A0C4echoyySSF",
127+
"target": "s:9ServerKit0A0C"
128+
}
129+
]
130+
}

0 commit comments

Comments
 (0)