Skip to content

Commit e48f483

Browse files
committed
TypeResolution: Replace more 'forContextual' uses with 'resolveContextualType'
1 parent 74ab73a commit e48f483

File tree

6 files changed

+63
-75
lines changed

6 files changed

+63
-75
lines changed

lib/Sema/CSGen.cpp

+5-8
Original file line numberDiff line numberDiff line change
@@ -1321,10 +1321,8 @@ namespace {
13211321
// Introduce type variables for unbound generics.
13221322
const auto genericOpener = OpenUnboundGenericType(CS, locator);
13231323
const auto placeholderHandler = HandlePlaceholderType(CS, locator);
1324-
const auto result = TypeResolution::forContextual(CS.DC, resCtx,
1325-
genericOpener,
1326-
placeholderHandler)
1327-
.resolveType(repr);
1324+
const auto result = TypeResolution::resolveContextualType(
1325+
repr, CS.DC, resCtx, genericOpener, placeholderHandler);
13281326
if (result->hasError()) {
13291327
return Type();
13301328
}
@@ -1597,13 +1595,12 @@ namespace {
15971595
auto *const locator = CS.getConstraintLocator(expr);
15981596
const auto options =
15991597
TypeResolutionOptions(TypeResolverContext::InExpression);
1600-
for (size_t i = 0, size = specializations.size(); i < size; ++i) {
1601-
const auto resolution = TypeResolution::forContextual(
1602-
CS.DC, options,
1598+
for (size_t i = 0, e = specializations.size(); i < e; ++i) {
1599+
const auto result = TypeResolution::resolveContextualType(
1600+
specializations[i], CS.DC, options,
16031601
// Introduce type variables for unbound generics.
16041602
OpenUnboundGenericType(CS, locator),
16051603
HandlePlaceholderType(CS, locator));
1606-
const auto result = resolution.resolveType(specializations[i]);
16071604
if (result->hasError())
16081605
return Type();
16091606

lib/Sema/CSSolver.cpp

+8-10
Original file line numberDiff line numberDiff line change
@@ -1098,16 +1098,14 @@ void ConstraintSystem::shrink(Expr *expr) {
10981098
auto *const typeRepr = coerceExpr->getCastTypeRepr();
10991099

11001100
if (typeRepr && isSuitableCollection(typeRepr)) {
1101-
const auto coercionType =
1102-
TypeResolution::forContextual(
1103-
CS.DC, None,
1104-
// FIXME: Should we really be unconditionally complaining
1105-
// about unbound generics and placeholders here? For
1106-
// example:
1107-
// let foo: [Array<Float>] = [[0], [1], [2]] as [Array]
1108-
// let foo: [Array<Float>] = [[0], [1], [2]] as [Array<_>]
1109-
/*unboundTyOpener*/ nullptr, /*placeholderHandler*/ nullptr)
1110-
.resolveType(typeRepr);
1101+
const auto coercionType = TypeResolution::resolveContextualType(
1102+
typeRepr, CS.DC, None,
1103+
// FIXME: Should we really be unconditionally complaining
1104+
// about unbound generics and placeholders here? For
1105+
// example:
1106+
// let foo: [Array<Float>] = [[0], [1], [2]] as [Array]
1107+
// let foo: [Array<Float>] = [[0], [1], [2]] as [Array<_>]
1108+
/*unboundTyOpener*/ nullptr, /*placeholderHandler*/ nullptr);
11111109

11121110
// Looks like coercion type is invalid, let's skip this sub-tree.
11131111
if (coercionType->hasError())

lib/Sema/ConstraintSystem.cpp

+8-8
Original file line numberDiff line numberDiff line change
@@ -1349,11 +1349,12 @@ ConstraintSystem::getTypeOfReference(ValueDecl *value,
13491349
if (auto typeDecl = dyn_cast<TypeDecl>(value)) {
13501350
// Resolve the reference to this type declaration in our current context.
13511351
auto type =
1352-
TypeResolution::forContextual(useDC, TypeResolverContext::InExpression,
1353-
/*unboundTyOpener*/ nullptr,
1354-
/*placeholderHandler*/ nullptr)
1352+
TypeResolution::forInterface(useDC, TypeResolverContext::InExpression,
1353+
/*unboundTyOpener*/ nullptr,
1354+
/*placeholderHandler*/ nullptr)
13551355
.resolveTypeInContext(typeDecl, /*foundDC*/ nullptr,
13561356
/*isSpecialized=*/false);
1357+
type = useDC->mapTypeIntoContext(type);
13571358

13581359
checkNestedTypeConstraints(*this, type, locator);
13591360

@@ -2393,11 +2394,10 @@ FunctionType::ExtInfo ConstraintSystem::closureEffects(ClosureExpr *expr) {
23932394
while (auto isp = dyn_cast<IsPattern>(pattern)) {
23942395
Type castType;
23952396
if (auto castTypeRepr = isp->getCastTypeRepr()) {
2396-
castType = TypeResolution::forContextual(
2397-
DC, TypeResolverContext::InExpression,
2398-
/*unboundTyOpener*/ nullptr,
2399-
/*placeholderHandler*/ nullptr)
2400-
.resolveType(castTypeRepr);
2397+
castType = TypeResolution::resolveContextualType(
2398+
castTypeRepr, DC, TypeResolverContext::InExpression,
2399+
/*unboundTyOpener*/ nullptr,
2400+
/*placeholderHandler*/ nullptr);
24012401
} else {
24022402
castType = isp->getCastType();
24032403
}

lib/Sema/PreCheckExpr.cpp

+6-10
Original file line numberDiff line numberDiff line change
@@ -599,7 +599,6 @@ Expr *TypeChecker::resolveDeclRefExpr(UnresolvedDeclRefExpr *UDRE,
599599

600600
if (typeContext->getSelfClassDecl())
601601
SelfType = DynamicSelfType::get(SelfType, Context);
602-
SelfType = DC->mapTypeIntoContext(SelfType);
603602
return new (Context)
604603
TypeExpr(new (Context) FixedTypeRepr(SelfType, Loc));
605604
}
@@ -1505,10 +1504,8 @@ TypeExpr *PreCheckExpression::simplifyNestedTypeExpr(UnresolvedDotExpr *UDE) {
15051504
// Fold 'T.U' into a nested type.
15061505
if (auto *ITR = dyn_cast<IdentTypeRepr>(InnerTypeRepr)) {
15071506
// Resolve the TypeRepr to get the base type for the lookup.
1508-
const auto options =
1509-
TypeResolutionOptions(TypeResolverContext::InExpression);
1510-
const auto resolution = TypeResolution::forContextual(
1511-
DC, options,
1507+
const auto BaseTy = TypeResolution::resolveContextualType(
1508+
InnerTypeRepr, DC, TypeResolverContext::InExpression,
15121509
[](auto unboundTy) {
15131510
// FIXME: Don't let unbound generic types escape type resolution.
15141511
// For now, just return the unbound generic type.
@@ -1517,7 +1514,6 @@ TypeExpr *PreCheckExpression::simplifyNestedTypeExpr(UnresolvedDotExpr *UDE) {
15171514
// FIXME: Don't let placeholder types escape type resolution.
15181515
// For now, just return the placeholder type.
15191516
PlaceholderType::get);
1520-
const auto BaseTy = resolution.resolveType(InnerTypeRepr);
15211517

15221518
if (BaseTy->mayHaveMembers()) {
15231519
// See if there is a member type with this name.
@@ -2060,8 +2056,8 @@ Expr *PreCheckExpression::simplifyTypeConstructionWithLiteralArg(Expr *E) {
20602056
TypeResolutionOptions(TypeResolverContext::InExpression) |
20612057
TypeResolutionFlags::SilenceErrors;
20622058

2063-
const auto resolution = TypeResolution::forContextual(
2064-
DC, options,
2059+
const auto result = TypeResolution::resolveContextualType(
2060+
typeExpr->getTypeRepr(), DC, options,
20652061
[](auto unboundTy) {
20662062
// FIXME: Don't let unbound generic types escape type resolution.
20672063
// For now, just return the unbound generic type.
@@ -2070,13 +2066,13 @@ Expr *PreCheckExpression::simplifyTypeConstructionWithLiteralArg(Expr *E) {
20702066
// FIXME: Don't let placeholder types escape type resolution.
20712067
// For now, just return the placeholder type.
20722068
PlaceholderType::get);
2073-
const auto result = resolution.resolveType(typeExpr->getTypeRepr());
2069+
20742070
if (result->hasError())
20752071
return nullptr;
20762072
castTy = result;
20772073
}
20782074

2079-
if (!castTy || !castTy->getAnyNominal())
2075+
if (!castTy->getAnyNominal())
20802076
return nullptr;
20812077

20822078
// Don't bother to convert deprecated selector syntax.

lib/Sema/TypeCheckAttr.cpp

+18-19
Original file line numberDiff line numberDiff line change
@@ -2756,13 +2756,13 @@ ResolveTypeEraserTypeRequest::evaluate(Evaluator &evaluator,
27562756
ProtocolDecl *PD,
27572757
TypeEraserAttr *attr) const {
27582758
if (auto *typeEraserRepr = attr->getParsedTypeEraserTypeRepr()) {
2759-
return TypeResolution::forContextual(PD, None,
2760-
// Unbound generics and placeholders
2761-
// are not allowed within this
2762-
// attribute.
2763-
/*unboundTyOpener*/ nullptr,
2764-
/*placeholderHandler*/ nullptr)
2765-
.resolveType(typeEraserRepr);
2759+
return TypeResolution::resolveContextualType(
2760+
typeEraserRepr, PD, None,
2761+
// Unbound generics and placeholders
2762+
// are not allowed within this
2763+
// attribute.
2764+
/*unboundTyOpener*/ nullptr,
2765+
/*placeholderHandler*/ nullptr);
27662766
} else {
27672767
auto *LazyResolver = attr->Resolver;
27682768
assert(LazyResolver && "type eraser was neither parsed nor deserialized?");
@@ -2939,9 +2939,9 @@ void AttributeChecker::visitImplementsAttr(ImplementsAttr *attr) {
29392939

29402940
Type T = attr->getProtocolType();
29412941
if (!T && attr->getProtocolTypeRepr()) {
2942-
T = TypeResolution::forContextual(DC, None, /*unboundTyOpener*/ nullptr,
2943-
/*placeholderHandler*/ nullptr)
2944-
.resolveType(attr->getProtocolTypeRepr());
2942+
T = TypeResolution::resolveContextualType(attr->getProtocolTypeRepr(), DC,
2943+
None, /*unboundTyOpener*/ nullptr,
2944+
/*placeholderHandler*/ nullptr);
29452945
}
29462946

29472947
// Definite error-types were already diagnosed in resolveType.
@@ -4741,11 +4741,10 @@ static bool typeCheckDerivativeAttr(ASTContext &Ctx, Decl *D,
47414741
if (auto *baseTypeRepr = attr->getBaseTypeRepr()) {
47424742
const auto options =
47434743
TypeResolutionOptions(None) | TypeResolutionFlags::AllowModule;
4744-
baseType =
4745-
TypeResolution::forContextual(derivative->getDeclContext(), options,
4746-
/*unboundTyOpener*/ nullptr,
4747-
/*placeholderHandler*/ nullptr)
4748-
.resolveType(baseTypeRepr);
4744+
baseType = TypeResolution::resolveContextualType(
4745+
baseTypeRepr, derivative->getDeclContext(), options,
4746+
/*unboundTyOpener*/ nullptr,
4747+
/*placeholderHandler*/ nullptr);
47494748
}
47504749
if (baseType && baseType->hasError())
47514750
return true;
@@ -5334,10 +5333,10 @@ void AttributeChecker::visitTransposeAttr(TransposeAttr *attr) {
53345333

53355334
Type baseType;
53365335
if (attr->getBaseTypeRepr()) {
5337-
baseType = TypeResolution::forContextual(transpose->getDeclContext(), None,
5338-
/*unboundTyOpener*/ nullptr,
5339-
/*placeholderHandler*/ nullptr)
5340-
.resolveType(attr->getBaseTypeRepr());
5336+
baseType = TypeResolution::resolveContextualType(
5337+
attr->getBaseTypeRepr(), transpose->getDeclContext(), None,
5338+
/*unboundTyOpener*/ nullptr,
5339+
/*placeholderHandler*/ nullptr);
53415340
}
53425341
auto lookupOptions =
53435342
(attr->getBaseTypeRepr() ? defaultMemberLookupOptions

lib/Sema/TypeCheckPattern.cpp

+18-20
Original file line numberDiff line numberDiff line change
@@ -606,18 +606,17 @@ class ResolvePattern : public ASTVisitor<ResolvePattern,
606606
auto *prefixRepr = IdentTypeRepr::create(Context, components);
607607

608608
// See first if the entire repr resolves to a type.
609-
const Type enumTy =
610-
TypeResolution::forContextual(
611-
DC, options,
612-
[](auto unboundTy) {
613-
// FIXME: Don't let unbound generic types escape type
614-
// resolution. For now, just return the unbound generic type.
615-
return unboundTy;
616-
},
617-
// FIXME: Don't let placeholder types escape type resolution.
618-
// For now, just return the placeholder type.
619-
PlaceholderType::get)
620-
.resolveType(prefixRepr);
609+
const Type enumTy = TypeResolution::resolveContextualType(
610+
prefixRepr, DC, options,
611+
[](auto unboundTy) {
612+
// FIXME: Don't let unbound generic types escape type
613+
// resolution. For now, just return the unbound generic type.
614+
return unboundTy;
615+
},
616+
// FIXME: Don't let placeholder types escape type resolution.
617+
// For now, just return the placeholder type.
618+
PlaceholderType::get);
619+
621620
auto *enumDecl = dyn_cast_or_null<EnumDecl>(enumTy->getAnyNominal());
622621
if (!enumDecl)
623622
return nullptr;
@@ -1307,14 +1306,13 @@ Pattern *TypeChecker::coercePatternToType(ContextualPattern pattern,
13071306
auto IP = cast<IsPattern>(P);
13081307

13091308
// Type-check the type parameter.
1310-
const auto castType =
1311-
TypeResolution::forContextual(dc, TypeResolverContext::InExpression,
1312-
// FIXME: Should we really unconditionally
1313-
// complain about unbound generics and
1314-
// placeholders here?
1315-
/*unboundTyOpener*/ nullptr,
1316-
/*placeholderHandler*/ nullptr)
1317-
.resolveType(IP->getCastTypeRepr());
1309+
const auto castType = TypeResolution::resolveContextualType(
1310+
IP->getCastTypeRepr(), dc, TypeResolverContext::InExpression,
1311+
// FIXME: Should we really unconditionally
1312+
// complain about unbound generics and
1313+
// placeholders here?
1314+
/*unboundTyOpener*/ nullptr,
1315+
/*placeholderHandler*/ nullptr);
13181316
if (castType->hasError())
13191317
return nullptr;
13201318
IP->setCastType(castType);

0 commit comments

Comments
 (0)