@@ -26,10 +26,10 @@ using namespace swift::constraints;
2626namespace {
2727
2828// Produce an implicit empty tuple expression.
29- Expr *getVoidExpr (ASTContext &ctx) {
29+ Expr *getVoidExpr (ASTContext &ctx, SourceLoc contextLoc = SourceLoc() ) {
3030 auto *voidExpr = TupleExpr::createEmpty (ctx,
31- /* LParenLoc=*/ SourceLoc () ,
32- /* RParenLoc=*/ SourceLoc () ,
31+ /* LParenLoc=*/ contextLoc ,
32+ /* RParenLoc=*/ contextLoc ,
3333 /* Implicit=*/ true );
3434 voidExpr->setType (ctx.TheEmptyTupleType );
3535 return voidExpr;
@@ -885,11 +885,13 @@ class SyntacticElementConstraintGenerator
885885 } else {
886886 // If this is simplify `return`, let's create an empty tuple
887887 // which is also useful if contextual turns out to be e.g. `Void?`.
888- resultExpr = getVoidExpr (cs.getASTContext ());
888+ // Also, attach return stmt source location so if there is a contextual
889+ // mismatch we can produce a diagnostic in a valid source location.
890+ resultExpr = getVoidExpr (cs.getASTContext (), returnStmt->getEndLoc ());
889891 }
890892
891893 SolutionApplicationTarget target (resultExpr, context.getAsDeclContext (),
892- CTP_ReturnStmt , resultType,
894+ CTP_ClosureResult , resultType,
893895 /* isDiscarded=*/ false );
894896
895897 if (cs.generateConstraints (target, FreeTypeVariableBinding::Disallow)) {
@@ -898,7 +900,7 @@ class SyntacticElementConstraintGenerator
898900 }
899901
900902 cs.setContextualType (target.getAsExpr (), TypeLoc::withoutLoc (resultType),
901- CTP_ReturnStmt );
903+ CTP_ClosureResult );
902904 cs.setSolutionApplicationTarget (returnStmt, target);
903905 }
904906
@@ -1508,7 +1510,7 @@ class SyntacticElementSolutionApplication
15081510 // number of times.
15091511 {
15101512 SolutionApplicationTarget target (resultExpr, context.getAsDeclContext (),
1511- CTP_ReturnStmt , resultType,
1513+ CTP_ClosureResult , resultType,
15121514 /* isDiscarded=*/ false );
15131515 cs.setSolutionApplicationTarget (returnStmt, target);
15141516
@@ -1574,7 +1576,7 @@ class SyntacticElementSolutionApplication
15741576
15751577 SolutionApplicationTarget resultTarget (
15761578 resultExpr, context.getAsDeclContext (),
1577- mode == convertToResult ? CTP_ReturnStmt : CTP_Unused,
1579+ mode == convertToResult ? CTP_ClosureResult : CTP_Unused,
15781580 mode == convertToResult ? resultType : Type (),
15791581 /* isDiscarded=*/ false );
15801582 if (auto newResultTarget = rewriteTarget (resultTarget))
0 commit comments