|
31 | 31 | #include "swift/AST/ParameterList.h"
|
32 | 32 | #include "swift/AST/ProtocolConformance.h"
|
33 | 33 | #include "swift/AST/TypeCheckRequests.h"
|
| 34 | +#include "swift/AST/TypeTransform.h" |
34 | 35 | #include "swift/Basic/Assertions.h"
|
35 | 36 | #include "swift/Basic/Defer.h"
|
36 | 37 | #include "swift/Basic/Statistic.h"
|
@@ -1199,49 +1200,59 @@ Type ConstraintSystem::replaceInferableTypesWithTypeVars(
|
1199 | 1200 | return type;
|
1200 | 1201 | }
|
1201 | 1202 |
|
| 1203 | +namespace { |
| 1204 | + |
| 1205 | +struct TypeOpener : public TypeTransform<TypeOpener> { |
| 1206 | + OpenedTypeMap &replacements; |
| 1207 | + ConstraintLocatorBuilder locator; |
| 1208 | + ConstraintSystem &cs; |
| 1209 | + |
| 1210 | + TypeOpener(OpenedTypeMap &replacements, |
| 1211 | + ConstraintLocatorBuilder locator, |
| 1212 | + ConstraintSystem &cs) |
| 1213 | + : TypeTransform<TypeOpener>(cs.getASTContext()), |
| 1214 | + replacements(replacements), locator(locator), cs(cs) {} |
| 1215 | + |
| 1216 | + std::optional<Type> transform(TypeBase *type, TypePosition pos) { |
| 1217 | + if (!type->hasTypeParameter()) |
| 1218 | + return Type(type); |
| 1219 | + |
| 1220 | + return std::nullopt; |
| 1221 | + } |
| 1222 | + |
| 1223 | + Type transformGenericTypeParamType(GenericTypeParamType *genericParam, |
| 1224 | + TypePosition pos) { |
| 1225 | + auto known = replacements.find( |
| 1226 | + cast<GenericTypeParamType>(genericParam->getCanonicalType())); |
| 1227 | + // FIXME: This should be an assert, however protocol generic signatures |
| 1228 | + // drop outer generic parameters. |
| 1229 | + // assert(known != replacements.end()); |
| 1230 | + if (known == replacements.end()) |
| 1231 | + return ErrorType::get(ctx); |
| 1232 | + return known->second; |
| 1233 | + } |
| 1234 | + |
| 1235 | + Type transformPackExpansionType(PackExpansionType *expansion, |
| 1236 | + TypePosition pos) { |
| 1237 | + return cs.openPackExpansionType(expansion, replacements, locator); |
| 1238 | + } |
| 1239 | + |
| 1240 | + bool shouldUnwrapVanishingTuples() const { |
| 1241 | + return false; |
| 1242 | + } |
| 1243 | +}; |
| 1244 | + |
| 1245 | +} |
| 1246 | + |
1202 | 1247 | Type ConstraintSystem::openType(Type type, OpenedTypeMap &replacements,
|
1203 | 1248 | ConstraintLocatorBuilder locator) {
|
1204 | 1249 | assert(!type->hasUnboundGenericType());
|
1205 | 1250 |
|
1206 | 1251 | if (!type->hasTypeParameter())
|
1207 | 1252 | return type;
|
1208 | 1253 |
|
1209 |
| - return type.transformRec([&](Type type) -> std::optional<Type> { |
1210 |
| - assert(!type->is<GenericFunctionType>()); |
1211 |
| - |
1212 |
| - // Preserve single element tuples if their element is |
1213 |
| - // pack expansion, otherwise it wouldn't be expanded. |
1214 |
| - if (auto *tuple = type->getAs<TupleType>()) { |
1215 |
| - if (tuple->getNumElements() == 1) { |
1216 |
| - const auto &elt = tuple->getElement(0); |
1217 |
| - if (!elt.hasName() && elt.getType()->is<PackExpansionType>()) { |
1218 |
| - return TupleType::get( |
1219 |
| - {openPackExpansionType( |
1220 |
| - elt.getType()->castTo<PackExpansionType>(), replacements, |
1221 |
| - locator)}, |
1222 |
| - tuple->getASTContext()); |
1223 |
| - } |
1224 |
| - } |
1225 |
| - } |
1226 |
| - |
1227 |
| - if (auto *expansion = type->getAs<PackExpansionType>()) { |
1228 |
| - return openPackExpansionType(expansion, replacements, locator); |
1229 |
| - } |
1230 |
| - |
1231 |
| - // Replace a generic type parameter with its corresponding type variable. |
1232 |
| - if (auto genericParam = type->getAs<GenericTypeParamType>()) { |
1233 |
| - auto known = replacements.find( |
1234 |
| - cast<GenericTypeParamType>(genericParam->getCanonicalType())); |
1235 |
| - // FIXME: This should be an assert, however protocol generic signatures |
1236 |
| - // drop outer generic parameters. |
1237 |
| - // assert(known != replacements.end()); |
1238 |
| - if (known == replacements.end()) |
1239 |
| - return ErrorType::get(getASTContext()); |
1240 |
| - return known->second; |
1241 |
| - } |
1242 |
| - |
1243 |
| - return std::nullopt; |
1244 |
| - }); |
| 1254 | + return TypeOpener(replacements, locator, *this) |
| 1255 | + .doIt(type, TypePosition::Invariant); |
1245 | 1256 | }
|
1246 | 1257 |
|
1247 | 1258 | Type ConstraintSystem::openPackExpansionType(PackExpansionType *expansion,
|
|
0 commit comments