Skip to content

Commit 535f2be

Browse files
authored
Merge pull request #67312 from xedin/init-accessor-type-fixes
[SILGen] InitAccessors: Lower initializes/accesses property types bef…
2 parents 255beb3 + eeb70cf commit 535f2be

File tree

3 files changed

+37
-8
lines changed

3 files changed

+37
-8
lines changed

lib/SIL/IR/SILFunctionType.cpp

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2373,22 +2373,27 @@ static CanSILFunctionType getSILFunctionTypeForInitAccessor(
23732373
// Drop `self` parameter.
23742374
inputs.pop_back();
23752375

2376+
auto getLoweredTypeOfProperty = [&](VarDecl *property) {
2377+
auto type = property->getInterfaceType();
2378+
AbstractionPattern pattern(genericSig, type->getCanonicalType());
2379+
auto loweredTy = TC.getLoweredType(pattern, type, context);
2380+
return loweredTy.getASTType();
2381+
};
2382+
23762383
// accessed properties appear as `inout` parameters because they could be
23772384
// read from and modified.
23782385
for (auto *property : accessor->getAccessedProperties()) {
2379-
inputs.push_back(
2380-
SILParameterInfo(property->getInterfaceType()->getCanonicalType(),
2381-
ParameterConvention::Indirect_Inout));
2386+
inputs.push_back(SILParameterInfo(getLoweredTypeOfProperty(property),
2387+
ParameterConvention::Indirect_Inout));
23822388
}
23832389

23842390
SmallVector<SILResultInfo, 8> results;
23852391

23862392
// initialized properties appear as `@out` results because they are
23872393
// initialized by the accessor.
23882394
for (auto *property : accessor->getInitializedProperties()) {
2389-
results.push_back(
2390-
SILResultInfo(property->getInterfaceType()->getCanonicalType(),
2391-
ResultConvention::Indirect));
2395+
results.push_back(SILResultInfo(getLoweredTypeOfProperty(property),
2396+
ResultConvention::Indirect));
23922397
}
23932398

23942399
auto calleeConvention = ParameterConvention::Direct_Unowned;

lib/SILGen/SILGenConstructor.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1584,8 +1584,8 @@ void SILGenFunction::emitInitAccessor(AccessorDecl *accessor) {
15841584
bool markUninitialized = false) {
15851585
auto *arg = ParamDecl::createImplicit(
15861586
getASTContext(), property->getBaseIdentifier(),
1587-
property->getBaseIdentifier(), type.getASTType()->mapTypeOutOfContext(),
1588-
accessor, ParamSpecifier::InOut);
1587+
property->getBaseIdentifier(), property->getInterfaceType(), accessor,
1588+
ParamSpecifier::InOut);
15891589

15901590
RegularLocation loc(property);
15911591
loc.markAutoGenerated();

test/SILOptimizer/init_accessors.swift

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,30 @@ func test_local_with_memberwise() {
395395
_ = TestMemberwiseGeneric(a: 1, pair: ("a", [0]))
396396
}
397397

398+
// CHECK-LABEL: sil private [ossa] @$s14init_accessors023test_type_lowering_for_A9_accessoryyF4TestL_V2fnADyxq_Gq_xc_tcfC : $@convention(method) <T, U> (@owned @callee_guaranteed @substituted <τ_0_0, τ_0_1> (@in_guaranteed τ_0_0) -> @out τ_0_1 for <T, U>, @thin Test<T, U>.Type) -> @owned Test<T, U>
399+
// CHECK: {{.*}} = function_ref @$s14init_accessors023test_type_lowering_for_A9_accessoryyF4TestL_V2fnyq_xcvi : $@convention(thin) <τ_0_0, τ_0_1> (@owned @callee_guaranteed @substituted <τ_0_0, τ_0_1> (@in_guaranteed τ_0_0) -> @out τ_0_1 for <τ_0_0, τ_0_1>) -> @out Optional<@callee_guaranteed @substituted <τ_0_0, τ_0_1> (@in_guaranteed τ_0_0) -> @out τ_0_1 for <τ_0_0, τ_0_1>>
400+
func test_type_lowering_for_init_accessor() {
401+
struct Test<T, U> {
402+
var _fn: ((T) -> U)? = nil
403+
404+
// CHECK-LABEL: sil private [ossa] @$s14init_accessors023test_type_lowering_for_A9_accessoryyF4TestL_V2fnyq_xcvi : $@convention(thin) <T, U> (@owned @callee_guaranteed @substituted <τ_0_0, τ_0_1> (@in_guaranteed τ_0_0) -> @out τ_0_1 for <T, U>) -> @out Optional<@callee_guaranteed @substituted <τ_0_0, τ_0_1> (@in_guaranteed τ_0_0) -> @out τ_0_1 for <T, U>>
405+
var fn: (T) -> U {
406+
@storageRestrictions(initializes: _fn)
407+
init { _fn = newValue }
408+
get { _fn! }
409+
set { _fn = newValue }
410+
}
411+
412+
init(fn: @escaping (T) -> U) {
413+
self.fn = fn
414+
}
415+
}
416+
417+
_ = Test<Int, () -> Void> { _ in
418+
return {}
419+
} // Ok
420+
}
421+
398422
func test_assignments() {
399423
struct Test {
400424
var _a: Int

0 commit comments

Comments
 (0)