Skip to content

Commit ecdcef8

Browse files
authored
Merge pull request #81606 from eeckstein/fix-builtin-emplace-6.2
[6.2] SILGen: insert an `end_lifetime` in the throw-branch of a `Builtin.emplace`
2 parents 8e0a389 + 09d2464 commit ecdcef8

File tree

3 files changed

+13
-0
lines changed

3 files changed

+13
-0
lines changed

lib/SILGen/SILGenBuiltin.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2170,6 +2170,13 @@ static ManagedValue emitBuiltinEmplace(SILGenFunction &SGF,
21702170
{
21712171
SGF.B.emitBlock(errorBB);
21722172

2173+
// When the closure throws an error, it needs to clean up the buffer. This
2174+
// means that the buffer is uninitialized at this point.
2175+
// We need an `end_lifetime` so that the move-only checker doesn't insert
2176+
// a wrong `destroy_addr` because it thinks that the buffer is initialized
2177+
// here.
2178+
SGF.B.createEndLifetime(loc, buffer);
2179+
21732180
SGF.Cleanups.emitCleanupsForReturn(CleanupLocation(loc), IsForUnwind);
21742181

21752182
SGF.B.createThrowAddr(loc);

lib/SILOptimizer/Mandatory/MoveOnlyUtils.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,7 @@ bool noncopyable::memInstMustConsume(Operand *memOper) {
297297
(CAI->getDest() == address && !CAI->isInitializationOfDest());
298298
}
299299
case SILInstructionKind::DestroyAddrInst:
300+
case SILInstructionKind::EndLifetimeInst:
300301
return true;
301302
case SILInstructionKind::DropDeinitInst:
302303
assert(memOper->get()->getType().isValueTypeWithDeinit());

test/stdlib/InlineArray.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
import StdlibUnittest
2525
import Synchronization
2626

27+
protocol P {}
28+
2729
@available(SwiftStdlib 6.2, *)
2830
@main
2931
enum InlineArrayTests {
@@ -172,6 +174,9 @@ enum InlineArrayTests {
172174
_expectThrows {
173175
let _ = try InlineArray<1, String> { _ in throw error }
174176
}
177+
_expectThrows {
178+
let _ = try InlineArray<1, any P> { _ in throw error }
179+
}
175180
_expectThrows {
176181
let _ = try InlineArray<2, String> { index in
177182
if index == 0 { "first" } else { throw error }

0 commit comments

Comments
 (0)