Skip to content

SILGen: insert an end_lifetime in the throw-branch of a Builtin.emplace #81567

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions lib/SILGen/SILGenBuiltin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2188,6 +2188,13 @@ static ManagedValue emitBuiltinEmplace(SILGenFunction &SGF,
{
SGF.B.emitBlock(errorBB);

// When the closure throws an error, it needs to clean up the buffer. This
// means that the buffer is uninitialized at this point.
// We need an `end_lifetime` so that the move-only checker doesn't insert
// a wrong `destroy_addr` because it thinks that the buffer is initialized
// here.
SGF.B.createEndLifetime(loc, buffer);

SGF.Cleanups.emitCleanupsForReturn(CleanupLocation(loc), IsForUnwind);

SGF.B.createThrowAddr(loc);
Expand Down
1 change: 1 addition & 0 deletions lib/SILOptimizer/Mandatory/MoveOnlyUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,7 @@ bool noncopyable::memInstMustConsume(Operand *memOper) {
(CAI->getDest() == address && !CAI->isInitializationOfDest());
}
case SILInstructionKind::DestroyAddrInst:
case SILInstructionKind::EndLifetimeInst:
return true;
case SILInstructionKind::DropDeinitInst:
assert(memOper->get()->getType().isValueTypeWithDeinit());
Expand Down
5 changes: 5 additions & 0 deletions test/stdlib/InlineArray.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import StdlibUnittest
import Synchronization

protocol P {}

@available(SwiftStdlib 6.2, *)
@main
enum InlineArrayTests {
Expand Down Expand Up @@ -172,6 +174,9 @@ enum InlineArrayTests {
_expectThrows {
let _ = try InlineArray<1, String> { _ in throw error }
}
_expectThrows {
let _ = try InlineArray<1, any P> { _ in throw error }
}
_expectThrows {
let _ = try InlineArray<2, String> { index in
if index == 0 { "first" } else { throw error }
Expand Down