Skip to content

Commit 0443a4d

Browse files
authored
[SourceKit] for cursor-info request, printing underlying types instead of namelias type. rdar://28216890 (#5339)
1 parent 14b689d commit 0443a4d

File tree

6 files changed

+54
-4
lines changed

6 files changed

+54
-4
lines changed

include/swift/AST/PrintOptions.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,10 @@ struct PrintOptions {
327327
/// formatting.
328328
bool PrintOriginalSourceText = false;
329329

330+
/// When printing a name alias type, whether print the underlying type instead
331+
/// of the alias.
332+
bool PrintNameAliasUnderlyingType = false;
333+
330334
/// \brief Print dependent types as references into this generic environment.
331335
GenericEnvironment *GenericEnv = nullptr;
332336

lib/AST/ASTPrinter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3496,7 +3496,7 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
34963496
}
34973497

34983498
void visitNameAliasType(NameAliasType *T) {
3499-
if (Options.PrintForSIL) {
3499+
if (Options.PrintForSIL || Options.PrintNameAliasUnderlyingType) {
35003500
visit(T->getSinglyDesugaredType());
35013501
return;
35023502
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
protocol FancyProtocol {
2+
associatedtype Thing
3+
func holdPinkyUp(x: Thing)
4+
}
5+
6+
struct Dashing: FancyProtocol {
7+
func holdPinkyUp(x: String) { print("Dashing: \(x)") }
8+
}
9+
class AnyFancyBoxBase<T>: FancyProtocol {
10+
func holdPinkyUp(x: T) {
11+
//never called
12+
fatalError()
13+
}
14+
}
15+
16+
final class _FancyBox<Base: FancyProtocol>: AnyFancyBoxBase<Base.Thing> {
17+
var base: Base
18+
init(_ base: Base) {
19+
self.base = base
20+
}
21+
override func holdPinkyUp(x: Base.Thing) {
22+
base.holdPinkyUp(x: x)
23+
}
24+
}
25+
26+
struct AnyFancy<T>: FancyProtocol {
27+
var _box: AnyFancyBoxBase<T>
28+
func holdPinkyUp(x: T) {
29+
_box.holdPinkyUp(x: x)
30+
}
31+
32+
init<U: FancyProtocol>(_ base: U) where U.Thing == T {
33+
_box = _FancyBox(base)
34+
}
35+
}
36+
37+
let dashing = Dashing()
38+
var anyFancy = AnyFancy(dashing)
39+
print("\(type(of: anyFancy))")
40+
anyFancy.holdPinkyUp(x: "")
41+
42+
// RUN: %sourcekitd-test -req=cursor -pos=40:3 %s -- %s | %FileCheck %s -check-prefix=CASE1
43+
44+
// CASE1: AnyFancy<String>

test/SourceKit/Mixed/cursor_mixed.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@ func test(_ b : Base) {
99
// CHECK: source.lang.swift.ref.function.method.instance ({{.*}}Mixed.framework/Headers/Mixed.h:5:9-5:23)
1010
// CHECK: doIt(_:)
1111
// CHECK: c:objc(cs)Base(im)doIt:
12-
// CHECK: (Base) -> (Int32) -> Void
12+
// CHECK: (Base) -> (Int32) -> ()
1313
// CHECK: Mixed
1414
// CHECK: <Declaration>func doIt(_ arg: <Type usr="s:Vs5Int32">Int32</Type>)</Declaration>

test/SourceKit/Mixed/cursor_mixed_header.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,5 @@ func test(_ b : BaseInHead) {
1313
// CHECK: source.lang.swift.ref.function.method.instance ({{.*}}Inputs/header.h:4:9-4:23)
1414
// CHECK: doIt(_:)
1515
// CHECK: c:objc(cs)BaseInHead(im)doIt:
16-
// CHECK: (BaseInHead) -> (Int32) -> Void
16+
// CHECK: (BaseInHead) -> (Int32) -> ()
1717
// CHECK: <Declaration>func doIt(_ arg: <Type usr="s:Vs5Int32">Int32</Type>)</Declaration>

tools/SourceKit/lib/SwiftLang/SwiftSourceDocInfo.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -644,7 +644,9 @@ static bool passCursorInfoForDecl(const ValueDecl *VD,
644644
unsigned TypenameBegin = SS.size();
645645
if (VD->hasType()) {
646646
llvm::raw_svector_ostream OS(SS);
647-
VD->getInterfaceType().print(OS);
647+
PrintOptions Options;
648+
Options.PrintNameAliasUnderlyingType = true;
649+
VD->getInterfaceType().print(OS, Options);
648650
}
649651
unsigned TypenameEnd = SS.size();
650652

0 commit comments

Comments
 (0)