Skip to content

Commit e99607c

Browse files
committed
Sema: Use ConstraintKind::Bind where possible instead of ::Equal
Solving Bind is a little easier than Equal. The only remaining uses of Equal are in the .member syntax and keypaths; if we can refactor those, we might be able to simplify LValue handling in the type checker in general.
1 parent 167f94f commit e99607c

File tree

6 files changed

+22
-22
lines changed

6 files changed

+22
-22
lines changed

lib/Sema/CSDiag.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1298,7 +1298,7 @@ static bool diagnoseTypeRequirementFailure(ConstraintSystem &cs,
12981298
rhs);
12991299
return true;
13001300

1301-
case ConstraintKind::Equal: { // same type
1301+
case ConstraintKind::Bind: { // same type
13021302
TC.diagnose(anchor->getLoc(), diag::types_not_equal, ownerType, lhs, rhs);
13031303
return true;
13041304
}

lib/Sema/CSGen.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1679,7 +1679,7 @@ namespace {
16791679
options))
16801680
return Type();
16811681

1682-
CS.addConstraint(ConstraintKind::Equal,
1682+
CS.addConstraint(ConstraintKind::Bind,
16831683
typeVars[i], specializations[i].getType(),
16841684
locator);
16851685
}
@@ -3737,7 +3737,7 @@ static bool canSatisfy(Type type1, Type type2, bool openArchetypes,
37373737
}
37383738

37393739
bool swift::canPossiblyEqual(Type T1, Type T2, DeclContext &DC) {
3740-
return canSatisfy(T1, T2, true, ConstraintKind::Equal, &DC);
3740+
return canSatisfy(T1, T2, true, ConstraintKind::Bind, &DC);
37413741
}
37423742

37433743
bool swift::canPossiblyConvertTo(Type T1, Type T2, DeclContext &DC) {

lib/Sema/CSRanking.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -592,7 +592,7 @@ static bool isDeclAsSpecializedAs(TypeChecker &tc, DeclContext *dc,
592592
break;
593593

594594
case SelfTypeRelationship::Equivalent:
595-
cs.addConstraint(ConstraintKind::Equal, selfTy1, selfTy2, locator);
595+
cs.addConstraint(ConstraintKind::Bind, selfTy1, selfTy2, locator);
596596
break;
597597

598598
case SelfTypeRelationship::Subclass:

lib/Sema/CSSimplify.cpp

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1278,7 +1278,7 @@ ConstraintSystem::matchSuperclassTypes(Type type1, Type type2,
12781278
if (super1->getClassOrBoundGenericClass() != classDecl2)
12791279
continue;
12801280

1281-
return matchTypes(super1, type2, ConstraintKind::Equal,
1281+
return matchTypes(super1, type2, ConstraintKind::Bind,
12821282
subflags, locator);
12831283
}
12841284

@@ -1302,7 +1302,7 @@ ConstraintSystem::matchDeepEqualityTypes(Type type1, Type type2,
13021302

13031303
// Match up the parents, exactly.
13041304
return matchTypes(nominal1->getParent(), nominal2->getParent(),
1305-
ConstraintKind::Equal, subflags,
1305+
ConstraintKind::Bind, subflags,
13061306
locator.withPathElement(ConstraintLocator::ParentType));
13071307
}
13081308

@@ -1314,7 +1314,7 @@ ConstraintSystem::matchDeepEqualityTypes(Type type1, Type type2,
13141314
"Mismatched parents of bound generics");
13151315
if (bound1->getParent()) {
13161316
auto result = matchTypes(bound1->getParent(), bound2->getParent(),
1317-
ConstraintKind::Equal, subflags,
1317+
ConstraintKind::Bind, subflags,
13181318
locator.withPathElement(
13191319
ConstraintLocator::ParentType));
13201320
if (result.isFailure())
@@ -1328,7 +1328,7 @@ ConstraintSystem::matchDeepEqualityTypes(Type type1, Type type2,
13281328
return getTypeMatchFailure(locator);
13291329
}
13301330
for (unsigned i = 0, n = args1.size(); i != n; ++i) {
1331-
auto result = matchTypes(args1[i], args2[i], ConstraintKind::Equal,
1331+
auto result = matchTypes(args1[i], args2[i], ConstraintKind::Bind,
13321332
subflags, locator.withPathElement(
13331333
LocatorPathElt::getGenericArgument(i)));
13341334

@@ -1953,7 +1953,7 @@ ConstraintSystem::matchTypes(Type type1, Type type2, ConstraintKind kind,
19531953
if (isa<MetatypeType>(meta1) &&
19541954
!(instanceType1->mayHaveSuperclass() &&
19551955
instanceType2->getClassOrBoundGenericClass())) {
1956-
subKind = ConstraintKind::Equal;
1956+
subKind = ConstraintKind::Bind;
19571957
}
19581958

19591959
return matchTypes(
@@ -1979,7 +1979,7 @@ ConstraintSystem::matchTypes(Type type1, Type type2, ConstraintKind kind,
19791979
return getTypeMatchFailure(locator);
19801980
return matchTypes(cast<LValueType>(desugar1)->getObjectType(),
19811981
cast<LValueType>(desugar2)->getObjectType(),
1982-
ConstraintKind::Equal, subflags,
1982+
ConstraintKind::Bind, subflags,
19831983
locator.withPathElement(
19841984
ConstraintLocator::LValueConversion));
19851985

@@ -1991,7 +1991,7 @@ ConstraintSystem::matchTypes(Type type1, Type type2, ConstraintKind kind,
19911991

19921992
return matchTypes(cast<InOutType>(desugar1)->getObjectType(),
19931993
cast<InOutType>(desugar2)->getObjectType(),
1994-
ConstraintKind::Equal, subflags,
1994+
ConstraintKind::Bind, subflags,
19951995
locator.withPathElement(ConstraintLocator::LValueConversion));
19961996

19971997
case TypeKind::UnboundGeneric:
@@ -2207,7 +2207,7 @@ ConstraintSystem::matchTypes(Type type1, Type type2, ConstraintKind kind,
22072207
auto inoutBaseType = inoutType1->getInOutObjectType();
22082208

22092209
Type simplifiedInoutBaseType = getFixedTypeRecursive(
2210-
inoutBaseType, kind == ConstraintKind::Equal);
2210+
inoutBaseType, /*wantRValue=*/true);
22112211

22122212
// FIXME: If the base is still a type variable, we can't tell
22132213
// what to do here. Might have to try \c ArrayToPointer and make
@@ -2309,7 +2309,7 @@ ConstraintSystem::matchTypes(Type type1, Type type2, ConstraintKind kind,
23092309
if (auto *lvt = type1->getAs<LValueType>()) {
23102310
if (auto *iot = type2->getAs<InOutType>()) {
23112311
return matchTypes(lvt->getObjectType(), iot->getObjectType(),
2312-
ConstraintKind::Equal, subflags,
2312+
ConstraintKind::Bind, subflags,
23132313
locator.withPathElement(
23142314
ConstraintLocator::LValueConversion));
23152315
}
@@ -2465,13 +2465,13 @@ ConstraintSystem::matchTypes(Type type1, Type type2, ConstraintKind kind,
24652465
// Determine the constraint kind. For a deep equality constraint, only
24662466
// perform equality.
24672467
if (*restriction == ConversionRestrictionKind::DeepEquality)
2468-
constraintKind = ConstraintKind::Equal;
2468+
constraintKind = ConstraintKind::Bind;
24692469

24702470
constraints.push_back(
24712471
Constraint::createRestricted(*this, constraintKind, *restriction,
24722472
type1, type2, fixedLocator));
24732473

2474-
if (constraints.back()->getKind() == ConstraintKind::Equal)
2474+
if (constraints.back()->getKind() == ConstraintKind::Bind)
24752475
constraints.back()->setFavored();
24762476

24772477
continue;
@@ -4090,7 +4090,7 @@ ConstraintSystem::simplifyBridgingConstraint(Type type1,
40904090
}
40914091

40924092
// Make sure we have the bridged value type.
4093-
if (matchTypes(unwrappedToType, bridgedValueType, ConstraintKind::Equal,
4093+
if (matchTypes(unwrappedToType, bridgedValueType, ConstraintKind::Bind,
40944094
subflags, locator).isFailure())
40954095
return SolutionKind::Error;
40964096

@@ -5686,7 +5686,7 @@ void ConstraintSystem::addConstraint(Requirement req,
56865686
kind = ConstraintKind::Subtype;
56875687
break;
56885688
case RequirementKind::SameType:
5689-
kind = ConstraintKind::Equal;
5689+
kind = ConstraintKind::Bind;
56905690
break;
56915691
case RequirementKind::Layout:
56925692
// Only a class constraint can be modeled as a constraint, and only that can

lib/Sema/ConstraintSystem.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,7 @@ Type ConstraintSystem::openUnboundGenericType(UnboundGenericType *unbound,
442442
cast<GenericTypeParamType>(pair.first));
443443
assert(found != replacements.end() &&
444444
"Missing generic parameter?");
445-
addConstraint(ConstraintKind::Equal, found->second, pair.second,
445+
addConstraint(ConstraintKind::Bind, found->second, pair.second,
446446
locator);
447447
}
448448
}
@@ -1149,7 +1149,7 @@ static void addSelfConstraint(ConstraintSystem &cs, Type objectTy, Type selfTy,
11491149
}
11501150

11511151
// Otherwise, the types must be equivalent.
1152-
cs.addConstraint(ConstraintKind::Equal, objectTy, selfTy,
1152+
cs.addConstraint(ConstraintKind::Bind, objectTy, selfTy,
11531153
cs.getConstraintLocator(locator));
11541154
}
11551155

@@ -1322,7 +1322,7 @@ ConstraintSystem::getTypeOfMemberReference(
13221322
// For a protocol, substitute the base object directly. We don't need a
13231323
// conformance constraint because we wouldn't have found the declaration
13241324
// if it didn't conform.
1325-
addConstraint(ConstraintKind::Equal, baseOpenedTy, selfObjTy,
1325+
addConstraint(ConstraintKind::Bind, baseOpenedTy, selfObjTy,
13261326
getConstraintLocator(locator));
13271327
} else if (!isDynamicResult) {
13281328
addSelfConstraint(*this, baseOpenedTy, selfObjTy, locator);
@@ -1847,7 +1847,7 @@ void ConstraintSystem::resolveOverload(ConstraintLocator *locator,
18471847
auto elementObjTy = createTypeVariable(
18481848
getConstraintLocator(locator, ConstraintLocator::FunctionArgument));
18491849
addConstraint(ConstraintKind::Equal, elementTy, elementObjTy, locator);
1850-
1850+
18511851
// The element result is an lvalue or rvalue based on the key path class.
18521852
addKeyPathApplicationConstraint(
18531853
keyPathIndexTy, choice.getBaseType(), elementTy, locator);

lib/Sema/TypeCheckProtocol.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -902,7 +902,7 @@ swift::matchWitness(TypeChecker &tc,
902902
// Match a type in the requirement to a type in the witness.
903903
auto matchTypes = [&](Type reqType,
904904
Type witnessType) -> Optional<RequirementMatch> {
905-
cs->addConstraint(ConstraintKind::Equal, reqType, witnessType, locator);
905+
cs->addConstraint(ConstraintKind::Bind, reqType, witnessType, locator);
906906
// FIXME: Check whether this has already failed.
907907
return None;
908908
};

0 commit comments

Comments
 (0)