@@ -1403,23 +1403,25 @@ namespace {
1403
1403
// special handling.
1404
1404
if (baseExpr) {
1405
1405
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
+ }
1419
1420
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
+ }
1423
1425
}
1424
1426
}
1425
1427
}
@@ -1964,9 +1966,10 @@ namespace {
1964
1966
1965
1967
auto *func = dyn_cast<FuncDecl>(member);
1966
1968
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);
1970
1973
}
1971
1974
1972
1975
// Handle all other references.
@@ -2110,10 +2113,9 @@ namespace {
2110
2113
//
2111
2114
// Note: For unbound references this is handled inside the thunk.
2112
2115
if (!isUnboundInstanceMember &&
2113
- ! member->getDeclContext ()->getSelfProtocolDecl ()) {
2116
+ member->getDeclContext ()->getSelfClassDecl ()) {
2114
2117
if (auto func = dyn_cast<AbstractFunctionDecl>(member)) {
2115
- if (func->hasDynamicSelfResult () &&
2116
- !baseTy->getOptionalObjectType ()) {
2118
+ if (func->hasDynamicSelfResult ()) {
2117
2119
// FIXME: Once CovariantReturnConversionExpr (unchecked_ref_cast)
2118
2120
// supports a class existential dest., consider using the opened
2119
2121
// type directly to avoid recomputing the 'Self' replacement and
@@ -2543,9 +2545,9 @@ namespace {
2543
2545
new (ctx) OtherConstructorDeclRefExpr (ref, loc, implicit, resultTy));
2544
2546
2545
2547
// 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 ());
2549
2551
if (!covariantTy->isEqual (resultTy))
2550
2552
ctorRef = cs.cacheType (
2551
2553
new (ctx) CovariantFunctionConversionExpr (ctorRef, covariantTy));
0 commit comments