Skip to content

Commit 44c2c07

Browse files
authored
Merge pull request swiftlang#72152 from meg-gupta/silcrash
Fix computation of argument index in the presence of indirect error results
2 parents 85ea5ff + f3b225a commit 44c2c07

File tree

3 files changed

+27
-2
lines changed

3 files changed

+27
-2
lines changed

lib/SILOptimizer/Mandatory/DIMemoryUseCollector.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -959,7 +959,8 @@ void ElementUseCollector::collectUses(SILValue Pointer, unsigned BaseEltNo) {
959959
unsigned ArgumentNumber = Op->getOperandNumber() - 1;
960960

961961
// If this is an out-parameter, it is like a store.
962-
unsigned NumIndirectResults = substConv.getNumIndirectSILResults();
962+
unsigned NumIndirectResults = substConv.getNumIndirectSILResults() +
963+
substConv.getNumIndirectSILErrorResults();
963964
if (ArgumentNumber < NumIndirectResults) {
964965
assert(!InStructSubElement && "We're initializing sub-members?");
965966
addElementUses(BaseEltNo, PointeeType, User, DIUseKind::Initialization);

lib/SILOptimizer/Mandatory/PMOMemoryUseCollector.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,8 @@ bool ElementUseCollector::collectUses(SILValue Pointer) {
364364
unsigned ArgumentNumber = UI->getOperandNumber() - 1;
365365

366366
// If this is an out-parameter, it is like a store.
367-
unsigned NumIndirectResults = substConv.getNumIndirectSILResults();
367+
unsigned NumIndirectResults = substConv.getNumIndirectSILResults() +
368+
substConv.getNumIndirectSILErrorResults();
368369
if (ArgumentNumber < NumIndirectResults) {
369370
// We do not support initializing sub members. This is an old
370371
// restriction from when this code was used by Definite

test/SIL/typed_throws_sil_crash.swift

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// RUN: %target-swift-frontend -module-name=test -emit-sil %s -enable-builtin-module
2+
3+
import Builtin
4+
5+
// Ensure compiler does not crash
6+
struct Wrapper {
7+
var a = [1, 2, 3]
8+
init() {
9+
_withUnsafeMutablePointer(to: &a) {
10+
bar($0)
11+
}
12+
}
13+
}
14+
15+
func _withUnsafeMutablePointer<T, E: Error, Result>(
16+
to value: inout T,
17+
_ body: (UnsafeMutablePointer<T>) throws(E) -> Result
18+
) throws(E) -> Result {
19+
try body(UnsafeMutablePointer<T>(Builtin.addressof(&value)))
20+
}
21+
22+
func bar<T>(_ arg: UnsafeMutablePointer<[T]>) {}
23+

0 commit comments

Comments
 (0)