Skip to content

Commit e7d2dcc

Browse files
committed
SILGen: Fix lowering of ArrayExpr to in-place ConvertingInitialization
1 parent d0779bd commit e7d2dcc

File tree

2 files changed

+45
-2
lines changed

2 files changed

+45
-2
lines changed

lib/SILGen/SILGenExpr.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3684,6 +3684,8 @@ RValue RValueEmitter::visitArrayExpr(ArrayExpr *E, SGFContext C) {
36843684
RValue arg(SGF, loc, arrayTy,
36853685
emitEndVarargs(SGF, loc, std::move(varargsInfo)));
36863686

3687+
arg = scope.popPreservingValue(std::move(arg));
3688+
36873689
// Add an argument label for init(arrayLiteral: T...) as the above tuple is of
36883690
// the form (T...) with no label.
36893691
assert(argLabels.size() == 1 && !argLabels[0].empty() &&
@@ -3693,8 +3695,8 @@ RValue RValueEmitter::visitArrayExpr(ArrayExpr *E, SGFContext C) {
36933695
arg.rewriteType(newType->getCanonicalType());
36943696

36953697
// Call the builtin initializer.
3696-
return scope.popPreservingValue(SGF.emitApplyAllocatingInitializer(
3697-
loc, init, std::move(arg), E->getType(), C));
3698+
return SGF.emitApplyAllocatingInitializer(
3699+
loc, init, std::move(arg), E->getType(), C);
36983700
}
36993701

37003702
RValue RValueEmitter::visitDictionaryExpr(DictionaryExpr *E, SGFContext C) {

test/SILGen/objc_bridging_array.swift

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
2+
// RUN: %target-swift-emit-silgen(mock-sdk: %clang-importer-sdk) %s | %FileCheck %s
3+
4+
// REQUIRES: objc_interop
5+
6+
import Foundation
7+
8+
class Child : NSObject {}
9+
10+
@objc protocol Parent {
11+
var children: [Child] { get set }
12+
}
13+
14+
func setChildren(p: Parent, c: Child) {
15+
p.children = [c]
16+
}
17+
18+
// CHECK-LABEL: sil hidden [ossa] @$s19objc_bridging_array11setChildren1p1cyAA6Parent_p_AA5ChildCtF : $@convention(thin) (@guaranteed Parent, @guaranteed Child) -> () {
19+
// CHECK: [[OPENED:%.*]] = open_existential_ref %0 : $Parent to $[[OPENED_TYPE:.* Parent]]
20+
// CHECK: [[COPIED:%.*]] = copy_value [[OPENED]] : $[[OPENED_TYPE]]
21+
// CHECK: [[LENGTH:%.*]] = integer_literal $Builtin.Word, 1
22+
// CHECK: [[FN:%.*]] = function_ref @$ss27_allocateUninitializedArrayySayxG_BptBwlF : $@convention(thin) <τ_0_0> (Builtin.Word) -> (@owned Array<τ_0_0>, Builtin.RawPointer)
23+
// CHECK: [[ARRAY_AND_BUFFER:%.*]] = apply [[FN]]<Child>([[LENGTH]]) : $@convention(thin) <τ_0_0> (Builtin.Word) -> (@owned Array<τ_0_0>, Builtin.RawPointer)
24+
// CHECK: ([[ARRAY:%.*]], [[BUFFER_PTR:%.*]]) = destructure_tuple [[ARRAY_AND_BUFFER]] : $(Array<Child>, Builtin.RawPointer)
25+
// CHECK: [[BUFFER:%.*]] = pointer_to_address [[BUFFER_PTR]] : $Builtin.RawPointer to [strict] $*Child
26+
// CHECK: [[CHILD:%.*]] = copy_value %1 : $Child
27+
// CHECK: store [[CHILD]] to [init] [[BUFFER]] : $*Child
28+
// CHECK: [[METATYPE:%.*]] = metatype $@thin Array<Child>.Type
29+
// CHECK: [[FN:%.*]] = function_ref @$sSa12arrayLiteralSayxGxd_tcfC : $@convention(method) <τ_0_0> (@owned Array<τ_0_0>, @thin Array<τ_0_0>.Type) -> @owned Array<τ_0_0>
30+
// CHECK: [[NEW_ARRAY:%.*]] = apply [[FN]]<Child>([[ARRAY]], [[METATYPE]]) : $@convention(method) <τ_0_0> (@owned Array<τ_0_0>, @thin Array<τ_0_0>.Type) -> @owned Array<τ_0_0>
31+
// CHECK: [[FN:%.*]] = function_ref @$sSa10FoundationE19_bridgeToObjectiveCSo7NSArrayCyF : $@convention(method) <τ_0_0> (@guaranteed Array<τ_0_0>) -> @owned NSArray
32+
// CHECK: [[BORROW_ARRAY:%.*]] = begin_borrow [[NEW_ARRAY]] : $Array<Child>
33+
// CHECK: [[BRIDGED_ARRAY:%.*]] = apply [[FN]]<Child>([[BORROW_ARRAY]]) : $@convention(method) <τ_0_0> (@guaranteed Array<τ_0_0>) -> @owned NSArray
34+
// CHECK: end_borrow [[BORROW_ARRAY]] : $Array<Child>
35+
// CHECK: destroy_value [[NEW_ARRAY]] : $Array<Child>
36+
// CHECK: [[FN:%.*]] = objc_method [[COPIED]] : $[[OPENED_TYPE]], #Parent.children!setter.1.foreign : <Self where Self : Parent> (Self) -> ([Child]) -> (), $@convention(objc_method) <τ_0_0 where τ_0_0 : Parent> (NSArray, τ_0_0) -> ()
37+
// CHECK: apply [[FN]]<[[OPENED_TYPE]]>([[BRIDGED_ARRAY]], [[COPIED]]) : $@convention(objc_method) <τ_0_0 where τ_0_0 : Parent> (NSArray, τ_0_0) -> ()
38+
// CHECK: destroy_value [[BRIDGED_ARRAY]] : $NSArray
39+
// CHECK: destroy_value [[COPIED]] : $[[OPENED_TYPE]]
40+
// CHECK: [[RESULT:%.*]] = tuple ()
41+
// CHECK: return [[RESULT]] : $()

0 commit comments

Comments
 (0)