Skip to content

Commit ae68558

Browse files
committed
[ConstraintSystem] Generalize and extract opening function params/result into substGenericArgs
1 parent 0da4a5b commit ae68558

File tree

3 files changed

+19
-15
lines changed

3 files changed

+19
-15
lines changed

include/swift/AST/Types.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3172,6 +3172,7 @@ class GenericFunctionType final : public AnyFunctionType,
31723172
/// Substitute the given generic arguments into this generic
31733173
/// function type and return the resulting non-generic type.
31743174
FunctionType *substGenericArgs(SubstitutionMap subs);
3175+
FunctionType *substGenericArgs(llvm::function_ref<Type(Type)> substFn) const;
31753176

31763177
void Profile(llvm::FoldingSetNodeID &ID) {
31773178
Profile(ID, getGenericSignature(), getParams(), getResult(),

lib/AST/Type.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2878,6 +2878,22 @@ GenericFunctionType::substGenericArgs(SubstitutionMap subs) {
28782878
substFn->getResult(), getExtInfo());
28792879
}
28802880

2881+
FunctionType *GenericFunctionType::substGenericArgs(
2882+
llvm::function_ref<Type(Type)> substFn) const {
2883+
llvm::SmallVector<AnyFunctionType::Param, 4> params;
2884+
params.reserve(getNumParams());
2885+
2886+
llvm::transform(getParams(), std::back_inserter(params),
2887+
[&](const AnyFunctionType::Param &param) {
2888+
return param.withType(substFn(param.getPlainType()));
2889+
});
2890+
2891+
auto resultTy = substFn(getResult());
2892+
2893+
// Build the resulting (non-generic) function type.
2894+
return FunctionType::get(params, resultTy, getExtInfo());
2895+
}
2896+
28812897
CanFunctionType
28822898
CanGenericFunctionType::substGenericArgs(SubstitutionMap subs) const {
28832899
return cast<FunctionType>(

lib/Sema/ConstraintSystem.cpp

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -647,21 +647,8 @@ FunctionType *ConstraintSystem::openFunctionType(
647647
[&](Type type) -> Type { return openType(type, replacements); });
648648
}
649649

650-
// Transform the parameters and output type.
651-
llvm::SmallVector<AnyFunctionType::Param, 4> openedParams;
652-
openedParams.reserve(genericFn->getNumParams());
653-
for (const auto &param : genericFn->getParams()) {
654-
auto type = openType(param.getPlainType(), replacements);
655-
openedParams.push_back(AnyFunctionType::Param(type, param.getLabel(),
656-
param.getParameterFlags()));
657-
}
658-
659-
auto resultTy = openType(genericFn->getResult(), replacements);
660-
661-
// Build the resulting (non-generic) function type.
662-
funcType = FunctionType::get(
663-
openedParams, resultTy,
664-
FunctionType::ExtInfo().withThrows(genericFn->throws()));
650+
funcType = genericFn->substGenericArgs(
651+
[&](Type type) { return openType(type, replacements); });
665652
}
666653

667654
return funcType->castTo<FunctionType>();

0 commit comments

Comments
 (0)