Skip to content

Commit ab63863

Browse files
committed
Always end interpolation even if ) is present
When parsing the end of an interpolation segment, even if there is a closing `)` present, the lexer may still think it's inside a nested position in an interpolated segment. As such, make sure to always tell it to move back to string literal mode for better diagnostics.
1 parent 8e48b28 commit ab63863

File tree

3 files changed

+6
-7
lines changed

3 files changed

+6
-7
lines changed

Sources/SwiftParser/StringLiterals.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -490,7 +490,7 @@ extension Parser {
490490
unexpectedBeforeRightParen.append(self.consumeAnyToken())
491491
}
492492
let rightParen = self.expectWithoutRecovery(.rightParen)
493-
if rightParen.isMissing, case .inStringInterpolation = self.currentToken.cursor.currentState {
493+
if case .inStringInterpolation = self.currentToken.cursor.currentState {
494494
// The parser has more knowledge that we have reached the end of the
495495
// string interpolation now, even if we haven't seen the closing ')'.
496496
// For example, consider the following code

Tests/SwiftParserTest/ExpressionTests.swift

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1656,13 +1656,12 @@ final class StatementExpressionTests: XCTestCase {
16561656
func testUnterminatedInterpolationAtEndOfMultilineStringLiteral() {
16571657
AssertParse(
16581658
#"""
1659-
"""\({(1️⃣})
1660-
2️⃣"""3️⃣
1659+
"""1️⃣\({(2️⃣})
1660+
"""
16611661
"""#,
16621662
diagnostics: [
1663-
DiagnosticSpec(locationMarker: "1️⃣", message: "expected value and ')' to end tuple"),
1664-
DiagnosticSpec(locationMarker: "2️⃣", message: #"unexpected code '"""' in string literal"#),
1665-
DiagnosticSpec(locationMarker: "3️⃣", message: #"expected '"""' to end string literal"#),
1663+
DiagnosticSpec(locationMarker: "1️⃣", message: "multi-line string literal content must begin on a new line"),
1664+
DiagnosticSpec(locationMarker: "2️⃣", message: "expected value and ')' to end tuple"),
16661665
]
16671666
)
16681667
}

Tests/SwiftParserTest/translated/UnclosedStringInterpolationTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ final class UnclosedStringInterpolationTests: XCTestCase {
111111
func testSkipUnexpectedOpeningParensInStringLiteral() {
112112
AssertParse(
113113
#"""
114-
"\(e 1️⃣H()2️⃣r
114+
"\(e 1️⃣H()r2️⃣
115115
"""#,
116116
diagnostics: [
117117
DiagnosticSpec(locationMarker: "1️⃣", message: "unexpected code 'H(' in string literal"),

0 commit comments

Comments
 (0)