@@ -2474,8 +2474,8 @@ bool TypeChecker::typeCheckBinding(Pattern *&pattern, Expr *&initializer,
2474
2474
// / Type checking listener for pattern binding initializers.
2475
2475
class BindingListener : public ExprTypeCheckListener {
2476
2476
ASTContext &context;
2477
- Pattern *&pattern;
2478
- Expr *&initializer ;
2477
+
2478
+ SolutionApplicationTarget target ;
2479
2479
2480
2480
// / The locator we're using.
2481
2481
ConstraintLocator *Locator;
@@ -2487,11 +2487,9 @@ bool TypeChecker::typeCheckBinding(Pattern *&pattern, Expr *&initializer,
2487
2487
VarDecl *wrappedVar = nullptr ;
2488
2488
2489
2489
public:
2490
- explicit BindingListener (ASTContext &ctx, Pattern *&pattern,
2491
- Expr *&initializer)
2492
- : context(ctx), pattern(pattern), initializer(initializer),
2493
- Locator(nullptr ) {
2494
- maybeApplyPropertyWrapper ();
2490
+ explicit BindingListener (ASTContext &ctx, SolutionApplicationTarget target)
2491
+ : context(ctx), target(target), Locator(nullptr ) {
2492
+ wrappedVar = target.getInitializationWrappedVar ();
2495
2493
}
2496
2494
2497
2495
// / Retrieve the type to which the pattern should be coerced.
@@ -2505,7 +2503,7 @@ bool TypeChecker::typeCheckBinding(Pattern *&pattern, Expr *&initializer,
2505
2503
if (cs) {
2506
2504
Type valueType = LValueType::get (initType);
2507
2505
auto dc = wrappedVar->getInnermostDeclContext ();
2508
- auto *loc = cs->getConstraintLocator (initializer );
2506
+ auto *loc = cs->getConstraintLocator (target. getAsExpr () );
2509
2507
2510
2508
for (unsigned i : indices (wrappedVar->getAttachedPropertyWrappers ())) {
2511
2509
auto wrapperInfo = wrappedVar->getAttachedPropertyWrapperTypeInfo (i);
@@ -2538,7 +2536,8 @@ bool TypeChecker::typeCheckBinding(Pattern *&pattern, Expr *&initializer,
2538
2536
Locator = cs.getConstraintLocator (expr, LocatorPathElt::ContextualType ());
2539
2537
2540
2538
// Collect constraints from the pattern.
2541
- Type patternType = cs.generateConstraints (pattern, Locator);
2539
+ Type patternType =
2540
+ cs.generateConstraints (target.getInitializationPattern (), Locator);
2542
2541
if (!patternType)
2543
2542
return true ;
2544
2543
@@ -2563,7 +2562,6 @@ bool TypeChecker::typeCheckBinding(Pattern *&pattern, Expr *&initializer,
2563
2562
}
2564
2563
2565
2564
// The expression has been pre-checked; save it in case we fail later.
2566
- initializer = expr;
2567
2565
return false ;
2568
2566
}
2569
2567
@@ -2593,70 +2591,20 @@ bool TypeChecker::typeCheckBinding(Pattern *&pattern, Expr *&initializer,
2593
2591
->setSemanticInit (expr);
2594
2592
}
2595
2593
2596
- initializer = expr;
2597
2594
return expr;
2598
2595
}
2599
-
2600
- private:
2601
- // If the pattern contains a single variable that has an attached
2602
- // property wrapper, set up the initializer expression to initialize
2603
- // the backing storage.
2604
- void maybeApplyPropertyWrapper () {
2605
- auto singleVar = pattern->getSingleVar ();
2606
- if (!singleVar)
2607
- return ;
2608
-
2609
- auto wrapperAttrs = singleVar->getAttachedPropertyWrappers ();
2610
- if (wrapperAttrs.empty ())
2611
- return ;
2612
-
2613
- // If the outermost property wrapper is directly initialized, form the
2614
- // call.
2615
- auto &ctx = singleVar->getASTContext ();
2616
- auto outermostWrapperAttr = wrapperAttrs.front ();
2617
- if (initializer) {
2618
- // Form init(wrappedValue:) call(s).
2619
- Expr *wrappedInitializer =
2620
- buildPropertyWrapperInitialValueCall (
2621
- singleVar, Type (), initializer, /* ignoreAttributeArgs=*/ false );
2622
- if (!wrappedInitializer)
2623
- return ;
2624
-
2625
- initializer = wrappedInitializer;
2626
- } else if (auto outermostArg = outermostWrapperAttr->getArg ()) {
2627
- Type outermostWrapperType =
2628
- singleVar->getAttachedPropertyWrapperType (0 );
2629
- if (!outermostWrapperType)
2630
- return ;
2631
-
2632
- auto typeExpr = TypeExpr::createImplicitHack (
2633
- outermostWrapperAttr->getTypeLoc ().getLoc (),
2634
- outermostWrapperType, ctx);
2635
- initializer = CallExpr::create (
2636
- ctx, typeExpr, outermostArg,
2637
- outermostWrapperAttr->getArgumentLabels (),
2638
- outermostWrapperAttr->getArgumentLabelLocs (),
2639
- /* hasTrailingClosure=*/ false ,
2640
- /* implicit=*/ false );
2641
- } else {
2642
- llvm_unreachable (" No initializer anywhere?" );
2643
- }
2644
- wrapperAttrs[0 ]->setSemanticInit (initializer);
2645
-
2646
- // Note that we have applied to property wrapper, so we can adjust
2647
- // the initializer type later.
2648
- wrappedVar = singleVar;
2649
- }
2650
2596
};
2651
2597
2652
2598
auto &Context = DC->getASTContext ();
2653
- BindingListener listener (Context, pattern, initializer);
2599
+ auto target = SolutionApplicationTarget::forInitialization (
2600
+ initializer, DC, patternType, pattern);
2601
+ initializer = target.getAsExpr ();
2602
+
2603
+ BindingListener listener (Context, target);
2654
2604
if (!initializer)
2655
2605
return true ;
2656
2606
2657
2607
// Type-check the initializer.
2658
- auto target = SolutionApplicationTarget::forInitialization (
2659
- initializer, DC, patternType, pattern);
2660
2608
bool unresolvedTypeExprs = false ;
2661
2609
auto resultTarget = typeCheckExpression (target, unresolvedTypeExprs,
2662
2610
None, &listener);
0 commit comments