Skip to content

Commit 8d05c50

Browse files
committed
[SwiftSyntax] Fix assertion failure if regex literal was not terminated at the end of the file
Fixes a SwiftSyntax parsing assertion failure if there is a regex literal at the end of the file. I.e. either a single line regex literal in a file without a trailing newline or a multi-line regex literal. This does not crash in non-assert builds.
1 parent 719ea8c commit 8d05c50

File tree

3 files changed

+13
-1
lines changed

3 files changed

+13
-1
lines changed

lib/Parse/SyntaxRegexFallbackLexing.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ bool syntaxparse_lexRegexLiteral(
114114
case '\0': {
115115
if (Ptr - 1 == BufferEnd) {
116116
// Reached to EOF.
117-
diagnose(BridgedDiagEngine, Ptr, diag::lex_regex_literal_unterminated);
117+
diagnose(BridgedDiagEngine, Ptr - 1, diag::lex_regex_literal_unterminated);
118118
// In multi-line mode, we don't want to skip over what is likely
119119
// otherwise valid Swift code, so resume from the first newline.
120120
*InputPtr = firstNewline ? firstNewline : (Ptr - 1);
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// RUN: %swift-syntax-parser-test -dump-diags %s | %FileCheck %s
2+
// CHECK: 7:1 Error: unterminated regex literal
3+
// CHECK: 1 error(s) 0 warnings(s) 0 note(s)
4+
5+
#/
6+
unterminatedLiteral
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// RUN: %swift-syntax-parser-test -dump-diags %s | %FileCheck %s
2+
// CHECK: 6:21 Error: unterminated regex literal
3+
// CHECK: 1 error(s) 0 warnings(s) 0 note(s)
4+
5+
// IMPORTANT: This file must not contain a trailing newline
6+
/unterminatedLiteral

0 commit comments

Comments
 (0)