@@ -1445,12 +1445,19 @@ namespace {
1445
1445
diag::super_with_no_base_class);
1446
1446
}
1447
1447
1448
- Type resolveTypeReferenceInExpression (TypeRepr *repr,
1449
- TypeResolverContext resCtx) {
1450
- TypeResolutionOptions options (resCtx);
1451
- options |= TypeResolutionFlags::AllowUnboundGenerics;
1452
- auto result = TypeResolution::forContextual (CS.DC , options)
1453
- .resolveType (repr);
1448
+ Type
1449
+ resolveTypeReferenceInExpression (TypeRepr *repr, TypeResolverContext resCtx,
1450
+ OpenUnboundGenericTypeFn unboundTyOpener) {
1451
+ if (!unboundTyOpener) {
1452
+ unboundTyOpener = [](auto unboundTy) {
1453
+ // FIXME: Don't let unbound generic types escape type resolution.
1454
+ // For now, just return the unbound generic type.
1455
+ return unboundTy;
1456
+ };
1457
+ }
1458
+ const auto result =
1459
+ TypeResolution::forContextual (CS.DC , resCtx, unboundTyOpener)
1460
+ .resolveType (repr);
1454
1461
if (result->hasError ()) {
1455
1462
return Type ();
1456
1463
}
@@ -1460,20 +1467,21 @@ namespace {
1460
1467
Type visitTypeExpr (TypeExpr *E) {
1461
1468
Type type;
1462
1469
// If this is an implicit TypeExpr, don't validate its contents.
1470
+ auto *const locator = CS.getConstraintLocator (E);
1463
1471
if (E->isImplicit ()) {
1464
1472
type = CS.getInstanceType (CS.cacheType (E));
1465
1473
assert (type && " Implicit type expr must have type set!" );
1474
+ type = CS.openUnboundGenericTypes (type, locator);
1466
1475
} else {
1467
1476
auto *repr = E->getTypeRepr ();
1468
1477
assert (repr && " Explicit node has no type repr!" );
1469
1478
type = resolveTypeReferenceInExpression (
1470
- repr, TypeResolverContext::InExpression);
1479
+ repr, TypeResolverContext::InExpression,
1480
+ OpenUnboundGenericType (CS, locator));
1471
1481
}
1472
1482
1473
1483
if (!type || type->hasError ()) return Type ();
1474
-
1475
- auto locator = CS.getConstraintLocator (E);
1476
- type = CS.openUnboundGenericTypes (type, locator);
1484
+
1477
1485
return MetatypeType::get (type);
1478
1486
}
1479
1487
@@ -1711,12 +1719,15 @@ namespace {
1711
1719
1712
1720
// Bind the specified generic arguments to the type variables in the
1713
1721
// open type.
1714
- auto locator = CS.getConstraintLocator (expr);
1722
+ auto *const locator = CS.getConstraintLocator (expr);
1723
+ const auto options =
1724
+ TypeResolutionOptions (TypeResolverContext::InExpression);
1715
1725
for (size_t i = 0 , size = specializations.size (); i < size; ++i) {
1716
- TypeResolutionOptions options (TypeResolverContext::InExpression);
1717
- options |= TypeResolutionFlags::AllowUnboundGenerics;
1718
- auto result = TypeResolution::forContextual (CS.DC , options)
1719
- .resolveType (specializations[i]);
1726
+ const auto resolution = TypeResolution::forContextual (
1727
+ CS.DC , options,
1728
+ // Introduce type variables for unbound generics.
1729
+ OpenUnboundGenericType (CS, locator));
1730
+ const auto result = resolution.resolveType (specializations[i]);
1720
1731
if (result->hasError ())
1721
1732
return Type ();
1722
1733
@@ -2190,7 +2201,7 @@ namespace {
2190
2201
2191
2202
return resolveTypeReferenceInExpression (
2192
2203
closure->getExplicitResultTypeRepr (),
2193
- TypeResolverContext::InExpression);
2204
+ TypeResolverContext::InExpression, nullptr );
2194
2205
};
2195
2206
2196
2207
Type resultTy;
@@ -2493,17 +2504,12 @@ namespace {
2493
2504
case PatternKind::Is: {
2494
2505
auto isPattern = cast<IsPattern>(pattern);
2495
2506
2496
- Type castType = resolveTypeReferenceInExpression (
2497
- isPattern->getCastTypeRepr (), TypeResolverContext::InExpression);
2498
-
2499
- if (!castType)
2500
- return Type ();
2501
-
2502
- castType = CS.openUnboundGenericTypes (
2503
- castType,
2504
- locator.withPathElement (LocatorPathElt::PatternMatch (pattern)));
2505
-
2506
- assert (castType);
2507
+ const Type castType = resolveTypeReferenceInExpression (
2508
+ isPattern->getCastTypeRepr (), TypeResolverContext::InExpression,
2509
+ OpenUnboundGenericType (CS,
2510
+ locator.withPathElement (
2511
+ LocatorPathElt::PatternMatch (pattern))));
2512
+ if (!castType) return Type ();
2507
2513
2508
2514
auto *subPattern = isPattern->getSubPattern ();
2509
2515
Type subPatternType = getTypeForPattern (
@@ -2559,7 +2565,14 @@ namespace {
2559
2565
}
2560
2566
return resolveTypeReferenceInExpression (
2561
2567
enumPattern->getParentTypeRepr (),
2562
- TypeResolverContext::InExpression);
2568
+ TypeResolverContext::InExpression, [](auto unboundTy) {
2569
+ // FIXME: We ought to pass an OpenUnboundGenericType object
2570
+ // rather than calling CS.openUnboundGenericType below, but
2571
+ // sometimes the parent type is resolved eagerly in
2572
+ // ResolvePattern::visitUnresolvedDotExpr, letting unbound
2573
+ // generics escape.
2574
+ return unboundTy;
2575
+ });
2563
2576
}();
2564
2577
2565
2578
if (!parentType)
@@ -2725,9 +2738,10 @@ namespace {
2725
2738
// of is-patterns applied to an irrefutable pattern.
2726
2739
pattern = pattern->getSemanticsProvidingPattern ();
2727
2740
while (auto isp = dyn_cast<IsPattern>(pattern)) {
2728
- Type castType = TypeResolution::forContextual (
2729
- CS.DC , TypeResolverContext::InExpression)
2730
- .resolveType (isp->getCastTypeRepr ());
2741
+ const Type castType = TypeResolution::forContextual (
2742
+ CS.DC , TypeResolverContext::InExpression,
2743
+ /* unboundTyOpener*/ nullptr )
2744
+ .resolveType (isp->getCastTypeRepr ());
2731
2745
if (castType->hasError ()) {
2732
2746
return false ;
2733
2747
}
@@ -3051,14 +3065,14 @@ namespace {
3051
3065
3052
3066
auto *const repr = expr->getCastTypeRepr ();
3053
3067
// Validate the resulting type.
3054
- const auto type = resolveTypeReferenceInExpression (
3055
- repr, TypeResolverContext::ExplicitCastExpr);
3056
- if (!type)
3068
+ const auto toType = resolveTypeReferenceInExpression (
3069
+ repr, TypeResolverContext::ExplicitCastExpr,
3070
+ // Introduce type variables for unbound generics.
3071
+ OpenUnboundGenericType (CS, CS.getConstraintLocator (expr)));
3072
+ if (!toType)
3057
3073
return nullptr ;
3058
3074
3059
- // Open the type we're casting to.
3060
- const auto toType =
3061
- CS.openUnboundGenericTypes (type, CS.getConstraintLocator (expr));
3075
+ // Cache the type we're casting to.
3062
3076
if (repr) CS.setType (repr, toType);
3063
3077
3064
3078
auto fromType = CS.getType (fromExpr);
@@ -3079,7 +3093,14 @@ namespace {
3079
3093
// Validate the resulting type.
3080
3094
auto *const repr = expr->getCastTypeRepr ();
3081
3095
const auto type = resolveTypeReferenceInExpression (
3082
- repr, TypeResolverContext::ExplicitCastExpr);
3096
+ repr, TypeResolverContext::ExplicitCastExpr, [](auto unboundTy) {
3097
+ // FIXME: We ought to pass an OpenUnboundGenericType object rather
3098
+ // than calling CS.openUnboundGenericType after resolving, but
3099
+ // sometimes the type expression is resolved eagerly in
3100
+ // PreCheckExpression::simplifyTypeConstructionWithLiteralArg,
3101
+ // letting unbound generics escape.
3102
+ return unboundTy;
3103
+ });
3083
3104
if (!type)
3084
3105
return nullptr ;
3085
3106
@@ -3111,14 +3132,14 @@ namespace {
3111
3132
3112
3133
// Validate the resulting type.
3113
3134
auto *const repr = expr->getCastTypeRepr ();
3114
- const auto type = resolveTypeReferenceInExpression (
3115
- repr, TypeResolverContext::ExplicitCastExpr);
3116
- if (!type)
3135
+ const auto toType = resolveTypeReferenceInExpression (
3136
+ repr, TypeResolverContext::ExplicitCastExpr,
3137
+ // Introduce type variables for unbound generics.
3138
+ OpenUnboundGenericType (CS, CS.getConstraintLocator (expr)));
3139
+ if (!toType)
3117
3140
return nullptr ;
3118
3141
3119
- // Open the type we're casting to.
3120
- const auto toType =
3121
- CS.openUnboundGenericTypes (type, CS.getConstraintLocator (expr));
3142
+ // Cache the type we're casting to.
3122
3143
if (repr) CS.setType (repr, toType);
3123
3144
3124
3145
auto fromType = CS.getType (fromExpr);
@@ -3137,17 +3158,16 @@ namespace {
3137
3158
3138
3159
Type visitIsExpr (IsExpr *expr) {
3139
3160
// Validate the type.
3161
+ // FIXME: Locator for the cast type?
3140
3162
auto &ctx = CS.getASTContext ();
3141
- const auto type = resolveTypeReferenceInExpression (
3142
- expr->getCastTypeRepr (),
3143
- TypeResolverContext::ExplicitCastExpr);
3144
- if (!type)
3163
+ const auto toType = resolveTypeReferenceInExpression (
3164
+ expr->getCastTypeRepr (), TypeResolverContext::ExplicitCastExpr,
3165
+ // Introduce type variables for unbound generics.
3166
+ OpenUnboundGenericType (CS, CS.getConstraintLocator (expr)));
3167
+ if (!toType)
3145
3168
return nullptr ;
3146
3169
3147
- // Open up the type we're checking.
3148
- // FIXME: Locator for the cast type?
3149
- const auto toType =
3150
- CS.openUnboundGenericTypes (type, CS.getConstraintLocator (expr));
3170
+ // Cache the type we're checking.
3151
3171
CS.setType (expr->getCastTypeRepr (), toType);
3152
3172
3153
3173
// Add a checked cast constraint.
@@ -3353,7 +3373,7 @@ namespace {
3353
3373
// Just resolve the referenced type.
3354
3374
// FIXME: The type reference needs to be opened into context.
3355
3375
return resolveTypeReferenceInExpression (
3356
- placeholderRepr, TypeResolverContext::InExpression);
3376
+ placeholderRepr, TypeResolverContext::InExpression, nullptr );
3357
3377
}
3358
3378
3359
3379
auto locator = CS.getConstraintLocator (E);
@@ -3423,11 +3443,13 @@ namespace {
3423
3443
3424
3444
// If a root type was explicitly given, then resolve it now.
3425
3445
if (auto rootRepr = E->getRootType ()) {
3426
- auto rootObjectTy = resolveTypeReferenceInExpression (
3427
- rootRepr, TypeResolverContext::InExpression);
3446
+ const auto rootObjectTy = resolveTypeReferenceInExpression (
3447
+ rootRepr, TypeResolverContext::InExpression,
3448
+ // Introduce type variables for unbound generics.
3449
+ OpenUnboundGenericType (CS, locator));
3428
3450
if (!rootObjectTy || rootObjectTy->hasError ())
3429
3451
return Type ();
3430
- rootObjectTy = CS. openUnboundGenericTypes (rootObjectTy, locator);
3452
+
3431
3453
// Allow \Derived.property to be inferred as \Base.property to
3432
3454
// simulate a sort of covariant conversion from
3433
3455
// KeyPath<Derived, T> to KeyPath<Base, T>.
@@ -3887,6 +3909,9 @@ static Type generateWrappedPropertyTypeConstraints(
3887
3909
3888
3910
auto wrapperAttributes = wrappedVar->getAttachedPropertyWrappers ();
3889
3911
for (unsigned i : indices (wrapperAttributes)) {
3912
+ // FIXME: We should somehow pass an OpenUnboundGenericTypeFn to
3913
+ // AttachedPropertyWrapperTypeRequest::evaluate to open up unbound
3914
+ // generics on the fly.
3890
3915
Type rawWrapperType = wrappedVar->getAttachedPropertyWrapperType (i);
3891
3916
auto wrapperInfo = wrappedVar->getAttachedPropertyWrapperTypeInfo (i);
3892
3917
if (rawWrapperType->hasError () || !wrapperInfo)
0 commit comments