[ASTPrinter] Escape @_lifetime arguments when needed

rdar://159992995
This commit is contained in:
Henrik G. Olsson
2025-09-05 22:18:02 -07:00
parent fe14d5d636
commit d7d839e9f3
2 changed files with 28 additions and 2 deletions

View File

@@ -12,6 +12,7 @@
#include "swift/AST/LifetimeDependence.h"
#include "swift/AST/ASTContext.h"
#include "swift/AST/ASTPrinter.h"
#include "swift/AST/Builtins.h"
#include "swift/AST/ConformanceLookup.h"
#include "swift/AST/Decl.h"
@@ -30,8 +31,14 @@ namespace swift {
std::string LifetimeDescriptor::getString() const {
switch (kind) {
case DescriptorKind::Named:
case DescriptorKind::Named: {
bool shouldEscape =
escapeIdentifierInContext(getName(), PrintNameContext::Normal);
if (shouldEscape) {
return ("`" + getName().str() + "`").str();
}
return getName().str().str();
}
case DescriptorKind::Ordered:
return std::to_string(getIndex());
case DescriptorKind::Self:

View File

@@ -24,6 +24,14 @@ public func barFunc(_ bar: inout S) -> S {
@_lifetime(`func`: copy `func`)
public func funcFunc(func: inout S) {}
public struct T : ~Escapable {
let s: S
@_lifetime(borrow self)
func selfFunc() -> S { return s }
@_lifetime(borrow `self`)
func selfFunc2(`self`: S) -> S { return `self` }
}
//--- interface.txt.expected
public struct S : ~Escapable {
@@ -35,5 +43,16 @@ public struct S : ~Escapable {
public func fooFunc(_ foo: inout S)
@_lifetime(&bar)
public func barFunc(_ bar: inout S) -> S
@_lifetime(func: copy func)
@_lifetime(`func`: copy `func`)
public func funcFunc(func: inout S)
public struct T : ~Escapable {
internal let s: S
@_lifetime(borrow self)
internal func selfFunc() -> S
@_lifetime(borrow `self`)
internal func selfFunc2(self: S) -> S
}