Skip to content

Commit 9e0dbb9

Browse files
committed
AST: Add TypeBase::withCovariantReturnType()
1 parent 5eb727b commit 9e0dbb9

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

include/swift/AST/Types.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1344,6 +1344,9 @@ class alignas(1 << TypeAlignInBits) TypeBase
13441344
/// the given type.
13451345
Type replaceDynamicSelfType(Type newSelfType);
13461346

1347+
/// Hack to deal with ConstructorDecl interface types.
1348+
Type withCovariantResultType();
1349+
13471350
/// Deprecated in favor of the above.
13481351
Type replaceCovariantResultType(Type newResultType,
13491352
unsigned uncurryLevel);

lib/AST/Type.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
13601396
Type TypeBase::replaceCovariantResultType(Type newResultType,
13611397
unsigned uncurryLevel) {
13621398
if (uncurryLevel == 0) {

0 commit comments

Comments
 (0)