Skip to content

Commit ecd25a7

Browse files
authored
Merge pull request #18970 from slavapestov/cs-cleanups
Small constraint solver cleanups
2 parents 5ca7700 + 4ed51fb commit ecd25a7

File tree

2 files changed

+15
-88
lines changed

2 files changed

+15
-88
lines changed

lib/Sema/CSApply.cpp

Lines changed: 8 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -5104,46 +5104,18 @@ Expr *ExprRewriter::coerceTupleToTuple(Expr *expr, TupleType *fromTuple,
51045104
auto *fromTupleExpr = dyn_cast<TupleExpr>(innerExpr);
51055105

51065106
/// Check each of the tuple elements in the destination.
5107-
bool hasVariadic = false;
5108-
unsigned variadicParamIdx = toTuple->getNumElements();
51095107
bool anythingShuffled = false;
5110-
bool hasInits = false;
51115108
SmallVector<TupleTypeElt, 4> toSugarFields;
51125109
SmallVector<TupleTypeElt, 4> fromTupleExprFields(
51135110
fromTuple->getElements().size());
5114-
SmallVector<Expr *, 2> callerDefaultArgs;
5115-
ConcreteDeclRef callee =
5116-
findCalleeDeclRef(cs, solution, cs.getConstraintLocator(locator));
51175111

51185112
for (unsigned i = 0, n = toTuple->getNumElements(); i != n; ++i) {
5113+
assert(sources[i] != TupleShuffleExpr::DefaultInitialize &&
5114+
sources[i] != TupleShuffleExpr::Variadic);
5115+
51195116
const auto &toElt = toTuple->getElement(i);
51205117
auto toEltType = toElt.getType();
51215118

5122-
// If we're default-initializing this member, there's nothing to do.
5123-
if (sources[i] == TupleShuffleExpr::DefaultInitialize) {
5124-
anythingShuffled = true;
5125-
hasInits = true;
5126-
toSugarFields.push_back(toElt);
5127-
5128-
// Create a caller-side default argument, if we need one.
5129-
if (auto defArg = getCallerDefaultArg(cs, dc, expr->getLoc(),
5130-
callee, i).first) {
5131-
callerDefaultArgs.push_back(defArg);
5132-
sources[i] = TupleShuffleExpr::CallerDefaultInitialize;
5133-
}
5134-
continue;
5135-
}
5136-
5137-
// If this is the variadic argument, note it.
5138-
if (sources[i] == TupleShuffleExpr::Variadic) {
5139-
assert(!hasVariadic && "two variadic parameters?");
5140-
toSugarFields.push_back(toElt);
5141-
hasVariadic = true;
5142-
variadicParamIdx = i;
5143-
anythingShuffled = true;
5144-
continue;
5145-
}
5146-
51475119
// If the source and destination index are different, we'll be shuffling.
51485120
if ((unsigned)sources[i] != i) {
51495121
anythingShuffled = true;
@@ -5200,51 +5172,6 @@ Expr *ExprRewriter::coerceTupleToTuple(Expr *expr, TupleType *fromTuple,
52005172
fromElt.getWithType(cs.getType(convertedElt));
52015173
}
52025174

5203-
// Convert all of the variadic arguments to the destination type.
5204-
ArraySliceType *arrayType = nullptr;
5205-
if (hasVariadic) {
5206-
Type toEltType = toTuple->getElements()[variadicParamIdx].getVarargBaseTy();
5207-
for (int fromFieldIdx : variadicArgs) {
5208-
const auto &fromElt = fromTuple->getElement(fromFieldIdx);
5209-
Type fromEltType = fromElt.getType();
5210-
5211-
// If the source and destination types match, there's nothing to do.
5212-
if (toEltType->isEqual(fromEltType)) {
5213-
fromTupleExprFields[fromFieldIdx] = fromElt;
5214-
continue;
5215-
}
5216-
5217-
// We need to convert the source element to the destination type.
5218-
if (!fromTupleExpr) {
5219-
// FIXME: Lame! We can't express this in the AST.
5220-
tc.diagnose(expr->getLoc(),
5221-
diag::tuple_conversion_not_expressible,
5222-
fromTuple, toTuple);
5223-
return nullptr;
5224-
}
5225-
5226-
// Actually convert the source element.
5227-
auto convertedElt = coerceToType(
5228-
fromTupleExpr->getElement(fromFieldIdx),
5229-
toEltType,
5230-
locator.withPathElement(
5231-
LocatorPathElt::getTupleElement(fromFieldIdx)));
5232-
if (!convertedElt)
5233-
return nullptr;
5234-
5235-
fromTupleExpr->setElement(fromFieldIdx, convertedElt);
5236-
5237-
fromTupleExprFields[fromFieldIdx] =
5238-
fromElt.getWithType(cs.getType(convertedElt));
5239-
}
5240-
5241-
// Find the appropriate injection function.
5242-
if (tc.requireArrayLiteralIntrinsics(expr->getStartLoc()))
5243-
return nullptr;
5244-
arrayType = cast<ArraySliceType>(
5245-
toTuple->getElements()[variadicParamIdx].getType().getPointer());
5246-
}
5247-
52485175
// Compute the updated 'from' tuple type, since we may have
52495176
// performed some conversions in place.
52505177
Type fromTupleType = TupleType::get(fromTupleExprFields, tc.Context);
@@ -5256,8 +5183,7 @@ Expr *ExprRewriter::coerceTupleToTuple(Expr *expr, TupleType *fromTuple,
52565183
}
52575184

52585185
// Compute the re-sugared tuple type.
5259-
Type toSugarType = hasInits? toTuple
5260-
: TupleType::get(toSugarFields, tc.Context);
5186+
Type toSugarType = TupleType::get(toSugarFields, tc.Context);
52615187

52625188
// If we don't have to shuffle anything, we're done.
52635189
if (!anythingShuffled && fromTupleExpr) {
@@ -5272,13 +5198,10 @@ Expr *ExprRewriter::coerceTupleToTuple(Expr *expr, TupleType *fromTuple,
52725198
// Create the tuple shuffle.
52735199
return
52745200
cs.cacheType(TupleShuffleExpr::create(tc.Context,
5275-
expr, sources,
5276-
TupleShuffleExpr::TupleToTuple,
5277-
callee,
5278-
variadicArgs,
5279-
arrayType,
5280-
callerDefaultArgs,
5281-
toSugarType));
5201+
expr, sources,
5202+
TupleShuffleExpr::TupleToTuple,
5203+
ConcreteDeclRef(), {}, Type(), {},
5204+
toSugarType));
52825205
}
52835206

52845207
static Type getMetatypeSuperclass(Type t, TypeChecker &tc) {

lib/Sema/CSSimplify.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -883,6 +883,10 @@ matchCallArguments(ConstraintSystem &cs, ConstraintKind kind,
883883
paramIdx));
884884
auto argTy = args[argIdx].getType();
885885

886+
// FIXME: This should be revisited. If one of argTy or paramTy
887+
// is a type variable, matchTypes() will add a constraint, and
888+
// when the constraint is later solved, we will have lost the
889+
// value of 'subflags'.
886890
if (!haveOneNonUserConversion) {
887891
subflags |= ConstraintSystem::TMF_ApplyingOperatorParameter;
888892
}
@@ -1403,7 +1407,7 @@ ConstraintSystem::matchExistentialTypes(Type type1, Type type2,
14031407

14041408
// Conformance to 'Any' always holds.
14051409
if (type2->isAny()) {
1406-
auto *fnTy = type1->getAs<AnyFunctionType>();
1410+
auto *fnTy = type1->getAs<FunctionType>();
14071411
if (!fnTy || !fnTy->isNoEscape())
14081412
return getTypeMatchSuccess();
14091413

@@ -1602,7 +1606,7 @@ ConstraintSystem::matchTypesBindTypeVar(
16021606
// Disallow bindings of noescape functions to type variables that
16031607
// represent an opened archetype. If we allowed this it would allow
16041608
// the noescape function to potentially escape.
1605-
if (auto *fnTy = type->getAs<AnyFunctionType>()) {
1609+
if (auto *fnTy = type->getAs<FunctionType>()) {
16061610
if (fnTy->isNoEscape() && typeVar->getImpl().getArchetype()) {
16071611
if (shouldAttemptFixes()) {
16081612
auto *fix = MarkExplicitlyEscaping::create(
@@ -2136,7 +2140,7 @@ ConstraintSystem::matchTypes(Type type1, Type type2, ConstraintKind kind,
21362140
// Penalize conversions to Any, and disallow conversions of
21372141
// noescape functions to Any.
21382142
if (kind >= ConstraintKind::Conversion && type2->isAny()) {
2139-
if (auto *fnTy = type1->getAs<AnyFunctionType>()) {
2143+
if (auto *fnTy = type1->getAs<FunctionType>()) {
21402144
if (fnTy->isNoEscape()) {
21412145
if (shouldAttemptFixes()) {
21422146
auto &ctx = getASTContext();

0 commit comments

Comments
 (0)