Skip to content

Commit 2a75214

Browse files
authored
Merge pull request swiftlang#68178 from xedin/rdar-114350227
[SILGen] InitAccessors: Materialize default argument if it's required…
2 parents 6de8597 + e96f9e5 commit 2a75214

File tree

2 files changed

+65
-5
lines changed

2 files changed

+65
-5
lines changed

lib/SILGen/SILGenFunction.cpp

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1868,14 +1868,14 @@ void SILGenFunction::emitAssignOrInit(SILLocation loc, ManagedValue selfValue,
18681868
// Emit the init accessor function partially applied to the base.
18691869
SILValue initFRef = emitGlobalFunctionRef(
18701870
loc, getAccessorDeclRef(field->getOpaqueAccessor(AccessorKind::Init)));
1871+
1872+
auto initTy = initFRef->getType().castTo<SILFunctionType>();
1873+
18711874
if (!substitutions.empty()) {
18721875
// If there are substitutions we need to emit partial apply to
18731876
// apply substitutions to the init accessor reference type.
1874-
auto initTy =
1875-
initFRef->getType().castTo<SILFunctionType>()->substGenericArgs(
1876-
SGM.M, substitutions, getTypeExpansionContext());
1877-
1878-
SILFunctionConventions setterConv(initTy, SGM.M);
1877+
initTy = initTy->substGenericArgs(SGM.M, substitutions,
1878+
getTypeExpansionContext());
18791879

18801880
// Emit partial apply without argument to produce a substituted
18811881
// init accessor reference.
@@ -1885,6 +1885,20 @@ void SILGenFunction::emitAssignOrInit(SILLocation loc, ManagedValue selfValue,
18851885
initFRef = emitManagedRValueWithCleanup(initPAI).getValue();
18861886
}
18871887

1888+
// Check whether value is supposed to be passed indirectly and
1889+
// materialize if required.
1890+
{
1891+
SILFunctionConventions initConv(initTy, SGM.M);
1892+
1893+
auto newValueArgIdx = initConv.getSILArgIndexOfFirstParam();
1894+
// If we need the argument in memory, materialize an address.
1895+
if (initConv.getSILArgumentConvention(newValueArgIdx)
1896+
.isIndirectConvention() &&
1897+
!newValue.getType().isAddress()) {
1898+
newValue = newValue.materialize(*this, loc);
1899+
}
1900+
}
1901+
18881902
SILValue setterFRef;
18891903
if (auto *setter = field->getOpaqueAccessor(AccessorKind::Set)) {
18901904
setterFRef = emitApplyOfSetterToBase(loc, SILDeclRef(setter), selfValue,
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// RUN: %target-swift-frontend -primary-file %s -Onone -emit-sil \
2+
// RUN: -enable-library-evolution \
3+
// RUN: -Xllvm -sil-print-after=definite-init \
4+
// RUN: -o /dev/null -module-name init_accessors 2>&1 | %FileCheck %s
5+
6+
public class Test {
7+
public enum State: Equatable {
8+
case start
9+
case failed(any Swift.Error)
10+
case completed
11+
12+
public static func == (lhs: Self, rhs: Self) -> Bool { false }
13+
}
14+
15+
private var _state: State = .start {
16+
didSet { }
17+
}
18+
19+
public private(set) var state: State = .start {
20+
@storageRestrictions(initializes: _state)
21+
init {
22+
_state = newValue
23+
}
24+
25+
get { _state }
26+
set { _state = newValue }
27+
}
28+
29+
// CHECK-LABEL: sil [ossa] @$s14init_accessors4TestCACycfc : $@convention(method) (@owned Test) -> @owned Test
30+
//
31+
// CHECK: [[DEFAULT_VALUE_INIT:%.*]] = function_ref @$s14init_accessors4TestC5stateAC5StateOvpfi : $@convention(thin) () -> @out Test.State
32+
// CHECK-NEXT: [[DEFAULT_VALUE_SLOT:%.*]] = alloc_stack $Test.State
33+
// CHECK-NEXT: {{.*}} = apply [[DEFAULT_VALUE_INIT]]([[DEFAULT_VALUE_SLOT]]) : $@convention(thin) () -> @out Test.State
34+
// CHECK-NEXT: [[DEFAULT_VALUE:%.*]] = load [take] [[DEFAULT_VALUE_SLOT]] : $*Test.State
35+
// CHECK: [[NEW_VALUE:%.*]] = alloc_stack $Test.State
36+
// CHECK-NEXT: store [[DEFAULT_VALUE]] to [init] [[NEW_VALUE]] : $*Test.State
37+
// CHECK: assign_or_init [init] #Test.state, self %0 : $Test, value [[NEW_VALUE]] : $*Test.State, init {{.*}} : $@convention(thin) (@in Test.State) -> @out Test.State, set {{.*}} : $@callee_guaranteed (@in Test.State) -> ()
38+
//
39+
// CHECK: [[START_STATE:%.*]] = enum $Test.State, #Test.State.start!enumelt
40+
// CHECK-NEXT: [[NEW_VALUE:%.*]] = alloc_stack $Test.State
41+
// CHECK-NEXT: store [[START_STATE]] to [trivial] [[NEW_VALUE]] : $*Test.State
42+
// CHECK: assign_or_init [set] #Test.state, self %0 : $Test, value [[NEW_VALUE]] : $*Test.State, init {{.*}} : $@convention(thin) (@in Test.State) -> @out Test.State, set {{.*}} : $@callee_guaranteed (@in Test.State) -> ()
43+
public init() {
44+
state = .start
45+
}
46+
}

0 commit comments

Comments
 (0)