Skip to content

Commit 45e3f1b

Browse files
committed
AST: Add TypeBase::eraseDynamicSelfType()
1 parent af3b5e8 commit 45e3f1b

File tree

2 files changed

+25
-6
lines changed

2 files changed

+25
-6
lines changed

include/swift/AST/Types.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1343,6 +1343,10 @@ class alignas(1 << TypeAlignInBits) TypeBase
13431343
/// argument labels removed.
13441344
Type removeArgumentLabels(unsigned numArgumentLabels);
13451345

1346+
/// Replace DynamicSelfType anywhere it appears in covariant position with
1347+
/// its underlying Self type.
1348+
Type eraseDynamicSelfType();
1349+
13461350
/// Replace DynamicSelfType anywhere it appears in covariant position with
13471351
/// the given type.
13481352
Type replaceDynamicSelfType(Type newSelfType);

lib/AST/Type.cpp

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1342,19 +1342,34 @@ Type TypeBase::removeArgumentLabels(unsigned numArgumentLabels) {
13421342
return FunctionType::get(unlabeledParams, result, fnType->getExtInfo());
13431343
}
13441344

1345+
Type TypeBase::eraseDynamicSelfType() {
1346+
if (!hasDynamicSelfType())
1347+
return Type(this);
1348+
1349+
return Type(this).transformWithPosition(
1350+
TypePosition::Covariant,
1351+
[&](TypeBase *t, TypePosition pos) -> std::optional<Type> {
1352+
if (isa<DynamicSelfType>(t) &&
1353+
pos == TypePosition::Covariant) {
1354+
return cast<DynamicSelfType>(t)->getSelfType();
1355+
}
1356+
return std::nullopt;
1357+
});
1358+
}
1359+
13451360
Type TypeBase::replaceDynamicSelfType(Type newSelfType) {
13461361
if (!hasDynamicSelfType())
13471362
return Type(this);
13481363

13491364
return Type(this).transformWithPosition(
13501365
TypePosition::Covariant,
13511366
[&](TypeBase *t, TypePosition pos) -> std::optional<Type> {
1352-
if (isa<DynamicSelfType>(t) &&
1353-
pos == TypePosition::Covariant) {
1354-
return newSelfType;
1355-
}
1356-
return std::nullopt;
1357-
});
1367+
if (isa<DynamicSelfType>(t) &&
1368+
pos == TypePosition::Covariant) {
1369+
return newSelfType;
1370+
}
1371+
return std::nullopt;
1372+
});
13581373
}
13591374

13601375
Type TypeBase::withCovariantResultType() {

0 commit comments

Comments
 (0)