@@ -1552,37 +1552,9 @@ static bool isExistentialMemberAccessWithExplicitBaseExpression(
1552
1552
}
1553
1553
1554
1554
Type ConstraintSystem::getMemberReferenceTypeFromOpenedType (
1555
- Type &openedType , Type baseObjTy, ValueDecl *value, DeclContext *outerDC,
1555
+ Type type , Type baseObjTy, ValueDecl *value, DeclContext *outerDC,
1556
1556
ConstraintLocator *locator, bool hasAppliedSelf, bool isDynamicLookup,
1557
1557
ArrayRef<OpenedType> replacements) {
1558
- // Check if we need to apply a layer of optionality to the uncurried type.
1559
- if (!isRequirementOrWitness (locator)) {
1560
- if (isDynamicLookup || value->getAttrs ().hasAttribute <OptionalAttr>()) {
1561
- const auto applyOptionality = [&](FunctionType *fnTy) -> Type {
1562
- Type resultTy;
1563
- // Optional and dynamic subscripts are a special case, because the
1564
- // optionality is applied to the result type and not the type of the
1565
- // reference.
1566
- if (isa<SubscriptDecl>(value)) {
1567
- auto *innerFn = fnTy->getResult ()->castTo <FunctionType>();
1568
- resultTy = FunctionType::get (
1569
- innerFn->getParams (),
1570
- OptionalType::get (innerFn->getResult ()->getRValueType ()),
1571
- innerFn->getExtInfo ());
1572
- } else {
1573
- resultTy = OptionalType::get (fnTy->getResult ()->getRValueType ());
1574
- }
1575
-
1576
- return FunctionType::get (fnTy->getParams (), resultTy,
1577
- fnTy->getExtInfo ());
1578
- };
1579
-
1580
- openedType = applyOptionality (openedType->castTo <FunctionType>());
1581
- }
1582
- }
1583
-
1584
- Type type = openedType;
1585
-
1586
1558
// Cope with dynamic 'Self'.
1587
1559
if (outerDC->getSelfClassDecl ()) {
1588
1560
if (type->hasDynamicSelfType ()) {
@@ -1657,6 +1629,26 @@ Type ConstraintSystem::getMemberReferenceTypeFromOpenedType(
1657
1629
return type;
1658
1630
}
1659
1631
1632
+ static FunctionType *applyOptionality (ValueDecl *value, FunctionType *fnTy) {
1633
+ Type resultTy;
1634
+
1635
+ // Optional and dynamic subscripts are a special case, because the
1636
+ // optionality is applied to the result type and not the type of the
1637
+ // reference.
1638
+ if (isa<SubscriptDecl>(value)) {
1639
+ auto *innerFn = fnTy->getResult ()->castTo <FunctionType>();
1640
+ resultTy = FunctionType::get (
1641
+ innerFn->getParams (),
1642
+ OptionalType::get (innerFn->getResult ()->getRValueType ()),
1643
+ innerFn->getExtInfo ());
1644
+ } else {
1645
+ resultTy = OptionalType::get (fnTy->getResult ()->getRValueType ());
1646
+ }
1647
+
1648
+ return FunctionType::get (fnTy->getParams (), resultTy,
1649
+ fnTy->getExtInfo ());
1650
+ }
1651
+
1660
1652
DeclReferenceType ConstraintSystem::getTypeOfMemberReference (
1661
1653
Type baseTy, ValueDecl *value, DeclContext *useDC, bool isDynamicLookup,
1662
1654
FunctionRefInfo functionRefInfo, ConstraintLocator *locator,
@@ -1954,6 +1946,14 @@ DeclReferenceType ConstraintSystem::getTypeOfMemberReference(
1954
1946
origFnType->getParams (), resultTy, origFnType->getExtInfo ());
1955
1947
}
1956
1948
1949
+ // Check if we need to apply a layer of optionality to the uncurried type.
1950
+ if (!isRequirementOrWitness (locator)) {
1951
+ if (isDynamicLookup || value->getAttrs ().hasAttribute <OptionalAttr>()) {
1952
+ openedType = applyOptionality (value, openedType->castTo <FunctionType>());
1953
+ origOpenedType = applyOptionality (value, origOpenedType->castTo <FunctionType>());
1954
+ }
1955
+ }
1956
+
1957
1957
// Compute the type of the reference.
1958
1958
Type type = getMemberReferenceTypeFromOpenedType (
1959
1959
openedType, baseObjTy, value, outerDC, locator, hasAppliedSelf,
0 commit comments