Skip to content

Commit fe14d5d

Browse files
committed
[ASTPrinter] Move LifetimeDescriptor::getString and add test (NFC)
This refactor and test case are preparations for the implementation in the next commit.
1 parent a782752 commit fe14d5d

File tree

3 files changed

+52
-11
lines changed

3 files changed

+52
-11
lines changed

include/swift/AST/LifetimeDependence.h

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -141,17 +141,7 @@ struct LifetimeDescriptor {
141141
return getName().str() == "immortal";
142142
}
143143

144-
std::string getString() const {
145-
switch (kind) {
146-
case DescriptorKind::Named:
147-
return getName().str().str();
148-
case DescriptorKind::Ordered:
149-
return std::to_string(getIndex());
150-
case DescriptorKind::Self:
151-
return "self";
152-
}
153-
llvm_unreachable("Invalid DescriptorKind");
154-
}
144+
std::string getString() const;
155145
};
156146

157147
class LifetimeEntry final

lib/AST/LifetimeDependence.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,18 @@
2828

2929
namespace swift {
3030

31+
std::string LifetimeDescriptor::getString() const {
32+
switch (kind) {
33+
case DescriptorKind::Named:
34+
return getName().str().str();
35+
case DescriptorKind::Ordered:
36+
return std::to_string(getIndex());
37+
case DescriptorKind::Self:
38+
return "self";
39+
}
40+
llvm_unreachable("Invalid DescriptorKind");
41+
}
42+
3143
LifetimeEntry *
3244
LifetimeEntry::create(const ASTContext &ctx, SourceLoc startLoc,
3345
SourceLoc endLoc, ArrayRef<LifetimeDescriptor> sources,

test/IDE/print_lifetime_attr.swift

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// REQUIRES: swift_feature_Lifetimes
2+
// REQUIRES: swift_feature_LifetimeDependence
3+
4+
// RUN: %empty-directory(%t)
5+
// RUN: split-file %s %t
6+
7+
// 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
8+
// RUN: diff %t/interface.txt %t/interface.txt.expected
9+
10+
//--- test.swift
11+
public struct S : ~Escapable {
12+
@_lifetime(immortal)
13+
init() {}
14+
}
15+
16+
@_lifetime(foo: copy foo)
17+
public func fooFunc(_ foo: inout S) {}
18+
19+
@_lifetime(&bar)
20+
public func barFunc(_ bar: inout S) -> S {
21+
return bar
22+
}
23+
24+
@_lifetime(`func`: copy `func`)
25+
public func funcFunc(func: inout S) {}
26+
27+
//--- interface.txt.expected
28+
29+
public struct S : ~Escapable {
30+
31+
@_lifetime(immortal)
32+
internal init()
33+
}
34+
@_lifetime(foo: copy foo)
35+
public func fooFunc(_ foo: inout S)
36+
@_lifetime(&bar)
37+
public func barFunc(_ bar: inout S) -> S
38+
@_lifetime(func: copy func)
39+
public func funcFunc(func: inout S)

0 commit comments

Comments
 (0)