Skip to content
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
12 changes: 1 addition & 11 deletions include/swift/AST/LifetimeDependence.h
Original file line number Diff line number Diff line change
Expand Up @@ -141,17 +141,7 @@ struct LifetimeDescriptor {
return getName().str() == "immortal";
}

std::string getString() const {
switch (kind) {
case DescriptorKind::Named:
return getName().str().str();
case DescriptorKind::Ordered:
return std::to_string(getIndex());
case DescriptorKind::Self:
return "self";
}
llvm_unreachable("Invalid DescriptorKind");
}
std::string getString() const;
};

class LifetimeEntry final
Expand Down
19 changes: 19 additions & 0 deletions lib/AST/LifetimeDependence.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -28,6 +29,24 @@

namespace swift {

std::string LifetimeDescriptor::getString() const {
switch (kind) {
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:
return "self";
}
llvm_unreachable("Invalid DescriptorKind");
}

LifetimeEntry *
LifetimeEntry::create(const ASTContext &ctx, SourceLoc startLoc,
SourceLoc endLoc, ArrayRef<LifetimeDescriptor> sources,
Expand Down
58 changes: 58 additions & 0 deletions test/IDE/print_lifetime_attr.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// REQUIRES: swift_feature_Lifetimes
// REQUIRES: swift_feature_LifetimeDependence

// RUN: %empty-directory(%t)
// RUN: split-file %s %t

// RUN: %target-swift-ide-test -enable-experimental-feature Lifetimes -enable-experimental-feature LifetimeDependence -print-swift-file-interface -source-filename %t/test.swift > %t/interface.txt
// RUN: diff %t/interface.txt %t/interface.txt.expected

//--- test.swift
public struct S : ~Escapable {
@_lifetime(immortal)
init() {}
}

@_lifetime(foo: copy foo)
public func fooFunc(_ foo: inout S) {}

@_lifetime(&bar)
public func barFunc(_ bar: inout S) -> S {
return bar
}

@_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 {

@_lifetime(immortal)
internal init()
}
@_lifetime(foo: copy foo)
public func fooFunc(_ foo: inout S)
@_lifetime(&bar)
public func barFunc(_ bar: inout S) -> S
@_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
}
8 changes: 4 additions & 4 deletions test/Interop/C/swiftify-import/counted-by-noescape.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import CountedByNoEscapeClang

// CHECK-NEXT: /// This is an auto-generated wrapper for safer interop
// CHECK-NEXT: @available(visionOS 1.0, tvOS 12.2, watchOS 5.2, iOS 12.2, macOS 10.14.4, *)
// CHECK-NEXT: @_lifetime(func: copy func)
// CHECK-NEXT: @_lifetime(`func`: copy `func`)
// CHECK-NEXT: @_alwaysEmitIntoClient @_disfavoredOverload public func clash(func: inout MutableSpan<Int32>?, clash where: Int32)

// CHECK-NEXT: /// This is an auto-generated wrapper for safer interop
Expand All @@ -38,7 +38,7 @@ import CountedByNoEscapeClang

// CHECK-NEXT: /// This is an auto-generated wrapper for safer interop
// CHECK-NEXT: @available(visionOS 1.0, tvOS 12.2, watchOS 5.2, iOS 12.2, macOS 10.14.4, *)
// CHECK-NEXT: @_lifetime(func: copy func)
// CHECK-NEXT: @_lifetime(`func`: copy `func`)
// CHECK-NEXT: @_alwaysEmitIntoClient @_disfavoredOverload public func funcRenamed(func: inout MutableSpan<Int32>?, extension: Int32, init: Int32, open: Int32, var: Int32, is: Int32, as: Int32, in: Int32, guard: Int32, where: Int32) -> UnsafeMutableRawPointer!

// CHECK-NEXT: /// This is an auto-generated wrapper for safer interop
Expand All @@ -53,7 +53,7 @@ import CountedByNoEscapeClang

// CHECK-NEXT: /// This is an auto-generated wrapper for safer interop
// CHECK-NEXT: @available(visionOS 1.0, tvOS 12.2, watchOS 5.2, iOS 12.2, macOS 10.14.4, *)
// CHECK-NEXT: @_lifetime(func: copy func)
// CHECK-NEXT: @_lifetime(`func`: copy `func`)
// CHECK-NEXT: @_alwaysEmitIntoClient @_disfavoredOverload public func keyword(_ func: inout MutableSpan<Int32>?, _ extension: Int32, _ init: Int32, _ open: Int32, _ var: Int32, _ is: Int32, _ as: Int32, _ in: Int32, _ guard: Int32, _ where: Int32)

// CHECK-NEXT: /// This is an auto-generated wrapper for safer interop
Expand Down Expand Up @@ -83,7 +83,7 @@ import CountedByNoEscapeClang

// CHECK-NEXT: /// This is an auto-generated wrapper for safer interop
// CHECK-NEXT: @available(visionOS 1.0, tvOS 12.2, watchOS 5.2, iOS 12.2, macOS 10.14.4, *)
// CHECK-NEXT: @_lifetime(func: copy func)
// CHECK-NEXT: @_lifetime(`func`: copy `func`)
// CHECK-NEXT: @_alwaysEmitIntoClient @_disfavoredOverload public func open(func: inout MutableSpan<Int32>?, open where: Int32)

// CHECK-NEXT: /// This is an auto-generated wrapper for safer interop
Expand Down