@@ -527,7 +527,8 @@ namespace {
527
527
auto choice = overload.choice ;
528
528
assert (choice.getKind () != OverloadChoiceKind::DeclViaDynamic);
529
529
auto *decl = choice.getDecl ();
530
- auto fullType = simplifyType (overload.adjustedOpenedFullType );
530
+ Type fullType = simplifyType (overload.openedFullType );
531
+ Type adjustedFullType = simplifyType (overload.adjustedOpenedFullType );
531
532
532
533
// Determine the declaration selected for this overloaded reference.
533
534
auto &ctx = cs.getASTContext ();
@@ -540,7 +541,7 @@ namespace {
540
541
if (decl->getDeclContext ()->isTypeContext () && isa<FuncDecl>(decl)) {
541
542
assert (cast<FuncDecl>(decl)->isOperator () && " Must be an operator" );
542
543
543
- auto baseTy = getBaseType (fullType ->castTo <FunctionType>());
544
+ auto baseTy = getBaseType (adjustedFullType ->castTo <FunctionType>());
544
545
545
546
// Handle operator requirements found in protocols.
546
547
if (auto proto = dyn_cast<ProtocolDecl>(decl->getDeclContext ())) {
@@ -564,9 +565,9 @@ namespace {
564
565
// function.
565
566
Type refType;
566
567
if (isMemberOperator)
567
- refType = fullType ;
568
+ refType = adjustedFullType ;
568
569
else
569
- refType = fullType ->castTo <AnyFunctionType>()->getResult ();
570
+ refType = adjustedFullType ->castTo <AnyFunctionType>()->getResult ();
570
571
571
572
// Build the AST for the call to the witness.
572
573
auto subMap = getOperatorSubstitutions (witness, refType);
@@ -588,7 +589,7 @@ namespace {
588
589
589
590
refExpr = DotSyntaxCallExpr::create (ctx, declRefExpr,
590
591
SourceLoc (), base);
591
- auto refType = fullType ->castTo <FunctionType>()->getResult ();
592
+ auto refType = adjustedFullType ->castTo <FunctionType>()->getResult ();
592
593
cs.setType (refExpr, refType);
593
594
} else {
594
595
refExpr = declRefExpr;
@@ -612,7 +613,7 @@ namespace {
612
613
613
614
if (isa<TypeDecl>(decl) && !isa<ModuleDecl>(decl)) {
614
615
auto typeExpr = TypeExpr::createImplicitHack (
615
- loc.getBaseNameLoc (), fullType ->getMetatypeInstanceType (), ctx);
616
+ loc.getBaseNameLoc (), adjustedFullType ->getMetatypeInstanceType (), ctx);
616
617
cs.cacheType (typeExpr);
617
618
return typeExpr;
618
619
}
@@ -622,7 +623,9 @@ namespace {
622
623
new (ctx) DeclRefExpr (ref, loc, implicit, semantics, fullType);
623
624
cs.cacheType (declRefExpr);
624
625
declRefExpr->setFunctionRefKind (choice.getFunctionRefKind ());
625
- Expr *result = forceUnwrapIfExpected (declRefExpr, locator);
626
+ Expr *result = adjustTypeForDeclReference (
627
+ declRefExpr, fullType, adjustedFullType);
628
+ result = forceUnwrapIfExpected (result, locator);
626
629
627
630
if (auto *fnDecl = dyn_cast<AbstractFunctionDecl>(decl)) {
628
631
if (AnyFunctionRef (fnDecl).hasExternalPropertyWrapperParameters () &&
@@ -1501,19 +1504,22 @@ namespace {
1501
1504
return result;
1502
1505
}
1503
1506
1504
- auto refTy = simplifyType (overload.adjustedOpenedFullType );
1507
+ Type refTy = simplifyType (overload.openedFullType );
1508
+ Type adjustedRefTy = simplifyType (overload.adjustedOpenedFullType );
1505
1509
1506
1510
// If we're referring to the member of a module, it's just a simple
1507
1511
// reference.
1508
1512
if (baseTy->is <ModuleType>()) {
1509
1513
assert (semantics == AccessSemantics::Ordinary &&
1510
1514
" Direct property access doesn't make sense for this" );
1511
- auto ref = new (context) DeclRefExpr (memberRef, memberLoc, Implicit);
1512
- cs.setType (ref, refTy);
1513
- ref->setFunctionRefKind (choice.getFunctionRefKind ());
1514
- auto *DSBI = cs.cacheType (new (context) DotSyntaxBaseIgnoredExpr (
1515
- base, dotLoc, ref, cs.getType (ref)));
1516
- return forceUnwrapIfExpected (DSBI, memberLocator);
1515
+ auto *dre = new (context) DeclRefExpr (memberRef, memberLoc, Implicit);
1516
+ cs.setType (dre, refTy);
1517
+ dre->setFunctionRefKind (choice.getFunctionRefKind ());
1518
+ Expr *ref = cs.cacheType (new (context) DotSyntaxBaseIgnoredExpr (
1519
+ base, dotLoc, dre, refTy));
1520
+
1521
+ ref = adjustTypeForDeclReference (ref, refTy, adjustedRefTy);
1522
+ return forceUnwrapIfExpected (ref, memberLocator);
1517
1523
}
1518
1524
1519
1525
const bool isUnboundInstanceMember =
@@ -1689,11 +1695,15 @@ namespace {
1689
1695
memberLoc, Implicit, semantics);
1690
1696
memberRefExpr->setIsSuper (isSuper);
1691
1697
1692
- if (hasDynamicSelf)
1698
+ if (hasDynamicSelf) {
1693
1699
refTy = refTy->replaceCovariantResultType (containerTy, 1 );
1700
+ adjustedRefTy = adjustedRefTy->replaceCovariantResultType (
1701
+ containerTy, 1 );
1702
+ }
1694
1703
cs.setType (memberRefExpr, refTy->castTo <FunctionType>()->getResult ());
1695
1704
1696
1705
Expr *result = memberRefExpr;
1706
+ result = adjustTypeForDeclReference (result, refTy, adjustedRefTy);
1697
1707
closeExistentials (result, locator);
1698
1708
1699
1709
// If the property is of dynamic 'Self' type, wrap an implicit
@@ -1710,8 +1720,11 @@ namespace {
1710
1720
return forceUnwrapIfExpected (result, memberLocator);
1711
1721
}
1712
1722
1713
- if (member->getInterfaceType ()->hasDynamicSelfType ())
1723
+ if (member->getInterfaceType ()->hasDynamicSelfType ()) {
1714
1724
refTy = refTy->replaceCovariantResultType (containerTy, 2 );
1725
+ adjustedRefTy = adjustedRefTy->replaceCovariantResultType (
1726
+ containerTy, 2 );
1727
+ }
1715
1728
1716
1729
// Handle all other references.
1717
1730
auto declRefExpr = new (context) DeclRefExpr (memberRef, memberLoc,
@@ -1721,6 +1734,8 @@ namespace {
1721
1734
cs.setType (declRefExpr, refTy);
1722
1735
Expr *ref = declRefExpr;
1723
1736
1737
+ ref = adjustTypeForDeclReference (ref, refTy, adjustedRefTy);
1738
+
1724
1739
// A partial application thunk consists of two nested closures:
1725
1740
//
1726
1741
// { self in { args... in self.method(args...) } }
@@ -1778,7 +1793,7 @@ namespace {
1778
1793
// on 'CovariantReturnConversionExpr'.
1779
1794
curryThunkTy = simplifyType (adjustedOpenedType)->castTo <FunctionType>();
1780
1795
} else {
1781
- curryThunkTy = refTy ->castTo <FunctionType>();
1796
+ curryThunkTy = adjustedRefTy ->castTo <FunctionType>();
1782
1797
1783
1798
// Check if we need to open an existential stored inside 'self'.
1784
1799
auto knownOpened = solution.OpenedExistentialTypes .find (
@@ -1816,7 +1831,7 @@ namespace {
1816
1831
baseTy, member, memberLocator.getBaseLocator ());
1817
1832
if (!replacementTy->isEqual (containerTy)) {
1818
1833
Type conversionTy =
1819
- refTy ->replaceCovariantResultType (replacementTy, 2 );
1834
+ adjustedRefTy ->replaceCovariantResultType (replacementTy, 2 );
1820
1835
if (isSuperPartialApplication) {
1821
1836
conversionTy =
1822
1837
conversionTy->castTo <FunctionType>()->getResult ();
@@ -1841,11 +1856,8 @@ namespace {
1841
1856
ref = forceUnwrapIfExpected (ref, memberLocator);
1842
1857
apply = ConstructorRefCallExpr::create (context, ref, base);
1843
1858
} else if (isUnboundInstanceMember) {
1844
- auto refType = cs.simplifyType (adjustedOpenedType);
1845
- if (!cs.getType (ref)->isEqual (refType)) {
1846
- ref = new (context) FunctionConversionExpr (ref, refType);
1847
- cs.cacheType (ref);
1848
- }
1859
+ ref = adjustTypeForDeclReference (
1860
+ ref, cs.getType (ref), cs.simplifyType (adjustedOpenedType));
1849
1861
1850
1862
// Reference to an unbound instance method.
1851
1863
Expr *result = new (context) DotSyntaxBaseIgnoredExpr (base, dotLoc,
0 commit comments