@@ -6792,12 +6792,25 @@ Expr *ExprRewriter::coerceToType(Expr *expr, Type toType,
6792
6792
case ConversionRestrictionKind::DoubleToCGFloat: {
6793
6793
auto conversionKind = knownRestriction->second ;
6794
6794
6795
- // If conversion wraps the whole body of a single-expression closure,
6796
- // let's use the passed-in expression since the closure itself doesn't
6797
- // get updated until coercion is done.
6798
- auto *argExpr = locator.endsWith <LocatorPathElt::ClosureBody>()
6799
- ? expr
6800
- : locator.trySimplifyToExpr ();
6795
+ auto shouldUseCoercedExpr = [&]() {
6796
+ // If conversion wraps the whole body of a single-expression closure,
6797
+ // let's use the passed-in expression since the closure itself doesn't
6798
+ // get updated until coercion is done.
6799
+ if (locator.endsWith <LocatorPathElt::ClosureBody>())
6800
+ return true ;
6801
+
6802
+ // Contextual type locator always uses the original version of
6803
+ // expression (before any coercions have been applied) because
6804
+ // otherwise it wouldn't be possible to find the overload choice.
6805
+ if (locator.endsWith <LocatorPathElt::ContextualType>())
6806
+ return true ;
6807
+
6808
+ // In all other cases use the expression associated with locator.
6809
+ return false ;
6810
+ };
6811
+
6812
+ auto *argExpr =
6813
+ shouldUseCoercedExpr () ? expr : locator.trySimplifyToExpr ();
6801
6814
assert (argExpr);
6802
6815
6803
6816
// Source requires implicit conversion to match destination
@@ -8575,7 +8588,7 @@ static Optional<SolutionApplicationTarget> applySolutionToInitialization(
8575
8588
// Convert the initializer to the type of the pattern.
8576
8589
auto &cs = solution.getConstraintSystem ();
8577
8590
auto locator = cs.getConstraintLocator (
8578
- initializer , LocatorPathElt::ContextualType (CTP_Initialization));
8591
+ target. getAsExpr () , LocatorPathElt::ContextualType (CTP_Initialization));
8579
8592
initializer = solution.coerceToType (initializer, initType, locator);
8580
8593
if (!initializer)
8581
8594
return None;
@@ -9087,10 +9100,12 @@ ExprWalker::rewriteTarget(SolutionApplicationTarget target) {
9087
9100
// do so now.
9088
9101
if (shouldCoerceToContextualType ()) {
9089
9102
ConstraintLocator *locator = nullptr ;
9103
+
9104
+ auto contextualTypePurpose = target.getExprContextualTypePurpose ();
9090
9105
// Bodies of single-expression closures use a special locator
9091
9106
// for contextual type conversion to make sure that result is
9092
9107
// convertible to `Void` when `return` is not used explicitly.
9093
- if (target. getExprContextualTypePurpose () == CTP_ClosureResult) {
9108
+ if (contextualTypePurpose == CTP_ClosureResult) {
9094
9109
auto *closure = cast<ClosureExpr>(target.getDeclContext ());
9095
9110
auto *returnStmt =
9096
9111
castToStmt<ReturnStmt>(closure->getBody ()->getLastElement ());
@@ -9100,8 +9115,7 @@ ExprWalker::rewriteTarget(SolutionApplicationTarget target) {
9100
9115
/* hasReturn=*/ !returnStmt->isImplicit ()));
9101
9116
} else {
9102
9117
locator = cs.getConstraintLocator (
9103
- resultExpr, LocatorPathElt::ContextualType (
9104
- target.getExprContextualTypePurpose ()));
9118
+ expr, LocatorPathElt::ContextualType (contextualTypePurpose));
9105
9119
}
9106
9120
9107
9121
assert (locator);
@@ -9113,7 +9127,7 @@ ExprWalker::rewriteTarget(SolutionApplicationTarget target) {
9113
9127
// We referenced an lvalue. Load it.
9114
9128
resultExpr = Rewriter.coerceToType (
9115
9129
resultExpr, cs.getType (resultExpr)->getRValueType (),
9116
- cs.getConstraintLocator (resultExpr ,
9130
+ cs.getConstraintLocator (expr ,
9117
9131
LocatorPathElt::ContextualType (
9118
9132
target.getExprContextualTypePurpose ())));
9119
9133
}
0 commit comments