Skip to content

Commit d7d839e

Browse files
committed
[ASTPrinter] Escape @_lifetime arguments when needed
rdar://159992995
1 parent fe14d5d commit d7d839e

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

lib/AST/LifetimeDependence.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
#include "swift/AST/LifetimeDependence.h"
1414
#include "swift/AST/ASTContext.h"
15+
#include "swift/AST/ASTPrinter.h"
1516
#include "swift/AST/Builtins.h"
1617
#include "swift/AST/ConformanceLookup.h"
1718
#include "swift/AST/Decl.h"
@@ -30,8 +31,14 @@ namespace swift {
3031

3132
std::string LifetimeDescriptor::getString() const {
3233
switch (kind) {
33-
case DescriptorKind::Named:
34+
case DescriptorKind::Named: {
35+
bool shouldEscape =
36+
escapeIdentifierInContext(getName(), PrintNameContext::Normal);
37+
if (shouldEscape) {
38+
return ("`" + getName().str() + "`").str();
39+
}
3440
return getName().str().str();
41+
}
3542
case DescriptorKind::Ordered:
3643
return std::to_string(getIndex());
3744
case DescriptorKind::Self:

test/IDE/print_lifetime_attr.swift

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,14 @@ public func barFunc(_ bar: inout S) -> S {
2424
@_lifetime(`func`: copy `func`)
2525
public func funcFunc(func: inout S) {}
2626

27+
public struct T : ~Escapable {
28+
let s: S
29+
@_lifetime(borrow self)
30+
func selfFunc() -> S { return s }
31+
@_lifetime(borrow `self`)
32+
func selfFunc2(`self`: S) -> S { return `self` }
33+
}
34+
2735
//--- interface.txt.expected
2836

2937
public struct S : ~Escapable {
@@ -35,5 +43,16 @@ public struct S : ~Escapable {
3543
public func fooFunc(_ foo: inout S)
3644
@_lifetime(&bar)
3745
public func barFunc(_ bar: inout S) -> S
38-
@_lifetime(func: copy func)
46+
@_lifetime(`func`: copy `func`)
3947
public func funcFunc(func: inout S)
48+
49+
public struct T : ~Escapable {
50+
51+
internal let s: S
52+
53+
@_lifetime(borrow self)
54+
internal func selfFunc() -> S
55+
56+
@_lifetime(borrow `self`)
57+
internal func selfFunc2(self: S) -> S
58+
}

0 commit comments

Comments
 (0)