Skip to content

RFC / DO NOT MERGE – Remove TVO_CanBindToInOut, add TVO_IsGenericTypeParam #15574

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions lib/Sema/CSDiag.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4802,7 +4802,6 @@ namespace {

auto tv = cs.createTypeVariable(inputLocator,
TVO_CanBindToLValue |
TVO_CanBindToInOut |
TVO_PrefersSubtypeBinding);

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

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

bool builtConstraints(ConstraintSystem &cs, Expr *expr) override {
auto *locator = cs.getConstraintLocator(expr);
auto valueType = cs.createTypeVariable(locator, TVO_CanBindToInOut);
auto valueType = cs.createTypeVariable(locator);

auto keyPathType =
BoundGenericClassType::get(Decl, ParentType, {RootType, valueType});
Expand Down
139 changes: 47 additions & 92 deletions lib/Sema/CSGen.cpp

Large diffs are not rendered by default.

8 changes: 2 additions & 6 deletions lib/Sema/CSSimplify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1690,8 +1690,7 @@ ConstraintSystem::matchTypes(Type type1, Type type2, ConstraintKind kind,
// left-hand side is bound to type variable which
// is wrapped in `inout` type to preserve inout/lvalue pairing.
if (auto *lvt = type2->getAs<LValueType>()) {
auto *tv = createTypeVariable(typeVar1->getImpl().getLocator(),
/*options=*/0);
auto *tv = createTypeVariable(typeVar1->getImpl().getLocator());
assignFixedType(typeVar1, InOutType::get(tv));

typeVar1 = tv;
Expand Down Expand Up @@ -2542,7 +2541,6 @@ ConstraintSystem::simplifyConstructionConstraint(
ConstraintLocator::ApplyFunction);
auto tv = createTypeVariable(applyLocator,
TVO_CanBindToLValue |
TVO_CanBindToInOut |
TVO_PrefersSubtypeBinding);

// The constructor will have function type T -> T2, for a fresh type
Expand Down Expand Up @@ -4701,9 +4699,7 @@ ConstraintSystem::simplifyRestrictedConstraintImpl(
return SolutionKind::Error;

auto constraintLocator = getConstraintLocator(locator);
auto tv = createTypeVariable(constraintLocator,
TVO_PrefersSubtypeBinding |
TVO_CanBindToInOut);
auto tv = createTypeVariable(constraintLocator, TVO_PrefersSubtypeBinding);

addConstraint(ConstraintKind::ConformsTo, tv,
hashableProtocol->getDeclaredType(), constraintLocator);
Expand Down
3 changes: 1 addition & 2 deletions lib/Sema/CSSolver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1364,8 +1364,7 @@ ConstraintSystem::solve(Expr *&expr,
if (allowFreeTypeVariables == FreeTypeVariableBinding::UnresolvedType) {
convertType = convertType.transform([&](Type type) -> Type {
if (type->is<UnresolvedType>())
return createTypeVariable(getConstraintLocator(expr),
TVO_CanBindToInOut);
return createTypeVariable(getConstraintLocator(expr));
return type;
});
}
Expand Down
41 changes: 15 additions & 26 deletions lib/Sema/ConstraintSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -941,8 +941,7 @@ ConstraintSystem::getTypeOfReference(ValueDecl *value,
return { found->second, found->second };

auto typeVar = createTypeVariable(getConstraintLocator(locator),
TVO_CanBindToLValue |
TVO_CanBindToInOut);
TVO_CanBindToLValue);
addConstraint(ConstraintKind::BindParam, valueType, typeVar,
getConstraintLocator(locator));
OpenedParameterTypes.insert(std::make_pair(param, typeVar));
Expand Down Expand Up @@ -1081,6 +1080,7 @@ void ConstraintSystem::openGeneric(
locator.withPathElement(LocatorPathElt(archetype)));

auto typeVar = createTypeVariable(locatorPtr,
TVO_IsGenericTypeParam |
TVO_PrefersSubtypeBinding);
auto result = replacements.insert(
std::make_pair(cast<GenericTypeParamType>(gp->getCanonicalType()),
Expand Down Expand Up @@ -1467,11 +1467,9 @@ resolveOverloadForDeclWithSpecialTypeCheckingSemantics(ConstraintSystem &CS,
// existentials (as seen from the current abstraction level), which can't
// be expressed in the type system currently.
auto input = CS.createTypeVariable(
CS.getConstraintLocator(locator, ConstraintLocator::FunctionArgument),
TVO_CanBindToInOut);
CS.getConstraintLocator(locator, ConstraintLocator::FunctionArgument));
auto output = CS.createTypeVariable(
CS.getConstraintLocator(locator, ConstraintLocator::FunctionResult),
TVO_CanBindToInOut);
CS.getConstraintLocator(locator, ConstraintLocator::FunctionResult));

auto inputArg = TupleTypeElt(input, CS.getASTContext().getIdentifier("of"));
auto inputTuple = TupleType::get(inputArg, CS.getASTContext());
Expand All @@ -1487,17 +1485,14 @@ resolveOverloadForDeclWithSpecialTypeCheckingSemantics(ConstraintSystem &CS,
// receives a copy of the argument closure that is temporarily made
// @escaping.
auto noescapeClosure = CS.createTypeVariable(
CS.getConstraintLocator(locator, ConstraintLocator::FunctionArgument),
TVO_CanBindToInOut);
CS.getConstraintLocator(locator, ConstraintLocator::FunctionArgument));
auto escapeClosure = CS.createTypeVariable(
CS.getConstraintLocator(locator, ConstraintLocator::FunctionArgument),
TVO_CanBindToInOut);
CS.getConstraintLocator(locator, ConstraintLocator::FunctionArgument));
CS.addConstraint(ConstraintKind::EscapableFunctionOf,
escapeClosure, noescapeClosure,
CS.getConstraintLocator(locator, ConstraintLocator::RvalueAdjustment));
auto result = CS.createTypeVariable(
CS.getConstraintLocator(locator, ConstraintLocator::FunctionResult),
TVO_CanBindToInOut);
CS.getConstraintLocator(locator, ConstraintLocator::FunctionResult));
auto bodyClosure = FunctionType::get(
ParenType::get(CS.getASTContext(), escapeClosure), result,
FunctionType::ExtInfo(FunctionType::Representation::Swift,
Expand All @@ -1522,17 +1517,14 @@ resolveOverloadForDeclWithSpecialTypeCheckingSemantics(ConstraintSystem &CS,
// The body closure receives a freshly-opened archetype constrained by the
// existential type as its input.
auto openedTy = CS.createTypeVariable(
CS.getConstraintLocator(locator, ConstraintLocator::FunctionArgument),
TVO_CanBindToInOut);
CS.getConstraintLocator(locator, ConstraintLocator::FunctionArgument));
auto existentialTy = CS.createTypeVariable(
CS.getConstraintLocator(locator, ConstraintLocator::FunctionArgument),
TVO_CanBindToInOut);
CS.getConstraintLocator(locator, ConstraintLocator::FunctionArgument));
CS.addConstraint(ConstraintKind::OpenedExistentialOf,
openedTy, existentialTy,
CS.getConstraintLocator(locator, ConstraintLocator::RvalueAdjustment));
auto result = CS.createTypeVariable(
CS.getConstraintLocator(locator, ConstraintLocator::FunctionResult),
TVO_CanBindToInOut);
CS.getConstraintLocator(locator, ConstraintLocator::FunctionResult));
auto bodyClosure = FunctionType::get(
ParenType::get(CS.getASTContext(), openedTy), result,
FunctionType::ExtInfo(FunctionType::Representation::Swift,
Expand Down Expand Up @@ -1630,7 +1622,7 @@ void ConstraintSystem::resolveOverload(ConstraintLocator *locator,
if (choice.isImplicitlyUnwrappedValueOrReturnValue()) {
// Build the disjunction to attempt binding both T? and T (or
// function returning T? and function returning T).
Type ty = createTypeVariable(locator, TVO_CanBindToInOut);
Type ty = createTypeVariable(locator);
buildDisjunctionForImplicitlyUnwrappedOptional(ty, refType,
locator);
addConstraint(ConstraintKind::Bind, boundType,
Expand Down Expand Up @@ -1660,7 +1652,7 @@ void ConstraintSystem::resolveOverload(ConstraintLocator *locator,
// For our original type T -> U?? we will generate:
// A disjunction V = { U?, U }
// and a disjunction boundType = { T -> V?, T -> V }
Type ty = createTypeVariable(locator, TVO_CanBindToInOut);
Type ty = createTypeVariable(locator);

buildDisjunctionForImplicitlyUnwrappedOptional(ty, optTy, locator);

Expand Down Expand Up @@ -1770,15 +1762,12 @@ void ConstraintSystem::resolveOverload(ConstraintLocator *locator,
// The element type is T or @lvalue T based on the key path subtype and
// the mutability of the base.
auto keyPathIndexTy = createTypeVariable(
getConstraintLocator(locator, ConstraintLocator::FunctionArgument),
TVO_CanBindToInOut);
getConstraintLocator(locator, ConstraintLocator::FunctionArgument));
auto elementTy = createTypeVariable(
getConstraintLocator(locator, ConstraintLocator::FunctionArgument),
TVO_CanBindToLValue |
TVO_CanBindToInOut);
TVO_CanBindToLValue);
auto elementObjTy = createTypeVariable(
getConstraintLocator(locator, ConstraintLocator::FunctionArgument),
TVO_CanBindToInOut);
getConstraintLocator(locator, ConstraintLocator::FunctionArgument));
addConstraint(ConstraintKind::Equal, elementTy, elementObjTy, locator);

// The element result is an lvalue or rvalue based on the key path class.
Expand Down
17 changes: 9 additions & 8 deletions lib/Sema/ConstraintSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,8 @@ enum TypeVariableOptions {
/// Whether the type variable can be bound to an lvalue type or not.
TVO_CanBindToLValue = 0x01,

/// Whether the type variable can be bound to an inout type or not.
TVO_CanBindToInOut = 0x02,
/// Whether the type variable is a generic type parameter or not.
TVO_IsGenericTypeParam = 0x02,

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

/// Whether this type variable can bind to an inout type.
bool canBindToInOut() const { return getRawOptions() & TVO_CanBindToInOut; }
bool canBindToInOut() const {
return !(getRawOptions() & TVO_IsGenericTypeParam);
}

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

bool mustBeMaterializable() const {
return !(getRawOptions() & TVO_CanBindToInOut) &&
!(getRawOptions() & TVO_CanBindToLValue);
return !canBindToInOut() && !canBindToLValue();
}

/// Retrieve the corresponding node in the constraint graph.
Expand Down Expand Up @@ -347,7 +348,7 @@ class TypeVariableType::Implementation {
if (record)
recordBinding(*record);
getTypeVariable()->Bits.TypeVariableType.Options &= ~TVO_CanBindToLValue;
getTypeVariable()->Bits.TypeVariableType.Options &= ~TVO_CanBindToInOut;
getTypeVariable()->Bits.TypeVariableType.Options |=TVO_IsGenericTypeParam;
}
}

Expand Down Expand Up @@ -391,7 +392,7 @@ class TypeVariableType::Implementation {
rep->getImpl().getTypeVariable()->Bits.TypeVariableType.Options
&= ~TVO_CanBindToLValue;
rep->getImpl().getTypeVariable()->Bits.TypeVariableType.Options
&= ~TVO_CanBindToInOut;
|= TVO_IsGenericTypeParam;
}
}

Expand Down Expand Up @@ -1566,7 +1567,7 @@ class ConstraintSystem {

/// \brief Create a new type variable.
TypeVariableType *createTypeVariable(ConstraintLocator *locator,
unsigned options);
unsigned options = 0);

/// Retrieve the set of active type variables.
ArrayRef<TypeVariableType *> getTypeVariables() const {
Expand Down
18 changes: 6 additions & 12 deletions lib/Sema/TypeCheckConstraints.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1995,9 +1995,7 @@ bool TypeChecker::typeCheckCompletionSequence(Expr *&expr, DeclContext *DC) {

// Add type variable for the code-completion expression.
auto tvRHS =
CS.createTypeVariable(CS.getConstraintLocator(CCE),
TVO_CanBindToLValue |
TVO_CanBindToInOut);
CS.createTypeVariable(CS.getConstraintLocator(CCE), TVO_CanBindToLValue);
CCE->setType(tvRHS);

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

SequenceType =
cs.createTypeVariable(Locator, /*options=*/0);
SequenceType = cs.createTypeVariable(Locator);
cs.addConstraint(ConstraintKind::Conversion, cs.getType(expr),
SequenceType, Locator);
cs.addConstraint(ConstraintKind::ConformsTo, SequenceType,
Expand Down Expand Up @@ -2402,7 +2399,7 @@ bool TypeChecker::typeCheckForEachBinding(DeclContext *dc, ForEachStmt *stmt) {
}

if (elementType.isNull()) {
elementType = cs.createTypeVariable(elementLocator, /*options=*/0);
elementType = cs.createTypeVariable(elementLocator);
}

// Add a conversion constraint between the element type of the sequence
Expand Down Expand Up @@ -2486,8 +2483,7 @@ Type ConstraintSystem::computeAssignDestType(Expr *dest, SourceLoc equalLoc) {
if (auto typeVar = dyn_cast<TypeVariableType>(destTy.getPointer())) {
// Newly allocated type should be explicitly materializable,
// it's invalid to use non-materializable types as assignment destination.
auto objectTv = createTypeVariable(getConstraintLocator(dest),
/*options=*/0);
auto objectTv = createTypeVariable(getConstraintLocator(dest));
auto refTv = LValueType::get(objectTv);
addConstraint(ConstraintKind::Bind, typeVar, refTv,
getConstraintLocator(dest));
Expand Down Expand Up @@ -2711,8 +2707,7 @@ static Type replaceArchetypesWithTypeVariables(ConstraintSystem &cs,
return Type();

auto locator = cs.getConstraintLocator(nullptr);
auto replacement = cs.createTypeVariable(locator,
TVO_CanBindToInOut);
auto replacement = cs.createTypeVariable(locator);

if (auto superclass = archetypeType->getSuperclass()) {
cs.addConstraint(ConstraintKind::Subtype, replacement,
Expand All @@ -2729,8 +2724,7 @@ static Type replaceArchetypesWithTypeVariables(ConstraintSystem &cs,
// FIXME: Remove this case
assert(cast<GenericTypeParamType>(origType));
auto locator = cs.getConstraintLocator(nullptr);
auto replacement = cs.createTypeVariable(locator,
TVO_CanBindToInOut);
auto replacement = cs.createTypeVariable(locator);
types[origType] = replacement;
return replacement;
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors

// RUN: not %target-swift-frontend %s -emit-ir
// XFAIL: *
&[_=(&_