Skip to content

Commit e47f996

Browse files
committed
Sema: ConstraintSystem::openType() uses TypeTransform
1 parent f3b2a79 commit e47f996

File tree

2 files changed

+47
-38
lines changed

2 files changed

+47
-38
lines changed

include/swift/Sema/ConstraintSystem.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4318,7 +4318,6 @@ class ConstraintSystem {
43184318
Type openType(Type type, OpenedTypeMap &replacements,
43194319
ConstraintLocatorBuilder locator);
43204320

4321-
private:
43224321
/// "Open" an opaque archetype type, similar to \c openType.
43234322
Type openOpaqueType(OpaqueTypeArchetypeType *type,
43244323
ConstraintLocatorBuilder locator);
@@ -4340,7 +4339,6 @@ class ConstraintSystem {
43404339
ASSERT(erased);
43414340
}
43424341

4343-
public:
43444342
/// Recurse over the given type and open any opaque archetype types.
43454343
Type openOpaqueType(Type type, ContextualTypePurpose context,
43464344
ConstraintLocatorBuilder locator);

lib/Sema/ConstraintSystem.cpp

Lines changed: 47 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include "swift/AST/ParameterList.h"
3232
#include "swift/AST/ProtocolConformance.h"
3333
#include "swift/AST/TypeCheckRequests.h"
34+
#include "swift/AST/TypeTransform.h"
3435
#include "swift/Basic/Assertions.h"
3536
#include "swift/Basic/Defer.h"
3637
#include "swift/Basic/Statistic.h"
@@ -1199,49 +1200,59 @@ Type ConstraintSystem::replaceInferableTypesWithTypeVars(
11991200
return type;
12001201
}
12011202

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+
12021247
Type ConstraintSystem::openType(Type type, OpenedTypeMap &replacements,
12031248
ConstraintLocatorBuilder locator) {
12041249
assert(!type->hasUnboundGenericType());
12051250

12061251
if (!type->hasTypeParameter())
12071252
return type;
12081253

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);
12451256
}
12461257

12471258
Type ConstraintSystem::openPackExpansionType(PackExpansionType *expansion,

0 commit comments

Comments
 (0)