@@ -2868,7 +2868,7 @@ bool TypeChecker::typeCheckForEachBinding(DeclContext *dc, ForEachStmt *stmt) {
2868
2868
SequenceType = solution.simplifyType (SequenceType);
2869
2869
2870
2870
// Perform any necessary conversions of the sequence (e.g. [T]! -> [T]).
2871
- if (tc. convertToType (expr, SequenceType, cs.DC )) {
2871
+ if (solution. coerceToType (expr, SequenceType, cs.getConstraintLocator (expr) )) {
2872
2872
return nullptr ;
2873
2873
}
2874
2874
@@ -3318,64 +3318,66 @@ Expr *TypeChecker::coerceToRValue(Expr *expr,
3318
3318
return expr;
3319
3319
}
3320
3320
3321
+ bool TypeChecker::convertToType (Expr *&expr, Type type, constraints::ConstraintSystem& cs,
3322
+ Optional<Pattern*> typeFromPattern) {
3323
+ // Attempt to solve the constraint system.
3324
+ SmallVector<Solution, 4 > viable;
3325
+ if ((cs.solve (expr, viable) || viable.size () != 1 ) &&
3326
+ cs.salvage (viable, expr)) {
3327
+ return true ;
3328
+ }
3329
+
3330
+ auto &solution = viable[0 ];
3331
+ if (getLangOpts ().DebugConstraintSolver ) {
3332
+ auto &log = Context.TypeCheckerDebug ->getStream ();
3333
+ log << " ---Solution---\n " ;
3334
+ solution.dump (log);
3335
+ }
3336
+
3337
+ cs.cacheExprTypes (expr);
3338
+
3339
+ // Perform the conversion.
3340
+ Expr *result = solution.coerceToType (expr, type,
3341
+ cs.getConstraintLocator (expr),
3342
+ /* ignoreTopLevelInjection*/ false ,
3343
+ typeFromPattern);
3344
+ if (!result) {
3345
+ return true ;
3346
+ }
3347
+
3348
+ cs.setExprTypes (expr);
3349
+
3350
+ if (getLangOpts ().DebugConstraintSolver ) {
3351
+ auto &log = Context.TypeCheckerDebug ->getStream ();
3352
+ log << " ---Type-checked expression---\n " ;
3353
+ result->dump (log);
3354
+ log << " \n " ;
3355
+ }
3356
+
3357
+ expr = result;
3358
+ return false ;
3359
+ }
3360
+
3321
3361
bool TypeChecker::convertToType (Expr *&expr, Type type, DeclContext *dc,
3322
3362
Optional<Pattern*> typeFromPattern) {
3323
3363
// TODO: need to add kind arg?
3324
3364
// Construct a constraint system from this expression.
3325
3365
ConstraintSystem cs (*this , dc, ConstraintSystemFlags::AllowFixes);
3326
-
3327
- // We need to walk through the expression to set the type variables.
3328
- cs.generateConstraints (expr);
3329
3366
3330
3367
// If there is a type that we're expected to convert to, add the conversion
3331
3368
// constraint.
3332
3369
cs.addConstraint (ConstraintKind::Conversion, expr->getType (), type,
3333
3370
cs.getConstraintLocator (expr));
3334
-
3335
- if (getLangOpts ().DebugConstraintSolver ) {
3336
- auto &log = Context.TypeCheckerDebug ->getStream ();
3337
- log << " ---Initial constraints for the given expression---\n " ;
3338
- expr->dump (log);
3339
- log << " \n " ;
3340
- cs.print (log);
3341
- }
3342
-
3343
- // Attempt to solve the constraint system.
3344
- SmallVector<Solution, 4 > viable;
3345
- if ((cs.solve (expr, viable) || viable.size () != 1 ) &&
3346
- cs.salvage (viable, expr)) {
3347
- return true ;
3348
- }
3349
-
3350
- auto &solution = viable[0 ];
3351
- if (getLangOpts ().DebugConstraintSolver ) {
3352
- auto &log = Context.TypeCheckerDebug ->getStream ();
3353
- log << " ---Solution---\n " ;
3354
- solution.dump (log);
3355
- }
3356
-
3357
- cs.cacheExprTypes (expr);
3358
-
3359
- // Perform the conversion.
3360
- Expr *result = solution.coerceToType (expr, type,
3361
- cs.getConstraintLocator (expr),
3362
- /* ignoreTopLevelInjection*/ false ,
3363
- typeFromPattern);
3364
- if (!result) {
3365
- return true ;
3366
- }
3367
-
3368
- cs.setExprTypes (expr);
3369
-
3371
+
3370
3372
if (getLangOpts ().DebugConstraintSolver ) {
3371
- auto &log = Context.TypeCheckerDebug ->getStream ();
3372
- log << " ---Type-checked expression---\n " ;
3373
- result->dump (log);
3374
- log << " \n " ;
3373
+ auto &log = Context.TypeCheckerDebug ->getStream ();
3374
+ log << " ---Initial constraints for the given expression---\n " ;
3375
+ expr->dump (log);
3376
+ log << " \n " ;
3377
+ cs.print (log);
3375
3378
}
3376
-
3377
- expr = result;
3378
- return false ;
3379
+
3380
+ return convertToType (expr, type, cs, typeFromPattern);
3379
3381
}
3380
3382
3381
3383
// ===----------------------------------------------------------------------===//
0 commit comments