@@ -1403,23 +1403,25 @@ namespace {
14031403 // special handling.
14041404 if (baseExpr) {
14051405 if (auto *fnDecl = dyn_cast<AbstractFunctionDecl>(declOrClosure)) {
1406- if (fnDecl->hasDynamicSelfResult ()) {
1407- Type convTy;
1408-
1409- if (cs.getType (baseExpr)->hasOpenedExistential ()) {
1410- // FIXME: Sometimes we need to convert to an opened existential
1411- // first, because CovariantReturnConversionExpr does not support
1412- // direct conversions from a class C to an existential C & P.
1413- convTy = cs.getType (baseExpr)->getMetatypeInstanceType ();
1414- convTy =
1415- thunkTy->getResult ()->replaceCovariantResultType (convTy, 0 );
1416- } else {
1417- convTy = thunkTy->getResult ();
1418- }
1406+ if (fnDecl->getDeclContext ()->getSelfClassDecl ()) {
1407+ if (fnDecl->hasDynamicSelfResult ()) {
1408+ Type convTy;
1409+
1410+ if (cs.getType (baseExpr)->hasOpenedExistential ()) {
1411+ // FIXME: Sometimes we need to convert to an opened existential
1412+ // first, because CovariantReturnConversionExpr does not support
1413+ // direct conversions from a class C to an existential C & P.
1414+ convTy = cs.getType (baseExpr)->getMetatypeInstanceType ();
1415+ if (thunkTy->getResult ()->getOptionalObjectType ())
1416+ convTy = OptionalType::get (thunkTy);
1417+ } else {
1418+ convTy = thunkTy->getResult ();
1419+ }
14191420
1420- if (!thunkBody->getType ()->isEqual (convTy)) {
1421- thunkBody = cs.cacheType (
1422- new (ctx) CovariantReturnConversionExpr (thunkBody, convTy));
1421+ if (!thunkBody->getType ()->isEqual (convTy)) {
1422+ thunkBody = cs.cacheType (
1423+ new (ctx) CovariantReturnConversionExpr (thunkBody, convTy));
1424+ }
14231425 }
14241426 }
14251427 }
@@ -1964,9 +1966,10 @@ namespace {
19641966
19651967 auto *func = dyn_cast<FuncDecl>(member);
19661968 if (func && func->getResultInterfaceType ()->hasDynamicSelfType ()) {
1967- refTy = refTy->replaceCovariantResultType (containerTy, 2 );
1968- adjustedRefTy = adjustedRefTy->replaceCovariantResultType (
1969- containerTy, 2 );
1969+ ASSERT (refTy->hasDynamicSelfType ());
1970+ refTy = refTy->replaceDynamicSelfType (containerTy);
1971+ adjustedRefTy = adjustedRefTy->replaceDynamicSelfType (
1972+ containerTy);
19701973 }
19711974
19721975 // Handle all other references.
@@ -2110,10 +2113,9 @@ namespace {
21102113 //
21112114 // Note: For unbound references this is handled inside the thunk.
21122115 if (!isUnboundInstanceMember &&
2113- ! member->getDeclContext ()->getSelfProtocolDecl ()) {
2116+ member->getDeclContext ()->getSelfClassDecl ()) {
21142117 if (auto func = dyn_cast<AbstractFunctionDecl>(member)) {
2115- if (func->hasDynamicSelfResult () &&
2116- !baseTy->getOptionalObjectType ()) {
2118+ if (func->hasDynamicSelfResult ()) {
21172119 // FIXME: Once CovariantReturnConversionExpr (unchecked_ref_cast)
21182120 // supports a class existential dest., consider using the opened
21192121 // type directly to avoid recomputing the 'Self' replacement and
@@ -2543,9 +2545,9 @@ namespace {
25432545 new (ctx) OtherConstructorDeclRefExpr (ref, loc, implicit, resultTy));
25442546
25452547 // Wrap in covariant `Self` return if needed.
2546- if (selfTy-> hasReferenceSemantics ()) {
2547- auto covariantTy = resultTy->replaceCovariantResultType (
2548- cs.getType (base)->getWithoutSpecifierType (), 2 );
2548+ if (ref. getDecl ()-> getDeclContext ()-> getSelfClassDecl ()) {
2549+ auto covariantTy = resultTy->withCovariantResultType ()
2550+ -> replaceDynamicSelfType ( cs.getType (base)->getWithoutSpecifierType ());
25492551 if (!covariantTy->isEqual (resultTy))
25502552 ctorRef = cs.cacheType (
25512553 new (ctx) CovariantFunctionConversionExpr (ctorRef, covariantTy));
0 commit comments