From f7c66eadce38b582ba00462f72776fbeefa1c0d3 Mon Sep 17 00:00:00 2001 From: Casper Jeukendrup <48658420+cbjeukendrup@users.noreply.github.com> Date: Sat, 15 Apr 2023 19:20:56 +0200 Subject: [PATCH] Prefer the first lexical error in a string literal segment Resolves: #1480 --- Sources/SwiftParser/Lexer/Cursor.swift | 5 ++++- Tests/SwiftParserTest/ExpressionTests.swift | 7 +++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/Sources/SwiftParser/Lexer/Cursor.swift b/Sources/SwiftParser/Lexer/Cursor.swift index 6497ac19456..40a645af84d 100644 --- a/Sources/SwiftParser/Lexer/Cursor.swift +++ b/Sources/SwiftParser/Lexer/Cursor.swift @@ -1913,7 +1913,10 @@ extension Lexer.Cursor { return Lexer.Result(.stringSegment, error: error) } case .error(let errorKind): - error = LexingDiagnostic(errorKind, position: self) + // Only overwrite error if we had not found an earlier error yet + if error == nil { + error = LexingDiagnostic(errorKind, position: self) + } self = clone case .endOfString: return Lexer.Result( diff --git a/Tests/SwiftParserTest/ExpressionTests.swift b/Tests/SwiftParserTest/ExpressionTests.swift index 17c6fbf026e..007bcc55f8a 100644 --- a/Tests/SwiftParserTest/ExpressionTests.swift +++ b/Tests/SwiftParserTest/ExpressionTests.swift @@ -611,6 +611,13 @@ final class ExpressionTests: XCTestCase { DiagnosticSpec(locationMarker: "2️⃣", message: #"expected '"' to end string literal"#, fixIts: [#"insert '"'"#]), ] ) + + assertParse( + ###""1️⃣\1 \1""###, + diagnostics: [ + DiagnosticSpec(message: "invalid escape sequence in literal") + ] + ) } func testAdjacentRawStringLiterals() {