Skip to content

Commit 4e6ecb9

Browse files
authored
Fix parsing bug for disambiguation with both parameter and return types (#1152) (#1154)
rdar://143817712
1 parent d7481b8 commit 4e6ecb9

File tree

2 files changed

+15
-8
lines changed

2 files changed

+15
-8
lines changed

Sources/SwiftDocC/Infrastructure/Link Resolution/PathHierarchy+TypeSignature.swift

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -474,20 +474,14 @@ extension PathHierarchy.PathParser {
474474
return PathComponent(full: String(original), name: name, disambiguation: .typeSignature(parameterTypes: parameterTypes, returnTypes: nil))
475475
} else if scanner.hasPrefix("->") {
476476
_ = scanner.take(2)
477-
let returnTypes = scanner.scanArguments() // The return types (tuple or not) can be parsed the same as the arguments
477+
let returnTypes = scanner.scanReturnTypes()
478478
return PathComponent(full: String(original), name: name, disambiguation: .typeSignature(parameterTypes: parameterTypes, returnTypes: returnTypes))
479479
}
480480
} else if let parameterStartRange = possibleDisambiguationText.range(of: "->") {
481481
let name = original[..<parameterStartRange.lowerBound]
482482
var scanner = StringScanner(original[parameterStartRange.upperBound...])
483483

484-
let returnTypes: [Substring]
485-
if scanner.peek() == "(" {
486-
_ = scanner.take() // the leading parenthesis
487-
returnTypes = scanner.scanArguments() // The return types (tuple or not) can be parsed the same as the arguments
488-
} else {
489-
returnTypes = [scanner.takeAll()]
490-
}
484+
let returnTypes = scanner.scanReturnTypes()
491485
return PathComponent(full: String(original), name: name, disambiguation: .typeSignature(parameterTypes: nil, returnTypes: returnTypes))
492486
}
493487

@@ -541,6 +535,15 @@ private struct StringScanner {
541535

542536
// MARK: Parsing argument types by scanning
543537

538+
mutating func scanReturnTypes() -> [Substring] {
539+
if peek() == "(" {
540+
_ = take() // the leading parenthesis
541+
return scanArguments() // The return types (tuple or not) can be parsed the same as the arguments
542+
} else {
543+
return [takeAll()]
544+
}
545+
}
546+
544547
mutating func scanArguments() -> [Substring] {
545548
guard peek() != ")" else {
546549
_ = take() // drop the ")"

Tests/SwiftDocCTests/Infrastructure/PathHierarchyTests.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3627,6 +3627,10 @@ class PathHierarchyTests: XCTestCase {
36273627

36283628
assertParsedPathComponents("something(first:second:third:)->(String,Int,Bool)", [("something(first:second:third:)", .typeSignature(parameterTypes: nil, returnTypes: ["String", "Int", "Bool"]))])
36293629

3630+
assertParsedPathComponents("something(first:second:third:)-(Int,_)->()", [("something(first:second:third:)", .typeSignature(parameterTypes: ["Int", "_"], returnTypes: []))])
3631+
assertParsedPathComponents("something(first:second:third:)-(Int,_)->Int", [("something(first:second:third:)", .typeSignature(parameterTypes: ["Int", "_"], returnTypes: ["Int"]))])
3632+
assertParsedPathComponents("something(first:second:third:)-(Int,_)->(String,Int,Bool)", [("something(first:second:third:)", .typeSignature(parameterTypes: ["Int", "_"], returnTypes: ["String", "Int", "Bool"]))])
3633+
36303634
// Check closure parameters
36313635
assertParsedPathComponents("map(_:)-((Element)->T)", [("map(_:)", .typeSignature(parameterTypes: ["(Element)->T"], returnTypes: nil))])
36323636
assertParsedPathComponents("map(_:)->[T]", [("map(_:)", .typeSignature(parameterTypes: nil, returnTypes: ["[T]"]))])

0 commit comments

Comments
 (0)