Skip to content

Commit 9fa6d89

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 7a4b5cf commit 9fa6d89

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
@@ -2689,33 +2689,35 @@ static SILFunction *getOrCreateKeyPathGetter(SILGenModule &SGM,
26892689
}
26902690

26912691
// Build the signature of the thunk as expected by the keypath runtime.
2692-
CanType loweredBaseTy, loweredPropTy;
2693-
AbstractionPattern opaque = AbstractionPattern::getOpaque();
2692+
auto signature = [&]() {
2693+
CanType loweredBaseTy, loweredPropTy;
2694+
AbstractionPattern opaque = AbstractionPattern::getOpaque();
26942695

2695-
loweredBaseTy = SGM.Types.getLoweredRValueType(
2696-
TypeExpansionContext::minimal(), opaque, baseType);
2697-
loweredPropTy = SGM.Types.getLoweredRValueType(
2698-
TypeExpansionContext::minimal(), opaque, propertyType);
2699-
2700-
auto paramConvention = ParameterConvention::Indirect_In_Guaranteed;
2696+
loweredBaseTy = SGM.Types.getLoweredRValueType(
2697+
TypeExpansionContext::minimal(), opaque, baseType);
2698+
loweredPropTy = SGM.Types.getLoweredRValueType(
2699+
TypeExpansionContext::minimal(), opaque, propertyType);
2700+
2701+
auto paramConvention = ParameterConvention::Indirect_In_Guaranteed;
27012702

2702-
SmallVector<SILParameterInfo, 2> params;
2703-
params.push_back({loweredBaseTy, paramConvention});
2704-
auto &C = SGM.getASTContext();
2705-
if (!indexes.empty())
2706-
params.push_back({C.getUnsafeRawPointerDecl()->getDeclaredType()
2707-
->getCanonicalType(),
2708-
ParameterConvention::Direct_Unowned});
2709-
2710-
SILResultInfo result(loweredPropTy, ResultConvention::Indirect);
2711-
2712-
auto signature = SILFunctionType::get(genericSig,
2713-
SILFunctionType::ExtInfo::getThin(),
2714-
SILCoroutineKind::None,
2715-
ParameterConvention::Direct_Unowned,
2716-
params, {}, result, None,
2717-
SubstitutionMap(), false,
2718-
SGM.getASTContext());
2703+
SmallVector<SILParameterInfo, 2> params;
2704+
params.push_back({loweredBaseTy, paramConvention});
2705+
auto &C = SGM.getASTContext();
2706+
if (!indexes.empty())
2707+
params.push_back({C.getUnsafeRawPointerDecl()->getDeclaredType()
2708+
->getCanonicalType(),
2709+
ParameterConvention::Direct_Unowned});
2710+
2711+
SILResultInfo result(loweredPropTy, ResultConvention::Indirect);
2712+
2713+
return SILFunctionType::get(genericSig,
2714+
SILFunctionType::ExtInfo::getThin(),
2715+
SILCoroutineKind::None,
2716+
ParameterConvention::Direct_Unowned,
2717+
params, {}, result, None,
2718+
SubstitutionMap(), false,
2719+
SGM.getASTContext());
2720+
}();
27192721

27202722
// Find the function and see if we already created it.
27212723
auto name = Mangle::ASTMangler()
@@ -2741,9 +2743,15 @@ static SILFunction *getOrCreateKeyPathGetter(SILGenModule &SGM,
27412743
}
27422744

27432745
SILGenFunction subSGF(SGM, *thunk, SGM.SwiftModule);
2746+
2747+
signature = subSGF.F.getLoweredFunctionTypeInContext(
2748+
subSGF.F.getTypeExpansionContext());
2749+
27442750
auto entry = thunk->begin();
2745-
auto resultArgTy = result.getSILStorageType(SGM.M, signature);
2746-
auto baseArgTy = params[0].getSILStorageType(SGM.M, signature);
2751+
auto resultArgTy = signature->getSingleResult().getSILStorageType(
2752+
SGM.M, signature);
2753+
auto baseArgTy = signature->getParameters()[0].getSILStorageType(
2754+
SGM.M, signature);
27472755
if (genericEnv) {
27482756
resultArgTy = genericEnv->mapTypeIntoContext(SGM.M, resultArgTy);
27492757
baseArgTy = genericEnv->mapTypeIntoContext(SGM.M, baseArgTy);
@@ -2752,7 +2760,8 @@ static SILFunction *getOrCreateKeyPathGetter(SILGenModule &SGM,
27522760
auto baseArg = entry->createFunctionArgument(baseArgTy);
27532761
SILValue indexPtrArg;
27542762
if (!indexes.empty()) {
2755-
auto indexArgTy = params[1].getSILStorageType(SGM.M, signature);
2763+
auto indexArgTy = signature->getParameters()[1].getSILStorageType(
2764+
SGM.M, signature);
27562765
indexPtrArg = entry->createFunctionArgument(indexArgTy);
27572766
}
27582767

@@ -2821,41 +2830,43 @@ static SILFunction *getOrCreateKeyPathSetter(SILGenModule &SGM,
28212830
}
28222831

28232832
// Build the signature of the thunk as expected by the keypath runtime.
2824-
CanType loweredBaseTy, loweredPropTy;
2825-
{
2826-
AbstractionPattern opaque = AbstractionPattern::getOpaque();
2833+
auto signature = [&]() {
2834+
CanType loweredBaseTy, loweredPropTy;
2835+
{
2836+
AbstractionPattern opaque = AbstractionPattern::getOpaque();
28272837

2828-
loweredBaseTy = SGM.Types.getLoweredRValueType(
2829-
TypeExpansionContext::minimal(), opaque, baseType);
2830-
loweredPropTy = SGM.Types.getLoweredRValueType(
2831-
TypeExpansionContext::minimal(), opaque, propertyType);
2832-
}
2833-
2834-
auto &C = SGM.getASTContext();
2835-
2836-
auto paramConvention = ParameterConvention::Indirect_In_Guaranteed;
2837-
2838-
SmallVector<SILParameterInfo, 3> params;
2839-
// property value
2840-
params.push_back({loweredPropTy, paramConvention});
2841-
// base
2842-
params.push_back({loweredBaseTy,
2843-
property->isSetterMutating()
2844-
? ParameterConvention::Indirect_Inout
2845-
: paramConvention});
2846-
// indexes
2847-
if (!indexes.empty())
2848-
params.push_back({C.getUnsafeRawPointerDecl()->getDeclaredType()
2849-
->getCanonicalType(),
2850-
ParameterConvention::Direct_Unowned});
2851-
2852-
auto signature = SILFunctionType::get(genericSig,
2853-
SILFunctionType::ExtInfo::getThin(),
2854-
SILCoroutineKind::None,
2855-
ParameterConvention::Direct_Unowned,
2856-
params, {}, {}, None,
2857-
SubstitutionMap(), false,
2858-
SGM.getASTContext());
2838+
loweredBaseTy = SGM.Types.getLoweredRValueType(
2839+
TypeExpansionContext::minimal(), opaque, baseType);
2840+
loweredPropTy = SGM.Types.getLoweredRValueType(
2841+
TypeExpansionContext::minimal(), opaque, propertyType);
2842+
}
2843+
2844+
auto &C = SGM.getASTContext();
2845+
2846+
auto paramConvention = ParameterConvention::Indirect_In_Guaranteed;
2847+
2848+
SmallVector<SILParameterInfo, 3> params;
2849+
// property value
2850+
params.push_back({loweredPropTy, paramConvention});
2851+
// base
2852+
params.push_back({loweredBaseTy,
2853+
property->isSetterMutating()
2854+
? ParameterConvention::Indirect_Inout
2855+
: paramConvention});
2856+
// indexes
2857+
if (!indexes.empty())
2858+
params.push_back({C.getUnsafeRawPointerDecl()->getDeclaredType()
2859+
->getCanonicalType(),
2860+
ParameterConvention::Direct_Unowned});
2861+
2862+
return SILFunctionType::get(genericSig,
2863+
SILFunctionType::ExtInfo::getThin(),
2864+
SILCoroutineKind::None,
2865+
ParameterConvention::Direct_Unowned,
2866+
params, {}, {}, None,
2867+
SubstitutionMap(), false,
2868+
SGM.getASTContext());
2869+
}();
28592870

28602871
// Mangle the name of the thunk to see if we already created it.
28612872
auto name = Mangle::ASTMangler()
@@ -2882,9 +2893,15 @@ static SILFunction *getOrCreateKeyPathSetter(SILGenModule &SGM,
28822893
}
28832894

28842895
SILGenFunction subSGF(SGM, *thunk, SGM.SwiftModule);
2896+
2897+
signature = subSGF.F.getLoweredFunctionTypeInContext(
2898+
subSGF.F.getTypeExpansionContext());
2899+
28852900
auto entry = thunk->begin();
2886-
auto valueArgTy = params[0].getSILStorageType(SGM.M, signature);
2887-
auto baseArgTy = params[1].getSILStorageType(SGM.M, signature);
2901+
auto valueArgTy = signature->getParameters()[0].getSILStorageType(
2902+
SGM.M, signature);
2903+
auto baseArgTy = signature->getParameters()[1].getSILStorageType(
2904+
SGM.M, signature);
28882905
if (genericEnv) {
28892906
valueArgTy = genericEnv->mapTypeIntoContext(SGM.M, valueArgTy);
28902907
baseArgTy = genericEnv->mapTypeIntoContext(SGM.M, baseArgTy);
@@ -2894,7 +2911,8 @@ static SILFunction *getOrCreateKeyPathSetter(SILGenModule &SGM,
28942911
SILValue indexPtrArg;
28952912

28962913
if (!indexes.empty()) {
2897-
auto indexArgTy = params[2].getSILStorageType(SGM.M, signature);
2914+
auto indexArgTy = signature->getParameters()[2].getSILStorageType(
2915+
SGM.M, signature);
28982916
indexPtrArg = entry->createFunctionArgument(indexArgTy);
28992917
}
29002918

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)