@@ -162,8 +162,8 @@ static bool isViableElement(ASTNode element) {
162162 return true ;
163163}
164164
165- using ElementInfo =
166- std::tuple<ASTNode, ContextualTypeInfo , ConstraintLocator *>;
165+ using ElementInfo = std::tuple<ASTNode, ContextualTypeInfo,
166+ /* isDiscarded= */ bool , ConstraintLocator *>;
167167
168168static void createConjunction (ConstraintSystem &cs,
169169 ArrayRef<ElementInfo> elements,
@@ -190,7 +190,8 @@ static void createConjunction(ConstraintSystem &cs,
190190 for (const auto &entry : elements) {
191191 ASTNode element = std::get<0 >(entry);
192192 ContextualTypeInfo context = std::get<1 >(entry);
193- ConstraintLocator *elementLoc = std::get<2 >(entry);
193+ bool isDiscarded = std::get<2 >(entry);
194+ ConstraintLocator *elementLoc = std::get<3 >(entry);
194195
195196 if (!isViableElement (element))
196197 continue ;
@@ -201,8 +202,8 @@ static void createConjunction(ConstraintSystem &cs,
201202 if (isIsolated)
202203 element.walk (paramCollector);
203204
204- constraints.push_back (
205- Constraint::createClosureBodyElement ( cs, element, context, elementLoc));
205+ constraints.push_back (Constraint::createClosureBodyElement (
206+ cs, element, context, elementLoc, isDiscarded ));
206207 }
207208
208209 // It's possible that there are no viable elements in the body,
@@ -220,8 +221,9 @@ static void createConjunction(ConstraintSystem &cs,
220221}
221222
222223ElementInfo makeElement (ASTNode node, ConstraintLocator *locator,
223- ContextualTypeInfo context = ContextualTypeInfo()) {
224- return std::make_tuple (node, context, locator);
224+ ContextualTypeInfo context = ContextualTypeInfo(),
225+ bool isDiscarded = false) {
226+ return std::make_tuple (node, context, isDiscarded, locator);
225227}
226228
227229static ProtocolDecl *getSequenceProtocol (ASTContext &ctx, SourceLoc loc,
@@ -751,6 +753,8 @@ class ClosureConstraintGenerator
751753
752754 void visitBraceStmt (BraceStmt *braceStmt) {
753755 if (isSupportedMultiStatementClosure ()) {
756+ auto &ctx = cs.getASTContext ();
757+
754758 if (isChildOf (StmtKind::Case)) {
755759 auto *caseStmt = cast<CaseStmt>(
756760 locator->castLastElementTo <LocatorPathElt::ClosureBodyElement>()
@@ -765,10 +769,15 @@ class ClosureConstraintGenerator
765769
766770 SmallVector<ElementInfo, 4 > elements;
767771 for (auto element : braceStmt->getElements ()) {
772+ bool isDiscarded =
773+ element.is <Expr *>() &&
774+ (!ctx.LangOpts .Playground && !ctx.LangOpts .DebuggerSupport );
775+
768776 elements.push_back (makeElement (
769777 element,
770778 cs.getConstraintLocator (
771- locator, LocatorPathElt::ClosureBodyElement (element))));
779+ locator, LocatorPathElt::ClosureBodyElement (element)),
780+ /* contextualInfo=*/ {}, isDiscarded));
772781 }
773782
774783 createConjunction (cs, elements, locator);
@@ -925,28 +934,22 @@ bool isConditionOfStmt(ConstraintLocatorBuilder locator) {
925934
926935ConstraintSystem::SolutionKind
927936ConstraintSystem::simplifyClosureBodyElementConstraint (
928- ASTNode element, ContextualTypeInfo context, TypeMatchOptions flags ,
929- ConstraintLocatorBuilder locator) {
937+ ASTNode element, ContextualTypeInfo context, bool isDiscarded ,
938+ TypeMatchOptions flags, ConstraintLocatorBuilder locator) {
930939 auto *closure = castToExpr<ClosureExpr>(locator.getAnchor ());
931940
932941 ClosureConstraintGenerator generator (*this , closure,
933942 getConstraintLocator (locator));
934943
935944 if (auto *expr = element.dyn_cast <Expr *>()) {
936- if (context.purpose != CTP_Unused) {
937- SolutionApplicationTarget target (expr, closure, context.purpose ,
938- context.getType (),
939- /* isDiscarded=*/ false );
940-
941- if (generateConstraints (target, FreeTypeVariableBinding::Disallow))
942- return SolutionKind::Error;
943-
944- setSolutionApplicationTarget (expr, target);
945- return SolutionKind::Solved;
946- }
945+ SolutionApplicationTarget target (expr, closure, context.purpose ,
946+ context.getType (), isDiscarded);
947947
948- if (! generateConstraints (expr, closure, /* isInputExpression= */ false ))
948+ if (generateConstraints (target, FreeTypeVariableBinding::Disallow ))
949949 return SolutionKind::Error;
950+
951+ setSolutionApplicationTarget (expr, target);
952+ return SolutionKind::Solved;
950953 } else if (auto *stmt = element.dyn_cast <Stmt *>()) {
951954 generator.visit (stmt);
952955 } else if (auto *cond = element.dyn_cast <StmtCondition *>()) {
@@ -1241,13 +1244,20 @@ class ClosureConstraintApplication
12411244 }
12421245
12431246 ASTNode visitBraceStmt (BraceStmt *braceStmt) {
1247+ auto &cs = solution.getConstraintSystem ();
1248+
12441249 for (auto &node : braceStmt->getElements ()) {
12451250 if (auto expr = node.dyn_cast <Expr *>()) {
12461251 // Rewrite the expression.
1247- if (auto rewrittenExpr = rewriteExpr (expr))
1248- node = rewrittenExpr;
1249- else
1252+ auto target = *cs.getSolutionApplicationTarget (expr);
1253+ if (auto rewrittenTarget = rewriteTarget (target)) {
1254+ node = rewrittenTarget->getAsExpr ();
1255+
1256+ if (target.isDiscardedExpr ())
1257+ TypeChecker::checkIgnoredExpr (castToExpr (node));
1258+ } else {
12501259 hadError = true ;
1260+ }
12511261 } else if (auto stmt = node.dyn_cast <Stmt *>()) {
12521262 node = visit (stmt);
12531263 } else {
0 commit comments