@@ -2603,32 +2603,60 @@ static void synthesizeDesignatedInitOverride(AbstractFunctionDecl *fn,
2603
2603
auto *ctor = cast<ConstructorDecl>(fn);
2604
2604
auto &ctx = ctor->getASTContext ();
2605
2605
2606
- auto *bodyParams = ctor->getParameters ();
2607
2606
auto *superclassCtor = (ConstructorDecl *) context;
2608
2607
2608
+ if (!superclassCtor->hasValidSignature ())
2609
+ ctx.getLazyResolver ()->resolveDeclSignature (superclassCtor);
2610
+
2609
2611
// Reference to super.init.
2610
2612
auto *selfDecl = ctor->getImplicitSelfDecl ();
2611
- Expr *superRef = new (ctx) SuperRefExpr (selfDecl, SourceLoc (),
2612
- /* Implicit=*/ true );
2613
- Expr *ctorRef = new (ctx) UnresolvedDotExpr (superRef, SourceLoc (),
2614
- superclassCtor->getFullName (),
2615
- DeclNameLoc (),
2616
- /* Implicit=*/ true );
2613
+ auto *superRef = buildSelfReference (selfDecl, SelfAccessorKind::Super,
2614
+ /* isLValue=*/ false , ctx, true );
2617
2615
2618
- auto ctorArgs = buildArgumentForwardingExpr (bodyParams->getArray (), ctx, false );
2616
+ SubstitutionMap subs;
2617
+ if (auto *genericEnv = fn->getGenericEnvironment ())
2618
+ subs = genericEnv->getForwardingSubstitutionMap ();
2619
+ subs = SubstitutionMap::getOverrideSubstitutions (superclassCtor, fn, subs);
2620
+ ConcreteDeclRef ctorRef (superclassCtor, subs);
2619
2621
2620
- Expr *superCall =
2621
- CallExpr::create (ctx, ctorRef, ctorArgs,
2622
+ auto type = superclassCtor->getInitializerInterfaceType ()
2623
+ .subst (subs, SubstFlags::UseErrorType);
2624
+ auto *ctorRefExpr =
2625
+ new (ctx) OtherConstructorDeclRefExpr (ctorRef, DeclNameLoc (),
2626
+ IsImplicit, type);
2627
+
2628
+ if (auto *funcTy = type->getAs <FunctionType>())
2629
+ type = funcTy->getResult ();
2630
+ auto *superclassCtorRefExpr =
2631
+ new (ctx) DotSyntaxCallExpr (ctorRefExpr, SourceLoc (), superRef, type);
2632
+ superclassCtorRefExpr->setIsSuper (true );
2633
+
2634
+ auto *bodyParams = ctor->getParameters ();
2635
+ auto ctorArgs = buildArgumentForwardingExpr (bodyParams->getArray (), ctx, true );
2636
+ Expr *superclassCallExpr =
2637
+ CallExpr::create (ctx, superclassCtorRefExpr, ctorArgs,
2622
2638
superclassCtor->getFullName ().getArgumentNames (), { },
2623
2639
/* hasTrailingClosure=*/ false , /* implicit=*/ true );
2640
+
2641
+ if (auto *funcTy = type->getAs <FunctionType>())
2642
+ type = funcTy->getResult ();
2643
+ superclassCallExpr->setType (type);
2644
+
2624
2645
if (superclassCtor->hasThrows ()) {
2625
- superCall = new (ctx) TryExpr (SourceLoc (), superCall, Type () ,
2626
- /* implicit=*/ true );
2646
+ superclassCallExpr = new (ctx) TryExpr (SourceLoc (), superclassCallExpr ,
2647
+ type, /* implicit=*/ true );
2627
2648
}
2628
- ctor->setBody (BraceStmt::create (ctx, SourceLoc (),
2629
- ASTNode (superCall),
2630
- SourceLoc (),
2649
+
2650
+ auto *rebindSelfExpr =
2651
+ new (ctx) RebindSelfInConstructorExpr (superclassCallExpr,
2652
+ selfDecl);
2653
+
2654
+ SmallVector<ASTNode, 2 > stmts;
2655
+ stmts.push_back (rebindSelfExpr);
2656
+ stmts.push_back (new (ctx) ReturnStmt (SourceLoc (), /* Result=*/ nullptr ));
2657
+ ctor->setBody (BraceStmt::create (ctx, SourceLoc (), stmts, SourceLoc (),
2631
2658
/* implicit=*/ true ));
2659
+ ctor->setBodyTypeCheckedIfPresent ();
2632
2660
}
2633
2661
2634
2662
ConstructorDecl *
@@ -2678,7 +2706,7 @@ swift::createDesignatedInitOverride(TypeChecker &tc,
2678
2706
// like 'class A : B<Int>'.
2679
2707
for (auto *decl : *bodyParams) {
2680
2708
auto paramTy = decl->getInterfaceType ();
2681
- auto substTy = paramTy.subst (subMap);
2709
+ auto substTy = paramTy.subst (subMap, SubstFlags::UseErrorType );
2682
2710
decl->setInterfaceType (substTy);
2683
2711
}
2684
2712
0 commit comments