@@ -162,8 +162,8 @@ static bool isViableElement(ASTNode element) {
162
162
return true ;
163
163
}
164
164
165
- using ElementInfo =
166
- std::tuple<ASTNode, ContextualTypeInfo , ConstraintLocator *>;
165
+ using ElementInfo = std::tuple<ASTNode, ContextualTypeInfo,
166
+ /* isDiscarded= */ bool , ConstraintLocator *>;
167
167
168
168
static void createConjunction (ConstraintSystem &cs,
169
169
ArrayRef<ElementInfo> elements,
@@ -190,7 +190,8 @@ static void createConjunction(ConstraintSystem &cs,
190
190
for (const auto &entry : elements) {
191
191
ASTNode element = std::get<0 >(entry);
192
192
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);
194
195
195
196
if (!isViableElement (element))
196
197
continue ;
@@ -201,8 +202,8 @@ static void createConjunction(ConstraintSystem &cs,
201
202
if (isIsolated)
202
203
element.walk (paramCollector);
203
204
204
- constraints.push_back (
205
- Constraint::createClosureBodyElement ( cs, element, context, elementLoc));
205
+ constraints.push_back (Constraint::createClosureBodyElement (
206
+ cs, element, context, elementLoc, isDiscarded ));
206
207
}
207
208
208
209
// It's possible that there are no viable elements in the body,
@@ -220,8 +221,9 @@ static void createConjunction(ConstraintSystem &cs,
220
221
}
221
222
222
223
ElementInfo 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);
225
227
}
226
228
227
229
static ProtocolDecl *getSequenceProtocol (ASTContext &ctx, SourceLoc loc,
@@ -751,6 +753,8 @@ class ClosureConstraintGenerator
751
753
752
754
void visitBraceStmt (BraceStmt *braceStmt) {
753
755
if (isSupportedMultiStatementClosure ()) {
756
+ auto &ctx = cs.getASTContext ();
757
+
754
758
if (isChildOf (StmtKind::Case)) {
755
759
auto *caseStmt = cast<CaseStmt>(
756
760
locator->castLastElementTo <LocatorPathElt::ClosureBodyElement>()
@@ -765,10 +769,15 @@ class ClosureConstraintGenerator
765
769
766
770
SmallVector<ElementInfo, 4 > elements;
767
771
for (auto element : braceStmt->getElements ()) {
772
+ bool isDiscarded =
773
+ element.is <Expr *>() &&
774
+ (!ctx.LangOpts .Playground && !ctx.LangOpts .DebuggerSupport );
775
+
768
776
elements.push_back (makeElement (
769
777
element,
770
778
cs.getConstraintLocator (
771
- locator, LocatorPathElt::ClosureBodyElement (element))));
779
+ locator, LocatorPathElt::ClosureBodyElement (element)),
780
+ /* contextualInfo=*/ {}, isDiscarded));
772
781
}
773
782
774
783
createConjunction (cs, elements, locator);
@@ -925,28 +934,22 @@ bool isConditionOfStmt(ConstraintLocatorBuilder locator) {
925
934
926
935
ConstraintSystem::SolutionKind
927
936
ConstraintSystem::simplifyClosureBodyElementConstraint (
928
- ASTNode element, ContextualTypeInfo context, TypeMatchOptions flags ,
929
- ConstraintLocatorBuilder locator) {
937
+ ASTNode element, ContextualTypeInfo context, bool isDiscarded ,
938
+ TypeMatchOptions flags, ConstraintLocatorBuilder locator) {
930
939
auto *closure = castToExpr<ClosureExpr>(locator.getAnchor ());
931
940
932
941
ClosureConstraintGenerator generator (*this , closure,
933
942
getConstraintLocator (locator));
934
943
935
944
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);
947
947
948
- if (! generateConstraints (expr, closure, /* isInputExpression= */ false ))
948
+ if (generateConstraints (target, FreeTypeVariableBinding::Disallow ))
949
949
return SolutionKind::Error;
950
+
951
+ setSolutionApplicationTarget (expr, target);
952
+ return SolutionKind::Solved;
950
953
} else if (auto *stmt = element.dyn_cast <Stmt *>()) {
951
954
generator.visit (stmt);
952
955
} else if (auto *cond = element.dyn_cast <StmtCondition *>()) {
@@ -1241,13 +1244,20 @@ class ClosureConstraintApplication
1241
1244
}
1242
1245
1243
1246
ASTNode visitBraceStmt (BraceStmt *braceStmt) {
1247
+ auto &cs = solution.getConstraintSystem ();
1248
+
1244
1249
for (auto &node : braceStmt->getElements ()) {
1245
1250
if (auto expr = node.dyn_cast <Expr *>()) {
1246
1251
// 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 {
1250
1259
hadError = true ;
1260
+ }
1251
1261
} else if (auto stmt = node.dyn_cast <Stmt *>()) {
1252
1262
node = visit (stmt);
1253
1263
} else {
0 commit comments