Skip to content

Commit 88979f9

Browse files
committed
[silgen] When emitting a top level error, end_lifetime the error after we have logged it.
This is not strictly necessary since we are going to be exiting the program, but the ownership verifier thinks that the error is leaked otherwise. By using the end_lifetime we avoid having a small increase in code-size since it is a no-op after ownership is removed. rdar://29791263
1 parent 965d697 commit 88979f9

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

lib/SILGen/SILGen.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1535,6 +1535,14 @@ class SourceFileScope {
15351535
sgm.getASTContext().getIdentifier("errorInMain"),
15361536
sgm.Types.getEmptyTupleType(), {}, {error});
15371537

1538+
// Then end the lifetime of the error.
1539+
//
1540+
// We do this to appease the ownership verifier. We do not care about
1541+
// actually destroying the value since we are going to immediately exit,
1542+
// so this saves us a slight bit of code-size since end_lifetime is
1543+
// stripped out after ownership is removed.
1544+
SGF.B.createEndLifetime(moduleLoc, error);
1545+
15381546
// Signal an abnormal exit by returning 1.
15391547
SGF.Cleanups.emitCleanupsForReturn(CleanupLocation::get(moduleLoc),
15401548
IsForUnwind);

test/SILGen/toplevel_errors.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: %target-swift-emit-silgen %s | %FileCheck %s
1+
// RUN: %target-swift-emit-silgen -enable-sil-ownership %s | %FileCheck %s
22

33
enum MyError : Error {
44
case A, B
@@ -21,6 +21,7 @@ throw MyError.A
2121

2222
// CHECK: bb2([[T0:%.*]] : @owned $Error):
2323
// CHECK: builtin "errorInMain"([[T0]] : $Error)
24+
// CHECK: end_lifetime [[T0]]
2425
// CHECK: [[T0:%.*]] = integer_literal $Builtin.Int32, 1
2526
// CHECK: [[T1:%.*]] = struct $Int32 ([[T0]] : $Builtin.Int32)
2627
// CHECK: br bb1([[T1]] : $Int32)

0 commit comments

Comments
 (0)