@@ -1357,6 +1357,42 @@ Type TypeBase::replaceDynamicSelfType(Type newSelfType) {
13571357 });
13581358}
13591359
1360+ Type TypeBase::withCovariantResultType () {
1361+ // Unwrap the outer function type.
1362+ auto fnType = this ->castTo <AnyFunctionType>();
1363+ ASSERT (fnType->getParams ().size () == 1 );
1364+
1365+ // Unwrap the inner function type.
1366+ auto resultFnType = fnType->getResult ()->castTo <FunctionType>();
1367+ auto resultType = resultFnType->getResult ();
1368+
1369+ bool wasOptional = false ;
1370+ if (auto objectType = resultType->getOptionalObjectType ()) {
1371+ wasOptional = true ;
1372+ resultType = objectType;
1373+ }
1374+
1375+ ASSERT (resultType->getClassOrBoundGenericClass ());
1376+ resultType = DynamicSelfType::get (resultType, getASTContext ());
1377+
1378+ // Rebuild the inner function type.
1379+ if (wasOptional)
1380+ resultType = OptionalType::get (resultType);
1381+
1382+ resultFnType = FunctionType::get (resultFnType->getParams (), resultType,
1383+ resultFnType->getExtInfo ());
1384+
1385+ // Rebuild the outer function type.
1386+ if (auto genericFn = dyn_cast<GenericFunctionType>(fnType)) {
1387+ return GenericFunctionType::get (genericFn->getGenericSignature (),
1388+ fnType->getParams (), resultFnType,
1389+ fnType->getExtInfo ());
1390+ }
1391+
1392+ return FunctionType::get (fnType->getParams (), resultFnType,
1393+ fnType->getExtInfo ());
1394+ }
1395+
13601396Type TypeBase::replaceCovariantResultType (Type newResultType,
13611397 unsigned uncurryLevel) {
13621398 if (uncurryLevel == 0 ) {
0 commit comments