Skip to content

Commit b03dc63

Browse files
committed
[AST] Adjust TypeBase::getTypeVariables to accept a set
Currently the pattern is to collect the type variables and then unique them. Instead of asking clients to do uniquing, let's just accept a set as an argument.
1 parent 4988127 commit b03dc63

File tree

9 files changed

+76
-112
lines changed

9 files changed

+76
-112
lines changed

include/swift/AST/Types.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -632,7 +632,7 @@ class alignas(1 << TypeAlignInBits) TypeBase {
632632
///
633633
/// \param typeVariables This vector is populated with the set of
634634
/// type variables referenced by this type.
635-
void getTypeVariables(SmallVectorImpl<TypeVariableType *> &typeVariables);
635+
void getTypeVariables(SmallPtrSetImpl<TypeVariableType *> &typeVariables);
636636

637637
/// Determine whether this type is a type parameter, which is either a
638638
/// GenericTypeParamType or a DependentMemberType.

include/swift/Sema/Constraint.h

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -381,43 +381,45 @@ class Constraint final : public llvm::ilist_node<Constraint>,
381381
void *operator new(size_t) = delete;
382382

383383
Constraint(ConstraintKind kind, ArrayRef<Constraint *> constraints,
384-
ConstraintLocator *locator, ArrayRef<TypeVariableType *> typeVars);
384+
ConstraintLocator *locator,
385+
SmallPtrSetImpl<TypeVariableType *> &typeVars);
385386

386387
/// Construct a new constraint.
387388
Constraint(ConstraintKind kind, Type first, Type second,
388389
ConstraintLocator *locator,
389-
ArrayRef<TypeVariableType *> typeVars);
390+
SmallPtrSetImpl<TypeVariableType *> &typeVars);
390391

391392
/// Construct a new constraint.
392393
Constraint(ConstraintKind kind, Type first, Type second, Type third,
393394
ConstraintLocator *locator,
394-
ArrayRef<TypeVariableType *> typeVars);
395+
SmallPtrSetImpl<TypeVariableType *> &typeVars);
395396

396397
/// Construct a new member constraint.
397398
Constraint(ConstraintKind kind, Type first, Type second, DeclNameRef member,
398399
DeclContext *useDC, FunctionRefKind functionRefKind,
399400
ConstraintLocator *locator,
400-
ArrayRef<TypeVariableType *> typeVars);
401+
SmallPtrSetImpl<TypeVariableType *> &typeVars);
401402

402403
/// Construct a new value witness constraint.
403404
Constraint(ConstraintKind kind, Type first, Type second,
404405
ValueDecl *requirement, DeclContext *useDC,
405406
FunctionRefKind functionRefKind, ConstraintLocator *locator,
406-
ArrayRef<TypeVariableType *> typeVars);
407+
SmallPtrSetImpl<TypeVariableType *> &typeVars);
407408

408409
/// Construct a new overload-binding constraint, which might have a fix.
409410
Constraint(Type type, OverloadChoice choice, DeclContext *useDC,
410411
ConstraintFix *fix, ConstraintLocator *locator,
411-
ArrayRef<TypeVariableType *> typeVars);
412+
SmallPtrSetImpl<TypeVariableType *> &typeVars);
412413

413414
/// Construct a restricted constraint.
414415
Constraint(ConstraintKind kind, ConversionRestrictionKind restriction,
415416
Type first, Type second, ConstraintLocator *locator,
416-
ArrayRef<TypeVariableType *> typeVars);
417-
417+
SmallPtrSetImpl<TypeVariableType *> &typeVars);
418+
418419
/// Construct a relational constraint with a fix.
419420
Constraint(ConstraintKind kind, ConstraintFix *fix, Type first, Type second,
420-
ConstraintLocator *locator, ArrayRef<TypeVariableType *> typeVars);
421+
ConstraintLocator *locator,
422+
SmallPtrSetImpl<TypeVariableType *> &typeVars);
421423

422424
/// Retrieve the type variables buffer, for internal mutation.
423425
MutableArrayRef<TypeVariableType *> getTypeVariablesBuffer() {

lib/AST/Type.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -445,13 +445,13 @@ Type TypeBase::addCurriedSelfType(const DeclContext *dc) {
445445
return FunctionType::get({selfParam}, type);
446446
}
447447

448-
void
449-
TypeBase::getTypeVariables(SmallVectorImpl<TypeVariableType *> &typeVariables) {
448+
void TypeBase::getTypeVariables(
449+
SmallPtrSetImpl<TypeVariableType *> &typeVariables) {
450450
// If we know we don't have any type variables, we're done.
451451
if (hasTypeVariable()) {
452452
auto addTypeVariables = [&](Type type) -> bool {
453453
if (auto tv = dyn_cast<TypeVariableType>(type.getPointer())) {
454-
typeVariables.push_back(tv);
454+
typeVariables.insert(tv);
455455
}
456456

457457
return false;

lib/Sema/CSBindings.cpp

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -830,9 +830,9 @@ PotentialBindings ConstraintSystem::inferBindingsFor(TypeVariableType *typeVar,
830830
/// \returns the type to bind to, if the binding is okay.
831831
static Optional<Type> checkTypeOfBinding(TypeVariableType *typeVar, Type type) {
832832
// If the type references the type variable, don't permit the binding.
833-
SmallVector<TypeVariableType *, 4> referencedTypeVars;
833+
SmallPtrSet<TypeVariableType *, 4> referencedTypeVars;
834834
type->getTypeVariables(referencedTypeVars);
835-
if (count(referencedTypeVars, typeVar))
835+
if (referencedTypeVars.count(typeVar))
836836
return None;
837837

838838
// If type variable is not allowed to bind to `lvalue`,
@@ -940,21 +940,9 @@ PotentialBindings::inferFromRelational(Constraint *constraint) {
940940
if (type->getWithoutSpecifierType()
941941
->lookThroughAllOptionalTypes()
942942
->is<DependentMemberType>()) {
943-
SmallVector<TypeVariableType *, 4> referencedVars;
944-
type->getTypeVariables(referencedVars);
945-
946-
bool containsSelf = false;
947-
for (auto *var : referencedVars) {
948-
// Add all type variables encountered in the type except
949-
// to the current type variable.
950-
if (var != TypeVar) {
951-
AdjacentVars.insert(var);
952-
continue;
953-
}
954-
955-
containsSelf = true;
956-
}
943+
type->getTypeVariables(AdjacentVars);
957944

945+
bool containsSelf = AdjacentVars.erase(TypeVar);
958946
// If inferred type doesn't contain the current type variable,
959947
// let's mark bindings as delayed until dependent member type
960948
// is resolved.
@@ -979,11 +967,8 @@ PotentialBindings::inferFromRelational(Constraint *constraint) {
979967
// Check whether we can perform this binding.
980968
if (auto boundType = checkTypeOfBinding(TypeVar, type)) {
981969
type = *boundType;
982-
if (type->hasTypeVariable()) {
983-
SmallVector<TypeVariableType *, 4> referencedVars;
984-
type->getTypeVariables(referencedVars);
985-
AdjacentVars.insert(referencedVars.begin(), referencedVars.end());
986-
}
970+
if (type->hasTypeVariable())
971+
type->getTypeVariables(AdjacentVars);
987972
} else {
988973
auto *bindingTypeVar = type->getRValueType()->getAs<TypeVariableType>();
989974

lib/Sema/CSGen.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2471,7 +2471,7 @@ namespace {
24712471
// the type variable for the pattern `i`.
24722472
struct CollectVarRefs : public ASTWalker {
24732473
ConstraintSystem &cs;
2474-
llvm::SmallVector<TypeVariableType *, 4> varRefs;
2474+
llvm::SmallPtrSet<TypeVariableType *, 4> varRefs;
24752475
bool hasErrorExprs = false;
24762476

24772477
CollectVarRefs(ConstraintSystem &cs) : cs(cs) { }
@@ -2532,10 +2532,12 @@ namespace {
25322532
if (!inferredType || inferredType->hasError())
25332533
return Type();
25342534

2535-
CS.addUnsolvedConstraint(
2536-
Constraint::create(CS, ConstraintKind::DefaultClosureType,
2537-
closureType, inferredType, locator,
2538-
collectVarRefs.varRefs));
2535+
SmallVector<TypeVariableType *, 4> referencedVars{
2536+
collectVarRefs.varRefs.begin(), collectVarRefs.varRefs.end()};
2537+
2538+
CS.addUnsolvedConstraint(Constraint::create(
2539+
CS, ConstraintKind::DefaultClosureType, closureType, inferredType,
2540+
locator, referencedVars));
25392541

25402542
CS.setClosureType(closure, inferredType);
25412543
return closureType;

lib/Sema/CSSimplify.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10549,13 +10549,16 @@ ConstraintSystem::addArgumentConversionConstraintImpl(
1054910549
if (auto *argTypeVar = first->getAs<TypeVariableType>()) {
1055010550
if (argTypeVar->getImpl().isClosureType()) {
1055110551
// Extract any type variables present in the parameter's result builder.
10552-
SmallVector<TypeVariableType *, 4> typeVars;
10552+
SmallPtrSet<TypeVariableType *, 4> typeVars;
1055310553
if (auto builderTy = getOpenedResultBuilderTypeFor(*this, locator))
1055410554
builderTy->getTypeVariables(typeVars);
1055510555

10556+
SmallVector<TypeVariableType *, 4> referencedVars{typeVars.begin(),
10557+
typeVars.end()};
10558+
1055610559
auto *loc = getConstraintLocator(locator);
1055710560
addUnsolvedConstraint(
10558-
Constraint::create(*this, kind, first, second, loc, typeVars));
10561+
Constraint::create(*this, kind, first, second, loc, referencedVars));
1055910562
return SolutionKind::Solved;
1056010563
}
1056110564
}

0 commit comments

Comments
 (0)