Skip to content

Commit c20a7c7

Browse files
committed
[Parser] Hoist up 'try' exprs out from sequence exprs in the parser.
If the left-most sequence expr is a 'try', hoist it up to turn '(try x) + y' into 'try (x + y)'. This is necessary to do in the parser because 'try' nodes are represented in the ASTScope tree to look up catch nodes. The scope tree must be syntactic because it's constructed before sequence folding happens during preCheckExpr. Otherwise, catch node lookup would find the incorrect catch node for 'try x + y' at the source location for 'y'. 'try' has restrictions for where it can appear within a sequence expr. This is still diagnosed in TypeChecker::foldSequence. (cherry picked from commit 8d64344)
1 parent 1284437 commit c20a7c7

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

lib/Parse/ParseExpr.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,23 @@ ParserResult<Expr> Parser::parseExprSequence(Diag<> Message,
372372
if (SequencedExprs.size() == 1)
373373
return makeParserResult(SequenceStatus, SequencedExprs[0]);
374374

375+
// If the left-most sequence expr is a 'try', hoist it up to turn
376+
// '(try x) + y' into 'try (x + y)'. This is necessary to do in the
377+
// parser because 'try' nodes are represented in the ASTScope tree
378+
// to look up catch nodes. The scope tree must be syntactic because
379+
// it's constructed before sequence folding happens during preCheckExpr.
380+
// Otherwise, catch node lookup would find the incorrect catch node for
381+
// 'try x + y' at the source location for 'y'.
382+
//
383+
// 'try' has restrictions for where it can appear within a sequence
384+
// expr. This is still diagnosed in TypeChecker::foldSequence.
385+
if (auto *tryEval = dyn_cast<AnyTryExpr>(SequencedExprs[0])) {
386+
SequencedExprs[0] = tryEval->getSubExpr();
387+
auto *sequence = SequenceExpr::create(Context, SequencedExprs);
388+
tryEval->setSubExpr(sequence);
389+
return makeParserResult(SequenceStatus, tryEval);
390+
}
391+
375392
return makeParserResult(SequenceStatus,
376393
SequenceExpr::create(Context, SequencedExprs));
377394
}

0 commit comments

Comments
 (0)