Skip to content

[Lexer] Fix assertion failure for unterminated string literal #19457

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions lib/Parse/Lexer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1817,6 +1817,12 @@ void Lexer::lexStringLiteral(unsigned CustomDelimiterLen) {
// Successfully scanned the body of the expression literal.
++CurPtr;
continue;
} else if ((*CurPtr == '\r' || *CurPtr == '\n') && IsMultilineString) {
// The only case we reach here is unterminated single line string in the
// interpolation. For better recovery, go on after emitting an error.
diagnose(CurPtr, diag::lex_unterminated_string);
wasErroneous = true;
continue;
}

// Being diagnosed below.
Expand Down
7 changes: 7 additions & 0 deletions test/Parse/multiline_errors.swift
Original file line number Diff line number Diff line change
Expand Up @@ -184,3 +184,10 @@ _ = """\
// FIXME: Bad diagnostics
// expected-error@-3 {{multi-line string literal content must begin on a new line}}
// expected-error@-4 {{escaped newline at the last line is not allowed}} {{8-9=}}

let _ = """
foo
\("bar
baz
"""
// expected-error@-3 {{unterminated string literal}}
Expand Down
6 changes: 6 additions & 0 deletions test/Parse/string_literal_eof4.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// RUN: %target-typecheck-verify-swift

// NOTE: DO NOT add a newline at EOF.
// expected-error@+1 {{unterminated string literal}}
_ = """
foo
7 changes: 7 additions & 0 deletions test/Parse/string_literal_eof5.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// RUN: %target-typecheck-verify-swift

// NOTE: DO NOT add a newline at EOF.
// expected-error@+1 {{unterminated string literal}}
_ = """
foo
\(
7 changes: 7 additions & 0 deletions test/Parse/string_literal_eof6.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// RUN: %target-typecheck-verify-swift

// NOTE: DO NOT add a newline at EOF.
// expected-error@+1 {{unterminated string literal}}
_ = """
foo
\("bar
9 changes: 9 additions & 0 deletions test/Parse/string_literal_eof7.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// RUN: %target-typecheck-verify-swift

// expected-error@+4 {{unterminated string literal}}
// expected-error@+1 {{unterminated string literal}}
_ = """
foo
\("bar
baz