Skip to content

Commit 4858847

Browse files
committed
document typealias behavior
1 parent d7a68c0 commit 4858847

File tree

5 files changed

+115
-22
lines changed

5 files changed

+115
-22
lines changed

lib/SymbolGraphGen/Symbol.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -224,13 +224,13 @@ const ValueDecl *Symbol::getDeclInheritingDocs() const {
224224

225225
namespace {
226226

227-
StringRef getFileNameForDecl(const ValueDecl *VD) {
228-
if (!VD) return StringRef{};
227+
StringRef getFileNameForDecl(const Decl *D) {
228+
if (!D) return StringRef{};
229229

230-
SourceLoc Loc = VD->getLoc(/*SerializedOK=*/true);
230+
SourceLoc Loc = D->getLoc(/*SerializedOK=*/true);
231231
if (Loc.isInvalid()) return StringRef{};
232232

233-
SourceManager &SourceM = VD->getASTContext().SourceMgr;
233+
SourceManager &SourceM = D->getASTContext().SourceMgr;
234234
return SourceM.getDisplayNameForLoc(Loc);
235235
}
236236

@@ -280,7 +280,7 @@ void Symbol::serializeDocComment(llvm::json::OStream &OS) const {
280280
StringRef FileName = getFileNameForDecl(ClangD);
281281
if (!FileName.empty())
282282
serializeFileURI(OS, FileName);
283-
if (const auto *ModuleD = VD->getModuleContext()) {
283+
if (const auto *ModuleD = D->getModuleContext()) {
284284
OS.attribute("module", ModuleD->getNameStr());
285285
}
286286
OS.attributeArray("lines", [&]() {
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
public struct ExternalS {}
2+
3+
public typealias ExternalA = ExternalS
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: %target-build-swift %S/Inputs/ExternalTypealias.swift -module-name ExternalTypealias -emit-module -emit-module-path %t/
3+
// RUN: %target-build-swift %s -module-name Typealias -emit-module -emit-module-path %t/ -I %t
4+
// RUN: %target-swift-symbolgraph-extract -module-name Typealias -I %t -pretty-print -output-dir %t
5+
// RUN: %FileCheck %s --input-file %t/Typealias.symbols.json
6+
7+
// RUN: %empty-directory(%t)
8+
// RUN: %target-build-swift %S/Inputs/ExternalTypealias.swift -module-name ExternalTypealias -emit-module -emit-module-path %t/
9+
// RUN: %target-build-swift %s -module-name Typealias -emit-module -emit-module-path %t/ -I %t
10+
// RUN: %target-swift-symbolgraph-extract -module-name Typealias -I %t -pretty-print -output-dir %t
11+
// RUN: %FileCheck %s --input-file %t/[email protected] --check-prefix EXTERNAL
12+
13+
// RUN: %empty-directory(%t)
14+
// RUN: %target-build-swift %S/Inputs/ExternalTypealias.swift -module-name ExternalTypealias -emit-module -emit-module-path %t/
15+
// RUN: %target-build-swift %s -module-name Typealias -emit-module -emit-module-path %t/ -I %t
16+
// RUN: %target-swift-symbolgraph-extract -module-name Typealias -I %t -pretty-print -output-dir %t -emit-extension-block-symbols
17+
// RUN: %FileCheck %s --input-file %t/[email protected] --check-prefix EBS_EXTERNAL
18+
19+
public struct S {}
20+
21+
public typealias A = S
22+
23+
// Members of a typealias should be associated with the original type,
24+
// not the typealias symbol
25+
public extension A {
26+
func foo() {}
27+
}
28+
29+
// CHECK: "kind": "memberOf"
30+
// CHECK-NEXT: "source": "s:9Typealias1SV3fooyyF"
31+
// CHECK-NEXT: "target": "s:9Typealias1SV"
32+
33+
34+
// This also applies to extensions to externally defined typealiases
35+
36+
import ExternalTypealias
37+
38+
public extension ExternalA {
39+
func foo() {}
40+
}
41+
42+
// EXTERNAL: "kind": "memberOf"
43+
// EXTERNAL-NEXT: "source": "s:17ExternalTypealias0A1SV0B0E3fooyyF"
44+
// EXTERNAL-NEXT: "target": "s:17ExternalTypealias0A1SV"
45+
46+
// EBS_EXTERNAL: "kind": "extensionTo"
47+
// EBS_EXTERNAL-NEXT: "source": "s:e:s:17ExternalTypealias0A1SV0B0E3fooyyF"
48+
// EBS_EXTERNAL-NEXT: "target": "s:17ExternalTypealias0A1SV"
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
public struct ExternalStruct {
22
public struct InnerStruct {}
33

4-
public enum InnerEnum { }
4+
public typealias InnerTypeAlias = InnerStruct
5+
6+
public enum InnerEnum {}
57
}

test/SymbolGraph/Symbols/Names.swift

Lines changed: 56 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,18 @@
55
// RUN: %FileCheck %s --input-file %t/Names.symbols.json
66
// RUN: %FileCheck %s --input-file %t/Names.symbols.json --check-prefix=FUNC
77
// RUN: %FileCheck %s --input-file %t/Names.symbols.json --check-prefix=INNERTYPE
8-
// RUN: %FileCheck %s --input-file %t/Names.symbols.json --check-prefix=INNERTYPEALIAS
98
// RUN: %FileCheck %s --input-file %t/Names.symbols.json --check-prefix=INNERENUM
109
// RUN: %FileCheck %s --input-file %t/Names.symbols.json --check-prefix=INNERCASE
1110

1211
// RUN: %FileCheck %s --input-file %t/[email protected] --check-prefix=EXT_INNERTYPE
1312
// RUN: %FileCheck %s --input-file %t/[email protected] --check-prefix=EXT_INNERENUM
1413

14+
// RUN: %FileCheck %s --input-file %t/Names.symbols.json --check-prefix=INNERTYPEALIAS
15+
// RUN: %FileCheck %s --input-file %t/Names.symbols.json --check-prefix=INNERTYPEALIAS_INNERINNERTYPE
16+
17+
// RUN: %FileCheck %s --input-file %t/[email protected] --check-prefix=EXT_INNERTYPEALIAS
18+
// RUN: %FileCheck %s --input-file %t/[email protected] --check-prefix=EXT_INNERTYPEALIAS_INNERINNERTYPE
19+
1520
public struct MyStruct {
1621
public struct InnerStruct {}
1722

@@ -24,6 +29,28 @@ public struct MyStruct {
2429
}
2530
}
2631

32+
public extension MyStruct.InnerTypeAlias {
33+
struct InnerInnerStruct {}
34+
}
35+
36+
import ExternalNames
37+
38+
public extension ExternalStruct.InnerStruct {
39+
func foo() {}
40+
}
41+
42+
public extension ExternalStruct.InnerTypeAlias {
43+
func bar() {}
44+
}
45+
46+
public extension ExternalStruct.InnerEnum {
47+
func foo() {}
48+
}
49+
50+
public extension ExternalStruct.InnerTypeAlias {
51+
struct InnerInnerStruct {}
52+
}
53+
2754
// CHECK-LABEL: "precise": "s:5Names8MyStructV"
2855
// CHECK: names
2956
// CHECK-NEXT: "title": "MyStruct"
@@ -36,10 +63,6 @@ public struct MyStruct {
3663
// INNERTYPE: names
3764
// INNERTYPE-NEXT: "title": "MyStruct.InnerStruct"
3865

39-
// INNERTYPEALIAS-LABEL: "precise": "s:5Names8MyStructV14InnerTypeAliasa"
40-
// INNERTYPEALIAS: names
41-
// INNERTYPEALIAS-NEXT: "title": "MyStruct.InnerTypeAlias"
42-
4366
// INNERENUM-LABEL: "precise": "s:5Names8MyStructV9InnerEnumO",
4467
// INNERENUM: names
4568
// INNERENUM-NEXT: "title": "MyStruct.InnerEnum"
@@ -48,21 +71,38 @@ public struct MyStruct {
4871
// INNERCASE: names
4972
// INNERCASE-NEXT: "title": "MyStruct.InnerEnum.InnerCase",
5073

51-
52-
import ExternalNames
53-
54-
public extension ExternalStruct.InnerStruct {
55-
func foo() {}
56-
}
57-
58-
public extension ExternalStruct.InnerEnum {
59-
func foo() {}
60-
}
61-
6274
// EXT_INNERTYPE-LABEL: "precise": "s:e:s:13ExternalNames0A6StructV05InnerC0V0B0E3fooyyF"
6375
// EXT_INNERTYPE: names
6476
// EXT_INNERTYPE-NEXT: "title": "ExternalStruct.InnerStruct"
6577

6678
// EXT_INNERENUM-LABEL: "precise": "s:e:s:13ExternalNames0A6StructV9InnerEnumO0B0E3fooyyF",
6779
// EXT_INNERENUM: names
6880
// EXT_INNERENUM-NEXT: "title": "ExternalStruct.InnerEnum"
81+
82+
83+
// The typealias symbol itself should use the name of the typealias
84+
85+
// INNERTYPEALIAS-LABEL: "precise": "s:5Names8MyStructV14InnerTypeAliasa"
86+
// INNERTYPEALIAS: names
87+
// INNERTYPEALIAS-NEXT: "title": "MyStruct.InnerTypeAlias"
88+
89+
// Types declared in extensions to typealiases should always use the name of their
90+
// original (aliased) parent type
91+
92+
// INNERTYPEALIAS_INNERINNERTYPE-LABEL: "precise": "s:5Names8MyStructV05InnerC0V0ddC0V"
93+
// INNERTYPEALIAS_INNERINNERTYPE: names
94+
// INNERTYPEALIAS_INNERINNERTYPE-NEXT: "title": "MyStruct.InnerStruct.InnerInnerStruct"
95+
96+
// Extension symbols which extend a typealias should use the name of the original
97+
// (aliased) type
98+
99+
// EXT_INNERTYPEALIAS-LABEL: "precise": "s:e:s:13ExternalNames0A6StructV05InnerC0V0B0E3baryyF"
100+
// EXT_INNERTYPEALIAS: names
101+
// EXT_INNERTYPEALIAS-NEXT: "title": "ExternalStruct.InnerStruct"
102+
103+
// Symbols declared in an extension to a typealias should also use the name of the original
104+
// (aliased) type
105+
106+
// EXT_INNERTYPEALIAS_INNERINNERTYPE-LABEL: "precise": "s:13ExternalNames0A6StructV05InnerC0V0B0E0ddC0V"
107+
// EXT_INNERTYPEALIAS_INNERINNERTYPE: names
108+
// EXT_INNERTYPEALIAS_INNERINNERTYPE-NEXT: "title": "ExternalStruct.InnerStruct.InnerInnerStruct"

0 commit comments

Comments
 (0)