@@ -481,6 +481,12 @@ static bool isDeclAsSpecializedAs(TypeChecker &tc, DeclContext *dc,
481
481
log << " \n " ;
482
482
}
483
483
484
+ auto *innerDC1 = decl1->getInnermostDeclContext ();
485
+ auto *innerDC2 = decl2->getInnermostDeclContext ();
486
+
487
+ auto *outerDC1 = decl1->getDeclContext ();
488
+ auto *outerDC2 = decl2->getDeclContext ();
489
+
484
490
if (!tc.specializedOverloadComparisonCache .count ({decl1, decl2})) {
485
491
486
492
auto compareSpecializations = [&] () -> bool {
@@ -516,18 +522,16 @@ static bool isDeclAsSpecializedAs(TypeChecker &tc, DeclContext *dc,
516
522
}
517
523
518
524
// Members of protocol extensions have special overloading rules.
519
- ProtocolDecl *inProtocolExtension1 = decl1-> getDeclContext ()
525
+ ProtocolDecl *inProtocolExtension1 = outerDC1
520
526
->getAsProtocolExtensionContext ();
521
- ProtocolDecl *inProtocolExtension2 = decl2-> getDeclContext ()
527
+ ProtocolDecl *inProtocolExtension2 = outerDC2
522
528
->getAsProtocolExtensionContext ();
523
529
if (inProtocolExtension1 && inProtocolExtension2) {
524
530
// Both members are in protocol extensions.
525
531
// Determine whether the 'Self' type from the first protocol extension
526
532
// satisfies all of the requirements of the second protocol extension.
527
- DeclContext *dc1 = decl1->getDeclContext ();
528
- DeclContext *dc2 = decl2->getDeclContext ();
529
- bool better1 = isProtocolExtensionAsSpecializedAs (tc, dc1, dc2);
530
- bool better2 = isProtocolExtensionAsSpecializedAs (tc, dc2, dc1);
533
+ bool better1 = isProtocolExtensionAsSpecializedAs (tc, outerDC1, outerDC2);
534
+ bool better2 = isProtocolExtensionAsSpecializedAs (tc, outerDC2, outerDC1);
531
535
if (better1 != better2) {
532
536
return better1;
533
537
}
@@ -554,8 +558,8 @@ static bool isDeclAsSpecializedAs(TypeChecker &tc, DeclContext *dc,
554
558
}
555
559
} else {
556
560
// Add a curried 'self' type.
557
- type1 = addCurriedSelfType (tc.Context , type1, decl1-> getDeclContext () );
558
- type2 = addCurriedSelfType (tc.Context , type2, decl2-> getDeclContext () );
561
+ type1 = addCurriedSelfType (tc.Context , type1, outerDC1 );
562
+ type2 = addCurriedSelfType (tc.Context , type2, outerDC2 );
559
563
560
564
// For a subscript declaration, only look at the input type (i.e., the
561
565
// indices).
@@ -578,31 +582,44 @@ static bool isDeclAsSpecializedAs(TypeChecker &tc, DeclContext *dc,
578
582
openedType2 = cs.openFunctionType (
579
583
funcType, /* numArgumentLabelsToRemove=*/ 0 , locator,
580
584
/* replacements=*/ unused,
581
- decl2-> getInnermostDeclContext () ,
582
- decl2-> getDeclContext () ,
585
+ innerDC2 ,
586
+ outerDC2 ,
583
587
/* skipProtocolSelfConstraint=*/ false );
584
588
} else {
589
+ cs.openGeneric (innerDC2,
590
+ outerDC2,
591
+ innerDC2->getGenericSignatureOfContext (),
592
+ /* skipProtocolSelfConstraint=*/ false ,
593
+ locator,
594
+ unused);
595
+
585
596
openedType2 = cs.openType (type2, locator, unused);
586
597
}
587
598
588
599
// Get the type of a reference to the first declaration, swapping in
589
600
// archetypes for the dependent types.
590
601
OpenedTypeMap replacements;
591
- auto dc1 = decl1->getInnermostDeclContext ();
592
602
Type openedType1;
593
603
if (auto *funcType = type1->getAs <AnyFunctionType>()) {
594
604
openedType1 = cs.openFunctionType (
595
605
funcType, /* numArgumentLabelsToRemove=*/ 0 , locator,
596
606
replacements,
597
- dc1 ,
598
- decl1-> getDeclContext () ,
607
+ innerDC1 ,
608
+ outerDC1 ,
599
609
/* skipProtocolSelfConstraint=*/ false );
600
610
} else {
611
+ cs.openGeneric (innerDC1,
612
+ outerDC1,
613
+ innerDC1->getGenericSignatureOfContext (),
614
+ /* skipProtocolSelfConstraint=*/ false ,
615
+ locator,
616
+ replacements);
617
+
601
618
openedType1 = cs.openType (type1, locator, replacements);
602
619
}
603
620
604
621
for (const auto &replacement : replacements) {
605
- if (auto mapped = dc1 ->mapTypeIntoContext (replacement.first )) {
622
+ if (auto mapped = innerDC1 ->mapTypeIntoContext (replacement.first )) {
606
623
cs.addConstraint (ConstraintKind::Bind, replacement.second , mapped,
607
624
locator);
608
625
}
@@ -611,12 +628,12 @@ static bool isDeclAsSpecializedAs(TypeChecker &tc, DeclContext *dc,
611
628
// Extract the self types from the declarations, if they have them.
612
629
Type selfTy1;
613
630
Type selfTy2;
614
- if (decl1-> getDeclContext () ->isTypeContext ()) {
631
+ if (outerDC1 ->isTypeContext ()) {
615
632
auto funcTy1 = openedType1->castTo <FunctionType>();
616
633
selfTy1 = funcTy1->getInput ()->getRValueInstanceType ();
617
634
openedType1 = funcTy1->getResult ();
618
635
}
619
- if (decl2-> getDeclContext () ->isTypeContext ()) {
636
+ if (outerDC2 ->isTypeContext ()) {
620
637
auto funcTy2 = openedType2->castTo <FunctionType>();
621
638
selfTy2 = funcTy2->getInput ()->getRValueInstanceType ();
622
639
openedType2 = funcTy2->getResult ();
@@ -625,8 +642,7 @@ static bool isDeclAsSpecializedAs(TypeChecker &tc, DeclContext *dc,
625
642
// Determine the relationship between the 'self' types and add the
626
643
// appropriate constraints. The constraints themselves never fail, but
627
644
// they help deduce type variables that were opened.
628
- switch (computeSelfTypeRelationship (tc, dc, decl1->getDeclContext (),
629
- decl2->getDeclContext ())) {
645
+ switch (computeSelfTypeRelationship (tc, dc, outerDC1, outerDC2)) {
630
646
case SelfTypeRelationship::Unrelated:
631
647
// Skip the self types parameter entirely.
632
648
break ;
@@ -645,15 +661,13 @@ static bool isDeclAsSpecializedAs(TypeChecker &tc, DeclContext *dc,
645
661
646
662
case SelfTypeRelationship::ConformsTo:
647
663
cs.addConstraint (ConstraintKind::ConformsTo, selfTy1,
648
- cast<ProtocolDecl>(decl2->getDeclContext ())
649
- ->getDeclaredType (),
664
+ cast<ProtocolDecl>(outerDC2)->getDeclaredType (),
650
665
locator);
651
666
break ;
652
667
653
668
case SelfTypeRelationship::ConformedToBy:
654
669
cs.addConstraint (ConstraintKind::ConformsTo, selfTy2,
655
- cast<ProtocolDecl>(decl1->getDeclContext ())
656
- ->getDeclaredType (),
670
+ cast<ProtocolDecl>(outerDC1)->getDeclaredType (),
657
671
locator);
658
672
break ;
659
673
}
@@ -675,10 +689,10 @@ static bool isDeclAsSpecializedAs(TypeChecker &tc, DeclContext *dc,
675
689
auto funcTy2 = openedType2->castTo <FunctionType>();
676
690
SmallVector<CallArgParam, 4 > params1 =
677
691
decomposeParamType (funcTy1->getInput (), decl1,
678
- decl1-> getDeclContext () ->isTypeContext ());
692
+ outerDC1 ->isTypeContext ());
679
693
SmallVector<CallArgParam, 4 > params2 =
680
694
decomposeParamType (funcTy2->getInput (), decl2,
681
- decl2-> getDeclContext () ->isTypeContext ());
695
+ outerDC2 ->isTypeContext ());
682
696
683
697
unsigned numParams1 = params1.size ();
684
698
unsigned numParams2 = params2.size ();
0 commit comments