Skip to content

Commit 8b5a639

Browse files
authored
Merge pull request #29778 from DougGregor/more-sinking
[Constraint system] Sink optional “some” pattern handling into constrain generation
2 parents 97df90f + a308a7e commit 8b5a639

File tree

4 files changed

+36
-37
lines changed

4 files changed

+36
-37
lines changed

lib/Sema/CSApply.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7441,6 +7441,13 @@ Optional<SolutionApplicationTarget> ConstraintSystem::applySolution(
74417441

74427442
solution.setExprTypes(resultExpr);
74437443
result.setExpr(resultExpr);
7444+
7445+
if (Context.TypeCheckerOpts.DebugConstraintSolver) {
7446+
auto &log = Context.TypeCheckerDebug->getStream();
7447+
log << "---Type-checked expression---\n";
7448+
resultExpr->dump(log);
7449+
log << "\n";
7450+
}
74447451
}
74457452

74467453
rewriter.finalize();

lib/Sema/CSGen.cpp

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3897,11 +3897,17 @@ bool ConstraintSystem::generateConstraints(
38973897
SolutionApplicationTarget &target,
38983898
FreeTypeVariableBinding allowFreeTypeVariables) {
38993899
if (Expr *expr = target.getAsExpr()) {
3900-
// Try to shrink the system by reducing disjunction domains. This
3901-
// goes through every sub-expression and generate its own sub-system, to
3902-
// try to reduce the domains of those subexpressions.
3903-
shrink(expr);
3904-
target.setExpr(expr);
3900+
// If the target requires an optional of some type, form a new appropriate
3901+
// type variable and update the target's type with an optional of that
3902+
// type variable.
3903+
if (target.isOptionalSomePatternInit()) {
3904+
assert(!target.getExprContextualType() &&
3905+
"some pattern cannot have contextual type pre-configured");
3906+
auto *convertTypeLocator =
3907+
getConstraintLocator(expr, LocatorPathElt::ContextualType());
3908+
Type var = createTypeVariable(convertTypeLocator, TVO_CanBindToNoEscape);
3909+
target.setExprConversionType(TypeChecker::getOptionalType(expr->getLoc(), var));
3910+
}
39053911

39063912
// Generate constraints for the main system.
39073913
expr = generateConstraints(expr, target.getDeclContext());
@@ -3941,6 +3947,14 @@ bool ConstraintSystem::generateConstraints(
39413947
return true;
39423948
}
39433949

3950+
if (getASTContext().TypeCheckerOpts.DebugConstraintSolver) {
3951+
auto &log = getASTContext().TypeCheckerDebug->getStream();
3952+
log << "---Initial constraints for the given expression---\n";
3953+
print(log, expr);
3954+
log << "\n";
3955+
print(log);
3956+
}
3957+
39443958
return false;
39453959
}
39463960

lib/Sema/CSSolver.cpp

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1252,17 +1252,10 @@ ConstraintSystem::solveImpl(SolutionApplicationTarget &target,
12521252
return SolutionResult::forError();;
12531253

12541254
// Notify the listener that we've built the constraint system.
1255-
Expr *expr = target.getAsExpr();
1256-
if (listener && listener->builtConstraints(*this, expr)) {
1257-
return SolutionResult::forError();
1258-
}
1259-
1260-
if (getASTContext().TypeCheckerOpts.DebugConstraintSolver) {
1261-
auto &log = getASTContext().TypeCheckerDebug->getStream();
1262-
log << "---Initial constraints for the given expression---\n";
1263-
print(log, expr);
1264-
log << "\n";
1265-
print(log);
1255+
if (Expr *expr = target.getAsExpr()) {
1256+
if (listener && listener->builtConstraints(*this, expr)) {
1257+
return SolutionResult::forError();
1258+
}
12661259
}
12671260

12681261
// Try to solve the constraint system using computed suggestions.
@@ -1272,8 +1265,6 @@ ConstraintSystem::solveImpl(SolutionApplicationTarget &target,
12721265
if (getExpressionTooComplex(solutions))
12731266
return SolutionResult::forTooComplex();
12741267

1275-
target.setExpr(expr);
1276-
12771268
switch (solutions.size()) {
12781269
case 0:
12791270
return SolutionResult::forUndiagnosedError();

lib/Sema/TypeCheckConstraints.cpp

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2079,24 +2079,18 @@ TypeChecker::typeCheckExpression(
20792079
target.getExprContextualTypeLoc(),
20802080
target.getExprContextualTypePurpose());
20812081

2082+
// Try to shrink the system by reducing disjunction domains. This
2083+
// goes through every sub-expression and generate its own sub-system, to
2084+
// try to reduce the domains of those subexpressions.
2085+
cs.shrink(expr);
2086+
target.setExpr(expr);
2087+
20822088
// If the client can handle unresolved type variables, leave them in the
20832089
// system.
20842090
auto allowFreeTypeVariables = FreeTypeVariableBinding::Disallow;
20852091
if (options.contains(TypeCheckExprFlags::AllowUnresolvedTypeVariables))
20862092
allowFreeTypeVariables = FreeTypeVariableBinding::UnresolvedType;
20872093

2088-
// If the target requires an optional of some type, form a new appropriate
2089-
// type variable and update the target's type with an optional of that
2090-
// type variable.
2091-
if (target.isOptionalSomePatternInit()) {
2092-
assert(!target.getExprContextualType() &&
2093-
"some pattern cannot have contextual type pre-configured");
2094-
auto *convertTypeLocator =
2095-
cs.getConstraintLocator(expr, LocatorPathElt::ContextualType());
2096-
Type var = cs.createTypeVariable(convertTypeLocator, TVO_CanBindToNoEscape);
2097-
target.setExprConversionType(getOptionalType(expr->getLoc(), var));
2098-
}
2099-
21002094
// Attempt to solve the constraint system.
21012095
auto viable = cs.solve(target, listener, allowFreeTypeVariables);
21022096
if (!viable) {
@@ -2138,13 +2132,6 @@ TypeChecker::typeCheckExpression(
21382132
return None;
21392133
}
21402134

2141-
if (Context.TypeCheckerOpts.DebugConstraintSolver) {
2142-
auto &log = Context.TypeCheckerDebug->getStream();
2143-
log << "---Type-checked expression---\n";
2144-
result->dump(log);
2145-
log << "\n";
2146-
}
2147-
21482135
// Unless the client has disabled them, perform syntactic checks on the
21492136
// expression now.
21502137
if (!cs.shouldSuppressDiagnostics() &&

0 commit comments

Comments
 (0)