@@ -2144,8 +2144,7 @@ ConstraintSystem::simplifyConstructionConstraint(
2144
2144
Type valueType, FunctionType *fnType, TypeMatchOptions flags,
2145
2145
FunctionRefKind functionRefKind, ConstraintLocator *locator) {
2146
2146
// Desugar the value type.
2147
- auto desugarValueType = getFixedTypeRecursive (valueType, true )
2148
- ->getDesugaredType ();
2147
+ auto desugarValueType = valueType->getDesugaredType ();
2149
2148
2150
2149
Type argType = fnType->getInput ();
2151
2150
Type resultType = fnType->getResult ();
@@ -2370,23 +2369,39 @@ static CheckedCastKind getCheckedCastKind(ConstraintSystem *cs,
2370
2369
ConstraintSystem::SolutionKind
2371
2370
ConstraintSystem::simplifyCheckedCastConstraint (
2372
2371
Type fromType, Type toType,
2372
+ TypeMatchOptions flags,
2373
2373
ConstraintLocatorBuilder locator) {
2374
+ TypeMatchOptions subflags = getDefaultDecompositionOptions (flags);
2375
+
2376
+ // / Form an unresolved result.
2377
+ auto formUnsolved = [&] {
2378
+ if (flags.contains (TMF_GenerateConstraints)) {
2379
+ addUnsolvedConstraint (
2380
+ Constraint::create (*this , ConstraintKind::CheckedCast, fromType,
2381
+ toType, DeclName (), FunctionRefKind::Compound,
2382
+ getConstraintLocator (locator)));
2383
+ return SolutionKind::Solved;
2384
+ }
2385
+
2386
+ return SolutionKind::Unsolved;
2387
+ };
2388
+
2374
2389
do {
2375
2390
// Dig out the fixed type to which this type refers.
2376
2391
fromType = getFixedTypeRecursive (fromType, /* wantRValue=*/ true );
2377
2392
2378
2393
// If we hit a type variable without a fixed type, we can't
2379
2394
// solve this yet.
2380
2395
if (fromType->isTypeVariableOrMember ())
2381
- return SolutionKind::Unsolved ;
2396
+ return formUnsolved () ;
2382
2397
2383
2398
// Dig out the fixed type to which this type refers.
2384
2399
toType = getFixedTypeRecursive (toType, /* wantRValue=*/ true );
2385
2400
2386
2401
// If we hit a type variable without a fixed type, we can't
2387
2402
// solve this yet.
2388
2403
if (toType->isTypeVariableOrMember ())
2389
- return SolutionKind::Unsolved ;
2404
+ return formUnsolved () ;
2390
2405
2391
2406
Type origFromType = fromType;
2392
2407
Type origToType = toType;
@@ -2414,6 +2429,9 @@ ConstraintSystem::simplifyCheckedCastConstraint(
2414
2429
}
2415
2430
}
2416
2431
2432
+ // We've decomposed the types further, so adopt the subflags.
2433
+ flags = subflags;
2434
+
2417
2435
// If nothing changed, we're done.
2418
2436
if (fromType.getPointer () == origFromType.getPointer () &&
2419
2437
toType.getPointer () == origToType.getPointer ())
@@ -2435,14 +2453,12 @@ ConstraintSystem::simplifyCheckedCastConstraint(
2435
2453
toBaseType)) {
2436
2454
// The class we're bridging through must be a subtype of the type we're
2437
2455
// coming from.
2438
- addConstraint (ConstraintKind::Subtype, classType, fromBaseType,
2439
- getConstraintLocator (locator));
2440
- return SolutionKind::Solved;
2456
+ return matchTypes (classType, fromBaseType, TypeMatchKind::Subtype,
2457
+ subflags, locator);
2441
2458
}
2442
2459
2443
- addConstraint (ConstraintKind::Subtype, toBaseType, fromBaseType,
2444
- getConstraintLocator (locator));
2445
- return SolutionKind::Solved;
2460
+ return matchTypes (toBaseType, fromBaseType, TypeMatchKind::Subtype,
2461
+ subflags, locator);
2446
2462
}
2447
2463
case CheckedCastKind::DictionaryDowncast: {
2448
2464
Type fromKeyType, fromValueType;
@@ -2462,8 +2478,7 @@ ConstraintSystem::simplifyCheckedCastConstraint(
2462
2478
}
2463
2479
2464
2480
// Perform subtype check on the possibly-bridged-through key type.
2465
- TypeMatchOptions subflags = TMF_GenerateConstraints;
2466
- auto result = matchTypes (toKeyType, fromKeyType, TypeMatchKind::Subtype,
2481
+ auto result = matchTypes (toKeyType, fromKeyType, TypeMatchKind::Subtype,
2467
2482
subflags, locator);
2468
2483
if (result == SolutionKind::Error)
2469
2484
return result;
@@ -2482,7 +2497,7 @@ ConstraintSystem::simplifyCheckedCastConstraint(
2482
2497
return result;
2483
2498
2484
2499
case SolutionKind::Unsolved:
2485
- return SolutionKind::Unsolved ;
2500
+ return SolutionKind::Solved ;
2486
2501
2487
2502
case SolutionKind::Error:
2488
2503
return SolutionKind::Error;
@@ -2501,13 +2516,13 @@ ConstraintSystem::simplifyCheckedCastConstraint(
2501
2516
toBaseType)) {
2502
2517
// The class we're bridging through must be a subtype of the type we're
2503
2518
// coming from.
2504
- addConstraint (ConstraintKind::Subtype, classType, fromBaseType,
2505
- getConstraintLocator ( locator) );
2519
+ addConstraint (ConstraintKind::Subtype, classType,
2520
+ fromBaseType, locator);
2506
2521
return SolutionKind::Solved;
2507
2522
}
2508
2523
2509
- addConstraint (ConstraintKind::Subtype, toBaseType, fromBaseType,
2510
- getConstraintLocator ( locator) );
2524
+ addConstraint (ConstraintKind::Subtype, toBaseType,
2525
+ fromBaseType, locator);
2511
2526
return SolutionKind::Solved;
2512
2527
}
2513
2528
@@ -4144,6 +4159,9 @@ ConstraintSystem::addConstraintImpl(ConstraintKind kind, Type first,
4144
4159
return simplifyConformsToConstraint (first, second, kind, locator,
4145
4160
subflags);
4146
4161
4162
+ case ConstraintKind::CheckedCast:
4163
+ return simplifyCheckedCastConstraint (first, second, subflags, locator);
4164
+
4147
4165
case ConstraintKind::Bind: // FIXME: This should go through matchTypes() above
4148
4166
4149
4167
default : {
@@ -4254,6 +4272,7 @@ ConstraintSystem::simplifyConstraint(const Constraint &constraint) {
4254
4272
case ConstraintKind::CheckedCast: {
4255
4273
auto result = simplifyCheckedCastConstraint (constraint.getFirstType (),
4256
4274
constraint.getSecondType (),
4275
+ None,
4257
4276
constraint.getLocator ());
4258
4277
// NOTE: simplifyCheckedCastConstraint() may return Unsolved, e.g. if the
4259
4278
// subexpression's type is unresolved. Don't record the fix until we
0 commit comments