@@ -1916,6 +1916,28 @@ namespace {
1916
1916
};
1917
1917
} // end anonymous namespace
1918
1918
1919
+ Type TypeResolution::resolveContextualType (
1920
+ TypeRepr *TyR, DeclContext *dc, TypeResolutionOptions opts,
1921
+ OpenUnboundGenericTypeFn unboundTyOpener,
1922
+ HandlePlaceholderTypeReprFn placeholderHandler,
1923
+ GenericParamList *silParams) {
1924
+ return resolveContextualType (TyR, dc, dc->getGenericEnvironmentOfContext (),
1925
+ opts, unboundTyOpener, placeholderHandler);
1926
+ }
1927
+
1928
+ Type TypeResolution::resolveContextualType (
1929
+ TypeRepr *TyR, DeclContext *dc, GenericEnvironment *genericEnv,
1930
+ TypeResolutionOptions opts, OpenUnboundGenericTypeFn unboundTyOpener,
1931
+ HandlePlaceholderTypeReprFn placeholderHandler,
1932
+ GenericParamList *silParams) {
1933
+ const auto resolution = TypeResolution::forInterface (
1934
+ dc, genericEnv, opts, unboundTyOpener, placeholderHandler);
1935
+ const auto ty = resolution.resolveType (TyR, silParams);
1936
+
1937
+ return GenericEnvironment::mapTypeIntoContext (
1938
+ resolution.getGenericSignature ().getGenericEnvironment (), ty);
1939
+ }
1940
+
1919
1941
Type TypeResolution::resolveType (TypeRepr *TyR,
1920
1942
GenericParamList *silParams) const {
1921
1943
auto &ctx = getASTContext ();
@@ -2936,10 +2958,10 @@ NeverNullType TypeResolver::resolveSILBoxType(SILBoxTypeRepr *repr,
2936
2958
auto *genericParams = repr->getGenericParams ();
2937
2959
2938
2960
if (genericParams) {
2939
- fieldResolution = TypeResolution::forContextual (
2940
- getDeclContext (), genericEnv, options,
2941
- resolution.getUnboundTypeOpener (),
2942
- resolution.getPlaceholderHandler ());
2961
+ fieldResolution =
2962
+ TypeResolution::forInterface ( getDeclContext (), genericEnv, options,
2963
+ resolution.getUnboundTypeOpener (),
2964
+ resolution.getPlaceholderHandler ());
2943
2965
}
2944
2966
2945
2967
TypeResolver fieldResolver{fieldResolution,
@@ -2954,11 +2976,6 @@ NeverNullType TypeResolver::resolveSILBoxType(SILBoxTypeRepr *repr,
2954
2976
CanGenericSignature genericSig;
2955
2977
if (auto *genericEnv = repr->getGenericEnvironment ()) {
2956
2978
genericSig = genericEnv->getGenericSignature ().getCanonicalSignature ();
2957
-
2958
- for (auto &field : fields) {
2959
- auto transTy = field.getLoweredType ()->mapTypeOutOfContext ();
2960
- field = {transTy->getCanonicalType (), field.isMutable ()};
2961
- }
2962
2979
}
2963
2980
2964
2981
// Resolve the generic arguments.
@@ -3020,14 +3037,6 @@ NeverNullType TypeResolver::resolveSILFunctionType(
3020
3037
: genericEnv;
3021
3038
3022
3039
{
3023
- TypeResolution functionResolution{resolution};
3024
- if (componentTypeEnv) {
3025
- functionResolution = TypeResolution::forContextual (
3026
- getDeclContext (), componentTypeEnv, options,
3027
- resolution.getUnboundTypeOpener (),
3028
- resolution.getPlaceholderHandler ());
3029
- }
3030
-
3031
3040
auto argsTuple = repr->getArgsTypeRepr ();
3032
3041
// SIL functions cannot be variadic.
3033
3042
if (argsTuple->hasEllipsis ()) {
@@ -3039,6 +3048,14 @@ NeverNullType TypeResolver::resolveSILFunctionType(
3039
3048
diagnose (element.UnderscoreLoc , diag::sil_function_input_label);
3040
3049
}
3041
3050
3051
+ TypeResolution functionResolution{resolution};
3052
+ if (componentTypeEnv) {
3053
+ functionResolution = TypeResolution::forInterface (
3054
+ getDeclContext (), componentTypeEnv, options,
3055
+ resolution.getUnboundTypeOpener (),
3056
+ resolution.getPlaceholderHandler ());
3057
+ }
3058
+
3042
3059
TypeResolver silResolver{functionResolution, componentGenericParams};
3043
3060
for (auto elt : argsTuple->getElements ()) {
3044
3061
auto elementOptions = options;
@@ -3088,9 +3105,9 @@ NeverNullType TypeResolver::resolveSILFunctionType(
3088
3105
if (!repr->getPatternSubstitutions ().empty ()) {
3089
3106
if (genericEnv) {
3090
3107
auto resolveSILParameters =
3091
- TypeResolution::forContextual (getDeclContext (), genericEnv, options,
3092
- resolution.getUnboundTypeOpener (),
3093
- resolution.getPlaceholderHandler ());
3108
+ TypeResolution::forInterface (getDeclContext (), genericEnv, options,
3109
+ resolution.getUnboundTypeOpener (),
3110
+ resolution.getPlaceholderHandler ());
3094
3111
patternSubs = resolveSubstitutions (repr->getPatternGenericEnvironment (),
3095
3112
repr->getPatternSubstitutions (),
3096
3113
TypeResolver{resolveSILParameters,
@@ -3116,53 +3133,6 @@ NeverNullType TypeResolver::resolveSILFunctionType(
3116
3133
return ErrorType::get (getASTContext ());
3117
3134
}
3118
3135
3119
- CanGenericSignature genericSig =
3120
- genericEnv ? genericEnv->getGenericSignature ().getCanonicalSignature ()
3121
- : CanGenericSignature ();
3122
-
3123
-
3124
- // FIXME: Remap the parsed context types to interface types.
3125
- SmallVector<SILParameterInfo, 4 > interfaceParams;
3126
- SmallVector<SILYieldInfo, 4 > interfaceYields;
3127
- SmallVector<SILResultInfo, 4 > interfaceResults;
3128
- Optional<SILResultInfo> interfaceErrorResult;
3129
- if (componentTypeEnv) {
3130
- for (auto ¶m : params) {
3131
- auto transParamType = param.getInterfaceType ()->mapTypeOutOfContext ()
3132
- ->getCanonicalType ();
3133
- interfaceParams.push_back (param.getWithInterfaceType (transParamType));
3134
- }
3135
- for (auto &yield : yields) {
3136
- auto transYieldType = yield.getInterfaceType ()->mapTypeOutOfContext ()
3137
- ->getCanonicalType ();
3138
- interfaceYields.push_back (yield.getWithInterfaceType (transYieldType));
3139
- }
3140
- for (auto &result : results) {
3141
- auto transResultType = result.getInterfaceType ()->mapTypeOutOfContext ()
3142
- ->getCanonicalType ();
3143
- interfaceResults.push_back (result.getWithInterfaceType (transResultType));
3144
- }
3145
-
3146
- if (errorResult) {
3147
- auto transErrorResultType = errorResult->getInterfaceType ()
3148
- ->mapTypeOutOfContext ()
3149
- ->getCanonicalType ();
3150
- interfaceErrorResult =
3151
- errorResult->getWithInterfaceType (transErrorResultType);
3152
- }
3153
- } else {
3154
- interfaceParams = params;
3155
- interfaceYields = yields;
3156
- interfaceResults = results;
3157
- interfaceErrorResult = errorResult;
3158
- }
3159
-
3160
- SubstitutionMap interfacePatternSubs = patternSubs;
3161
- if (interfacePatternSubs && repr->getGenericEnvironment ()) {
3162
- interfacePatternSubs =
3163
- interfacePatternSubs.mapReplacementTypesOutOfContext ();
3164
- }
3165
-
3166
3136
ProtocolConformanceRef witnessMethodConformance;
3167
3137
if (witnessMethodProtocol) {
3168
3138
auto resolved = resolveType (witnessMethodProtocol, options);
@@ -3179,7 +3149,11 @@ NeverNullType TypeResolver::resolveSILFunctionType(
3179
3149
if (invocationSubs) {
3180
3150
selfType = selfType.subst (invocationSubs);
3181
3151
}
3182
-
3152
+
3153
+ // Only once we've done all the necessary substitutions, map the type
3154
+ // into the function's environment.
3155
+ selfType = GenericEnvironment::mapTypeIntoContext (genericEnv, selfType);
3156
+
3183
3157
// The Self type can be nested in a few layers of metatypes (etc.).
3184
3158
while (auto metatypeType = selfType->getAs <MetatypeType>()) {
3185
3159
auto next = metatypeType->getInstanceType ();
@@ -3201,16 +3175,19 @@ NeverNullType TypeResolver::resolveSILFunctionType(
3201
3175
assert (results.size () <= 1 && yields.size () == 0 &&
3202
3176
" C functions and blocks have at most 1 result and 0 yields." );
3203
3177
auto result = results.empty () ? Optional<SILResultInfo>() : results[0 ];
3204
- clangFnType = getASTContext ().getCanonicalClangFunctionType (
3205
- interfaceParams, result, representation);
3178
+ clangFnType = getASTContext ().getCanonicalClangFunctionType (params, result,
3179
+ representation);
3206
3180
extInfoBuilder = extInfoBuilder.withClangFunctionType (clangFnType);
3207
3181
}
3208
3182
3183
+ CanGenericSignature genericSig =
3184
+ genericEnv ? genericEnv->getGenericSignature ().getCanonicalSignature ()
3185
+ : CanGenericSignature ();
3186
+
3209
3187
return SILFunctionType::get (genericSig, extInfoBuilder.build (), coroutineKind,
3210
- callee, interfaceParams, interfaceYields,
3211
- interfaceResults, interfaceErrorResult,
3212
- interfacePatternSubs, invocationSubs,
3213
- getASTContext (), witnessMethodConformance);
3188
+ callee, params, yields, results, errorResult,
3189
+ patternSubs, invocationSubs, getASTContext (),
3190
+ witnessMethodConformance);
3214
3191
}
3215
3192
3216
3193
SILYieldInfo TypeResolver::resolveSILYield (TypeAttributes &attrs,
@@ -3955,9 +3932,9 @@ Type CustomAttrTypeRequest::evaluate(Evaluator &eval, CustomAttr *attr,
3955
3932
};
3956
3933
}
3957
3934
3958
- const auto type = TypeResolution::forContextual (dc, options, unboundTyOpener,
3959
- /* placeholderHandler */ nullptr )
3960
- . resolveType (attr-> getTypeRepr () );
3935
+ const auto type = TypeResolution::resolveContextualType (
3936
+ attr-> getTypeRepr (), dc, options, unboundTyOpener,
3937
+ /* placeholderHandler */ nullptr );
3961
3938
3962
3939
// We always require the type to resolve to a nominal type. If the type was
3963
3940
// not a nominal type, we should have already diagnosed an error via
0 commit comments