Skip to content

Commit 8df19d2

Browse files
committed
[ConstraintSystem] NFC: Associate transitive binding inference with PotentialBindings
1 parent fa553ab commit 8df19d2

File tree

2 files changed

+36
-34
lines changed

2 files changed

+36
-34
lines changed

lib/Sema/CSBindings.cpp

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,36 +21,37 @@
2121
using namespace swift;
2222
using namespace constraints;
2323

24-
void ConstraintSystem::inferTransitiveSupertypeBindings(
25-
const llvm::SmallDenseMap<TypeVariableType *, PotentialBindings>
26-
&inferredBindings,
27-
PotentialBindings &bindings) {
28-
auto *typeVar = bindings.TypeVar;
24+
void ConstraintSystem::PotentialBindings::inferTransitiveBindings(
25+
ConstraintSystem &cs,
26+
const llvm::SmallDenseMap<TypeVariableType *,
27+
ConstraintSystem::PotentialBindings>
28+
&inferredBindings) {
29+
using BindingKind = ConstraintSystem::AllowedBindingKind;
2930

3031
llvm::SmallVector<Constraint *, 4> subtypeOf;
3132
// First, let's collect all of the `subtype` constraints associated
3233
// with this type variable.
33-
llvm::copy_if(bindings.Sources, std::back_inserter(subtypeOf),
34+
llvm::copy_if(Sources, std::back_inserter(subtypeOf),
3435
[&](const Constraint *constraint) -> bool {
3536
if (constraint->getKind() != ConstraintKind::Subtype)
3637
return false;
3738

38-
auto rhs = simplifyType(constraint->getSecondType());
39-
return rhs->getAs<TypeVariableType>() == typeVar;
39+
auto rhs = cs.simplifyType(constraint->getSecondType());
40+
return rhs->getAs<TypeVariableType>() == TypeVar;
4041
});
4142

4243
if (subtypeOf.empty())
4344
return;
4445

4546
// We need to make sure that there are no duplicate bindings in the
46-
// set, other we'll produce multiple identical solutions.
47+
// set, otherwise solver would produce multiple identical solutions.
4748
llvm::SmallPtrSet<CanType, 4> existingTypes;
48-
for (const auto &binding : bindings.Bindings)
49+
for (const auto &binding : Bindings)
4950
existingTypes.insert(binding.BindingType->getCanonicalType());
5051

5152
for (auto *constraint : subtypeOf) {
5253
auto *tv =
53-
simplifyType(constraint->getFirstType())->getAs<TypeVariableType>();
54+
cs.simplifyType(constraint->getFirstType())->getAs<TypeVariableType>();
5455
if (!tv)
5556
continue;
5657

@@ -63,8 +64,8 @@ void ConstraintSystem::inferTransitiveSupertypeBindings(
6364
// either be Exact or Supertypes in order for it to make sense
6465
// to add Supertype bindings based on the relationship between
6566
// our type variables.
66-
if (binding.Kind != AllowedBindingKind::Exact &&
67-
binding.Kind != AllowedBindingKind::Supertypes)
67+
if (binding.Kind != BindingKind::Exact &&
68+
binding.Kind != BindingKind::Supertypes)
6869
continue;
6970

7071
auto type = binding.BindingType;
@@ -75,16 +76,16 @@ void ConstraintSystem::inferTransitiveSupertypeBindings(
7576
if (!existingTypes.insert(type->getCanonicalType()).second)
7677
continue;
7778

78-
if (ConstraintSystem::typeVarOccursInType(typeVar, type))
79+
if (ConstraintSystem::typeVarOccursInType(TypeVar, type))
7980
continue;
8081

81-
bindings.addPotentialBinding(
82-
binding.withSameSource(type, AllowedBindingKind::Supertypes));
82+
addPotentialBinding(
83+
binding.withSameSource(type, BindingKind::Supertypes));
8384
}
8485

8586
// Infer transitive protocol requirements.
8687
for (auto *protocol : relatedBindings->getSecond().Protocols) {
87-
bindings.Protocols.push_back(protocol);
88+
Protocols.push_back(protocol);
8889
}
8990
}
9091
}
@@ -113,7 +114,7 @@ ConstraintSystem::determineBestBindings() {
113114

114115
auto &bindings = cachedBindings->getSecond();
115116

116-
inferTransitiveSupertypeBindings(cache, bindings);
117+
bindings.inferTransitiveBindings(*this, cache);
117118

118119
if (isDebugMode()) {
119120
bindings.dump(typeVar, llvm::errs(), solverState->depth * 2);

lib/Sema/ConstraintSystem.h

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4557,6 +4557,23 @@ class ConstraintSystem {
45574557
/// if it has only concrete types or would resolve a closure.
45584558
bool favoredOverDisjunction(Constraint *disjunction) const;
45594559

4560+
/// Detect `subtype` relationship between two type variables and
4561+
/// attempt to infer supertype bindings transitively e.g.
4562+
///
4563+
/// Given A <: T1 <: T2 transitively A <: T2
4564+
///
4565+
/// Which gives us a new (superclass A) binding for T2 as well as T1.
4566+
///
4567+
/// \param cs The constraint system this type variable is associated with.
4568+
///
4569+
/// \param inferredBindings The set of all bindings inferred for type
4570+
/// variables in the workset.
4571+
void inferTransitiveBindings(
4572+
ConstraintSystem &cs,
4573+
const llvm::SmallDenseMap<TypeVariableType *,
4574+
ConstraintSystem::PotentialBindings>
4575+
&inferredBindings);
4576+
45604577
void dump(llvm::raw_ostream &out,
45614578
unsigned indent = 0) const LLVM_ATTRIBUTE_USED {
45624579
out.indent(indent);
@@ -4625,22 +4642,6 @@ class ConstraintSystem {
46254642
bool &addOptionalSupertypeBindings) const;
46264643
PotentialBindings getPotentialBindings(TypeVariableType *typeVar) const;
46274644

4628-
/// Detect `subtype` relationship between two type variables and
4629-
/// attempt to infer supertype bindings transitively e.g.
4630-
///
4631-
/// Given A <: T1 <: T2 transitively A <: T2
4632-
///
4633-
/// Which gives us a new (superclass A) binding for T2 as well as T1.
4634-
///
4635-
/// \param inferredBindings The set of all bindings inferred for type
4636-
/// variables in the workset.
4637-
/// \param bindings The type variable we aim to infer new supertype
4638-
/// bindings for.
4639-
void inferTransitiveSupertypeBindings(
4640-
const llvm::SmallDenseMap<TypeVariableType *, PotentialBindings>
4641-
&inferredBindings,
4642-
PotentialBindings &bindings);
4643-
46444645
private:
46454646
/// Add a constraint to the constraint system.
46464647
SolutionKind addConstraintImpl(ConstraintKind kind, Type first, Type second,

0 commit comments

Comments
 (0)