Skip to content

Commit a983323

Browse files
authored
Merge pull request swiftlang#83768 from slavapestov/replace-covariant-result-type
Sema: Remove a bunch of usages of replaceCovariantResultType()
2 parents a482f37 + d93b120 commit a983323

File tree

9 files changed

+82
-80
lines changed

9 files changed

+82
-80
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

include/swift/Sema/ConstraintSystem.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4474,8 +4474,7 @@ class ConstraintSystem {
44744474
Type getUnopenedTypeOfReference(VarDecl *value, Type baseType,
44754475
DeclContext *UseDC,
44764476
ConstraintLocator *locator,
4477-
bool wantInterfaceType = false,
4478-
bool adjustForPreconcurrency = true);
4477+
bool wantInterfaceType);
44794478

44804479
/// Given the opened type and a pile of information about a member reference,
44814480
/// determine the reference type of the member reference.

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) {

lib/AST/TypeSubstitution.cpp

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -867,33 +867,26 @@ Type TypeBase::getTypeOfMember(const ValueDecl *member,
867867

868868
Type TypeBase::adjustSuperclassMemberDeclType(const ValueDecl *baseDecl,
869869
const ValueDecl *derivedDecl,
870-
Type memberType) {
870+
Type type) {
871871
auto subs = SubstitutionMap::getOverrideSubstitutions(
872872
baseDecl, derivedDecl);
873873

874-
if (auto *genericMemberType = memberType->getAs<GenericFunctionType>()) {
875-
memberType = FunctionType::get(genericMemberType->getParams(),
876-
genericMemberType->getResult(),
877-
genericMemberType->getExtInfo());
874+
if (auto *genericMemberType = type->getAs<GenericFunctionType>()) {
875+
type = genericMemberType->substGenericArgs(subs);
876+
} else {
877+
type = type.subst(subs);
878878
}
879879

880-
auto type = memberType.subst(subs);
881880
if (baseDecl->getDeclContext()->getSelfProtocolDecl())
882881
return type;
883882

884883
if (auto *afd = dyn_cast<AbstractFunctionDecl>(baseDecl)) {
885884
type = type->replaceSelfParameterType(this);
886-
if (afd->hasDynamicSelfResult())
887-
type = type->replaceCovariantResultType(this, /*uncurryLevel=*/2);
888-
} else if (auto *sd = dyn_cast<SubscriptDecl>(baseDecl)) {
889-
if (sd->getElementInterfaceType()->hasDynamicSelfType())
890-
type = type->replaceCovariantResultType(this, /*uncurryLevel=*/1);
891-
} else if (auto *vd = dyn_cast<VarDecl>(baseDecl)) {
892-
if (vd->getValueInterfaceType()->hasDynamicSelfType())
893-
type = type->replaceCovariantResultType(this, /*uncurryLevel=*/0);
885+
if (isa<ConstructorDecl>(afd))
886+
return type->replaceCovariantResultType(this, /*uncurryLevel=*/2);
894887
}
895888

896-
return type;
889+
return type->replaceDynamicSelfType(this);
897890
}
898891

899892
//===----------------------------------------------------------------------===//

lib/SIL/Verifier/SILVerifier.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4286,13 +4286,16 @@ class SILVerifier : public SILVerifierBase<SILVerifier> {
42864286
if (fnDecl->hasDynamicSelfResult()) {
42874287
auto anyObjectTy = C.getAnyObjectType();
42884288
for (auto &result : results) {
4289-
auto newResultTy =
4289+
auto resultTy =
42904290
result
42914291
.getReturnValueType(F.getModule(), methodTy,
4292-
F.getTypeExpansionContext())
4293-
->replaceCovariantResultType(anyObjectTy, 0);
4294-
result = SILResultInfo(newResultTy->getCanonicalType(),
4295-
result.getConvention());
4292+
F.getTypeExpansionContext());
4293+
if (resultTy->getOptionalObjectType())
4294+
resultTy = OptionalType::get(anyObjectTy)->getCanonicalType();
4295+
else
4296+
resultTy = anyObjectTy;
4297+
4298+
result = SILResultInfo(resultTy, result.getConvention());
42964299
}
42974300
}
42984301
}

lib/SILGen/SILGenApply.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -148,10 +148,10 @@ getPartialApplyOfDynamicMethodFormalType(SILGenModule &SGM, SILDeclRef member,
148148

149149
// Adjust the result type to replace dynamic-self with AnyObject.
150150
CanType resultType = completeMethodTy.getResult();
151-
if (auto fnDecl = dyn_cast<FuncDecl>(member.getDecl())) {
152-
if (fnDecl->hasDynamicSelfResult()) {
151+
if (isa<FuncDecl>(member.getDecl())) {
152+
if (resultType->hasDynamicSelfType()) {
153153
auto anyObjectTy = SGM.getASTContext().getAnyObjectType();
154-
resultType = resultType->replaceCovariantResultType(anyObjectTy, 0)
154+
resultType = resultType->replaceDynamicSelfType(anyObjectTy)
155155
->getCanonicalType();
156156
}
157157
}

lib/Sema/CSApply.cpp

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1921,18 +1921,17 @@ namespace {
19211921
base->setImplicit();
19221922
}
19231923

1924-
const auto hasDynamicSelf =
1925-
varDecl->getValueInterfaceType()->hasDynamicSelfType();
1924+
const auto hasDynamicSelf = refTy->hasDynamicSelfType();
19261925

19271926
auto memberRefExpr
19281927
= new (ctx) MemberRefExpr(base, dotLoc, memberRef,
19291928
memberLoc, Implicit, semantics);
19301929
memberRefExpr->setIsSuper(isSuper);
19311930

19321931
if (hasDynamicSelf) {
1933-
refTy = refTy->replaceCovariantResultType(containerTy, 1);
1934-
adjustedRefTy = adjustedRefTy->replaceCovariantResultType(
1935-
containerTy, 1);
1932+
refTy = refTy->replaceDynamicSelfType(containerTy);
1933+
adjustedRefTy = adjustedRefTy->replaceDynamicSelfType(
1934+
containerTy);
19361935
}
19371936

19381937
cs.setType(memberRefExpr, resultType(refTy));
@@ -2482,19 +2481,21 @@ namespace {
24822481
if (!base)
24832482
return nullptr;
24842483

2485-
const auto hasDynamicSelf =
2486-
subscript->getElementInterfaceType()->hasDynamicSelfType();
2484+
bool hasDynamicSelf = fullSubscriptTy->hasDynamicSelfType();
24872485

24882486
// Form the subscript expression.
24892487
auto subscriptExpr = SubscriptExpr::create(
24902488
ctx, base, args, subscriptRef, isImplicit, semantics);
24912489
cs.setType(subscriptExpr, fullSubscriptTy->getResult());
24922490
subscriptExpr->setIsSuper(isSuper);
2493-
cs.setType(subscriptExpr,
2494-
hasDynamicSelf
2495-
? fullSubscriptTy->getResult()->replaceCovariantResultType(
2496-
containerTy, 0)
2497-
: fullSubscriptTy->getResult());
2491+
2492+
if (!hasDynamicSelf) {
2493+
cs.setType(subscriptExpr, fullSubscriptTy->getResult());
2494+
} else {
2495+
cs.setType(subscriptExpr,
2496+
fullSubscriptTy->getResult()
2497+
->replaceDynamicSelfType(containerTy));
2498+
}
24982499

24992500
Expr *result = subscriptExpr;
25002501
closeExistentials(result, locator);

lib/Sema/TypeCheckConcurrency.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7908,6 +7908,10 @@ AnyFunctionType *swift::adjustFunctionTypeForConcurrency(
79087908
(isa<AbstractFunctionDecl>(decl) &&
79097909
cast<AbstractFunctionDecl>(decl)->hasImplicitSelfDecl()));
79107910
if (!hasImplicitSelfDecl) {
7911+
// Fast path.
7912+
if (fnType->getExtInfo().getIsolation() == *funcIsolation)
7913+
return fnType;
7914+
79117915
return fnType->withExtInfo(
79127916
fnType->getExtInfo().withIsolation(*funcIsolation));
79137917
}
@@ -7917,6 +7921,10 @@ AnyFunctionType *swift::adjustFunctionTypeForConcurrency(
79177921
if (!innerFnType)
79187922
return fnType;
79197923

7924+
// Fast path.
7925+
if (innerFnType->getExtInfo().getIsolation() == *funcIsolation)
7926+
return fnType;
7927+
79207928
// Update the inner function type with the isolation.
79217929
innerFnType = innerFnType->withExtInfo(
79227930
innerFnType->getExtInfo().withIsolation(*funcIsolation));

lib/Sema/TypeOfReference.cpp

Lines changed: 21 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -651,8 +651,7 @@ ClosureIsolatedByPreconcurrency::operator()(const ClosureExpr *expr) const {
651651

652652
Type ConstraintSystem::getUnopenedTypeOfReference(
653653
VarDecl *value, Type baseType, DeclContext *UseDC,
654-
ConstraintLocator *locator, bool wantInterfaceType,
655-
bool adjustForPreconcurrency) {
654+
ConstraintLocator *locator, bool wantInterfaceType) {
656655
Type requestedType;
657656
if (Type type = getTypeIfAvailable(value)) {
658657
requestedType = type;
@@ -671,14 +670,6 @@ Type ConstraintSystem::getUnopenedTypeOfReference(
671670
if (auto *expansion = requestedType->getAs<PackExpansionType>())
672671
requestedType = expansion->getPatternType();
673672

674-
// Adjust the type for concurrency if requested.
675-
if (adjustForPreconcurrency) {
676-
requestedType = adjustVarTypeForConcurrency(
677-
requestedType, value, UseDC,
678-
GetClosureType{*this},
679-
ClosureIsolatedByPreconcurrency{*this});
680-
}
681-
682673
// If we're dealing with contextual types, and we referenced this type from
683674
// a different context, map the type.
684675
if (!wantInterfaceType && requestedType->hasArchetype()) {
@@ -1019,11 +1010,11 @@ ConstraintSystem::getTypeOfReference(ValueDecl *value,
10191010

10201011
// If this is a method whose result type is dynamic Self, replace
10211012
// DynamicSelf with the actual object type.
1022-
if (func->getResultInterfaceType()->hasDynamicSelfType()) {
1013+
if (openedType->hasDynamicSelfType()) {
10231014
auto params = openedType->getParams();
10241015
assert(params.size() == 1);
10251016
Type selfTy = params.front().getPlainType()->getMetatypeInstanceType();
1026-
openedType = openedType->replaceCovariantResultType(selfTy, 2)
1017+
openedType = openedType->replaceDynamicSelfType(selfTy)
10271018
->castTo<FunctionType>();
10281019
}
10291020

@@ -1057,15 +1048,6 @@ ConstraintSystem::getTypeOfReference(ValueDecl *value,
10571048
*this, funcDecl, functionRefInfo, openedType->castTo<FunctionType>(),
10581049
locator, preparedOverload);
10591050

1060-
auto origOpenedType = openedType;
1061-
if (!isRequirementOrWitness(locator)) {
1062-
unsigned numApplies = getNumApplications(/*hasAppliedSelf*/ false,
1063-
functionRefInfo);
1064-
openedType = adjustFunctionTypeForConcurrency(
1065-
origOpenedType->castTo<FunctionType>(), /*baseType=*/Type(), funcDecl,
1066-
useDC, numApplies, false, replacements, locator, preparedOverload);
1067-
}
1068-
10691051
if (isForCodeCompletion() && openedType->hasError()) {
10701052
// In code completion, replace error types by placeholder types so we can
10711053
// match the types we know instead of bailing out completely.
@@ -1076,6 +1058,15 @@ ConstraintSystem::getTypeOfReference(ValueDecl *value,
10761058
// If we opened up any type variables, record the replacements.
10771059
recordOpenedTypes(locator, replacements, preparedOverload);
10781060

1061+
auto origOpenedType = openedType;
1062+
if (!isRequirementOrWitness(locator)) {
1063+
unsigned numApplies = getNumApplications(/*hasAppliedSelf*/ false,
1064+
functionRefInfo);
1065+
openedType = adjustFunctionTypeForConcurrency(
1066+
origOpenedType->castTo<FunctionType>(), /*baseType=*/Type(), funcDecl,
1067+
useDC, numApplies, false, replacements, locator, preparedOverload);
1068+
}
1069+
10791070
return { origOpenedType, openedType, origOpenedType, openedType, Type() };
10801071
}
10811072

@@ -1128,12 +1119,16 @@ ConstraintSystem::getTypeOfReference(ValueDecl *value,
11281119
auto *varDecl = cast<VarDecl>(value);
11291120

11301121
// Determine the type of the value, opening up that type if necessary.
1131-
// FIXME: @preconcurrency
11321122
bool wantInterfaceType = !varDecl->getDeclContext()->isLocalContext();
11331123
Type valueType =
11341124
getUnopenedTypeOfReference(varDecl, Type(), useDC,
11351125
getConstraintLocator(locator),
11361126
wantInterfaceType);
1127+
// FIXME: Adjust the type for concurrency if requested.
1128+
valueType = adjustVarTypeForConcurrency(
1129+
valueType, varDecl, useDC,
1130+
GetClosureType{*this},
1131+
ClosureIsolatedByPreconcurrency{*this});
11371132

11381133
Type thrownErrorType;
11391134
if (auto accessor = varDecl->getEffectfulGetAccessor()) {
@@ -1476,19 +1471,13 @@ Type ConstraintSystem::getMemberReferenceTypeFromOpenedType(
14761471
getDynamicSelfReplacementType(baseObjTy, value, locator);
14771472

14781473
if (auto func = dyn_cast<AbstractFunctionDecl>(value)) {
1479-
if (func->hasDynamicSelfResult() &&
1474+
if (isa<ConstructorDecl>(func) &&
14801475
!baseObjTy->getOptionalObjectType()) {
14811476
type = type->replaceCovariantResultType(replacementTy, 2);
14821477
}
1483-
} else if (auto *decl = dyn_cast<SubscriptDecl>(value)) {
1484-
if (decl->getElementInterfaceType()->hasDynamicSelfType()) {
1485-
type = type->replaceCovariantResultType(replacementTy, 2);
1486-
}
1487-
} else if (auto *decl = dyn_cast<VarDecl>(value)) {
1488-
if (decl->getValueInterfaceType()->hasDynamicSelfType()) {
1489-
type = type->replaceCovariantResultType(replacementTy, 1);
1490-
}
14911478
}
1479+
1480+
type = type->replaceDynamicSelfType(replacementTy);
14921481
}
14931482

14941483
// Check if we need to apply a layer of optionality to the uncurried type.
@@ -1724,8 +1713,7 @@ DeclReferenceType ConstraintSystem::getTypeOfMemberReference(
17241713

17251714
refType = getUnopenedTypeOfReference(cast<VarDecl>(value), baseTy, useDC,
17261715
locator,
1727-
/*wantInterfaceType=*/true,
1728-
/*adjustForPreconcurrency=*/false);
1716+
/*wantInterfaceType=*/true);
17291717
}
17301718

17311719
auto selfTy = outerDC->getSelfInterfaceType();

0 commit comments

Comments
 (0)