Skip to content

Commit a9aba54

Browse files
author
Nathan Hawes
committed
[ASTPrinter][SourceKit] Print 'Any' as a keyword.
It's treated as a keyword by syntactic highlighting, but wasn't annotated as a keyword by code completion, cursor info, or doc info. Resolves rdar://problem/61114942
1 parent 5239668 commit a9aba54

File tree

7 files changed

+79
-27
lines changed

7 files changed

+79
-27
lines changed

lib/AST/ASTPrinter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4349,7 +4349,7 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
43494349
if (T->hasExplicitAnyObject())
43504350
Printer << "AnyObject";
43514351
else
4352-
Printer << "Any";
4352+
Printer.printKeyword("Any", Options);
43534353
} else {
43544354
interleave(T->getMembers(), [&](Type Ty) { visit(Ty); },
43554355
[&] { Printer << " & "; });

lib/AST/TypeRepr.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -546,7 +546,7 @@ CompositionTypeRepr *CompositionTypeRepr::create(const ASTContext &C,
546546
void CompositionTypeRepr::printImpl(ASTPrinter &Printer,
547547
const PrintOptions &Opts) const {
548548
if (getTypes().empty()) {
549-
Printer << "Any";
549+
Printer.printKeyword("Any", Opts);
550550
} else {
551551
interleave(getTypes(), [&](TypeRepr *T) { printTypeRepr(T, Printer, Opts);},
552552
[&] { Printer << " & "; });

test/IDE/complete_annotation.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func testGlobal() {
4949
// GLOBAL_EXPR-DAG: Keyword[#function]/None: <name>#function</name>; name=#function
5050
// GLOBAL_EXPR-DAG: Decl[Module]/None: <name>Swift</name>; name=Swift
5151
// GLOBAL_EXPR-DAG: Decl[Struct]/OtherModule[Swift]: <name>Int</name>; name=Int
52-
// GLOBAL_EXPR-DAG: Decl[FreeFunction]/OtherModule[Swift]: <name>print</name>(<callarg><callarg.label>_</callarg.label> <callarg.param>items</callarg.param>: <callarg.type>Any</callarg.type>...</callarg>, <callarg><callarg.label>to</callarg.label> <callarg.param>output</callarg.param>: &amp;<callarg.type><typeid.sys>TextOutputStream</typeid.sys></callarg.type></callarg>); name=print(items: Any..., to: &TextOutputStream)
52+
// GLOBAL_EXPR-DAG: Decl[FreeFunction]/OtherModule[Swift]: <name>print</name>(<callarg><callarg.label>_</callarg.label> <callarg.param>items</callarg.param>: <callarg.type><keyword>Any</keyword></callarg.type>...</callarg>, <callarg><callarg.label>to</callarg.label> <callarg.param>output</callarg.param>: &amp;<callarg.type><typeid.sys>TextOutputStream</typeid.sys></callarg.type></callarg>); name=print(items: Any..., to: &TextOutputStream)
5353
// GLOBAL_EXPR: End completions
5454

5555

test/SourceKit/CursorInfo/cursor_info.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,8 @@ enum E7: String {
228228
case b = "\u{66}"
229229
}
230230

231+
func checkAnyIsAKeyword(x: Any) {}
232+
231233
// REQUIRES: objc_interop
232234
// RUN: %empty-directory(%t.tmp)
233235
// RUN: %swiftc_driver -emit-module -o %t.tmp/FooSwiftModule.swiftmodule %S/Inputs/FooSwiftModule.swift
@@ -765,3 +767,7 @@ enum E7: String {
765767

766768
// RUN: %sourcekitd-test -req=cursor -pos=227:14 %s -- -F %S/../Inputs/libIDE-mock-sdk -I %t.tmp %s | %FileCheck -check-prefix=CHECK94 %s
767769
// CHECK94: <empty cursor info; internal diagnostic: "Resolved to incomplete expression or statement.">
770+
771+
// RUN: %sourcekitd-test -req=cursor -pos=231:6 %s -- -F %S/../Inputs/libIDE-mock-sdk -I %t.tmp %s | %FileCheck -check-prefix=CHECK95 %s
772+
// CHECK95: <Declaration>func checkAnyIsAKeyword(x: Any)</Declaration>
773+
// CHECK95-NEXT: <decl.function.free><syntaxtype.keyword>func</syntaxtype.keyword> <decl.name>checkAnyIsAKeyword</decl.name>(<decl.var.parameter><decl.var.parameter.argument_label>x</decl.var.parameter.argument_label>: <decl.var.parameter.type><syntaxtype.keyword>Any</syntaxtype.keyword></decl.var.parameter.type></decl.var.parameter>)</decl.function.free>

test/SourceKit/DocSupport/Inputs/cake.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,3 +119,5 @@ public protocol P {
119119
public extension P {
120120
func bar() where Self: Equatable {}
121121
}
122+
123+
public func shouldPrintAnyAsKeyword(x: Any) {}

test/SourceKit/DocSupport/doc_clang_module.swift.response

Lines changed: 24 additions & 24 deletions
Large diffs are not rendered by default.

test/SourceKit/DocSupport/doc_swift_module.swift.response

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,8 @@ extension S3 {
197197

198198
func genfoo<T1, T2>(x ix: T1, y iy: T2) where T1 : cake.Prot, T2 : cake.C1, T1.Element == Int
199199

200+
func shouldPrintAnyAsKeyword(x x: Any)
201+
200202

201203
[
202204
{
@@ -1918,6 +1920,31 @@ func genfoo<T1, T2>(x ix: T1, y iy: T2) where T1 : cake.Prot, T2 : cake.C1, T1.E
19181920
key.usr: "s:Si",
19191921
key.offset: 2408,
19201922
key.length: 3
1923+
},
1924+
{
1925+
key.kind: source.lang.swift.syntaxtype.keyword,
1926+
key.offset: 2413,
1927+
key.length: 4
1928+
},
1929+
{
1930+
key.kind: source.lang.swift.syntaxtype.identifier,
1931+
key.offset: 2418,
1932+
key.length: 23
1933+
},
1934+
{
1935+
key.kind: source.lang.swift.syntaxtype.argument,
1936+
key.offset: 2442,
1937+
key.length: 1
1938+
},
1939+
{
1940+
key.kind: source.lang.swift.syntaxtype.parameter,
1941+
key.offset: 2444,
1942+
key.length: 1
1943+
},
1944+
{
1945+
key.kind: source.lang.swift.syntaxtype.keyword,
1946+
key.offset: 2447,
1947+
key.length: 3
19211948
}
19221949
]
19231950
[
@@ -3070,5 +3097,22 @@ func genfoo<T1, T2>(x ix: T1, y iy: T2) where T1 : cake.Prot, T2 : cake.C1, T1.E
30703097
key.length: 2
30713098
}
30723099
]
3100+
},
3101+
{
3102+
key.kind: source.lang.swift.decl.function.free,
3103+
key.name: "shouldPrintAnyAsKeyword(x:)",
3104+
key.usr: "s:4cake23shouldPrintAnyAsKeyword1xyyp_tF",
3105+
key.offset: 2413,
3106+
key.length: 38,
3107+
key.fully_annotated_decl: "<decl.function.free><syntaxtype.keyword>func</syntaxtype.keyword> <decl.name>shouldPrintAnyAsKeyword</decl.name>(<decl.var.parameter><decl.var.parameter.argument_label>x</decl.var.parameter.argument_label>: <decl.var.parameter.type><syntaxtype.keyword>Any</syntaxtype.keyword></decl.var.parameter.type></decl.var.parameter>)</decl.function.free>",
3108+
key.entities: [
3109+
{
3110+
key.kind: source.lang.swift.decl.var.local,
3111+
key.keyword: "x",
3112+
key.name: "x",
3113+
key.offset: 2447,
3114+
key.length: 3
3115+
}
3116+
]
30733117
}
30743118
]

0 commit comments

Comments
 (0)