From 93b297cb2b87acc2793f3ff7fde9950d143c113a Mon Sep 17 00:00:00 2001 From: Alex Hoppen Date: Tue, 10 Jun 2025 15:19:03 +0200 Subject: [PATCH] Relax precondition in `postProcessMultilineStringLiteral` MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The only place where we were dropping unexpected tokens or trivia around a string segment was when inferring the indentation of the closing quote. Instead of having a precondition, add the conditions that the last segment doesn’t have any of these here and fall back to the same logic that is used when the indentaiton could not be inferred. Fixes #3097 --- Sources/SwiftParser/StringLiterals.swift | 31 ++++------------- .../translated/StringLiteralEofTests.swift | 33 +++++++++++++++++++ 2 files changed, 40 insertions(+), 24 deletions(-) diff --git a/Sources/SwiftParser/StringLiterals.swift b/Sources/SwiftParser/StringLiterals.swift index b03c8877def..d3b9c8adc4d 100644 --- a/Sources/SwiftParser/StringLiterals.swift +++ b/Sources/SwiftParser/StringLiterals.swift @@ -340,22 +340,6 @@ extension Parser { unexpectedBeforeClosingQuote: [RawTokenSyntax], closingQuote: RawTokenSyntax ) { - // ------------------------------------------------------------------------- - // Precondition - - precondition( - allSegments.allSatisfy { - if case .stringSegment(let segment) = $0 { - return segment.unexpectedBeforeContent == nil - && segment.unexpectedAfterContent == nil - && segment.content.leadingTriviaByteLength == 0 - } else { - return true - } - }, - "String segment produced by the lexer should not have unexpected text or trivia because we would drop it during post-processing" - ) - // ------------------------------------------------------------------------- // Variables @@ -395,6 +379,9 @@ extension Parser { // Parse indentation of the closing quote if let lastSegment, + lastSegment.unexpectedBeforeContent == nil, + lastSegment.unexpectedAfterContent == nil, + lastSegment.content.leadingTriviaByteLength == 0, let parsedTrivia = parseIndentationTrivia(text: lastSegment.content.tokenText) { indentationTrivia = parsedTrivia @@ -409,10 +396,9 @@ extension Parser { arena: self.arena ) } else { - if let lastSegment = lastSegment { - indentationTrivia = TriviaParser.parseTrivia(lastSegment.content.tokenText, position: .leading).prefix(while: { - $0.isIndentationWhitespace - }) + if let lastSegment { + indentationTrivia = TriviaParser.parseTrivia(lastSegment.content.tokenText, position: .leading) + .prefix(while: \.isIndentationWhitespace) let indentationByteLength = indentationTrivia.reduce(0, { $0 + $1.byteLength }) indentation = SyntaxText(rebasing: lastSegment.content.tokenText[0..)" + """# + ) + } }