Skip to content

Commit d985bf6

Browse files
authored
Merge pull request #81567 from eeckstein/fix-builtin-emplace
SILGen: insert an `end_lifetime` in the throw-branch of a `Builtin.emplace`
2 parents 35ee368 + ddbc3a2 commit d985bf6

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
@@ -2188,6 +2188,13 @@ static ManagedValue emitBuiltinEmplace(SILGenFunction &SGF,
21882188
{
21892189
SGF.B.emitBlock(errorBB);
21902190

2191+
// When the closure throws an error, it needs to clean up the buffer. This
2192+
// means that the buffer is uninitialized at this point.
2193+
// We need an `end_lifetime` so that the move-only checker doesn't insert
2194+
// a wrong `destroy_addr` because it thinks that the buffer is initialized
2195+
// here.
2196+
SGF.B.createEndLifetime(loc, buffer);
2197+
21912198
SGF.Cleanups.emitCleanupsForReturn(CleanupLocation(loc), IsForUnwind);
21922199

21932200
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)