Skip to content

Commit 9c2cdea

Browse files
committed
Fix compilation errors in Swift <5.8
1 parent cc975ae commit 9c2cdea

File tree

3 files changed

+18
-2
lines changed

3 files changed

+18
-2
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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import XCTest
1515
@_spi(RawSyntax) import SwiftParser
1616

1717
fileprivate func lex(_ sourceBytes: [UInt8]) -> [Lexer.Lexeme] {
18-
return sourceBytes.withUnsafeBufferPointer { buf in
18+
return sourceBytes.withUnsafeBufferPointer { (buf) -> [Lexer.Lexeme] in
1919
var lexemes = [Lexer.Lexeme]()
2020
for token in Lexer.tokenize(buf, from: 0) {
2121
lexemes.append(token)

0 commit comments

Comments
 (0)