@@ -1552,37 +1552,9 @@ static bool isExistentialMemberAccessWithExplicitBaseExpression(
15521552}
15531553
15541554Type ConstraintSystem::getMemberReferenceTypeFromOpenedType (
1555- Type &openedType , Type baseObjTy, ValueDecl *value, DeclContext *outerDC,
1555+ Type type , Type baseObjTy, ValueDecl *value, DeclContext *outerDC,
15561556 ConstraintLocator *locator, bool hasAppliedSelf, bool isDynamicLookup,
15571557 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-
15861558 // Cope with dynamic 'Self'.
15871559 if (outerDC->getSelfClassDecl ()) {
15881560 if (type->hasDynamicSelfType ()) {
@@ -1657,6 +1629,26 @@ Type ConstraintSystem::getMemberReferenceTypeFromOpenedType(
16571629 return type;
16581630}
16591631
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+
16601652DeclReferenceType ConstraintSystem::getTypeOfMemberReference (
16611653 Type baseTy, ValueDecl *value, DeclContext *useDC, bool isDynamicLookup,
16621654 FunctionRefInfo functionRefInfo, ConstraintLocator *locator,
@@ -1954,6 +1946,14 @@ DeclReferenceType ConstraintSystem::getTypeOfMemberReference(
19541946 origFnType->getParams (), resultTy, origFnType->getExtInfo ());
19551947 }
19561948
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+
19571957 // Compute the type of the reference.
19581958 Type type = getMemberReferenceTypeFromOpenedType (
19591959 openedType, baseObjTy, value, outerDC, locator, hasAppliedSelf,
0 commit comments