Skip to content

Commit 74ab73a

Browse files
committed
TypeResolution: Start eating away at 'forContextual' usage
1 parent ce1afc3 commit 74ab73a

File tree

3 files changed

+73
-83
lines changed

3 files changed

+73
-83
lines changed

lib/Sema/TypeCheckPattern.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -485,8 +485,8 @@ class ResolvePattern : public ASTVisitor<ResolvePattern,
485485
auto *repr = IdentTypeRepr::create(Context, components);
486486

487487
// See if the repr resolves to a type.
488-
const auto resolution = TypeResolution::forContextual(
489-
DC, options,
488+
const auto ty = TypeResolution::resolveContextualType(
489+
repr, DC, options,
490490
[](auto unboundTy) {
491491
// FIXME: Don't let unbound generic types escape type resolution.
492492
// For now, just return the unbound generic type.
@@ -495,7 +495,7 @@ class ResolvePattern : public ASTVisitor<ResolvePattern,
495495
// FIXME: Don't let placeholder types escape type resolution.
496496
// For now, just return the placeholder type.
497497
PlaceholderType::get);
498-
const auto ty = resolution.resolveType(repr);
498+
499499
auto *enumDecl = dyn_cast_or_null<EnumDecl>(ty->getAnyNominal());
500500
if (!enumDecl)
501501
return nullptr;
@@ -751,9 +751,9 @@ validateTypedPattern(TypedPattern *TP, DeclContext *dc,
751751
return named->getDecl()->getDeclContext()->mapTypeIntoContext(opaqueTy);
752752
}
753753

754-
auto ty = TypeResolution::forContextual(dc, options, unboundTyOpener,
755-
placeholderHandler)
756-
.resolveType(Repr);
754+
const auto ty = TypeResolution::resolveContextualType(
755+
Repr, dc, options, unboundTyOpener, placeholderHandler);
756+
757757
if (ty->hasError()) {
758758
return ErrorType::get(Context);
759759
}

lib/Sema/TypeCheckType.cpp

Lines changed: 54 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1916,6 +1916,28 @@ namespace {
19161916
};
19171917
} // end anonymous namespace
19181918

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+
19191941
Type TypeResolution::resolveType(TypeRepr *TyR,
19201942
GenericParamList *silParams) const {
19211943
auto &ctx = getASTContext();
@@ -2936,10 +2958,10 @@ NeverNullType TypeResolver::resolveSILBoxType(SILBoxTypeRepr *repr,
29362958
auto *genericParams = repr->getGenericParams();
29372959

29382960
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());
29432965
}
29442966

29452967
TypeResolver fieldResolver{fieldResolution,
@@ -2954,11 +2976,6 @@ NeverNullType TypeResolver::resolveSILBoxType(SILBoxTypeRepr *repr,
29542976
CanGenericSignature genericSig;
29552977
if (auto *genericEnv = repr->getGenericEnvironment()) {
29562978
genericSig = genericEnv->getGenericSignature().getCanonicalSignature();
2957-
2958-
for (auto &field : fields) {
2959-
auto transTy = field.getLoweredType()->mapTypeOutOfContext();
2960-
field = {transTy->getCanonicalType(), field.isMutable()};
2961-
}
29622979
}
29632980

29642981
// Resolve the generic arguments.
@@ -3020,14 +3037,6 @@ NeverNullType TypeResolver::resolveSILFunctionType(
30203037
: genericEnv;
30213038

30223039
{
3023-
TypeResolution functionResolution{resolution};
3024-
if (componentTypeEnv) {
3025-
functionResolution = TypeResolution::forContextual(
3026-
getDeclContext(), componentTypeEnv, options,
3027-
resolution.getUnboundTypeOpener(),
3028-
resolution.getPlaceholderHandler());
3029-
}
3030-
30313040
auto argsTuple = repr->getArgsTypeRepr();
30323041
// SIL functions cannot be variadic.
30333042
if (argsTuple->hasEllipsis()) {
@@ -3039,6 +3048,14 @@ NeverNullType TypeResolver::resolveSILFunctionType(
30393048
diagnose(element.UnderscoreLoc, diag::sil_function_input_label);
30403049
}
30413050

3051+
TypeResolution functionResolution{resolution};
3052+
if (componentTypeEnv) {
3053+
functionResolution = TypeResolution::forInterface(
3054+
getDeclContext(), componentTypeEnv, options,
3055+
resolution.getUnboundTypeOpener(),
3056+
resolution.getPlaceholderHandler());
3057+
}
3058+
30423059
TypeResolver silResolver{functionResolution, componentGenericParams};
30433060
for (auto elt : argsTuple->getElements()) {
30443061
auto elementOptions = options;
@@ -3088,9 +3105,9 @@ NeverNullType TypeResolver::resolveSILFunctionType(
30883105
if (!repr->getPatternSubstitutions().empty()) {
30893106
if (genericEnv) {
30903107
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());
30943111
patternSubs = resolveSubstitutions(repr->getPatternGenericEnvironment(),
30953112
repr->getPatternSubstitutions(),
30963113
TypeResolver{resolveSILParameters,
@@ -3116,53 +3133,6 @@ NeverNullType TypeResolver::resolveSILFunctionType(
31163133
return ErrorType::get(getASTContext());
31173134
}
31183135

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 &param : 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-
31663136
ProtocolConformanceRef witnessMethodConformance;
31673137
if (witnessMethodProtocol) {
31683138
auto resolved = resolveType(witnessMethodProtocol, options);
@@ -3179,7 +3149,11 @@ NeverNullType TypeResolver::resolveSILFunctionType(
31793149
if (invocationSubs) {
31803150
selfType = selfType.subst(invocationSubs);
31813151
}
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+
31833157
// The Self type can be nested in a few layers of metatypes (etc.).
31843158
while (auto metatypeType = selfType->getAs<MetatypeType>()) {
31853159
auto next = metatypeType->getInstanceType();
@@ -3201,16 +3175,19 @@ NeverNullType TypeResolver::resolveSILFunctionType(
32013175
assert(results.size() <= 1 && yields.size() == 0 &&
32023176
"C functions and blocks have at most 1 result and 0 yields.");
32033177
auto result = results.empty() ? Optional<SILResultInfo>() : results[0];
3204-
clangFnType = getASTContext().getCanonicalClangFunctionType(
3205-
interfaceParams, result, representation);
3178+
clangFnType = getASTContext().getCanonicalClangFunctionType(params, result,
3179+
representation);
32063180
extInfoBuilder = extInfoBuilder.withClangFunctionType(clangFnType);
32073181
}
32083182

3183+
CanGenericSignature genericSig =
3184+
genericEnv ? genericEnv->getGenericSignature().getCanonicalSignature()
3185+
: CanGenericSignature();
3186+
32093187
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);
32143191
}
32153192

32163193
SILYieldInfo TypeResolver::resolveSILYield(TypeAttributes &attrs,
@@ -3955,9 +3932,9 @@ Type CustomAttrTypeRequest::evaluate(Evaluator &eval, CustomAttr *attr,
39553932
};
39563933
}
39573934

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);
39613938

39623939
// We always require the type to resolve to a nominal type. If the type was
39633940
// not a nominal type, we should have already diagnosed an error via

lib/Sema/TypeCheckType.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,19 @@ class TypeResolution {
353353
OpenUnboundGenericTypeFn unboundTyOpener,
354354
HandlePlaceholderTypeReprFn placeholderHandler);
355355

356+
static Type
357+
resolveContextualType(TypeRepr *TyR, DeclContext *dc,
358+
TypeResolutionOptions opts,
359+
OpenUnboundGenericTypeFn unboundTyOpener,
360+
HandlePlaceholderTypeReprFn placeholderHandler,
361+
GenericParamList *silParams = nullptr);
362+
363+
static Type resolveContextualType(
364+
TypeRepr *TyR, DeclContext *dc, GenericEnvironment *genericEnv,
365+
TypeResolutionOptions opts, OpenUnboundGenericTypeFn unboundTyOpener,
366+
HandlePlaceholderTypeReprFn placeholderHandler,
367+
GenericParamList *silParams = nullptr);
368+
356369
public:
357370
TypeResolution withOptions(TypeResolutionOptions opts) const;
358371

0 commit comments

Comments
 (0)