@@ -198,25 +198,28 @@ static Expr *makeBinOp(ASTContext &Ctx, Expr *Op, Expr *LHS, Expr *RHS,
198198
199199 // If the left-hand-side is a 'try', 'await', or 'unsafe', hoist it up
200200 // turning "(try x) + y" into try (x + y).
201- if (auto *tryEval = dyn_cast<AnyTryExpr>(LHS)) {
202- auto sub = makeBinOp (Ctx, Op, tryEval->getSubExpr (), RHS,
203- opPrecedence, isEndOfSequence);
204- tryEval->setSubExpr (sub);
205- return tryEval;
206- }
207-
208- if (auto *await = dyn_cast<AwaitExpr>(LHS)) {
209- auto sub = makeBinOp (Ctx, Op, await->getSubExpr (), RHS,
210- opPrecedence, isEndOfSequence);
211- await->setSubExpr (sub);
212- return await;
213- }
201+ if (LHS->isAlwaysLeftFolded ()) {
202+ if (auto *tryEval = dyn_cast<AnyTryExpr>(LHS)) {
203+ auto sub = makeBinOp (Ctx, Op, tryEval->getSubExpr (), RHS, opPrecedence,
204+ isEndOfSequence);
205+ tryEval->setSubExpr (sub);
206+ return tryEval;
207+ }
214208
215- if (auto *unsafe = dyn_cast<UnsafeExpr>(LHS)) {
216- auto sub = makeBinOp (Ctx, Op, unsafe->getSubExpr (), RHS,
217- opPrecedence, isEndOfSequence);
218- unsafe->setSubExpr (sub);
219- return unsafe;
209+ if (auto *await = dyn_cast<AwaitExpr>(LHS)) {
210+ auto sub = makeBinOp (Ctx, Op, await->getSubExpr (), RHS, opPrecedence,
211+ isEndOfSequence);
212+ await->setSubExpr (sub);
213+ return await;
214+ }
215+
216+ if (auto *unsafe = dyn_cast<UnsafeExpr>(LHS)) {
217+ auto sub = makeBinOp (Ctx, Op, unsafe->getSubExpr (), RHS, opPrecedence,
218+ isEndOfSequence);
219+ unsafe->setSubExpr (sub);
220+ return unsafe;
221+ }
222+ llvm_unreachable (" Unhandled left-folded case!" );
220223 }
221224
222225 // If the right operand is a try, await, or unsafe, it's an error unless
@@ -235,7 +238,7 @@ static Expr *makeBinOp(ASTContext &Ctx, Expr *Op, Expr *LHS, Expr *RHS,
235238 // x ? try foo() : try bar() $#! 1
236239 // assuming $#! is some crazy operator with lower precedence
237240 // than the conditional operator.
238- if (isa<AnyTryExpr>( RHS) || isa<AwaitExpr>(RHS) || isa<UnsafeExpr>(RHS )) {
241+ if (RHS-> isAlwaysLeftFolded ( )) {
239242 // If you change this, also change TRY_KIND_SELECT in diagnostics.
240243 enum class TryKindForDiagnostics : unsigned {
241244 Try,
0 commit comments