Skip to content

Fix computation of argument index in the presence of indirect error results #72152

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
Mar 8, 2024
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
3 changes: 2 additions & 1 deletion lib/SILOptimizer/Mandatory/DIMemoryUseCollector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -959,7 +959,8 @@ void ElementUseCollector::collectUses(SILValue Pointer, unsigned BaseEltNo) {
unsigned ArgumentNumber = Op->getOperandNumber() - 1;

// If this is an out-parameter, it is like a store.
unsigned NumIndirectResults = substConv.getNumIndirectSILResults();
unsigned NumIndirectResults = substConv.getNumIndirectSILResults() +
substConv.getNumIndirectSILErrorResults();
if (ArgumentNumber < NumIndirectResults) {
assert(!InStructSubElement && "We're initializing sub-members?");
addElementUses(BaseEltNo, PointeeType, User, DIUseKind::Initialization);
Expand Down
3 changes: 2 additions & 1 deletion lib/SILOptimizer/Mandatory/PMOMemoryUseCollector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,8 @@ bool ElementUseCollector::collectUses(SILValue Pointer) {
unsigned ArgumentNumber = UI->getOperandNumber() - 1;

// If this is an out-parameter, it is like a store.
unsigned NumIndirectResults = substConv.getNumIndirectSILResults();
unsigned NumIndirectResults = substConv.getNumIndirectSILResults() +
substConv.getNumIndirectSILErrorResults();
if (ArgumentNumber < NumIndirectResults) {
// We do not support initializing sub members. This is an old
// restriction from when this code was used by Definite
Expand Down
23 changes: 23 additions & 0 deletions test/SIL/typed_throws_sil_crash.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// RUN: %target-swift-frontend -module-name=test -emit-sil %s -enable-builtin-module

import Builtin

// Ensure compiler does not crash
struct Wrapper {
var a = [1, 2, 3]
init() {
_withUnsafeMutablePointer(to: &a) {
bar($0)
}
}
}

func _withUnsafeMutablePointer<T, E: Error, Result>(
to value: inout T,
_ body: (UnsafeMutablePointer<T>) throws(E) -> Result
) throws(E) -> Result {
try body(UnsafeMutablePointer<T>(Builtin.addressof(&value)))
}

func bar<T>(_ arg: UnsafeMutablePointer<[T]>) {}