Skip to content

Commit a6fd082

Browse files
authored
Merge pull request #18616 from akyrtzi/input-complete-invalid-interpolation
[parser] Make corrections for the isInputIncomplete() functionality
2 parents d61dad9 + 1f117b4 commit a6fd082

File tree

8 files changed

+31
-1
lines changed

8 files changed

+31
-1
lines changed

include/swift/Parse/Parser.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,10 @@ class Parser {
192192
bool isInputIncomplete() const { return IsInputIncomplete; }
193193

194194
void checkForInputIncomplete() {
195-
IsInputIncomplete = IsInputIncomplete || Tok.is(tok::eof);
195+
IsInputIncomplete = IsInputIncomplete ||
196+
// Check whether parser reached EOF but the real EOF, not the end of a
197+
// string interpolation segment.
198+
(Tok.is(tok::eof) && Tok.getText() != ")");
196199
}
197200

198201
/// \brief This is the current token being considered by the parser.

lib/Parse/ParseExpr.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1765,6 +1765,10 @@ ParserResult<Expr> Parser::parseExprPrimary(Diag<> ID, bool isExprBasic) {
17651765
// Eat an invalid token in an expression context. Error tokens are diagnosed
17661766
// by the lexer, so there is no reason to emit another diagnostic.
17671767
case tok::unknown:
1768+
if (Tok.getText().startswith("\"\"\"")) {
1769+
// This was due to unterminated multi-line string.
1770+
IsInputIncomplete = true;
1771+
}
17681772
consumeToken(tok::unknown);
17691773
return nullptr;
17701774

lib/Parse/ParseStmt.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,10 @@ ParserStatus Parser::parseBraceItems(SmallVectorImpl<ASTNode> &Entries,
319319
// Eat invalid tokens instead of allowing them to produce downstream errors.
320320
if (Tok.is(tok::unknown)) {
321321
SyntaxParsingContext ErrContext(SyntaxContext, SyntaxContextKind::Stmt);
322+
if (Tok.getText().startswith("\"\"\"")) {
323+
// This was due to unterminated multi-line string.
324+
IsInputIncomplete = true;
325+
}
322326
consumeToken();
323327
continue;
324328
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
foo("\()")
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
"""
2+
some
3+
multiline
4+
string
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
a +
2+
"""
3+
some
4+
multiline
5+
string
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
"""
2+
some
3+
multiline
4+
string
5+
\(

test/IDE/test-input-complete/test_input.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@
3030
// RUN: %swift-ide-test -test-input-complete -source-filename %S/Inputs/type_incomplete2.swift | %FileCheck %s -check-prefix=INCOMPLETE
3131
// RUN: %swift-ide-test -test-input-complete -source-filename %S/Inputs/type_incomplete3.swift | %FileCheck %s -check-prefix=INCOMPLETE
3232
// RUN: %swift-ide-test -test-input-complete -source-filename %S/Inputs/type_incomplete4.swift | %FileCheck %s -check-prefix=INCOMPLETE
33+
// RUN: %swift-ide-test -test-input-complete -source-filename %S/Inputs/invalid_interpolation1.swift | %FileCheck %s -check-prefix=COMPLETE
34+
// RUN: %swift-ide-test -test-input-complete -source-filename %S/Inputs/multi_line_string1.swift | %FileCheck %s -check-prefix=INCOMPLETE
35+
// RUN: %swift-ide-test -test-input-complete -source-filename %S/Inputs/multi_line_string2.swift | %FileCheck %s -check-prefix=INCOMPLETE
36+
// RUN: %swift-ide-test -test-input-complete -source-filename %S/Inputs/multi_line_string3.swift | %FileCheck %s -check-prefix=INCOMPLETE
3337

3438
// INCOMPLETE: IS_INCOMPLETE
3539
// COMPLETE: IS_COMPLETE

0 commit comments

Comments
 (0)