Skip to content

Commit 3387154

Browse files
committed
Clean up TypeVariableType::Implementation::mustBeMaterializable(), etc
1 parent 1374147 commit 3387154

File tree

5 files changed

+28
-42
lines changed

5 files changed

+28
-42
lines changed

lib/Sema/CSGen.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1610,6 +1610,7 @@ namespace {
16101610
CS.getConstraintLocator(expr,
16111611
ConstraintLocator::ApplyArgument),
16121612
(TVO_CanBindToLValue |
1613+
TVO_CanBindToInOut |
16131614
TVO_PrefersSubtypeBinding));
16141615
CS.addConstraint(
16151616
ConstraintKind::FunctionInput, methodTy, argTy,

lib/Sema/CSSimplify.cpp

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1547,6 +1547,12 @@ ConstraintSystem::matchTypesBindTypeVar(
15471547
// If the left-hand type variable cannot bind to an lvalue,
15481548
// but we still have an lvalue, fail.
15491549
if (!typeVar->getImpl().canBindToLValue() && type->hasLValueType()) {
1550+
return getTypeMatchFailure(locator);
1551+
}
1552+
1553+
// If the left-hand type variable cannot bind to an inout,
1554+
// but we still have an inout, fail.
1555+
if (!typeVar->getImpl().canBindToInOut() && type->is<InOutType>()) {
15501556
return getTypeMatchFailure(locator);
15511557
}
15521558

@@ -1568,15 +1574,6 @@ ConstraintSystem::matchTypesBindTypeVar(
15681574
}
15691575
}
15701576

1571-
// Check whether the type variable must be bound to a materializable
1572-
// type.
1573-
if (typeVar->getImpl().mustBeMaterializable()) {
1574-
if (!type->isMaterializable())
1575-
return getTypeMatchFailure(locator);
1576-
1577-
setMustBeMaterializableRecursive(type);
1578-
}
1579-
15801577
// Okay. Bind below.
15811578

15821579
// A constraint that binds any pointer to a void pointer is
@@ -1588,6 +1585,16 @@ ConstraintSystem::matchTypesBindTypeVar(
15881585
return getTypeMatchSuccess();
15891586
}
15901587

1588+
if (!typeVar->getImpl().canBindToLValue()) {
1589+
// When binding a fixed type to a type variable that cannot contain
1590+
// lvalues, any type variables within the fixed type cannot contain
1591+
// lvalues either.
1592+
type.visit([&](Type t) {
1593+
if (auto *tvt = dyn_cast<TypeVariableType>(t.getPointer()))
1594+
typeVar->getImpl().setCannotBindToLValue(getSavedBindings());
1595+
});
1596+
}
1597+
15911598
assignFixedType(typeVar, type);
15921599

15931600
return getTypeMatchSuccess();
@@ -2623,6 +2630,7 @@ ConstraintSystem::simplifyConstructionConstraint(
26232630
auto argType = createTypeVariable(
26242631
getConstraintLocator(locator, ConstraintLocator::ApplyArgument),
26252632
(TVO_CanBindToLValue |
2633+
TVO_CanBindToInOut |
26262634
TVO_PrefersSubtypeBinding));
26272635
addConstraint(ConstraintKind::FunctionInput, memberType, argType, locator);
26282636
}

lib/Sema/ConstraintGraph.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -698,7 +698,7 @@ bool ConstraintGraph::contractEdges() {
698698
auto type = binding.BindingType;
699699
isNotContractable = type.findIf([&](Type nestedType) -> bool {
700700
if (auto tv = nestedType->getAs<TypeVariableType>()) {
701-
if (!tv->getImpl().mustBeMaterializable())
701+
if (tv->getImpl().canBindToInOut())
702702
return true;
703703
}
704704

lib/Sema/ConstraintSystem.cpp

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -201,23 +201,6 @@ void ConstraintSystem::assignFixedType(TypeVariableType *typeVar, Type type,
201201
addTypeVariableConstraintsToWorkList(typeVar);
202202
}
203203

204-
void ConstraintSystem::setMustBeMaterializableRecursive(Type type)
205-
{
206-
assert(type->isMaterializable() &&
207-
"argument to setMustBeMaterializableRecursive may not be inherently "
208-
"non-materializable");
209-
type = getFixedTypeRecursive(type, /*wantRValue=*/false);
210-
type = type->lookThroughAllOptionalTypes();
211-
212-
if (auto typeVar = type->getAs<TypeVariableType>()) {
213-
typeVar->getImpl().setMustBeMaterializable(getSavedBindings());
214-
} else if (auto *tupleTy = type->getAs<TupleType>()) {
215-
for (auto elt : tupleTy->getElementTypes()) {
216-
setMustBeMaterializableRecursive(elt);
217-
}
218-
}
219-
}
220-
221204
void ConstraintSystem::addTypeVariableConstraintsToWorkList(
222205
TypeVariableType *typeVar) {
223206
// Gather the constraints affected by a change to this type variable.

lib/Sema/ConstraintSystem.h

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -232,11 +232,6 @@ class TypeVariableType::Implementation {
232232
return getRawOptions() & TVO_PrefersSubtypeBinding;
233233
}
234234

235-
bool mustBeMaterializable() const {
236-
return !(getRawOptions() & TVO_CanBindToInOut) &&
237-
!(getRawOptions() & TVO_CanBindToLValue);
238-
}
239-
240235
/// Retrieve the corresponding node in the constraint graph.
241236
constraints::ConstraintGraphNode *getGraphNode() const { return GraphNode; }
242237

@@ -343,10 +338,16 @@ class TypeVariableType::Implementation {
343338
if (record)
344339
otherRep->getImpl().recordBinding(*record);
345340
otherRep->getImpl().ParentOrFixed = getTypeVariable();
346-
if (!mustBeMaterializable() && otherRep->getImpl().mustBeMaterializable()) {
341+
342+
if (canBindToLValue() && !otherRep->getImpl().canBindToLValue()) {
347343
if (record)
348344
recordBinding(*record);
349345
getTypeVariable()->Bits.TypeVariableType.Options &= ~TVO_CanBindToLValue;
346+
}
347+
348+
if (canBindToInOut() && !otherRep->getImpl().canBindToInOut()) {
349+
if (record)
350+
recordBinding(*record);
350351
getTypeVariable()->Bits.TypeVariableType.Options &= ~TVO_CanBindToInOut;
351352
}
352353
}
@@ -380,15 +381,13 @@ class TypeVariableType::Implementation {
380381
rep->getImpl().ParentOrFixed = type.getPointer();
381382
}
382383

383-
void setMustBeMaterializable(constraints::SavedTypeVariableBindings *record) {
384+
void setCannotBindToLValue(constraints::SavedTypeVariableBindings *record) {
384385
auto rep = getRepresentative(record);
385-
if (!rep->getImpl().mustBeMaterializable()) {
386+
if (rep->getImpl().canBindToLValue()) {
386387
if (record)
387388
rep->getImpl().recordBinding(*record);
388389
rep->getImpl().getTypeVariable()->Bits.TypeVariableType.Options
389390
&= ~TVO_CanBindToLValue;
390-
rep->getImpl().getTypeVariable()->Bits.TypeVariableType.Options
391-
&= ~TVO_CanBindToInOut;
392391
}
393392
}
394393

@@ -2114,11 +2113,6 @@ class ConstraintSystem {
21142113
/// a complete solution from partial solutions.
21152114
void assignFixedType(TypeVariableType *typeVar, Type type,
21162115
bool updateState = true);
2117-
2118-
/// Set the TVO_MustBeMaterializable bit on all type variables
2119-
/// necessary to ensure that the type in question is materializable in a
2120-
/// viable solution.
2121-
void setMustBeMaterializableRecursive(Type type);
21222116

21232117
/// Determine if the type in question is an Array<T> and, if so, provide the
21242118
/// element type of the array.

0 commit comments

Comments
 (0)