Skip to content

Commit 9daab13

Browse files
authored
Support suggesting a combination of parameter types and return types as disambiguation when the function returns 'void' (#1156)
* Support suggesting a combination of parameter types and return types as disambiguation when the function returns 'void' rdar://143897549 * Update year in header comment for modified files
1 parent 1ad9b6a commit 9daab13

File tree

4 files changed

+74
-5
lines changed

4 files changed

+74
-5
lines changed

Sources/SwiftDocC/DocumentationService/Convert/Symbol Link Resolution/LinkCompletionTools.swift

Lines changed: 1 addition & 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) 2024 Apple Inc. and the Swift project authors
4+
Copyright (c) 2024-2025 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

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -266,8 +266,7 @@ extension PathHierarchy.DisambiguationContainer {
266266
types: { element in
267267
guard let parameterTypes = element.parameterTypes,
268268
!parameterTypes.isEmpty,
269-
let returnTypes = element.returnTypes,
270-
!returnTypes.isEmpty
269+
let returnTypes = element.returnTypes
271270
else {
272271
return nil
273272
}

Tests/SwiftDocCTests/Infrastructure/PathHierarchyTests.swift

Lines changed: 50 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-2024 Apple Inc. and the Swift project authors
4+
Copyright (c) 2022-2025 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
@@ -3460,6 +3460,55 @@ class PathHierarchyTests: XCTestCase {
34603460
])
34613461
}
34623462

3463+
// Each overload requires a combination parameters and return values to disambiguate
3464+
do {
3465+
// Int -> ()
3466+
// Bool -> ()
3467+
// Int -> Int
3468+
let catalog = Folder(name: "unit-test.docc", content: [
3469+
JSONFile(name: "ModuleName.symbols.json", content: makeSymbolGraph(
3470+
moduleName: "ModuleName",
3471+
symbols: [
3472+
// Int -> Void
3473+
makeSymbol(id: "function-overload-1", kind: .func, pathComponents: ["doSomething(first:)"], signature: .init(
3474+
parameters: [
3475+
makeParameter("first", decl: [intType]), // Int
3476+
], returns: makeFragments([ // ->
3477+
voidType // ()
3478+
])
3479+
)),
3480+
3481+
// Bool -> Void
3482+
makeSymbol(id: "function-overload-2", kind: .func, pathComponents: ["doSomething(first:)"], signature: .init(
3483+
parameters: [
3484+
makeParameter("first", decl: [boolType]), // Bool
3485+
], returns: makeFragments([ // ->
3486+
voidType // ()
3487+
])
3488+
)),
3489+
3490+
// Int -> Int
3491+
makeSymbol(id: "function-overload-3", kind: .func, pathComponents: ["doSomething(first:)"], signature: .init(
3492+
parameters: [
3493+
makeParameter("first", decl: [intType]), // Int
3494+
], returns: makeFragments([ // ->
3495+
intType // Int
3496+
])
3497+
)),
3498+
]
3499+
))
3500+
])
3501+
3502+
let (_, context) = try loadBundle(catalog: catalog)
3503+
let tree = context.linkResolver.localResolver.pathHierarchy
3504+
3505+
try assertPathCollision("ModuleName/doSomething(first:)", in: tree, collisions: [
3506+
(symbolID: "function-overload-1", disambiguation: "-(Int)->()"), // ( Int ) -> ()
3507+
(symbolID: "function-overload-2", disambiguation: "-(Bool)"), // ( Bool )
3508+
(symbolID: "function-overload-3", disambiguation: "->_"), // -> _
3509+
])
3510+
}
3511+
34633512
// Two overloads with more than 64 parameters, but some unique
34643513
do {
34653514
let spellOutFormatter = NumberFormatter()

Tests/SwiftDocCTests/Infrastructure/Symbol Link Resolution/LinkCompletionToolsTests.swift

Lines changed: 22 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) 2024 Apple Inc. and the Swift project authors
4+
Copyright (c) 2024-2025 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
@@ -161,4 +161,25 @@ class LinkCompletionToolsTests: XCTestCase {
161161
"-(_,String)", "->Wrapped", "-(_,Double)",
162162
])
163163
}
164+
165+
func testSuggestingBothParameterAndReturnTypesInTheSameDisambiguation() {
166+
let overloads = [
167+
(parameters: ["Int"], returns: []), // (Int) -> Void
168+
(parameters: ["Bool"], returns: []), // (Bool) -> Void
169+
(parameters: ["Int"], returns: ["Int"]), // (Int) -> Int
170+
].map {
171+
LinkCompletionTools.SymbolInformation(
172+
kind: "func",
173+
symbolIDHash: "\($0)".stableHashString,
174+
parameterTypes: $0.parameters,
175+
returnTypes: $0.returns
176+
)
177+
}
178+
179+
XCTAssertEqual(LinkCompletionTools.suggestedDisambiguation(forCollidingSymbols: overloads), [
180+
"-(Int)->()", // Only parameter type would be ambiguous with 3rd overload & only return type would be ambiguous with 2nd overload.
181+
"-(Bool)", // The only overload with a `Bool` value
182+
"->_", // The only overload that returns something
183+
])
184+
}
164185
}

0 commit comments

Comments
 (0)