Skip to content

Commit f9a0ad2

Browse files
authored
Merge pull request #382 from tayloraswift/desugar-static-self
desugar static Self type tokens when computing autographs
2 parents a6ffaa8 + 00cc04f commit f9a0ad2

15 files changed

+168
-57
lines changed

Scripts/Linux/GenerateTestSymbolGraphs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ swift build \
1111
-Xswiftc -emit-extension-block-symbols \
1212
-Xswiftc -include-spi-symbols \
1313
-Xswiftc -skip-inherited-docs \
14-
-Xswiftc -pretty-print
14+
-Xswiftc -pretty-print \
15+
-Xswiftc -swift-version -Xswiftc 6
1516

1617
swift package --package-path TestModules dump-package > TestModules/Package.swift.json
1718

Sources/MarkdownPluginSwift/Signatures/Signature.Expanded (ext).swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ extension Signature.Expanded
88
sugarDictionary:Scalar,
99
sugarArray:Scalar,
1010
sugarOptional:Scalar,
11+
desugarSelf:String? = nil,
1112
landmarks:inout SignatureLandmarks)
1213
{
1314
var utf8:[UInt8] = []
@@ -29,7 +30,8 @@ extension Signature.Expanded
2930
}
3031
}
3132

32-
let sugarMap:SignatureSyntax.SugarMap = linkTargets.reduce(into: .init())
33+
let sugarMap:SignatureSyntax.SugarMap = linkTargets.reduce(
34+
into: .init(staticSelf: desugarSelf))
3335
{
3436
switch $1.value
3537
{
@@ -84,7 +86,7 @@ extension Signature.Expanded
8486

8587
@inlinable
8688
init(utf8:[UInt8],
87-
sugarMap:SignatureSyntax.SugarMap = .init(),
89+
sugarMap:SignatureSyntax.SugarMap = .init(staticSelf: nil),
8890
linkBoundaries:borrowing [Int],
8991
linkTargets:inout [Int: Scalar],
9092
landmarks:inout SignatureLandmarks)

Sources/MarkdownPluginSwift/Signatures/SignatureSyntax.Autographer.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,10 @@ extension SignatureSyntax.Autographer
215215
}
216216
else if name.text == "`Self`"
217217
{
218-
self.autograph.append("Self")
218+
/// This is serendipitously safe because if `Self` is dynamic or has been shadowed
219+
/// by a generic parameter, it will appear without the enclosing backticks, even if
220+
/// the original source code included unnecessary backticks.
221+
self.autograph.append(self.sugarMap.staticSelf ?? "Self")
219222
return
220223
}
221224

Sources/MarkdownPluginSwift/Signatures/SignatureSyntax.SugarMap.swift

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,15 @@ extension SignatureSyntax
33
@frozen @usableFromInline
44
struct SugarMap
55
{
6+
/// The value that will be substituted for residual `` `Self` `` tokens when generating
7+
/// autographs.
8+
///
9+
/// This exists to correct for an
10+
/// [upstream bug in the Swift compiler](https://github.com/swiftlang/swift/issues/78343),
11+
/// and does not affect human-visible signature fragments.
12+
@usableFromInline
13+
let staticSelf:String?
14+
615
@usableFromInline
716
var dictionaries:Set<Int>
817
@usableFromInline
@@ -11,8 +20,9 @@ extension SignatureSyntax
1120
var optionals:Set<Int>
1221

1322
@inlinable
14-
init()
23+
init(staticSelf:String?)
1524
{
25+
self.staticSelf = staticSelf
1626
self.dictionaries = []
1727
self.arrays = []
1828
self.optionals = []

Sources/MarkdownPluginSwiftTests/Autographs.swift

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,4 +192,25 @@ struct Autographs
192192
#expect(self.landmarks.inputs == ["[T]", "[U:V].Type"])
193193
#expect(self.landmarks.output == ["V?"])
194194
}
195+
196+
@Test mutating
197+
func DesugaringStaticSelf()
198+
{
199+
let _:Signature<Symbol.Decl>.Expanded = .init([
200+
"func a(_: `Self`, _: [(`Self`)]) -> `Self`?"
201+
],
202+
sugarDictionary: .sSD,
203+
sugarArray: .sSa,
204+
sugarOptional: .sSq,
205+
desugarSelf: "DesugaredSelf<A,B>.NestedType",
206+
landmarks: &self.landmarks)
207+
208+
#expect(self.landmarks.inputs == [
209+
"DesugaredSelf<A,B>.NestedType",
210+
"[DesugaredSelf<A,B>.NestedType]"
211+
])
212+
#expect(self.landmarks.output == [
213+
"DesugaredSelf<A,B>.NestedType?"
214+
])
215+
}
195216
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import JSONDecoding
2+
import Signatures
3+
4+
struct GenericParameterWithPosition
5+
{
6+
let depth:UInt
7+
let index:Int
8+
let name:String
9+
}
10+
extension GenericParameterWithPosition
11+
{
12+
var parameter:GenericParameter { .init(name: self.name, depth: self.depth) }
13+
var position:(UInt, Int) { (self.depth, self.index) }
14+
}
15+
extension GenericParameterWithPosition:JSONObjectDecodable
16+
{
17+
enum CodingKey:String, Sendable
18+
{
19+
case name
20+
case depth
21+
case index
22+
}
23+
24+
init(json:JSON.ObjectDecoder<CodingKey>) throws
25+
{
26+
self.init(
27+
depth: try json[.depth].decode(),
28+
index: try json[.index].decode(),
29+
name: try json[.name].decode())
30+
}
31+
}

Sources/SymbolGraphParts/Generics/GenericParameter (ext).swift

Lines changed: 0 additions & 17 deletions
This file was deleted.

0 commit comments

Comments
 (0)