@@ -1242,32 +1242,7 @@ class SyntacticElementConstraintGenerator
1242
1242
}
1243
1243
1244
1244
void visitReturnStmt (ReturnStmt *returnStmt) {
1245
- // Single-expression closures are effectively a `return` statement,
1246
- // so let's give them a special locator as to indicate that.
1247
- // Return statements might not have a result if we have a closure whose
1248
- // implicit returned value is coerced to Void.
1249
- if (context.isSingleExpressionClosure (cs) && returnStmt->hasResult ()) {
1250
- auto *expr = returnStmt->getResult ();
1251
- assert (expr && " single expression closure without expression?" );
1252
-
1253
- expr = cs.generateConstraints (expr, context.getAsDeclContext ());
1254
- if (!expr) {
1255
- hadError = true ;
1256
- return ;
1257
- }
1258
-
1259
- auto contextualResultInfo = getContextualResultInfo ();
1260
- cs.addConstraint (ConstraintKind::Conversion, cs.getType (expr),
1261
- contextualResultInfo.getType (),
1262
- cs.getConstraintLocator (
1263
- context.getAsAbstractClosureExpr ().get (),
1264
- LocatorPathElt::ClosureBody (
1265
- /* hasImpliedReturn=*/ returnStmt->isImplied ())));
1266
- return ;
1267
- }
1268
-
1269
1245
Expr *resultExpr;
1270
-
1271
1246
if (returnStmt->hasResult ()) {
1272
1247
resultExpr = returnStmt->getResult ();
1273
1248
assert (resultExpr && " non-empty result without expression?" );
@@ -1280,9 +1255,18 @@ class SyntacticElementConstraintGenerator
1280
1255
}
1281
1256
1282
1257
auto contextualResultInfo = getContextualResultInfo ();
1258
+
1259
+ // Single-expression closures need their contextual type locator anchored
1260
+ // on the closure itself.
1261
+ if (context.isSingleExpressionClosure (cs) && returnStmt->hasResult ()) {
1262
+ contextualResultInfo.locator = cs.getConstraintLocator (
1263
+ context.getAsAbstractClosureExpr ().get (),
1264
+ {LocatorPathElt::ClosureBody (
1265
+ /* hasImpliedReturn=*/ returnStmt->isImplied ())});
1266
+ }
1267
+
1283
1268
SyntacticElementTarget target (resultExpr, context.getAsDeclContext (),
1284
- contextualResultInfo,
1285
- /* isDiscarded=*/ false );
1269
+ contextualResultInfo, /* isDiscarded=*/ false );
1286
1270
1287
1271
if (cs.generateConstraints (target)) {
1288
1272
hadError = true ;
@@ -2155,22 +2139,15 @@ class SyntacticElementSolutionApplication
2155
2139
mode = convertToResult;
2156
2140
}
2157
2141
2158
- llvm::Optional<SyntacticElementTarget> resultTarget;
2159
- if (auto target = cs.getTargetFor (returnStmt)) {
2160
- resultTarget = *target;
2161
- } else {
2162
- // Single-expression closures have to handle returns in a special
2163
- // way so the target has to be created for them during solution
2164
- // application based on the resolved type.
2165
- assert (context.isSingleExpressionClosure (cs));
2166
- resultTarget = SyntacticElementTarget (
2167
- resultExpr, context.getAsDeclContext (),
2168
- mode == convertToResult ? CTP_ClosureResult : CTP_Unused,
2169
- mode == convertToResult ? resultType : Type (),
2170
- /* isDiscarded=*/ false );
2171
- }
2172
-
2173
- if (auto newResultTarget = rewriteTarget (*resultTarget)) {
2142
+ auto target = *cs.getTargetFor (returnStmt);
2143
+
2144
+ // If we're not converting to a result, unset the contextual type.
2145
+ if (mode != convertToResult) {
2146
+ target.setExprConversionType (Type ());
2147
+ target.setExprContextualTypePurpose (CTP_Unused);
2148
+ }
2149
+
2150
+ if (auto newResultTarget = rewriteTarget (target)) {
2174
2151
resultExpr = newResultTarget->getAsExpr ();
2175
2152
}
2176
2153
0 commit comments