Skip to content

Commit b3bbf0c

Browse files
authored
Merge pull request #35544 from rintaro/astprinter-async-rdar73426591
[ASTPrinter] Print 'async' as a keyword
2 parents 9ef7b2a + 969ca0c commit b3bbf0c

File tree

3 files changed

+61
-6
lines changed

3 files changed

+61
-6
lines changed

lib/AST/ASTPrinter.cpp

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2907,8 +2907,10 @@ void PrintAST::printFunctionParameters(AbstractFunctionDecl *AFD) {
29072907
printParameterList(BodyParams, parameterListTypes,
29082908
AFD->argumentNameIsAPIByDefault());
29092909

2910-
if (AFD->hasAsync())
2911-
Printer << " " << "async";
2910+
if (AFD->hasAsync()) {
2911+
Printer << " ";
2912+
Printer.printKeyword("async", Options);
2913+
}
29122914

29132915
if (AFD->hasThrows()) {
29142916
if (AFD->getAttrs().hasAttribute<RethrowsAttr>())
@@ -4336,8 +4338,10 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
43364338
// If we're stripping argument labels from types, do it when printing.
43374339
visitAnyFunctionTypeParams(T->getParams(), /*printLabels*/false);
43384340

4339-
if (T->isAsync())
4340-
Printer << " " << "async";
4341+
if (T->isAsync()) {
4342+
Printer << " ";
4343+
Printer.printKeyword("async", Options);
4344+
}
43414345

43424346
if (T->isThrowing())
43434347
Printer << " " << tok::kw_throws;
@@ -4379,8 +4383,10 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
43794383

43804384
visitAnyFunctionTypeParams(T->getParams(), /*printLabels*/true);
43814385

4382-
if (T->isAsync())
4383-
Printer << " " << "async";
4386+
if (T->isAsync()) {
4387+
Printer << " ";
4388+
Printer.printKeyword("async", Options);
4389+
}
43844390

43854391
if (T->isThrowing())
43864392
Printer << " " << tok::kw_throws;

lib/AST/TypeRepr.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,10 @@ void FunctionTypeRepr::printImpl(ASTPrinter &Printer,
225225
const PrintOptions &Opts) const {
226226
Printer.callPrintStructurePre(PrintStructureKind::FunctionType);
227227
printTypeRepr(ArgsTy, Printer, Opts);
228+
if (isAsync()) {
229+
Printer << " ";
230+
Printer.printKeyword("async", Opts);
231+
}
228232
if (isThrowing()) {
229233
Printer << " ";
230234
Printer.printKeyword("throws", Opts);
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// BEGIN MyModule.swift
2+
public actor class MyActor {
3+
public func asyncFunc(fn: () async -> Void) async throws {}
4+
}
5+
6+
func test(act: MyActor) async throws {
7+
try await act.asyncFunc {}
8+
}
9+
10+
// BEGIN App.swift
11+
import MyModule
12+
13+
func test(act: MyActor) async throws {
14+
try await act.asyncFunc {}
15+
}
16+
17+
// RUN: %empty-directory(%t)
18+
// RUN: %{python} %utils/split_file.py -o %t %s
19+
20+
// RUN: %empty-directory(%t/Modules)
21+
// RUN: %target-swift-frontend -emit-module -o %t/Modules/MyModule.swiftmodule -module-name MyModule %t/MyModule.swift -enable-experimental-concurrency
22+
23+
// RUN: %sourcekitd-test -req=cursor -pos=1:20 %t/MyModule.swift -- %t/MyModule.swift -Xfrontend -enable-experimental-concurrency | %FileCheck -check-prefix=ACTOR %s
24+
// RUN: %sourcekitd-test -req=cursor -pos=2:15 %t/MyModule.swift -- %t/MyModule.swift -Xfrontend -enable-experimental-concurrency | %FileCheck -check-prefix=FUNC %s
25+
// RUN: %sourcekitd-test -req=cursor -pos=5:16 %t/MyModule.swift -- %t/MyModule.swift -Xfrontend -enable-experimental-concurrency | %FileCheck -check-prefix=ACTOR %s
26+
// RUN: %sourcekitd-test -req=cursor -pos=6:19 %t/MyModule.swift -- %t/MyModule.swift -Xfrontend -enable-experimental-concurrency | %FileCheck -check-prefix=FUNC %s
27+
28+
// ACTOR: <Declaration>public actor class MyActor</Declaration>
29+
// ACTOR: <decl.class><syntaxtype.keyword>public</syntaxtype.keyword> <syntaxtype.keyword>actor</syntaxtype.keyword> <syntaxtype.keyword>class</syntaxtype.keyword> <decl.name>MyActor</decl.name></decl.class>
30+
31+
// FUNC: <Declaration>public func asyncFunc(fn: () async -&gt; <Type usr="s:s4Voida">Void</Type>) async throws</Declaration>
32+
// FUNC: <decl.function.method.instance><syntaxtype.keyword>public</syntaxtype.keyword> <syntaxtype.keyword>func</syntaxtype.keyword> <decl.name>asyncFunc</decl.name>(<decl.var.parameter><decl.var.parameter.argument_label>fn</decl.var.parameter.argument_label>: <decl.var.parameter.type>() <syntaxtype.keyword>async</syntaxtype.keyword> -&gt; <decl.function.returntype><ref.typealias usr="s:s4Voida">Void</ref.typealias></decl.function.returntype></decl.var.parameter.type></decl.var.parameter>) <syntaxtype.keyword>async</syntaxtype.keyword> <syntaxtype.keyword>throws</syntaxtype.keyword></decl.function.method.instance>
33+
34+
// RUN: %sourcekitd-test -req=cursor -pos=3:16 %t/App.swift -- %t/App.swift -I %t/Modules -Xfrontend -enable-experimental-concurrency | %FileCheck -check-prefix=ACTOR_XMOD %s
35+
// RUN: %sourcekitd-test -req=cursor -pos=4:19 %t/App.swift -- %t/App.swift -I %t/Modules -Xfrontend -enable-experimental-concurrency | %FileCheck -check-prefix=FUNC_XMOD %s
36+
37+
// ACTOR_XMOD: <Declaration>actor class MyActor</Declaration>
38+
// ACTOR_XMOD: <decl.class><syntaxtype.keyword>actor</syntaxtype.keyword> <syntaxtype.keyword>class</syntaxtype.keyword> <decl.name>MyActor</decl.name></decl.class>
39+
40+
// FUNC_XMOD: <Declaration>func asyncFunc(fn: () async -&gt; <Type usr="s:s4Voida">Void</Type>) async throws</Declaration>
41+
// FUNC_XMOD: <decl.function.method.instance><syntaxtype.keyword>func</syntaxtype.keyword> <decl.name>asyncFunc</decl.name>(<decl.var.parameter><decl.var.parameter.argument_label>fn</decl.var.parameter.argument_label>: <decl.var.parameter.type>() <syntaxtype.keyword>async</syntaxtype.keyword> -&gt; <decl.function.returntype><ref.typealias usr="s:s4Voida">Void</ref.typealias></decl.function.returntype></decl.var.parameter.type></decl.var.parameter>) <syntaxtype.keyword>async</syntaxtype.keyword> <syntaxtype.keyword>throws</syntaxtype.keyword></decl.function.method.instance>
42+
43+
44+
45+

0 commit comments

Comments
 (0)