Skip to content

Commit f8e2723

Browse files
authored
Merge pull request #20033 from gottesmm/pr-aef61f8fa8d192698e4fe42ceef1bc68d72337e8
[silgen] When emitting a top level error, destroy the error after we …
2 parents f010298 + 88979f9 commit f8e2723

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)