Skip to content

Commit a79facf

Browse files
committed
Fix ASTPrinting of lifetime dependence
This fixes the errors while compiling functions with lifetime dependence in the textual interface files. Also fixes rdar://122573346
1 parent 503239a commit a79facf

File tree

3 files changed

+126
-1
lines changed

3 files changed

+126
-1
lines changed

lib/AST/ASTPrinter.cpp

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4920,7 +4920,15 @@ void PrintAST::visitFuncDecl(FuncDecl *decl) {
49204920

49214921
Printer.printDeclResultTypePre(decl, ResultTyLoc);
49224922
Printer.callPrintStructurePre(PrintStructureKind::FunctionReturnType);
4923-
4923+
{
4924+
if (auto *typeRepr = dyn_cast_or_null<LifetimeDependentReturnTypeRepr>(
4925+
decl->getResultTypeRepr())) {
4926+
for (auto &dep : typeRepr->getLifetimeDependencies()) {
4927+
Printer << " " << dep.getLifetimeDependenceKindString() << "(";
4928+
Printer << dep.getParamString() << ") ";
4929+
}
4930+
}
4931+
}
49244932
{
49254933
auto fnTy = decl->getInterfaceType();
49264934
bool hasTransferring = false;
@@ -5152,6 +5160,17 @@ void PrintAST::visitConstructorDecl(ConstructorDecl *decl) {
51525160

51535161
printGenericDeclGenericParams(decl);
51545162
printFunctionParameters(decl);
5163+
if (decl->hasLifetimeDependentReturn()) {
5164+
Printer << " -> ";
5165+
auto *typeRepr =
5166+
cast<LifetimeDependentReturnTypeRepr>(decl->getResultTypeRepr());
5167+
for (auto &dep : typeRepr->getLifetimeDependencies()) {
5168+
Printer << dep.getLifetimeDependenceKindString() << "(";
5169+
Printer << dep.getParamString() << ") ";
5170+
}
5171+
// TODO: Handle failable initializers with lifetime dependent returns
5172+
Printer << "Self";
5173+
}
51555174
});
51565175

51575176
printDeclGenericRequirements(decl);
@@ -7441,6 +7460,13 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
74417460
}
74427461

74437462
Printer << " -> ";
7463+
7464+
if (T->hasLifetimeDependenceInfo()) {
7465+
auto lifetimeDependenceInfo = T->getExtInfo().getLifetimeDependenceInfo();
7466+
assert(!lifetimeDependenceInfo.empty());
7467+
Printer << lifetimeDependenceInfo.getString() << " ";
7468+
}
7469+
74447470
Printer.callPrintStructurePre(PrintStructureKind::FunctionReturnType);
74457471
T->getResult().print(Printer, Options);
74467472
Printer.printStructurePost(PrintStructureKind::FunctionReturnType);
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
public struct AnotherView : ~Escapable {
2+
@usableFromInline let _ptr: UnsafeRawBufferPointer
3+
@usableFromInline let _count: Int
4+
@_unsafeNonescapableResult
5+
internal init(_ ptr: UnsafeRawBufferPointer, _ count: Int) {
6+
self._ptr = ptr
7+
self._count = count
8+
}
9+
}
10+
11+
public struct BufferView : ~Escapable {
12+
@usableFromInline let _ptr: UnsafeRawBufferPointer
13+
@usableFromInline let _count: Int
14+
@_unsafeNonescapableResult
15+
@usableFromInline
16+
internal init(_ ptr: UnsafeRawBufferPointer, _ count: Int) {
17+
self._ptr = ptr
18+
self._count = count
19+
}
20+
21+
@inlinable
22+
internal init(_ ptr: UnsafeRawBufferPointer, _ a: borrowing Array<Int>) -> _borrow(a) Self {
23+
self.init(ptr, a.count)
24+
return self
25+
}
26+
@inlinable
27+
internal init(_ ptr: UnsafeRawBufferPointer, _ a: consuming AnotherView) -> _consume(a) Self {
28+
self.init(ptr, a._count)
29+
return self
30+
}
31+
}
32+
33+
@inlinable
34+
public func derive(_ x: consuming BufferView) -> _consume(x) BufferView {
35+
return BufferView(x._ptr, x._count)
36+
}
37+
38+
@inlinable
39+
public func use(_ x: consuming BufferView) {}
40+
41+
@inlinable
42+
public func consumeAndCreate(_ view: consuming BufferView) -> _consume(view) BufferView {
43+
return BufferView(view._ptr, view._count)
44+
}
45+
46+
@inlinable
47+
public func deriveThisOrThat(_ this: consuming BufferView, _ that: consuming BufferView) -> _consume(this, that) BufferView {
48+
if (Int.random(in: 1..<100) == 0) {
49+
return BufferView(this._ptr, this._count)
50+
}
51+
return BufferView(that._ptr, that._count)
52+
}
53+
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// RUN: %empty-directory(%t)
2+
3+
// RUN: %target-swift-frontend -swift-version 5 -enable-library-evolution -emit-module \
4+
// RUN: -disable-experimental-parser-round-trip \
5+
// RUN: -enable-experimental-feature NoncopyableGenerics \
6+
// RUN: -enable-experimental-feature NonescapableTypes \
7+
// RUN: -o %t/lifetime_dependence.swiftmodule \
8+
// RUN: -emit-module-interface-path %t/lifetime_dependence.swiftinterface \
9+
// RUN: %S/Inputs/lifetime_dependence.swift
10+
// REQUIRES: asserts
11+
12+
// Check the interfaces
13+
14+
// RUN: %FileCheck %s < %t/lifetime_dependence.swiftinterface
15+
16+
// See if we can compile a module through just the interface and typecheck using it.
17+
18+
// RUN: %target-swift-frontend -compile-module-from-interface \
19+
// RUN: -disable-experimental-parser-round-trip \
20+
// RUN: -enable-experimental-feature NoncopyableGenerics \
21+
// RUN: -enable-experimental-feature NonescapableTypes \
22+
// RUN: %t/lifetime_dependence.swiftinterface -o %t/lifetime_dependence.swiftmodule
23+
24+
// RUN: %target-swift-frontend -typecheck -I %t %s \
25+
// RUN: -disable-experimental-parser-round-trip \
26+
// RUN: -enable-experimental-feature NoncopyableGenerics \
27+
// RUN: -enable-experimental-feature NonescapableTypes
28+
29+
import lifetime_dependence
30+
31+
// CHECK: #if compiler(>=5.3) && $NoncopyableGenerics
32+
// CHECK: @inlinable internal init(_ ptr: Swift.UnsafeRawBufferPointer, _ a: borrowing Swift.Array<Swift.Int>) -> _borrow(a) Self {
33+
// CHECK: @inlinable internal init(_ ptr: Swift.UnsafeRawBufferPointer, _ a: consuming lifetime_dependence.AnotherView) -> _consume(a) Self {
34+
// CHECK: #endif
35+
36+
// CHECK: #if compiler(>=5.3) && $NoncopyableGenerics
37+
// CHECK: @inlinable public func derive(_ x: consuming lifetime_dependence.BufferView) -> _consume(x) lifetime_dependence.BufferView {
38+
// CHECK: #endif
39+
40+
// CHECK: #if compiler(>=5.3) && $NoncopyableGenerics
41+
// CHECK: @inlinable public func consumeAndCreate(_ view: consuming lifetime_dependence.BufferView) -> _consume(view) lifetime_dependence.BufferView {
42+
// CHECK: #endif
43+
44+
// CHECK: #if compiler(>=5.3) && $NoncopyableGenerics
45+
// CHECK: @inlinable public func deriveThisOrThat(_ this: consuming lifetime_dependence.BufferView, _ that: consuming lifetime_dependence.BufferView) -> _consume(this) _consume(that) lifetime_dependence.BufferView {
46+
// CHECK: #endif

0 commit comments

Comments
 (0)