|
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,63 @@ 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 | + bool shouldDesugarTypeAliases() const { |
| 1245 | + return true; |
| 1246 | + } |
| 1247 | +}; |
| 1248 | + |
| 1249 | +} |
| 1250 | + |
1202 | 1251 | Type ConstraintSystem::openType(Type type, OpenedTypeMap &replacements, |
1203 | 1252 | ConstraintLocatorBuilder locator) { |
1204 | 1253 | assert(!type->hasUnboundGenericType()); |
1205 | 1254 |
|
1206 | 1255 | if (!type->hasTypeParameter()) |
1207 | 1256 | return type; |
1208 | 1257 |
|
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 | | - }); |
| 1258 | + return TypeOpener(replacements, locator, *this) |
| 1259 | + .doIt(type, TypePosition::Invariant); |
1245 | 1260 | } |
1246 | 1261 |
|
1247 | 1262 | Type ConstraintSystem::openPackExpansionType(PackExpansionType *expansion, |
|
0 commit comments