Skip to content

Commit bb8f0fc

Browse files
committed
[Sema] NFC: Flip polarity of TVO_CanBindToInOut flag
We want InOutType to go away and the majority of callers pass TVO_CanBindToInOut, therefore change the default to be TVO_CanBindToInOut and allow the weird cases that need reconciling to use TVO_CannotBindToInOut.
1 parent 99ccfb1 commit bb8f0fc

File tree

7 files changed

+83
-142
lines changed

7 files changed

+83
-142
lines changed

lib/Sema/CSDiag.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4802,7 +4802,6 @@ namespace {
48024802

48034803
auto tv = cs.createTypeVariable(inputLocator,
48044804
TVO_CanBindToLValue |
4805-
TVO_CanBindToInOut |
48064805
TVO_PrefersSubtypeBinding);
48074806

48084807
// In order to make this work, we pick the most general function type and
@@ -5256,8 +5255,8 @@ bool FailureDiagnosis::diagnoseTrailingClosureErrors(ApplyExpr *callExpr) {
52565255
} else if (auto *typeVar = resultType->getAs<TypeVariableType>()) {
52575256
auto tv =
52585257
cs.createTypeVariable(cs.getConstraintLocator(expr),
5259-
TVO_CanBindToLValue | TVO_CanBindToInOut |
5260-
TVO_PrefersSubtypeBinding);
5258+
TVO_CanBindToLValue |
5259+
TVO_PrefersSubtypeBinding);
52615260

52625261
auto extInfo = FunctionType::ExtInfo().withThrows();
52635262
auto fTy = FunctionType::get(ParenType::get(cs.getASTContext(), tv),
@@ -7073,7 +7072,7 @@ bool FailureDiagnosis::visitKeyPathExpr(KeyPathExpr *KPE) {
70737072

70747073
bool builtConstraints(ConstraintSystem &cs, Expr *expr) override {
70757074
auto *locator = cs.getConstraintLocator(expr);
7076-
auto valueType = cs.createTypeVariable(locator, TVO_CanBindToInOut);
7075+
auto valueType = cs.createTypeVariable(locator);
70777076

70787077
auto keyPathType =
70797078
BoundGenericClassType::get(Decl, ParentType, {RootType, valueType});

lib/Sema/CSGen.cpp

Lines changed: 47 additions & 87 deletions
Large diffs are not rendered by default.

lib/Sema/CSSimplify.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1691,7 +1691,7 @@ ConstraintSystem::matchTypes(Type type1, Type type2, ConstraintKind kind,
16911691
// is wrapped in `inout` type to preserve inout/lvalue pairing.
16921692
if (auto *lvt = type2->getAs<LValueType>()) {
16931693
auto *tv = createTypeVariable(typeVar1->getImpl().getLocator(),
1694-
/*options=*/0);
1694+
TVO_CannotBindToInOut);
16951695
assignFixedType(typeVar1, InOutType::get(tv));
16961696

16971697
typeVar1 = tv;
@@ -2542,7 +2542,6 @@ ConstraintSystem::simplifyConstructionConstraint(
25422542
ConstraintLocator::ApplyFunction);
25432543
auto tv = createTypeVariable(applyLocator,
25442544
TVO_CanBindToLValue |
2545-
TVO_CanBindToInOut |
25462545
TVO_PrefersSubtypeBinding);
25472546

25482547
// The constructor will have function type T -> T2, for a fresh type
@@ -4701,9 +4700,7 @@ ConstraintSystem::simplifyRestrictedConstraintImpl(
47014700
return SolutionKind::Error;
47024701

47034702
auto constraintLocator = getConstraintLocator(locator);
4704-
auto tv = createTypeVariable(constraintLocator,
4705-
TVO_PrefersSubtypeBinding |
4706-
TVO_CanBindToInOut);
4703+
auto tv = createTypeVariable(constraintLocator, TVO_PrefersSubtypeBinding);
47074704

47084705
addConstraint(ConstraintKind::ConformsTo, tv,
47094706
hashableProtocol->getDeclaredType(), constraintLocator);

lib/Sema/CSSolver.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1364,8 +1364,7 @@ ConstraintSystem::solve(Expr *&expr,
13641364
if (allowFreeTypeVariables == FreeTypeVariableBinding::UnresolvedType) {
13651365
convertType = convertType.transform([&](Type type) -> Type {
13661366
if (type->is<UnresolvedType>())
1367-
return createTypeVariable(getConstraintLocator(expr),
1368-
TVO_CanBindToInOut);
1367+
return createTypeVariable(getConstraintLocator(expr));
13691368
return type;
13701369
});
13711370
}

lib/Sema/ConstraintSystem.cpp

Lines changed: 15 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -941,8 +941,7 @@ ConstraintSystem::getTypeOfReference(ValueDecl *value,
941941
return { found->second, found->second };
942942

943943
auto typeVar = createTypeVariable(getConstraintLocator(locator),
944-
TVO_CanBindToLValue |
945-
TVO_CanBindToInOut);
944+
TVO_CanBindToLValue);
946945
addConstraint(ConstraintKind::BindParam, valueType, typeVar,
947946
getConstraintLocator(locator));
948947
OpenedParameterTypes.insert(std::make_pair(param, typeVar));
@@ -1081,6 +1080,7 @@ void ConstraintSystem::openGeneric(
10811080
locator.withPathElement(LocatorPathElt(archetype)));
10821081

10831082
auto typeVar = createTypeVariable(locatorPtr,
1083+
TVO_CannotBindToInOut |
10841084
TVO_PrefersSubtypeBinding);
10851085
auto result = replacements.insert(
10861086
std::make_pair(cast<GenericTypeParamType>(gp->getCanonicalType()),
@@ -1467,11 +1467,9 @@ resolveOverloadForDeclWithSpecialTypeCheckingSemantics(ConstraintSystem &CS,
14671467
// existentials (as seen from the current abstraction level), which can't
14681468
// be expressed in the type system currently.
14691469
auto input = CS.createTypeVariable(
1470-
CS.getConstraintLocator(locator, ConstraintLocator::FunctionArgument),
1471-
TVO_CanBindToInOut);
1470+
CS.getConstraintLocator(locator, ConstraintLocator::FunctionArgument));
14721471
auto output = CS.createTypeVariable(
1473-
CS.getConstraintLocator(locator, ConstraintLocator::FunctionResult),
1474-
TVO_CanBindToInOut);
1472+
CS.getConstraintLocator(locator, ConstraintLocator::FunctionResult));
14751473

14761474
auto inputArg = TupleTypeElt(input, CS.getASTContext().getIdentifier("of"));
14771475
auto inputTuple = TupleType::get(inputArg, CS.getASTContext());
@@ -1487,17 +1485,14 @@ resolveOverloadForDeclWithSpecialTypeCheckingSemantics(ConstraintSystem &CS,
14871485
// receives a copy of the argument closure that is temporarily made
14881486
// @escaping.
14891487
auto noescapeClosure = CS.createTypeVariable(
1490-
CS.getConstraintLocator(locator, ConstraintLocator::FunctionArgument),
1491-
TVO_CanBindToInOut);
1488+
CS.getConstraintLocator(locator, ConstraintLocator::FunctionArgument));
14921489
auto escapeClosure = CS.createTypeVariable(
1493-
CS.getConstraintLocator(locator, ConstraintLocator::FunctionArgument),
1494-
TVO_CanBindToInOut);
1490+
CS.getConstraintLocator(locator, ConstraintLocator::FunctionArgument));
14951491
CS.addConstraint(ConstraintKind::EscapableFunctionOf,
14961492
escapeClosure, noescapeClosure,
14971493
CS.getConstraintLocator(locator, ConstraintLocator::RvalueAdjustment));
14981494
auto result = CS.createTypeVariable(
1499-
CS.getConstraintLocator(locator, ConstraintLocator::FunctionResult),
1500-
TVO_CanBindToInOut);
1495+
CS.getConstraintLocator(locator, ConstraintLocator::FunctionResult));
15011496
auto bodyClosure = FunctionType::get(
15021497
ParenType::get(CS.getASTContext(), escapeClosure), result,
15031498
FunctionType::ExtInfo(FunctionType::Representation::Swift,
@@ -1522,17 +1517,14 @@ resolveOverloadForDeclWithSpecialTypeCheckingSemantics(ConstraintSystem &CS,
15221517
// The body closure receives a freshly-opened archetype constrained by the
15231518
// existential type as its input.
15241519
auto openedTy = CS.createTypeVariable(
1525-
CS.getConstraintLocator(locator, ConstraintLocator::FunctionArgument),
1526-
TVO_CanBindToInOut);
1520+
CS.getConstraintLocator(locator, ConstraintLocator::FunctionArgument));
15271521
auto existentialTy = CS.createTypeVariable(
1528-
CS.getConstraintLocator(locator, ConstraintLocator::FunctionArgument),
1529-
TVO_CanBindToInOut);
1522+
CS.getConstraintLocator(locator, ConstraintLocator::FunctionArgument));
15301523
CS.addConstraint(ConstraintKind::OpenedExistentialOf,
15311524
openedTy, existentialTy,
15321525
CS.getConstraintLocator(locator, ConstraintLocator::RvalueAdjustment));
15331526
auto result = CS.createTypeVariable(
1534-
CS.getConstraintLocator(locator, ConstraintLocator::FunctionResult),
1535-
TVO_CanBindToInOut);
1527+
CS.getConstraintLocator(locator, ConstraintLocator::FunctionResult));
15361528
auto bodyClosure = FunctionType::get(
15371529
ParenType::get(CS.getASTContext(), openedTy), result,
15381530
FunctionType::ExtInfo(FunctionType::Representation::Swift,
@@ -1630,7 +1622,7 @@ void ConstraintSystem::resolveOverload(ConstraintLocator *locator,
16301622
if (choice.isImplicitlyUnwrappedValueOrReturnValue()) {
16311623
// Build the disjunction to attempt binding both T? and T (or
16321624
// function returning T? and function returning T).
1633-
Type ty = createTypeVariable(locator, TVO_CanBindToInOut);
1625+
Type ty = createTypeVariable(locator);
16341626
buildDisjunctionForImplicitlyUnwrappedOptional(ty, refType,
16351627
locator);
16361628
addConstraint(ConstraintKind::Bind, boundType,
@@ -1660,7 +1652,7 @@ void ConstraintSystem::resolveOverload(ConstraintLocator *locator,
16601652
// For our original type T -> U?? we will generate:
16611653
// A disjunction V = { U?, U }
16621654
// and a disjunction boundType = { T -> V?, T -> V }
1663-
Type ty = createTypeVariable(locator, TVO_CanBindToInOut);
1655+
Type ty = createTypeVariable(locator);
16641656

16651657
buildDisjunctionForImplicitlyUnwrappedOptional(ty, optTy, locator);
16661658

@@ -1770,15 +1762,12 @@ void ConstraintSystem::resolveOverload(ConstraintLocator *locator,
17701762
// The element type is T or @lvalue T based on the key path subtype and
17711763
// the mutability of the base.
17721764
auto keyPathIndexTy = createTypeVariable(
1773-
getConstraintLocator(locator, ConstraintLocator::FunctionArgument),
1774-
TVO_CanBindToInOut);
1765+
getConstraintLocator(locator, ConstraintLocator::FunctionArgument));
17751766
auto elementTy = createTypeVariable(
17761767
getConstraintLocator(locator, ConstraintLocator::FunctionArgument),
1777-
TVO_CanBindToLValue |
1778-
TVO_CanBindToInOut);
1768+
TVO_CanBindToLValue);
17791769
auto elementObjTy = createTypeVariable(
1780-
getConstraintLocator(locator, ConstraintLocator::FunctionArgument),
1781-
TVO_CanBindToInOut);
1770+
getConstraintLocator(locator, ConstraintLocator::FunctionArgument));
17821771
addConstraint(ConstraintKind::Equal, elementTy, elementObjTy, locator);
17831772

17841773
// The element result is an lvalue or rvalue based on the key path class.

lib/Sema/ConstraintSystem.h

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ enum TypeVariableOptions {
162162
TVO_CanBindToLValue = 0x01,
163163

164164
/// Whether the type variable can be bound to an inout type or not.
165-
TVO_CanBindToInOut = 0x02,
165+
TVO_CannotBindToInOut = 0x02,
166166

167167
/// Whether a more specific deduction for this type variable implies a
168168
/// better solution to the constraint system.
@@ -224,7 +224,9 @@ class TypeVariableType::Implementation {
224224
bool canBindToLValue() const { return getRawOptions() & TVO_CanBindToLValue; }
225225

226226
/// Whether this type variable can bind to an inout type.
227-
bool canBindToInOut() const { return getRawOptions() & TVO_CanBindToInOut; }
227+
bool canBindToInOut() const {
228+
return !(getRawOptions() & TVO_CannotBindToInOut);
229+
}
228230

229231
/// Whether this type variable prefers a subtype binding over a supertype
230232
/// binding.
@@ -233,8 +235,7 @@ class TypeVariableType::Implementation {
233235
}
234236

235237
bool mustBeMaterializable() const {
236-
return !(getRawOptions() & TVO_CanBindToInOut) &&
237-
!(getRawOptions() & TVO_CanBindToLValue);
238+
return !canBindToInOut() && !canBindToLValue();
238239
}
239240

240241
/// Retrieve the corresponding node in the constraint graph.
@@ -347,7 +348,7 @@ class TypeVariableType::Implementation {
347348
if (record)
348349
recordBinding(*record);
349350
getTypeVariable()->Bits.TypeVariableType.Options &= ~TVO_CanBindToLValue;
350-
getTypeVariable()->Bits.TypeVariableType.Options &= ~TVO_CanBindToInOut;
351+
getTypeVariable()->Bits.TypeVariableType.Options |= TVO_CannotBindToInOut;
351352
}
352353
}
353354

@@ -391,7 +392,7 @@ class TypeVariableType::Implementation {
391392
rep->getImpl().getTypeVariable()->Bits.TypeVariableType.Options
392393
&= ~TVO_CanBindToLValue;
393394
rep->getImpl().getTypeVariable()->Bits.TypeVariableType.Options
394-
&= ~TVO_CanBindToInOut;
395+
|= TVO_CannotBindToInOut;
395396
}
396397
}
397398

@@ -1566,7 +1567,7 @@ class ConstraintSystem {
15661567

15671568
/// \brief Create a new type variable.
15681569
TypeVariableType *createTypeVariable(ConstraintLocator *locator,
1569-
unsigned options);
1570+
unsigned options = 0);
15701571

15711572
/// Retrieve the set of active type variables.
15721573
ArrayRef<TypeVariableType *> getTypeVariables() const {

lib/Sema/TypeCheckConstraints.cpp

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1995,9 +1995,7 @@ bool TypeChecker::typeCheckCompletionSequence(Expr *&expr, DeclContext *DC) {
19951995

19961996
// Add type variable for the code-completion expression.
19971997
auto tvRHS =
1998-
CS.createTypeVariable(CS.getConstraintLocator(CCE),
1999-
TVO_CanBindToLValue |
2000-
TVO_CanBindToInOut);
1998+
CS.createTypeVariable(CS.getConstraintLocator(CCE), TVO_CanBindToLValue);
20011999
CCE->setType(tvRHS);
20022000

20032001
if (auto generated = CS.generateConstraints(expr)) {
@@ -2328,8 +2326,7 @@ bool TypeChecker::typeCheckForEachBinding(DeclContext *dc, ForEachStmt *stmt) {
23282326
return true;
23292327
}
23302328

2331-
SequenceType =
2332-
cs.createTypeVariable(Locator, /*options=*/0);
2329+
SequenceType = cs.createTypeVariable(Locator, TVO_CannotBindToInOut);
23332330
cs.addConstraint(ConstraintKind::Conversion, cs.getType(expr),
23342331
SequenceType, Locator);
23352332
cs.addConstraint(ConstraintKind::ConformsTo, SequenceType,
@@ -2402,7 +2399,8 @@ bool TypeChecker::typeCheckForEachBinding(DeclContext *dc, ForEachStmt *stmt) {
24022399
}
24032400

24042401
if (elementType.isNull()) {
2405-
elementType = cs.createTypeVariable(elementLocator, /*options=*/0);
2402+
elementType = cs.createTypeVariable(elementLocator,
2403+
TVO_CannotBindToInOut);
24062404
}
24072405

24082406
// Add a conversion constraint between the element type of the sequence
@@ -2487,7 +2485,7 @@ Type ConstraintSystem::computeAssignDestType(Expr *dest, SourceLoc equalLoc) {
24872485
// Newly allocated type should be explicitly materializable,
24882486
// it's invalid to use non-materializable types as assignment destination.
24892487
auto objectTv = createTypeVariable(getConstraintLocator(dest),
2490-
/*options=*/0);
2488+
TVO_CannotBindToInOut);
24912489
auto refTv = LValueType::get(objectTv);
24922490
addConstraint(ConstraintKind::Bind, typeVar, refTv,
24932491
getConstraintLocator(dest));
@@ -2711,8 +2709,7 @@ static Type replaceArchetypesWithTypeVariables(ConstraintSystem &cs,
27112709
return Type();
27122710

27132711
auto locator = cs.getConstraintLocator(nullptr);
2714-
auto replacement = cs.createTypeVariable(locator,
2715-
TVO_CanBindToInOut);
2712+
auto replacement = cs.createTypeVariable(locator);
27162713

27172714
if (auto superclass = archetypeType->getSuperclass()) {
27182715
cs.addConstraint(ConstraintKind::Subtype, replacement,
@@ -2729,8 +2726,7 @@ static Type replaceArchetypesWithTypeVariables(ConstraintSystem &cs,
27292726
// FIXME: Remove this case
27302727
assert(cast<GenericTypeParamType>(origType));
27312728
auto locator = cs.getConstraintLocator(nullptr);
2732-
auto replacement = cs.createTypeVariable(locator,
2733-
TVO_CanBindToInOut);
2729+
auto replacement = cs.createTypeVariable(locator);
27342730
types[origType] = replacement;
27352731
return replacement;
27362732
},

0 commit comments

Comments
 (0)