Repository: apple/swift-syntax (swift)

Issue:
Prefer the first lexical error in a string literal segment
### Description

In `lexInStringLiteral` we currently overwrite the current lexer error when evaluating the result of `lexCharacterInStringLiteral`, we ought to only overwrite if the error isn't already present such that we prefer the first lexical error.

### Steps to Reproduce

_No response_

Pull request description:
Resolves: #1480

Patch:
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(


Test patch:
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() {