Skip to content

Commit 8f91555

Browse files
committed
SILGen: Fix keypath getter/setter emission with opaque result types
Opaque result types are substituted away inside the SILFunction, so make sure we're looking at the correct SILFunctionType.
1 parent 2ac4b42 commit 8f91555

File tree

2 files changed

+97
-65
lines changed

2 files changed

+97
-65
lines changed

lib/SILGen/SILGenExpr.cpp

Lines changed: 83 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -2659,33 +2659,35 @@ static SILFunction *getOrCreateKeyPathGetter(SILGenModule &SGM,
26592659
}
26602660

26612661
// Build the signature of the thunk as expected by the keypath runtime.
2662-
CanType loweredBaseTy, loweredPropTy;
2663-
AbstractionPattern opaque = AbstractionPattern::getOpaque();
2662+
auto signature = [&]() {
2663+
CanType loweredBaseTy, loweredPropTy;
2664+
AbstractionPattern opaque = AbstractionPattern::getOpaque();
26642665

2665-
loweredBaseTy = SGM.Types.getLoweredRValueType(
2666-
TypeExpansionContext::minimal(), opaque, baseType);
2667-
loweredPropTy = SGM.Types.getLoweredRValueType(
2668-
TypeExpansionContext::minimal(), opaque, propertyType);
2669-
2670-
auto paramConvention = ParameterConvention::Indirect_In_Guaranteed;
2666+
loweredBaseTy = SGM.Types.getLoweredRValueType(
2667+
TypeExpansionContext::minimal(), opaque, baseType);
2668+
loweredPropTy = SGM.Types.getLoweredRValueType(
2669+
TypeExpansionContext::minimal(), opaque, propertyType);
2670+
2671+
auto paramConvention = ParameterConvention::Indirect_In_Guaranteed;
26712672

2672-
SmallVector<SILParameterInfo, 2> params;
2673-
params.push_back({loweredBaseTy, paramConvention});
2674-
auto &C = SGM.getASTContext();
2675-
if (!indexes.empty())
2676-
params.push_back({C.getUnsafeRawPointerDecl()->getDeclaredType()
2677-
->getCanonicalType(),
2678-
ParameterConvention::Direct_Unowned});
2679-
2680-
SILResultInfo result(loweredPropTy, ResultConvention::Indirect);
2681-
2682-
auto signature = SILFunctionType::get(genericSig,
2683-
SILFunctionType::ExtInfo::getThin(),
2684-
SILCoroutineKind::None,
2685-
ParameterConvention::Direct_Unowned,
2686-
params, {}, result, None,
2687-
SubstitutionMap(), false,
2688-
SGM.getASTContext());
2673+
SmallVector<SILParameterInfo, 2> params;
2674+
params.push_back({loweredBaseTy, paramConvention});
2675+
auto &C = SGM.getASTContext();
2676+
if (!indexes.empty())
2677+
params.push_back({C.getUnsafeRawPointerDecl()->getDeclaredType()
2678+
->getCanonicalType(),
2679+
ParameterConvention::Direct_Unowned});
2680+
2681+
SILResultInfo result(loweredPropTy, ResultConvention::Indirect);
2682+
2683+
return SILFunctionType::get(genericSig,
2684+
SILFunctionType::ExtInfo::getThin(),
2685+
SILCoroutineKind::None,
2686+
ParameterConvention::Direct_Unowned,
2687+
params, {}, result, None,
2688+
SubstitutionMap(), false,
2689+
SGM.getASTContext());
2690+
}();
26892691

26902692
// Find the function and see if we already created it.
26912693
auto name = Mangle::ASTMangler()
@@ -2711,9 +2713,15 @@ static SILFunction *getOrCreateKeyPathGetter(SILGenModule &SGM,
27112713
}
27122714

27132715
SILGenFunction subSGF(SGM, *thunk, SGM.SwiftModule);
2716+
2717+
signature = subSGF.F.getLoweredFunctionTypeInContext(
2718+
subSGF.F.getTypeExpansionContext());
2719+
27142720
auto entry = thunk->begin();
2715-
auto resultArgTy = result.getSILStorageType(SGM.M, signature);
2716-
auto baseArgTy = params[0].getSILStorageType(SGM.M, signature);
2721+
auto resultArgTy = signature->getSingleResult().getSILStorageType(
2722+
SGM.M, signature);
2723+
auto baseArgTy = signature->getParameters()[0].getSILStorageType(
2724+
SGM.M, signature);
27172725
if (genericEnv) {
27182726
resultArgTy = genericEnv->mapTypeIntoContext(SGM.M, resultArgTy);
27192727
baseArgTy = genericEnv->mapTypeIntoContext(SGM.M, baseArgTy);
@@ -2722,7 +2730,8 @@ static SILFunction *getOrCreateKeyPathGetter(SILGenModule &SGM,
27222730
auto baseArg = entry->createFunctionArgument(baseArgTy);
27232731
SILValue indexPtrArg;
27242732
if (!indexes.empty()) {
2725-
auto indexArgTy = params[1].getSILStorageType(SGM.M, signature);
2733+
auto indexArgTy = signature->getParameters()[1].getSILStorageType(
2734+
SGM.M, signature);
27262735
indexPtrArg = entry->createFunctionArgument(indexArgTy);
27272736
}
27282737

@@ -2792,41 +2801,43 @@ static SILFunction *getOrCreateKeyPathSetter(SILGenModule &SGM,
27922801
}
27932802

27942803
// Build the signature of the thunk as expected by the keypath runtime.
2795-
CanType loweredBaseTy, loweredPropTy;
2796-
{
2797-
AbstractionPattern opaque = AbstractionPattern::getOpaque();
2804+
auto signature = [&]() {
2805+
CanType loweredBaseTy, loweredPropTy;
2806+
{
2807+
AbstractionPattern opaque = AbstractionPattern::getOpaque();
27982808

2799-
loweredBaseTy = SGM.Types.getLoweredRValueType(
2800-
TypeExpansionContext::minimal(), opaque, baseType);
2801-
loweredPropTy = SGM.Types.getLoweredRValueType(
2802-
TypeExpansionContext::minimal(), opaque, propertyType);
2803-
}
2804-
2805-
auto &C = SGM.getASTContext();
2806-
2807-
auto paramConvention = ParameterConvention::Indirect_In_Guaranteed;
2808-
2809-
SmallVector<SILParameterInfo, 3> params;
2810-
// property value
2811-
params.push_back({loweredPropTy, paramConvention});
2812-
// base
2813-
params.push_back({loweredBaseTy,
2814-
property->isSetterMutating()
2815-
? ParameterConvention::Indirect_Inout
2816-
: paramConvention});
2817-
// indexes
2818-
if (!indexes.empty())
2819-
params.push_back({C.getUnsafeRawPointerDecl()->getDeclaredType()
2820-
->getCanonicalType(),
2821-
ParameterConvention::Direct_Unowned});
2822-
2823-
auto signature = SILFunctionType::get(genericSig,
2824-
SILFunctionType::ExtInfo::getThin(),
2825-
SILCoroutineKind::None,
2826-
ParameterConvention::Direct_Unowned,
2827-
params, {}, {}, None,
2828-
SubstitutionMap(), false,
2829-
SGM.getASTContext());
2809+
loweredBaseTy = SGM.Types.getLoweredRValueType(
2810+
TypeExpansionContext::minimal(), opaque, baseType);
2811+
loweredPropTy = SGM.Types.getLoweredRValueType(
2812+
TypeExpansionContext::minimal(), opaque, propertyType);
2813+
}
2814+
2815+
auto &C = SGM.getASTContext();
2816+
2817+
auto paramConvention = ParameterConvention::Indirect_In_Guaranteed;
2818+
2819+
SmallVector<SILParameterInfo, 3> params;
2820+
// property value
2821+
params.push_back({loweredPropTy, paramConvention});
2822+
// base
2823+
params.push_back({loweredBaseTy,
2824+
property->isSetterMutating()
2825+
? ParameterConvention::Indirect_Inout
2826+
: paramConvention});
2827+
// indexes
2828+
if (!indexes.empty())
2829+
params.push_back({C.getUnsafeRawPointerDecl()->getDeclaredType()
2830+
->getCanonicalType(),
2831+
ParameterConvention::Direct_Unowned});
2832+
2833+
return SILFunctionType::get(genericSig,
2834+
SILFunctionType::ExtInfo::getThin(),
2835+
SILCoroutineKind::None,
2836+
ParameterConvention::Direct_Unowned,
2837+
params, {}, {}, None,
2838+
SubstitutionMap(), false,
2839+
SGM.getASTContext());
2840+
}();
28302841

28312842
// Mangle the name of the thunk to see if we already created it.
28322843
auto name = Mangle::ASTMangler()
@@ -2853,9 +2864,15 @@ static SILFunction *getOrCreateKeyPathSetter(SILGenModule &SGM,
28532864
}
28542865

28552866
SILGenFunction subSGF(SGM, *thunk, SGM.SwiftModule);
2867+
2868+
signature = subSGF.F.getLoweredFunctionTypeInContext(
2869+
subSGF.F.getTypeExpansionContext());
2870+
28562871
auto entry = thunk->begin();
2857-
auto valueArgTy = params[0].getSILStorageType(SGM.M, signature);
2858-
auto baseArgTy = params[1].getSILStorageType(SGM.M, signature);
2872+
auto valueArgTy = signature->getParameters()[0].getSILStorageType(
2873+
SGM.M, signature);
2874+
auto baseArgTy = signature->getParameters()[1].getSILStorageType(
2875+
SGM.M, signature);
28592876
if (genericEnv) {
28602877
valueArgTy = genericEnv->mapTypeIntoContext(SGM.M, valueArgTy);
28612878
baseArgTy = genericEnv->mapTypeIntoContext(SGM.M, baseArgTy);
@@ -2865,7 +2882,8 @@ static SILFunction *getOrCreateKeyPathSetter(SILGenModule &SGM,
28652882
SILValue indexPtrArg;
28662883

28672884
if (!indexes.empty()) {
2868-
auto indexArgTy = params[2].getSILStorageType(SGM.M, signature);
2885+
auto indexArgTy = signature->getParameters()[2].getSILStorageType(
2886+
SGM.M, signature);
28692887
indexPtrArg = entry->createFunctionArgument(indexArgTy);
28702888
}
28712889

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// RUN: %target-swift-emit-silgen %s -enable-library-evolution -disable-availability-checking | %FileCheck %s
2+
3+
public protocol V {}
4+
public struct E : V {}
5+
6+
public class HasProperty {
7+
public var foo: some V = E()
8+
}
9+
10+
// CHECK-LABEL: sil shared [thunk] [ossa] @$s33opaque_result_type_class_property11HasPropertyC3fooQrvpACTK : $@convention(thin) (@in_guaranteed HasProperty) -> @out @_opaqueReturnTypeOf("$s33opaque_result_type_class_property11HasPropertyC3fooQrvp", 0) 🦸 {
11+
// CHECK: bb0(%0 : $*E, %1 : $*HasProperty):
12+
13+
// CHECK-LABEL: sil shared [thunk] [ossa] @$s33opaque_result_type_class_property11HasPropertyC3fooQrvpACTk : $@convention(thin) (@in_guaranteed @_opaqueReturnTypeOf("$s33opaque_result_type_class_property11HasPropertyC3fooQrvp", 0) 🦸, @in_guaranteed HasProperty) -> () {
14+
// CHECK: bb0(%0 : $*E, %1 : $*HasProperty):

0 commit comments

Comments
 (0)