Skip to content

[AST] Indicate which closures are escaping in AST dumps #17097

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 18, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions lib/AST/ASTDumper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2301,6 +2301,11 @@ class PrintExpr : public ExprVisitor<PrintExpr> {
OS << " ";
E->getCaptureInfo().print(PrintWithColorRAII(OS, CapturesColor).getOS());
}
// Printing a function type doesn't indicate whether it's escaping because it doesn't
// matter in 99% of contexts. AbstractClosureExpr nodes are one of the only exceptions.
if (auto Ty = GetTypeOfExpr(E))
if (!Ty->getAs<AnyFunctionType>()->getExtInfo().isNoEscape())
PrintWithColorRAII(OS, ClosureModifierColor) << " escaping";

return OS;
}
Expand Down
13 changes: 13 additions & 0 deletions test/Driver/dump-parse.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,16 @@ func generic<T: Hashable>(_: T) {}
// CHECK-AST: (pattern_binding_decl
// CHECK-AST: (declref_expr type='(Int) -> ()' location={{.*}} range={{.*}} decl=main.(file).generic@{{.*}} [with (substitution_map generic_signature=<T where T : Hashable> (substitution T -> Int))] function_ref=unapplied))
let _: (Int) -> () = generic

// Closures should be marked as escaping or not.
func escaping(_: @escaping (Int) -> Int) {}
escaping({ $0 })
// CHECK-AST: (declref_expr type='(@escaping (Int) -> Int) -> ()'
// CHECK-AST-NEXT: (paren_expr
// CHECK-AST-NEXT: (closure_expr type='(Int) -> Int' {{.*}} discriminator=0 escaping single-expression

func nonescaping(_: (Int) -> Int) {}
nonescaping({ $0 })
// CHECK-AST: (declref_expr type='((Int) -> Int) -> ()'
// CHECK-AST-NEXT: (paren_expr
// CHECK-AST-NEXT: (closure_expr type='(Int) -> Int' {{.*}} discriminator=1 single-expression
4 changes: 2 additions & 2 deletions test/expr/capture/generic_params.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ func doSomething<T>(_ t: T) {}

func outerGeneric<T>(t: T, x: AnyObject) {
// Simple case -- closure captures outer generic parameter
// CHECK: closure_expr type='() -> ()' {{.*}} discriminator=0 captures=(<generic> t) single-expression
// CHECK: closure_expr type='() -> ()' {{.*}} discriminator=0 captures=(<generic> t) escaping single-expression
_ = { doSomething(t) }

// Special case -- closure does not capture outer generic parameters
// CHECK: closure_expr type='() -> ()' {{.*}} discriminator=1 captures=(x) single-expression
// CHECK: closure_expr type='() -> ()' {{.*}} discriminator=1 captures=(x) escaping single-expression
_ = { doSomething(x) }

// Special case -- closure captures outer generic parameter, but it does not
Expand Down