Skip to content

Commit e143fb3

Browse files
---
yaml --- r: 341215 b: refs/heads/rxwei-patch-1 c: 5c33156 h: refs/heads/master i: 341213: 8885165 341211: 23a65ac 341207: b03b577 341199: 2f46e41 341183: 78e2498
1 parent c19976f commit e143fb3

File tree

4 files changed

+61
-49
lines changed

4 files changed

+61
-49
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1015,7 +1015,7 @@ refs/tags/swift-DEVELOPMENT-SNAPSHOT-2018-08-18-a: b10b1fce14385faa6d44f6b933e95
10151015
refs/heads/rdar-43033749-fix-batch-mode-no-diags-swift-5.0-branch: a14e64eaad30de89f0f5f0b2a782eed7ecdcb255
10161016
refs/heads/revert-19006-error-bridging-integer-type: 8a9065a3696535305ea53fe9b71f91cbe6702019
10171017
refs/heads/revert-19050-revert-19006-error-bridging-integer-type: ecf752d54b05dd0a20f510f0bfa54a3fec3bcaca
1018-
refs/heads/rxwei-patch-1: 2ef18488d65486ddaec924baeac97bb10e9f2dfb
1018+
refs/heads/rxwei-patch-1: 5c33156417cd8b5b5d3e36bb5515b2f63b290b73
10191019
refs/heads/shahmishal-patch-1: e58ec0f7488258d42bef51bc3e6d7b3dc74d7b2a
10201020
refs/heads/typelist-existential: 4046359efd541fb5c72d69a92eefc0a784df8f5e
10211021
refs/tags/swift-4.2-DEVELOPMENT-SNAPSHOT-2018-08-20-a: 4319ba09e4fb8650ee86061075c74a016b6baab9

branches/rxwei-patch-1/lib/Sema/CSApply.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3707,7 +3707,7 @@ namespace {
37073707
Expr *sub = expr->getSubExpr();
37083708

37093709
cs.setExprTypes(sub);
3710-
if (tc.convertToType(sub, toType, cs.DC))
3710+
if (tc.convertToType(sub, toType, cs))
37113711
return nullptr;
37123712
cs.cacheExprTypes(sub);
37133713

branches/rxwei-patch-1/lib/Sema/TypeCheckConstraints.cpp

Lines changed: 49 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -2868,7 +2868,7 @@ bool TypeChecker::typeCheckForEachBinding(DeclContext *dc, ForEachStmt *stmt) {
28682868
SequenceType = solution.simplifyType(SequenceType);
28692869

28702870
// 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))) {
28722872
return nullptr;
28732873
}
28742874

@@ -3318,64 +3318,66 @@ Expr *TypeChecker::coerceToRValue(Expr *expr,
33183318
return expr;
33193319
}
33203320

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+
33213361
bool TypeChecker::convertToType(Expr *&expr, Type type, DeclContext *dc,
33223362
Optional<Pattern*> typeFromPattern) {
33233363
// TODO: need to add kind arg?
33243364
// Construct a constraint system from this expression.
33253365
ConstraintSystem cs(*this, dc, ConstraintSystemFlags::AllowFixes);
3326-
3327-
// We need to walk through the expression to set the type variables.
3328-
cs.generateConstraints(expr);
33293366

33303367
// If there is a type that we're expected to convert to, add the conversion
33313368
// constraint.
33323369
cs.addConstraint(ConstraintKind::Conversion, expr->getType(), type,
33333370
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+
33703372
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);
33753378
}
3376-
3377-
expr = result;
3378-
return false;
3379+
3380+
return convertToType(expr, type, cs, typeFromPattern);
33793381
}
33803382

33813383
//===----------------------------------------------------------------------===//

branches/rxwei-patch-1/lib/Sema/TypeChecker.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1550,6 +1550,16 @@ class TypeChecker final : public LazyResolver {
15501550
bool convertToType(Expr *&expr, Type type, DeclContext *dc,
15511551
Optional<Pattern*> typeFromPattern = None);
15521552

1553+
/// Convert the given expression to the given type.
1554+
///
1555+
/// \param expr The expression, which will be updated in place.
1556+
/// \param type The type to convert to.
1557+
/// \param cs The constraint system that will be used to the conversion.
1558+
///
1559+
/// \returns true if an error occurred, false otherwise.
1560+
bool convertToType(Expr *&expr, Type type, constraints::ConstraintSystem& cs,
1561+
Optional<Pattern*> typeFromPattern = None);
1562+
15531563
/// Coerce the given expression to materializable type, if it
15541564
/// isn't already.
15551565
Expr *coerceToRValue(Expr *expr,

0 commit comments

Comments
 (0)