Skip to content

Commit 8007295

Browse files
committed
AST: Introduce TypeBase::replaceDynamicSelfType()
1 parent a399c46 commit 8007295

File tree

2 files changed

+20
-10
lines changed

2 files changed

+20
-10
lines changed

include/swift/AST/Types.h

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1337,16 +1337,11 @@ class alignas(1 << TypeAlignInBits) TypeBase
13371337
/// argument labels removed.
13381338
Type removeArgumentLabels(unsigned numArgumentLabels);
13391339

1340-
/// Replace the base type of the result type of the given function
1341-
/// type with a new result type, as per a DynamicSelf or other
1342-
/// covariant return transformation. The optionality of the
1343-
/// existing result will be preserved.
1344-
///
1345-
/// \param newResultType The new result type.
1346-
///
1347-
/// \param uncurryLevel The number of uncurry levels to apply before
1348-
/// replacing the type. With uncurry level == 0, this simply
1349-
/// replaces the current type with the new result type.
1340+
/// Replace DynamicSelfType anywhere it appears in covariant position with
1341+
/// the given type.
1342+
Type replaceDynamicSelfType(Type newSelfType);
1343+
1344+
/// Deprecated in favor of the above.
13501345
Type replaceCovariantResultType(Type newResultType,
13511346
unsigned uncurryLevel);
13521347

lib/AST/Type.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1329,6 +1329,21 @@ Type TypeBase::removeArgumentLabels(unsigned numArgumentLabels) {
13291329
return FunctionType::get(unlabeledParams, result, fnType->getExtInfo());
13301330
}
13311331

1332+
Type TypeBase::replaceDynamicSelfType(Type newSelfType) {
1333+
if (!hasDynamicSelfType())
1334+
return Type(this);
1335+
1336+
return Type(this).transformWithPosition(
1337+
TypePosition::Covariant,
1338+
[&](TypeBase *t, TypePosition pos) -> std::optional<Type> {
1339+
if (isa<DynamicSelfType>(t) &&
1340+
pos == TypePosition::Covariant) {
1341+
return newSelfType;
1342+
}
1343+
return std::nullopt;
1344+
});
1345+
}
1346+
13321347
Type TypeBase::replaceCovariantResultType(Type newResultType,
13331348
unsigned uncurryLevel) {
13341349
if (uncurryLevel == 0) {

0 commit comments

Comments
 (0)