Skip to content

Commit d79f7ce

Browse files
committed
[Parser] Parse empty interpolations
Previously, the parser had an odd asymmetry which caused the same function to accept foo(), but reject “\()”. This change fixes the issue. Already tested by test/Parse/try.swift, which uses this construct in one of its throwing interpolation tests.
1 parent 0e51882 commit d79f7ce

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

lib/Parse/Parser.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -917,6 +917,10 @@ ParserStatus
917917
Parser::parseList(tok RightK, SourceLoc LeftLoc, SourceLoc &RightLoc,
918918
bool AllowSepAfterLast, Diag<> ErrorDiag, SyntaxKind Kind,
919919
llvm::function_ref<ParserStatus()> callback) {
920+
auto TokIsStringInterpolationEOF = [&]() -> bool {
921+
return Tok.is(tok::eof) && Tok.getText() == ")" && RightK == tok::r_paren;
922+
};
923+
920924
llvm::Optional<SyntaxParsingContext> ListContext;
921925
ListContext.emplace(SyntaxContext, Kind);
922926
if (Kind == SyntaxKind::Unknown)
@@ -929,6 +933,10 @@ Parser::parseList(tok RightK, SourceLoc LeftLoc, SourceLoc &RightLoc,
929933
RightLoc = consumeToken(RightK);
930934
return makeParserSuccess();
931935
}
936+
if (TokIsStringInterpolationEOF()) {
937+
RightLoc = Tok.getLoc();
938+
return makeParserSuccess();
939+
}
932940

933941
ParserStatus Status;
934942
while (true) {
@@ -948,7 +956,7 @@ Parser::parseList(tok RightK, SourceLoc LeftLoc, SourceLoc &RightLoc,
948956
// If the lexer stopped with an EOF token whose spelling is ")", then this
949957
// is actually the tuple that is a string literal interpolation context.
950958
// Just accept the ")" and build the tuple as we usually do.
951-
if (Tok.is(tok::eof) && Tok.getText() == ")" && RightK == tok::r_paren) {
959+
if (TokIsStringInterpolationEOF()) {
952960
RightLoc = Tok.getLoc();
953961
return Status;
954962
}

0 commit comments

Comments
 (0)