Skip to content

[6.0] Make swift-syntax build in Swift 6 language mode #2627

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,15 @@ import SyntaxSupport
import Utils

let tokenSpecStaticMembersFile = SourceFileSyntax(leadingTrivia: copyrightHeader) {
DeclSyntax("@_spi(RawSyntax) import SwiftSyntax")
DeclSyntax(
"""
#if swift(>=6)
@_spi(RawSyntax) internal import SwiftSyntax
#else
@_spi(RawSyntax) import SwiftSyntax
#endif
"""
)

try! ExtensionDeclSyntax("extension TokenSpec") {
for tokenSpec in Token.allCases.map(\.spec) where tokenSpec.kind != .keyword {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,15 @@ import SyntaxSupport
import Utils

let childNameForDiagnosticFile = SourceFileSyntax(leadingTrivia: copyrightHeader) {
DeclSyntax("@_spi(ExperimentalLanguageFeatures) import SwiftSyntax")
DeclSyntax(
"""
#if swift(>=6)
@_spi(ExperimentalLanguageFeatures) internal import SwiftSyntax
#else
@_spi(ExperimentalLanguageFeatures) import SwiftSyntax
#endif
"""
)

try! FunctionDeclSyntax(
"private func childNameForDiagnostics(_ keyPath: AnyKeyPath) -> String?"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,15 @@ import SyntaxSupport
import Utils

let syntaxKindNameForDiagnosticFile = SourceFileSyntax(leadingTrivia: copyrightHeader) {
DeclSyntax("@_spi(ExperimentalLanguageFeatures) import SwiftSyntax")
DeclSyntax(
"""
#if swift(>=6)
@_spi(ExperimentalLanguageFeatures) internal import SwiftSyntax
#else
@_spi(ExperimentalLanguageFeatures) import SwiftSyntax
#endif
"""
)

try! ExtensionDeclSyntax("extension SyntaxKind") {
try VariableDeclSyntax("var nameForDiagnostics: String?") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,15 @@ import SyntaxSupport
import Utils

let tokenNameForDiagnosticFile = SourceFileSyntax(leadingTrivia: copyrightHeader) {
DeclSyntax("@_spi(RawSyntax) import SwiftSyntax")
DeclSyntax(
"""
#if swift(>=6)
@_spi(RawSyntax) internal import SwiftSyntax
#else
@_spi(RawSyntax) import SwiftSyntax
#endif
"""
)

try! ExtensionDeclSyntax("extension TokenKind") {
try! VariableDeclSyntax("var nameForDiagnostics: String") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,15 @@ import SyntaxSupport
import Utils

let syntaxExpressibleByStringInterpolationConformancesFile = SourceFileSyntax(leadingTrivia: copyrightHeader) {
DeclSyntax("import SwiftSyntax")
DeclSyntax(
"""
#if swift(>=6)
internal import SwiftSyntax
#else
import SwiftSyntax
#endif
"""
)

let typesExpressibleByStringInterpolation =
SYNTAX_NODES
Expand Down
8 changes: 7 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,13 @@ let package = Package(
dependencies: ["_InstructionCounter", "_SwiftSyntaxTestSupport", "SwiftIDEUtils", "SwiftParser", "SwiftSyntax"],
exclude: ["Inputs"]
),
]
],
// Disable Swift 6 mode when the `SWIFTSYNTAX_DISABLE_SWIFT_6_MODE` environment variable is set. This works around the following
// issue: The self-hosted SwiftPM job has Xcode 15.3 (Swift 5.10) installed and builds a Swift 6 SwiftPM from source.
// It then tries to build itself as a fat binary using the just-built Swift 6 SwiftPM, which uses xcbuild from Xcode
// as the build system. But the xcbuild in the installed Xcode is too old and doesn't know about Swift 6 mode, so it
// fails with: SWIFT_VERSION '6' is unsupported, supported versions are: 4.0, 4.2, 5.0 (rdar://126952308)
swiftLanguageVersions: hasEnvironmentVariable("SWIFTSYNTAX_DISABLE_SWIFT_6_MODE") ? [.v5] : [.v5, .version("6")]
)

// This is a fake target that depends on all targets in the package.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,13 @@
//
//===----------------------------------------------------------------------===//

#if swift(>=6)
internal import SwiftDiagnostics
internal import SwiftSyntax
#else
import SwiftDiagnostics
import SwiftSyntax
#endif

/// Errors in macro handing.
enum MacroExpansionError {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func encodeToJSON(value: some Encodable) throws -> [UInt8] {
}

/// Intermediate representation for serializing JSON structure.
private class JSONReference {
private final class JSONReference {
enum Backing {
case null
case trueKeyword
Expand Down Expand Up @@ -60,9 +60,17 @@ private class JSONReference {
backing = .array(arr)
}

#if swift(>=6)
// nonisolated(unsafe) is fine for these properties because they represent primitives
// that are never modified.
nonisolated(unsafe) static let null: JSONReference = .init(backing: .null)
nonisolated(unsafe) static let trueKeyword: JSONReference = .init(backing: .trueKeyword)
nonisolated(unsafe) static let falseKeyword: JSONReference = .init(backing: .falseKeyword)
#else
static let null: JSONReference = .init(backing: .null)
static let trueKeyword: JSONReference = .init(backing: .trueKeyword)
static let falseKeyword: JSONReference = .init(backing: .falseKeyword)
#endif

@inline(__always)
static func newArray() -> JSONReference {
Expand Down
9 changes: 9 additions & 0 deletions Sources/SwiftCompilerPluginMessageHandling/Macros.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,21 @@
//
//===----------------------------------------------------------------------===//

#if swift(>=6)
internal import SwiftBasicFormat
internal import SwiftDiagnostics
internal import SwiftOperators
internal import SwiftSyntax
@_spi(MacroExpansion) @_spi(ExperimentalLanguageFeature) internal import SwiftSyntaxMacroExpansion
@_spi(ExperimentalLanguageFeature) internal import SwiftSyntaxMacros
#else
import SwiftBasicFormat
import SwiftDiagnostics
import SwiftOperators
import SwiftSyntax
@_spi(MacroExpansion) @_spi(ExperimentalLanguageFeature) import SwiftSyntaxMacroExpansion
@_spi(ExperimentalLanguageFeature) import SwiftSyntaxMacros
#endif

extension CompilerPluginMessageHandler {
/// Get concrete macro type from a pair of module name and type name.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,19 @@
//
//===----------------------------------------------------------------------===//

#if swift(>=6)
internal import SwiftDiagnostics
internal import SwiftOperators
internal import SwiftParser
internal import SwiftSyntax
internal import SwiftSyntaxMacros
#else
import SwiftDiagnostics
import SwiftOperators
import SwiftParser
import SwiftSyntax
import SwiftSyntaxMacros
#endif

/// Manages known source code combined with their filename/fileID. This can be
/// used to get line/column from a syntax node in the managed source code.
Expand Down
4 changes: 4 additions & 0 deletions Sources/SwiftIDEUtils/SyntaxClassification.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@
//
//===----------------------------------------------------------------------===//

#if swift(>=6)
@_spi(RawSyntax) internal import SwiftSyntax
#else
@_spi(RawSyntax) import SwiftSyntax
#endif

public enum SyntaxClassification: Sendable {
/// An attribute starting with an `@`.
Expand Down
4 changes: 4 additions & 0 deletions Sources/SwiftIDEUtils/Utils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@
//
//===----------------------------------------------------------------------===//

#if swift(>=6)
internal import SwiftSyntax
#else
import SwiftSyntax
#endif

extension Range<AbsolutePosition> {
/// Shift the range `utf8Offset` bytes to the right, ie. add `utf8Offset` to the upper and lower bound.
Expand Down
4 changes: 2 additions & 2 deletions Sources/SwiftOperators/OperatorError+Diagnostics.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@

#if swift(>=6)
public import SwiftDiagnostics
import SwiftParser
import SwiftSyntax
internal import SwiftParser
internal import SwiftSyntax
#else
import SwiftDiagnostics
import SwiftParser
Expand Down
4 changes: 4 additions & 0 deletions Sources/SwiftOperators/OperatorTable+Defaults.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@
//
//===----------------------------------------------------------------------===//

#if swift(>=6)
internal import SwiftSyntax
#else
import SwiftSyntax
#endif

/// Prefabricated operator precedence graphs.
extension OperatorTable {
Expand Down
4 changes: 4 additions & 0 deletions Sources/SwiftOperators/OperatorTable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@
//
//===----------------------------------------------------------------------===//

#if swift(>=6)
internal import SwiftSyntax
#else
import SwiftSyntax
#endif

/// Maintains and validates information about all operators in a Swift program.
///
Expand Down
4 changes: 4 additions & 0 deletions Sources/SwiftOperators/PrecedenceGraph.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@
//
//===----------------------------------------------------------------------===//

#if swift(>=6)
internal import SwiftSyntax
#else
import SwiftSyntax
#endif

/// Describes the relative precedence of two groups.
enum Precedence: Sendable {
Expand Down
4 changes: 4 additions & 0 deletions Sources/SwiftParser/Attributes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@
//
//===----------------------------------------------------------------------===//

#if swift(>=6)
@_spi(RawSyntax) internal import SwiftSyntax
#else
@_spi(RawSyntax) import SwiftSyntax
#endif

extension Parser {
mutating func parseAttributeList() -> RawAttributeListSyntax {
Expand Down
4 changes: 4 additions & 0 deletions Sources/SwiftParser/Availability.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@
//
//===----------------------------------------------------------------------===//

#if swift(>=6)
@_spi(RawSyntax) internal import SwiftSyntax
#else
@_spi(RawSyntax) import SwiftSyntax
#endif

extension Parser {
/// Parse a list of availability arguments.
Expand Down
4 changes: 4 additions & 0 deletions Sources/SwiftParser/Declarations.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@
//
//===----------------------------------------------------------------------===//

#if swift(>=6)
@_spi(RawSyntax) internal import SwiftSyntax
#else
@_spi(RawSyntax) import SwiftSyntax
#endif

extension DeclarationModifier {
var canHaveParenthesizedArgument: Bool {
Expand Down
4 changes: 4 additions & 0 deletions Sources/SwiftParser/Directives.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@
//
//===----------------------------------------------------------------------===//

#if swift(>=6)
@_spi(RawSyntax) internal import SwiftSyntax
#else
@_spi(RawSyntax) import SwiftSyntax
#endif

extension Parser {
private enum IfConfigContinuationClauseStartKeyword: TokenSpecSet {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@
//
//===----------------------------------------------------------------------===//

#if swift(>=6)
@_spi(RawSyntax) public import SwiftSyntax
#else
@_spi(RawSyntax) import SwiftSyntax
#endif

extension ExprSyntax {
/// Parse the source code of this node as a `VersionTupleSyntax`.
Expand Down
4 changes: 4 additions & 0 deletions Sources/SwiftParser/Expressions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@
//
//===----------------------------------------------------------------------===//

#if swift(>=6)
@_spi(RawSyntax) internal import SwiftSyntax
#else
@_spi(RawSyntax) import SwiftSyntax
#endif

extension TokenConsumer {
mutating func atStartOfExpression() -> Bool {
Expand Down
4 changes: 4 additions & 0 deletions Sources/SwiftParser/IsValidIdentifier.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@
//
//===----------------------------------------------------------------------===//

#if swift(>=6)
@_spi(RawSyntax) internal import SwiftSyntax
#else
@_spi(RawSyntax) import SwiftSyntax
#endif

/// Context in which to check if a name can be used as an identifier.
///
Expand Down
4 changes: 4 additions & 0 deletions Sources/SwiftParser/Lexer/Cursor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@
//
//===----------------------------------------------------------------------===//

#if swift(>=6)
@_spi(RawSyntax) @_spi(BumpPtrAllocator) internal import SwiftSyntax
#else
@_spi(RawSyntax) @_spi(BumpPtrAllocator) import SwiftSyntax
#endif

extension SyntaxText {
fileprivate func containsPlaceholderEnd() -> Bool {
Expand Down
4 changes: 4 additions & 0 deletions Sources/SwiftParser/Lexer/LexemeSequence.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@
//
//===----------------------------------------------------------------------===//

#if swift(>=6)
@_spi(RawSyntax) @_spi(BumpPtrAllocator) internal import SwiftSyntax
#else
@_spi(RawSyntax) @_spi(BumpPtrAllocator) import SwiftSyntax
#endif

extension Lexer {
/// A sequence of ``Lexer/Lexeme`` tokens starting from a ``Lexer/Cursor``
Expand Down
4 changes: 4 additions & 0 deletions Sources/SwiftParser/Lexer/RegexLiteralLexer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@
//
//===----------------------------------------------------------------------===//

#if swift(>=6)
@_spi(RawSyntax) @_spi(BumpPtrAllocator) internal import SwiftSyntax
#else
@_spi(RawSyntax) @_spi(BumpPtrAllocator) import SwiftSyntax
#endif

/// A separate lexer specifically for regex literals.
fileprivate struct RegexLiteralLexer {
Expand Down
4 changes: 4 additions & 0 deletions Sources/SwiftParser/Lookahead.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@
//
//===----------------------------------------------------------------------===//

#if swift(>=6)
@_spi(RawSyntax) internal import SwiftSyntax
#else
@_spi(RawSyntax) import SwiftSyntax
#endif

extension Parser {
/// Token lookahead for the parser.
Expand Down
4 changes: 4 additions & 0 deletions Sources/SwiftParser/LoopProgressCondition.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@
//
//===----------------------------------------------------------------------===//

#if swift(>=6)
@_spi(RawSyntax) internal import SwiftSyntax
#else
@_spi(RawSyntax) import SwiftSyntax
#endif

/// A type that can be used to make sure that a loop in the parser makes process.
///
Expand Down
Loading