Skip to content

Commit 10f15c7

Browse files
committed
Fix compilation errors in Swift <5.8
1 parent 70a84cc commit 10f15c7

File tree

3 files changed

+20
-4
lines changed

3 files changed

+20
-4
lines changed

Sources/SwiftParser/StringLiterals.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ fileprivate class StringLiteralExpressionIndentationChecker {
4848
// Only checking tokens on a newline
4949
return nil
5050
}
51-
let hasSufficientIndentation = token.tokenView.leadingTrivia { leadingTrivia in
51+
let hasSufficientIndentation = token.tokenView.leadingTrivia { leadingTrivia -> Bool in
5252
let indentationStartIndex = leadingTrivia.lastIndex(where: { $0 == UInt8(ascii: "\n") || $0 == UInt8(ascii: "\r") })?.advanced(by: 1) ?? leadingTrivia.startIndex
5353
return SyntaxText(rebasing: leadingTrivia[indentationStartIndex...]).hasPrefix(expectedIndentation)
5454
}

Sources/SwiftSyntax/Raw/RawSyntaxNodeProtocol.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,22 @@ extension RawSyntax: RawSyntaxNodeProtocol {
6464
}
6565
}
6666

67+
#if swift(<5.8)
68+
// Cherry-pick this function from SE-0370
69+
extension Slice {
70+
@inlinable
71+
public func initialize<S>(
72+
from source: S
73+
) -> (unwritten: S.Iterator, index: Index)
74+
where S: Sequence, Base == UnsafeMutableBufferPointer<S.Element> {
75+
let buffer = Base(rebasing: self)
76+
let (iterator, index) = buffer.initialize(from: source)
77+
let distance = buffer.distance(from: buffer.startIndex, to: index)
78+
return (iterator, startIndex.advanced(by: distance))
79+
}
80+
}
81+
#endif
82+
6783
@_spi(RawSyntax)
6884
public struct RawTokenSyntax: RawSyntaxNodeProtocol {
6985
public typealias SyntaxType = TokenSyntax

Tests/SwiftParserTest/LexerTests.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -756,7 +756,7 @@ public class LexerTests: XCTestCase {
756756

757757
func testBOMAtStartOfFile() throws {
758758
let sourceBytes: [UInt8] = [0xef, 0xbb, 0xbf]
759-
let lexemes = sourceBytes.withUnsafeBufferPointer { buf in
759+
let lexemes = sourceBytes.withUnsafeBufferPointer { buf -> [Lexer.Lexeme] in
760760
var lexemes = [Lexer.Lexeme]()
761761
for token in Lexer.tokenize(buf, from: 0) {
762762
lexemes.append(token)
@@ -780,7 +780,7 @@ public class LexerTests: XCTestCase {
780780

781781
func testBOMInTheMiddleOfIdentifier() throws {
782782
let sourceBytes: [UInt8] = [UInt8(ascii: "a"), 0xef, 0xbb, 0xbf, UInt8(ascii: "b")]
783-
let lexemes = sourceBytes.withUnsafeBufferPointer { buf in
783+
let lexemes = sourceBytes.withUnsafeBufferPointer { buf -> [Lexer.Lexeme] in
784784
var lexemes = [Lexer.Lexeme]()
785785
for token in Lexer.tokenize(buf, from: 0) {
786786
lexemes.append(token)
@@ -803,7 +803,7 @@ public class LexerTests: XCTestCase {
803803

804804
func testBOMAsLeadingTriviaInSourceFile() throws {
805805
let sourceBytes: [UInt8] = [UInt8(ascii: "1"), UInt8(ascii: " "), UInt8(ascii: "+"), UInt8(ascii: " "), 0xef, 0xbb, 0xbf, UInt8(ascii: "2")]
806-
let lexemes = sourceBytes.withUnsafeBufferPointer { buf in
806+
let lexemes = sourceBytes.withUnsafeBufferPointer { buf -> [Lexer.Lexeme] in
807807
var lexemes = [Lexer.Lexeme]()
808808
for token in Lexer.tokenize(buf, from: 0) {
809809
lexemes.append(token)

0 commit comments

Comments
 (0)