Destroy temporaries at the end of expression statements. (#7513)

Instead of tracking the cleanup scope depth on entry to each scope,
track an "ambient" cleanup scope depth that's *after* the destructors of
local variables in that scope. This gets increased to include the
destructors of local variables when we create a name-binding
declaration. Then, when we reach a point where temporaries should be
destroyed, run cleanups that are after the ambient cleanup scope depth
on the stack. This happens:

* At the `;` of a statement expression.
* At the `)` of an `if` or `while` statement.
* After performing the implied `HasValue()` call in a `for` statement.

Per informal agreement with leads, this means we lifetime-extend all
temporaries created in the initializer of a name-binding declaration to
the full scope of that declaration, but that temporaries created in an
expression statement are destroyed at the `;`.
This commit is contained in:
Richard Smith
2026-07-16 15:12:39 +00:00
committed by GitHub
parent 81e495ee53
commit 703529fc55
37 changed files with 844 additions and 384 deletions
@@ -137,6 +137,7 @@ fn M() {
// CHECK:STDOUT: call void @llvm.lifetime.start.p0(ptr %.loc29_6.3.temp), !dbg !51
// CHECK:STDOUT: %H.call.loc29 = call i32 @_CH.Main.5bd8d5767580997a(i32 %x), !dbg !51
// CHECK:STDOUT: store i32 %H.call.loc29, ptr %.loc29_6.3.temp, align 4, !dbg !51
// CHECK:STDOUT: call void @"_COp.5e27612b9dd31a14:core.Destroy.Core"(ptr %.loc29_6.3.temp), !dbg !51
// CHECK:STDOUT: %H.call.loc30 = call %type @_CH.Main.ee509b43cf4eb3c5(%type zeroinitializer), !dbg !60
// CHECK:STDOUT: call void @llvm.lifetime.start.p0(ptr %.loc31_8.3.temp), !dbg !52
// CHECK:STDOUT: %G.call = call i32 @_CG.Main.58016a73bff04416(i32 %x), !dbg !52
@@ -145,6 +146,8 @@ fn M() {
// CHECK:STDOUT: %.loc31_8.5 = load i32, ptr %.loc31_8.3.temp, align 4, !dbg !52
// CHECK:STDOUT: %H.call.loc31 = call i32 @_CH.Main.5bd8d5767580997a(i32 %.loc31_8.5), !dbg !53
// CHECK:STDOUT: store i32 %H.call.loc31, ptr %.loc31_9.3.temp, align 4, !dbg !53
// CHECK:STDOUT: call void @"_COp.5e27612b9dd31a14:core.Destroy.Core"(ptr %.loc31_9.3.temp), !dbg !53
// CHECK:STDOUT: call void @"_COp.5e27612b9dd31a14:core.Destroy.Core"(ptr %.loc31_8.3.temp), !dbg !52
// CHECK:STDOUT: call void @llvm.lifetime.start.p0(ptr %var_f64.var), !dbg !54
// CHECK:STDOUT: store double poison, ptr %var_f64.var, align 8, !dbg !54
// CHECK:STDOUT: %.loc35_5 = load double, ptr %var_f64.var, align 8, !dbg !61
@@ -167,9 +170,6 @@ fn M() {
// CHECK:STDOUT: call void @"_COp.807eb4f2d3ed7e54:core.Destroy.Core"(ptr %.loc43_6.3.temp), !dbg !59
// CHECK:STDOUT: call void @"_COp.807eb4f2d3ed7e54:core.Destroy.Core"(ptr %c.var), !dbg !58
// CHECK:STDOUT: call void @"_COp.30d8007fdcc57f83:core.Destroy.Core"(ptr %var_f64.var), !dbg !54
// CHECK:STDOUT: call void @"_COp.5e27612b9dd31a14:core.Destroy.Core"(ptr %.loc31_9.3.temp), !dbg !53
// CHECK:STDOUT: call void @"_COp.5e27612b9dd31a14:core.Destroy.Core"(ptr %.loc31_8.3.temp), !dbg !52
// CHECK:STDOUT: call void @"_COp.5e27612b9dd31a14:core.Destroy.Core"(ptr %.loc29_6.3.temp), !dbg !51
// CHECK:STDOUT: ret i32 %x, !dbg !69
// CHECK:STDOUT: }
// CHECK:STDOUT:
@@ -194,6 +194,7 @@ fn M() {
// CHECK:STDOUT: call void @llvm.lifetime.start.p0(ptr %.loc29_6.3.temp), !dbg !77
// CHECK:STDOUT: %H.call.loc29 = call double @_CH.Main.c27b765f2159c9a0(double %x), !dbg !77
// CHECK:STDOUT: store double %H.call.loc29, ptr %.loc29_6.3.temp, align 8, !dbg !77
// CHECK:STDOUT: call void @"_COp.30d8007fdcc57f83:core.Destroy.Core"(ptr %.loc29_6.3.temp), !dbg !77
// CHECK:STDOUT: %H.call.loc30 = call %type @_CH.Main.ee509b43cf4eb3c5(%type zeroinitializer), !dbg !86
// CHECK:STDOUT: call void @llvm.lifetime.start.p0(ptr %.loc31_8.3.temp), !dbg !78
// CHECK:STDOUT: %G.call = call double @_CG.Main.fa1f0912a5bbe922(double %x), !dbg !78
@@ -202,6 +203,8 @@ fn M() {
// CHECK:STDOUT: %.loc31_8.5 = load double, ptr %.loc31_8.3.temp, align 8, !dbg !78
// CHECK:STDOUT: %H.call.loc31 = call double @_CH.Main.c27b765f2159c9a0(double %.loc31_8.5), !dbg !79
// CHECK:STDOUT: store double %H.call.loc31, ptr %.loc31_9.3.temp, align 8, !dbg !79
// CHECK:STDOUT: call void @"_COp.30d8007fdcc57f83:core.Destroy.Core"(ptr %.loc31_9.3.temp), !dbg !79
// CHECK:STDOUT: call void @"_COp.30d8007fdcc57f83:core.Destroy.Core"(ptr %.loc31_8.3.temp), !dbg !78
// CHECK:STDOUT: call void @llvm.lifetime.start.p0(ptr %var_f64.var), !dbg !80
// CHECK:STDOUT: store double poison, ptr %var_f64.var, align 8, !dbg !80
// CHECK:STDOUT: %.loc35_5 = load double, ptr %var_f64.var, align 8, !dbg !87
@@ -224,9 +227,6 @@ fn M() {
// CHECK:STDOUT: call void @"_COp.807eb4f2d3ed7e54:core.Destroy.Core"(ptr %.loc43_6.3.temp), !dbg !85
// CHECK:STDOUT: call void @"_COp.807eb4f2d3ed7e54:core.Destroy.Core"(ptr %c.var), !dbg !84
// CHECK:STDOUT: call void @"_COp.30d8007fdcc57f83:core.Destroy.Core"(ptr %var_f64.var), !dbg !80
// CHECK:STDOUT: call void @"_COp.30d8007fdcc57f83:core.Destroy.Core"(ptr %.loc31_9.3.temp), !dbg !79
// CHECK:STDOUT: call void @"_COp.30d8007fdcc57f83:core.Destroy.Core"(ptr %.loc31_8.3.temp), !dbg !78
// CHECK:STDOUT: call void @"_COp.30d8007fdcc57f83:core.Destroy.Core"(ptr %.loc29_6.3.temp), !dbg !77
// CHECK:STDOUT: ret double %x, !dbg !95
// CHECK:STDOUT: }
// CHECK:STDOUT: