@@ -2423,9 +2423,6 @@ bool TypeChecker::typeCheckBinding(Pattern *&pattern, Expr *&initializer,
2423
2423
// / The locator we're using.
2424
2424
ConstraintLocator *Locator;
2425
2425
2426
- // / The type of the initializer.
2427
- Type initType;
2428
-
2429
2426
// / The variable to that has property wrappers that have been applied to the initializer expression.
2430
2427
VarDecl *wrappedVar = nullptr ;
2431
2428
@@ -2435,80 +2432,20 @@ bool TypeChecker::typeCheckBinding(Pattern *&pattern, Expr *&initializer,
2435
2432
wrappedVar = target.getInitializationWrappedVar ();
2436
2433
}
2437
2434
2438
- // / Retrieve the type to which the pattern should be coerced.
2439
- Type getPatternInitType (ConstraintSystem *cs) const {
2440
- if (!wrappedVar || initType->hasError () ||
2441
- initType->is <TypeVariableType>())
2442
- return initType;
2443
-
2444
- // When we have an active constraint system, form value-member
2445
- // constraints to dig in to the appropriate resulting property.
2446
- if (cs) {
2447
- Type valueType = LValueType::get (initType);
2448
- auto dc = wrappedVar->getInnermostDeclContext ();
2449
- auto *loc = cs->getConstraintLocator (target.getAsExpr ());
2450
-
2451
- for (unsigned i : indices (wrappedVar->getAttachedPropertyWrappers ())) {
2452
- auto wrapperInfo = wrappedVar->getAttachedPropertyWrapperTypeInfo (i);
2453
- if (!wrapperInfo)
2454
- break ;
2455
-
2456
- loc = cs->getConstraintLocator (loc, ConstraintLocator::Member);
2457
- Type memberType = cs->createTypeVariable (loc, TVO_CanBindToLValue);
2458
- cs->addValueMemberConstraint (
2459
- valueType, wrapperInfo.valueVar ->createNameRef (),
2460
- memberType, dc, FunctionRefKind::Unapplied, { }, loc);
2461
- valueType = memberType;
2462
- }
2463
-
2464
- // Set up an equality constraint to drop the lvalue-ness of the value
2465
- // type we produced.
2466
- Type propertyType = cs->createTypeVariable (loc, 0 );
2467
- cs->addConstraint (ConstraintKind::Equal, propertyType, valueType, loc);
2468
- return propertyType;
2469
- }
2470
-
2471
- // Otherwise, compute the wrapped value type directly.
2472
- return computeWrappedValueType (wrappedVar, initType);
2473
- }
2474
-
2475
2435
bool builtConstraints (ConstraintSystem &cs, Expr *expr) override {
2476
- assert (!expr->isSemanticallyInOutExpr ());
2477
-
2478
- // Save the locator we're using for the expression.
2436
+ // The expression has been pre-checked; save it in case we fail later.
2479
2437
Locator = cs.getConstraintLocator (expr, LocatorPathElt::ContextualType ());
2438
+ return false ;
2439
+ }
2480
2440
2481
- // Collect constraints from the pattern.
2482
- Type patternType =
2483
- cs.generateConstraints (target.getInitializationPattern (), Locator);
2484
- if (!patternType)
2485
- return true ;
2486
-
2441
+ Expr *appliedSolution (Solution &solution, Expr *expr) override {
2442
+ Type initType;
2487
2443
if (wrappedVar) {
2488
- // When we have applied a property wrapper, the initializer type
2489
- // is the initialization of the property wrapper instance.
2490
- initType = cs.getType (expr);
2491
-
2492
- // Add an equal constraint between the pattern type and the
2493
- // property wrapper's "value" type.
2494
- cs.addConstraint (ConstraintKind::Equal, patternType,
2495
- getPatternInitType (&cs), Locator, /* isFavored*/ true );
2444
+ initType = solution.getType (expr);
2496
2445
} else {
2497
- // The initializer type is the type of the pattern.
2498
- initType = patternType;
2499
-
2500
- // Add a conversion constraint between the types.
2501
- if (!initType->is <OpaqueTypeArchetypeType>()) {
2502
- cs.addConstraint (ConstraintKind::Conversion, cs.getType (expr),
2503
- patternType, Locator, /* isFavored*/ true );
2504
- }
2446
+ initType = solution.getType (target.getInitializationPattern ());
2505
2447
}
2506
2448
2507
- // The expression has been pre-checked; save it in case we fail later.
2508
- return false ;
2509
- }
2510
-
2511
- Expr *appliedSolution (Solution &solution, Expr *expr) override {
2512
2449
{
2513
2450
// Figure out what type the constraints decided on.
2514
2451
auto ty = solution.simplifyType (initType);
@@ -2520,8 +2457,6 @@ bool TypeChecker::typeCheckBinding(Pattern *&pattern, Expr *&initializer,
2520
2457
if (!expr)
2521
2458
return nullptr ;
2522
2459
2523
- assert (solution.getConstraintSystem ().getType (expr)->isEqual (initType));
2524
-
2525
2460
// Record the property wrapper type and note that the initializer has
2526
2461
// been subsumed by the backing property.
2527
2462
if (wrappedVar) {
@@ -2542,7 +2477,7 @@ bool TypeChecker::typeCheckBinding(Pattern *&pattern, Expr *&initializer,
2542
2477
auto target = SolutionApplicationTarget::forInitialization (
2543
2478
initializer, DC, patternType, pattern);
2544
2479
initializer = target.getAsExpr ();
2545
-
2480
+
2546
2481
BindingListener listener (Context, target);
2547
2482
if (!initializer)
2548
2483
return true ;
@@ -2561,10 +2496,17 @@ bool TypeChecker::typeCheckBinding(Pattern *&pattern, Expr *&initializer,
2561
2496
: TypeResolverContext::InExpression;
2562
2497
options |= TypeResolutionFlags::OverrideType;
2563
2498
2564
- auto initTy = listener.getPatternInitType (nullptr );
2499
+ Type initTy = initializer->getType ();
2500
+ if (auto wrappedVar = target.getInitializationWrappedVar ()) {
2501
+ if (!initTy->hasError () && !initTy->is <TypeVariableType>())
2502
+ initTy = computeWrappedValueType (wrappedVar, initTy);
2503
+ }
2504
+
2565
2505
if (initTy->hasDependentMember ())
2566
2506
return true ;
2567
2507
2508
+ initTy = initTy->reconstituteSugar (/* recursive =*/ false );
2509
+
2568
2510
// Apply the solution to the pattern as well.
2569
2511
auto contextualPattern =
2570
2512
ContextualPattern::forRawPattern (pattern, DC);
0 commit comments