Skip to content

Commit ae99651

Browse files
committed
Sema: Replace calls to subst() on a GenericFunctionType with substGenericArgs()
1 parent 9f8ce05 commit ae99651

File tree

6 files changed

+43
-22
lines changed

6 files changed

+43
-22
lines changed

lib/Sema/CodeSynthesis.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -666,7 +666,9 @@ synthesizeDesignatedInitOverride(AbstractFunctionDecl *fn, void *context) {
666666
.subst(subs);
667667
ConcreteDeclRef ctorRef(superclassCtor, subs);
668668

669-
auto type = superclassCtor->getInitializerInterfaceType().subst(subs);
669+
auto type = superclassCtor->getInitializerInterfaceType();
670+
if (auto *genericFnType = type->getAs<GenericFunctionType>())
671+
type = genericFnType->substGenericArgs(subs);
670672
auto *ctorRefExpr =
671673
new (ctx) OtherConstructorDeclRefExpr(ctorRef, DeclNameLoc(),
672674
IsImplicit, type);

lib/Sema/DerivedConformanceEquatableHashable.cpp

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -829,23 +829,20 @@ deriveBodyHashable_hashValue(AbstractFunctionDecl *hashValueDecl, void *) {
829829
auto substitutions = SubstitutionMap::get(
830830
hashFunc->getGenericSignature(),
831831
[&](SubstitutableType *dependentType) {
832-
if (auto gp = dyn_cast<GenericTypeParamType>(dependentType)) {
833-
if (gp->getDepth() == 0 && gp->getIndex() == 0)
834-
return selfType;
835-
}
836-
837-
return Type(dependentType);
832+
auto gp = cast<GenericTypeParamType>(dependentType);
833+
ASSERT(gp->getDepth() == 0 && gp->getIndex() == 0);
834+
return selfType;
838835
},
839836
LookUpConformanceInModule());
840837
ConcreteDeclRef hashFuncRef(hashFunc, substitutions);
841838

842-
Type hashFuncType = hashFunc->getInterfaceType().subst(substitutions);
839+
auto *hashFuncType = hashFunc->getInterfaceType()->castTo<GenericFunctionType>()
840+
->substGenericArgs(substitutions);
843841
auto hashExpr = new (C) DeclRefExpr(hashFuncRef, DeclNameLoc(),
844842
/*implicit*/ true,
845843
AccessSemantics::Ordinary,
846844
hashFuncType);
847-
Type hashFuncResultType =
848-
hashFuncType->castTo<AnyFunctionType>()->getResult();
845+
Type hashFuncResultType = hashFuncType->getResult();
849846
auto *argList = ArgumentList::forImplicitSingle(C, C.Id_for, selfRef);
850847
auto *callExpr = CallExpr::createImplicit(C, hashExpr, argList);
851848
callExpr->setType(hashFuncResultType);

lib/Sema/TypeCheckEffects.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1731,8 +1731,12 @@ class ApplyClassifier {
17311731
}
17321732

17331733
// Use the most significant result from the arguments.
1734-
auto *fnSubstType = fnInterfaceType.subst(fnRef.getSubstitutions())
1735-
->getAs<AnyFunctionType>();
1734+
FunctionType *fnSubstType = nullptr;
1735+
if (auto *fnGenericType = fnInterfaceType->getAs<GenericFunctionType>())
1736+
fnSubstType = fnGenericType->substGenericArgs(fnRef.getSubstitutions());
1737+
else
1738+
fnSubstType = fnInterfaceType->getAs<FunctionType>();
1739+
17361740
if (!fnSubstType) {
17371741
result.merge(Classification::forInvalidCode());
17381742
return;

lib/Sema/TypeCheckProtocol.cpp

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3856,15 +3856,22 @@ filterProtocolRequirements(
38563856

38573857
auto OverloadTy = Req->getOverloadSignatureType();
38583858
if (OverloadTy) {
3859-
OverloadTy =
3860-
OverloadTy.subst(getProtocolSubstitutionMap(Req))->getCanonicalType();
3859+
auto Subs = getProtocolSubstitutionMap(Req);
3860+
// FIXME: This is wrong if the overload has its own generic parameters
3861+
if (auto GenericFnTy = dyn_cast<GenericFunctionType>(OverloadTy))
3862+
OverloadTy = GenericFnTy.substGenericArgs(Subs);
3863+
else
3864+
OverloadTy = OverloadTy.subst(Subs)->getCanonicalType();
38613865
}
38623866
if (llvm::any_of(DeclsByName[Req->getName()], [&](ValueDecl *OtherReq) {
38633867
auto OtherOverloadTy = OtherReq->getOverloadSignatureType();
38643868
if (OtherOverloadTy) {
3865-
OtherOverloadTy =
3866-
OtherOverloadTy.subst(getProtocolSubstitutionMap(OtherReq))
3867-
->getCanonicalType();
3869+
auto Subs = getProtocolSubstitutionMap(OtherReq);
3870+
// FIXME: This is wrong if the overload has its own generic parameters
3871+
if (auto GenericFnTy = dyn_cast<GenericFunctionType>(OtherOverloadTy))
3872+
OtherOverloadTy = GenericFnTy.substGenericArgs(Subs);
3873+
else
3874+
OtherOverloadTy = OtherOverloadTy.subst(Subs)->getCanonicalType();
38683875
}
38693876
return conflicting(Req->getASTContext(), Req->getOverloadSignature(),
38703877
OverloadTy, OtherReq->getOverloadSignature(),
@@ -7401,7 +7408,10 @@ bool swift::forEachConformance(
74017408
if (forEachConformance(subs, body, visitedConformances))
74027409
return true;
74037410

7404-
type = type.subst(subs);
7411+
if (auto *genericFnType = type->getAs<GenericFunctionType>())
7412+
type = genericFnType->substGenericArgs(subs);
7413+
else
7414+
type = type.subst(subs);
74057415
}
74067416

74077417
if (forEachConformance(type, body, visitedConformances))

lib/Sema/TypeCheckStorage.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1910,7 +1910,9 @@ synthesizeObservedSetterBody(AccessorDecl *Set, TargetImpl target,
19101910

19111911
auto callObserver = [&](AccessorDecl *observer, VarDecl *arg) {
19121912
ConcreteDeclRef ref(observer, subs);
1913-
auto type = observer->getInterfaceType().subst(subs);
1913+
auto type = observer->getInterfaceType();
1914+
if (auto *genericFnType = type->getAs<GenericFunctionType>())
1915+
type = genericFnType->substGenericArgs(subs);
19141916
Expr *Callee = new (Ctx) DeclRefExpr(ref, DeclNameLoc(), /*imp*/true);
19151917
Callee->setType(type);
19161918

@@ -2101,7 +2103,9 @@ synthesizeModifyCoroutineBodyWithSimpleDidSet(AccessorDecl *accessor,
21012103

21022104
auto callDidSet = [&]() {
21032105
ConcreteDeclRef ref(DidSet, subs);
2104-
auto type = DidSet->getInterfaceType().subst(subs);
2106+
auto type = DidSet->getInterfaceType();
2107+
if (auto *genericFnType = type->getAs<GenericFunctionType>())
2108+
type = genericFnType->substGenericArgs(subs);
21052109
Expr *Callee = new (ctx) DeclRefExpr(ref, DeclNameLoc(), /*imp*/ true);
21062110
Callee->setType(type);
21072111

lib/Sema/TypeCheckUnsafe.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,8 +208,12 @@ bool swift::enumerateUnsafeUses(ConcreteDeclRef declRef,
208208
auto subs = declRef.getSubstitutions();
209209
{
210210
auto type = decl->getInterfaceType();
211-
if (subs)
212-
type = type.subst(subs);
211+
if (subs) {
212+
if (auto *genericFnType = type->getAs<GenericFunctionType>())
213+
type = genericFnType->substGenericArgs(subs);
214+
else
215+
type = type.subst(subs);
216+
}
213217

214218
bool shouldReturnTrue = false;
215219
diagnoseUnsafeType(ctx, loc, type, [&](Type unsafeType) {

0 commit comments

Comments
 (0)