@@ -1946,16 +1946,17 @@ Type ConstraintSystem::getEffectiveOverloadType(ConstraintLocator *locator,
19461946 if (!allowMembers)
19471947 return Type ();
19481948
1949- const auto withDynamicSelfResultReplaced = [&](Type type,
1950- unsigned uncurryLevel) {
1951- const Type baseObjTy = overload.getBaseType ()
1952- ->getRValueType ()
1953- ->getMetatypeInstanceType ()
1954- ->lookThroughAllOptionalTypes ();
1955-
1956- return type->replaceCovariantResultType (
1957- getDynamicSelfReplacementType (baseObjTy, decl, locator),
1958- uncurryLevel);
1949+ auto getBaseObjectType = [&] () -> Type {
1950+ return overload.getBaseType ()
1951+ ->getRValueType ()
1952+ ->getMetatypeInstanceType ()
1953+ ->lookThroughAllOptionalTypes ();
1954+ };
1955+
1956+ auto withDynamicSelfResultReplaced = [&](Type type) {
1957+ return type->replaceDynamicSelfType (
1958+ getDynamicSelfReplacementType (
1959+ getBaseObjectType (), decl, locator));
19591960 };
19601961
19611962 SmallVector<OpenedType, 4 > emptyReplacements;
@@ -1966,8 +1967,7 @@ Type ConstraintSystem::getEffectiveOverloadType(ConstraintLocator *locator,
19661967 useDC, *this , locator))
19671968 elementTy = LValueType::get (elementTy);
19681969 else if (elementTy->hasDynamicSelfType ()) {
1969- elementTy = withDynamicSelfResultReplaced (elementTy,
1970- /* uncurryLevel=*/ 0 );
1970+ elementTy = withDynamicSelfResultReplaced (elementTy);
19711971 }
19721972
19731973 // See ConstraintSystem::resolveOverload() -- optional and dynamic
@@ -1991,7 +1991,7 @@ Type ConstraintSystem::getEffectiveOverloadType(ConstraintLocator *locator,
19911991 var, overload.getBaseType (), useDC, *this , locator)) {
19921992 type = LValueType::get (type);
19931993 } else if (type->hasDynamicSelfType ()) {
1994- type = withDynamicSelfResultReplaced (type, /* uncurryLevel= */ 0 );
1994+ type = withDynamicSelfResultReplaced (type);
19951995 }
19961996 type = adjustVarTypeForConcurrency (
19971997 type, var, useDC, GetClosureType{*this },
@@ -2008,28 +2008,35 @@ Type ConstraintSystem::getEffectiveOverloadType(ConstraintLocator *locator,
20082008 return Type ();
20092009 }
20102010
2011- // Cope with 'Self' returns.
2012- if (!decl->getDeclContext ()->getSelfProtocolDecl ()) {
2013- if (isa<AbstractFunctionDecl>(decl) &&
2014- cast<AbstractFunctionDecl>(decl)->hasDynamicSelfResult ()) {
2015- if (!overload.getBaseType ())
2011+ // Hack for constructors.
2012+ if (isa<ConstructorDecl>(decl) &&
2013+ !decl->getDeclContext ()->getSelfProtocolDecl ()) {
2014+ if (!overload.getBaseType ())
2015+ return Type ();
2016+
2017+ if (!overload.getBaseType ()->getOptionalObjectType ()) {
2018+ // `Int??(0)` if we look through all optional types for `Self`
2019+ // we'll end up with incorrect type `Int?` for result because
2020+ // the actual result type is `Int??`.
2021+ if (overload.getBaseType ()
2022+ ->getRValueType ()
2023+ ->getMetatypeInstanceType ()
2024+ ->getOptionalObjectType ())
20162025 return Type ();
20172026
2018- if (!overload.getBaseType ()->getOptionalObjectType ()) {
2019- // `Int??(0)` if we look through all optional types for `Self`
2020- // we'll end up with incorrect type `Int?` for result because
2021- // the actual result type is `Int??`.
2022- if (isa<ConstructorDecl>(decl) && overload.getBaseType ()
2023- ->getRValueType ()
2024- ->getMetatypeInstanceType ()
2025- ->getOptionalObjectType ())
2026- return Type ();
2027-
2028- type = withDynamicSelfResultReplaced (type, /* uncurryLevel=*/ 2 );
2029- }
2027+ type = type->replaceCovariantResultType (
2028+ getBaseObjectType (), /* uncurryLevel=*/ 2 );
20302029 }
20312030 }
20322031
2032+ // Cope with 'Self' returns.
2033+ if (type->hasDynamicSelfType ()) {
2034+ if (!overload.getBaseType ())
2035+ return Type ();
2036+
2037+ type = withDynamicSelfResultReplaced (type);
2038+ }
2039+
20332040 auto hasAppliedSelf =
20342041 doesMemberRefApplyCurriedSelf (overload.getBaseType (), decl);
20352042 unsigned numApplies = getNumApplications (hasAppliedSelf,
0 commit comments