@@ -1255,11 +1255,21 @@ class SyntacticElementConstraintGenerator
12551255 auto *resultExpr = thenStmt->getResult ();
12561256 auto contextInfo = cs.getContextualTypeInfo (resultExpr);
12571257
1258+ // First check to make sure the ThenStmt is in a valid position.
1259+ SmallVector<ThenStmt *, 4 > validThenStmts;
1260+ if (auto SVE = context.getAsSingleValueStmtExpr ())
1261+ (void )SVE.get ()->getThenStmts (validThenStmts);
1262+
1263+ if (!llvm::is_contained (validThenStmts, thenStmt)) {
1264+ auto *thenLoc = cs.getConstraintLocator (thenStmt);
1265+ (void )cs.recordFix (IgnoreOutOfPlaceThenStmt::create (cs, thenLoc));
1266+ }
1267+
12581268 // For an if/switch expression, if the contextual type for the branch is
12591269 // still a type variable, we can drop it. This avoids needlessly
12601270 // propagating the type of the branch to subsequent branches, instead
12611271 // we'll let the join handle the conversion.
1262- if (contextInfo && isExpr<SingleValueStmtExpr>(locator-> getAnchor ()) ) {
1272+ if (contextInfo) {
12631273 auto contextualFixedTy =
12641274 cs.getFixedTypeRecursive (contextInfo->getType (), /* wantRValue*/ true );
12651275 if (contextualFixedTy->isTypeVariableOrMember ())
@@ -2176,29 +2186,22 @@ class SyntacticElementSolutionApplication
21762186 }
21772187
21782188 ASTNode visitThenStmt (ThenStmt *thenStmt) {
2179- // Note we're defensive here over whether we're in a SingleValueStmtExpr,
2180- // as we don't diagnose out-of-place ThenStmts until MiscDiagnostics. If we
2181- // have an out-of-place ThenStmt, just rewrite the expr without a contextual
2182- // type.
21832189 auto SVE = context.getAsSingleValueStmtExpr ();
2184- auto ty = SVE ? solution.getResolvedType (SVE.get ()) : Type ();
2190+ assert (SVE && " Should have diagnosed an out-of-place ThenStmt" );
2191+ auto ty = solution.getResolvedType (SVE.get ());
21852192
21862193 // We need to fixup the conversion type to the full result type,
21872194 // not the branch result type. This is necessary as there may be
21882195 // an additional conversion required for the branch.
21892196 auto target = solution.getTargetFor (thenStmt->getResult ());
2190- if (ty)
2191- target->setExprConversionType (ty);
2197+ target->setExprConversionType (ty);
21922198
21932199 auto *resultExpr = thenStmt->getResult ();
21942200 if (auto newResultTarget = rewriteTarget (*target))
21952201 resultExpr = newResultTarget->getAsExpr ();
21962202
21972203 thenStmt->setResult (resultExpr);
21982204
2199- if (!SVE)
2200- return thenStmt;
2201-
22022205 // If the expression was typed as Void, its branches are effectively
22032206 // discarded, so treat them as ignored expressions.
22042207 if (ty->lookThroughAllOptionalTypes ()->isVoid ()) {
0 commit comments