Skip to content

Commit bbc0a95

Browse files
committed
[Constraint system] Simplify creation of contextual conversion constraint.
We were storing more state than necessary in the constraint system.
1 parent 9569714 commit bbc0a95

File tree

5 files changed

+48
-55
lines changed

5 files changed

+48
-55
lines changed

lib/Sema/CSGen.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3831,8 +3831,7 @@ bool ConstraintSystem::generateConstraints(StmtCondition condition,
38313831

38323832
case StmtConditionElement::CK_Boolean: {
38333833
Expr *condExpr = condElement.getBoolean();
3834-
setContextualType(condExpr, TypeLoc::withoutLoc(boolTy), CTP_Condition,
3835-
/*isOpaqueReturnType=*/false);
3834+
setContextualType(condExpr, TypeLoc::withoutLoc(boolTy), CTP_Condition);
38363835

38373836
condExpr = generateConstraints(condExpr, dc);
38383837
if (!condExpr) {

lib/Sema/CSSimplify.cpp

Lines changed: 39 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -9095,57 +9095,57 @@ void ConstraintSystem::addConstraint(ConstraintKind kind, Type first,
90959095
}
90969096

90979097
void ConstraintSystem::addContextualConversionConstraint(
9098-
Expr *expr, ContextualTypeInfo contextualType) {
9099-
Type convertType = contextualType.getType();
9100-
if (convertType.isNull())
9098+
Expr *expr, Type conversionType, ContextualTypePurpose purpose,
9099+
bool isOpaqueReturnType) {
9100+
if (conversionType.isNull())
91019101
return;
91029102

91039103
// Determine the type of the constraint.
91049104
auto constraintKind = ConstraintKind::Conversion;
9105-
switch (contextualType.purpose) {
9106-
case CTP_ReturnStmt:
9107-
case CTP_ReturnSingleExpr:
9108-
case CTP_Initialization:
9109-
if (contextualType.isOpaqueReturnType)
9110-
constraintKind = ConstraintKind::OpaqueUnderlyingType;
9111-
break;
9105+
switch (purpose) {
9106+
case CTP_ReturnStmt:
9107+
case CTP_ReturnSingleExpr:
9108+
case CTP_Initialization:
9109+
if (isOpaqueReturnType)
9110+
constraintKind = ConstraintKind::OpaqueUnderlyingType;
9111+
break;
91129112

9113-
case CTP_CallArgument:
9114-
constraintKind = ConstraintKind::ArgumentConversion;
9115-
break;
9113+
case CTP_CallArgument:
9114+
constraintKind = ConstraintKind::ArgumentConversion;
9115+
break;
91169116

9117-
case CTP_YieldByReference:
9118-
// In a by-reference yield, we expect the contextual type to be an
9119-
// l-value type, so the result must be bound to that.
9120-
constraintKind = ConstraintKind::Bind;
9121-
break;
9117+
case CTP_YieldByReference:
9118+
// In a by-reference yield, we expect the contextual type to be an
9119+
// l-value type, so the result must be bound to that.
9120+
constraintKind = ConstraintKind::Bind;
9121+
break;
91229122

9123-
case CTP_ArrayElement:
9124-
case CTP_AssignSource:
9125-
case CTP_CalleeResult:
9126-
case CTP_CannotFail:
9127-
case CTP_Condition:
9128-
case CTP_Unused:
9129-
case CTP_YieldByValue:
9130-
case CTP_ThrowStmt:
9131-
case CTP_EnumCaseRawValue:
9132-
case CTP_DefaultParameter:
9133-
case CTP_AutoclosureDefaultParameter:
9134-
case CTP_ClosureResult:
9135-
case CTP_DictionaryKey:
9136-
case CTP_DictionaryValue:
9137-
case CTP_CoerceOperand:
9138-
case CTP_SubscriptAssignSource:
9139-
case CTP_ForEachStmt:
9140-
break;
9123+
case CTP_ArrayElement:
9124+
case CTP_AssignSource:
9125+
case CTP_CalleeResult:
9126+
case CTP_CannotFail:
9127+
case CTP_Condition:
9128+
case CTP_Unused:
9129+
case CTP_YieldByValue:
9130+
case CTP_ThrowStmt:
9131+
case CTP_EnumCaseRawValue:
9132+
case CTP_DefaultParameter:
9133+
case CTP_AutoclosureDefaultParameter:
9134+
case CTP_ClosureResult:
9135+
case CTP_DictionaryKey:
9136+
case CTP_DictionaryValue:
9137+
case CTP_CoerceOperand:
9138+
case CTP_SubscriptAssignSource:
9139+
case CTP_ForEachStmt:
9140+
break;
91419141
}
91429142

91439143
// Add the constraint.
9144-
bool isForSingleExprFunction = (contextualType.purpose == CTP_ReturnSingleExpr);
9144+
bool isForSingleExprFunction = (purpose == CTP_ReturnSingleExpr);
91459145
auto *convertTypeLocator = getConstraintLocator(
91469146
expr, LocatorPathElt::ContextualType(isForSingleExprFunction));
9147-
addConstraint(constraintKind, getType(expr), convertType,
9148-
convertTypeLocator, /*isFavored*/ true);
9147+
addConstraint(constraintKind, getType(expr), conversionType,
9148+
convertTypeLocator, /*isFavored*/ true);
91499149
}
91509150

91519151
Type ConstraintSystem::addJoinConstraint(

lib/Sema/CSSolver.cpp

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -240,8 +240,7 @@ void ConstraintSystem::applySolution(const Solution &solution) {
240240
for (const auto &contextualType : solution.contextualTypes) {
241241
if (!getContextualTypeInfo(contextualType.first)) {
242242
setContextualType(contextualType.first, contextualType.second.typeLoc,
243-
contextualType.second.purpose,
244-
contextualType.second.isOpaqueReturnType);
243+
contextualType.second.purpose);
245244
}
246245
}
247246

@@ -1243,8 +1242,6 @@ ConstraintSystem::solveImpl(SolutionApplicationTarget &target,
12431242
Expr *expr = target.getAsExpr();
12441243
Timer.emplace(expr, *this);
12451244

1246-
Expr *origExpr = expr;
1247-
12481245
// Try to shrink the system by reducing disjunction domains. This
12491246
// goes through every sub-expression and generate its own sub-system, to
12501247
// try to reduce the domains of those subexpressions.
@@ -1277,9 +1274,8 @@ ConstraintSystem::solveImpl(SolutionApplicationTarget &target,
12771274
});
12781275
}
12791276

1280-
ContextualTypeInfo info{
1281-
TypeLoc::withoutLoc(convertType), ctp, isOpaqueReturnType};
1282-
addContextualConversionConstraint(expr, info);
1277+
addContextualConversionConstraint(expr, convertType, ctp,
1278+
isOpaqueReturnType);
12831279
}
12841280

12851281
// Notify the listener that we've built the constraint system.

lib/Sema/ConstraintSystem.h

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -792,7 +792,6 @@ using OpenedTypeMap =
792792
struct ContextualTypeInfo {
793793
TypeLoc typeLoc;
794794
ContextualTypePurpose purpose;
795-
bool isOpaqueReturnType = false;
796795

797796
Type getType() const { return typeLoc.getType(); }
798797
};
@@ -2336,12 +2335,11 @@ class ConstraintSystem {
23362335
}
23372336

23382337
void setContextualType(
2339-
const Expr *expr, TypeLoc T, ContextualTypePurpose purpose,
2340-
bool isOpaqueReturnType) {
2338+
const Expr *expr, TypeLoc T, ContextualTypePurpose purpose) {
23412339
assert(expr != nullptr && "Expected non-null expression!");
23422340
assert(contextualTypes.count(expr) == 0 &&
23432341
"Already set this contextual type");
2344-
contextualTypes[expr] = { T, purpose, isOpaqueReturnType };
2342+
contextualTypes[expr] = { T, purpose };
23452343
}
23462344

23472345
Optional<ContextualTypeInfo> getContextualTypeInfo(const Expr *expr) const {
@@ -2573,7 +2571,8 @@ class ConstraintSystem {
25732571

25742572
/// Add the appropriate constraint for a contextual conversion.
25752573
void addContextualConversionConstraint(
2576-
Expr *expr, ContextualTypeInfo contextualType);
2574+
Expr *expr, Type conversionType, ContextualTypePurpose purpose,
2575+
bool isOpaqueReturnType);
25772576

25782577
/// Add a "join" constraint between a set of types, producing the common
25792578
/// supertype.

lib/Sema/TypeCheckConstraints.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2134,8 +2134,7 @@ TypeChecker::typeCheckExpression(
21342134
cs.setContextualType(
21352135
contextualTypeExpr,
21362136
target.getExprContextualTypeLoc(),
2137-
target.getExprContextualTypePurpose(),
2138-
target.infersOpaqueReturnType());
2137+
target.getExprContextualTypePurpose());
21392138

21402139
// If the client can handle unresolved type variables, leave them in the
21412140
// system.

0 commit comments

Comments
 (0)