Skip to content

Commit 7cb4358

Browse files
committed
Make swift-syntax (mostly) build in Swift 6 mode
This almost makes swift-syntax build in Swift 6 mode. The only change holding us back is that `CommandLine.arguments` is a static variable and accessing it in Swift 6 mode raises an error (rdar://113799564).
1 parent 25ce3a2 commit 7cb4358

File tree

156 files changed

+766
-179
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

156 files changed

+766
-179
lines changed

CodeGeneration/Sources/generate-swift-syntax/templates/swiftparser/IsLexerClassifiedFile.swift

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,15 @@ import SyntaxSupport
1616
import Utils
1717

1818
let isLexerClassifiedFile = SourceFileSyntax(leadingTrivia: copyrightHeader) {
19-
DeclSyntax("import SwiftSyntax")
19+
DeclSyntax(
20+
"""
21+
#if swift(>=6)
22+
public import SwiftSyntax
23+
#else
24+
import SwiftSyntax
25+
#endif
26+
"""
27+
)
2028

2129
try! ExtensionDeclSyntax(
2230
"""

CodeGeneration/Sources/generate-swift-syntax/templates/swiftparser/LayoutNodesParsableFile.swift

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,15 @@ import SyntaxSupport
1616
import Utils
1717

1818
let layoutNodesParsableFile = SourceFileSyntax(leadingTrivia: copyrightHeader) {
19-
DeclSyntax("@_spi(RawSyntax) import SwiftSyntax")
19+
DeclSyntax(
20+
"""
21+
#if swift(>=6)
22+
@_spi(RawSyntax) public import SwiftSyntax
23+
#else
24+
@_spi(RawSyntax) import SwiftSyntax
25+
#endif
26+
"""
27+
)
2028

2129
DeclSyntax(
2230
"""

CodeGeneration/Sources/generate-swift-syntax/templates/swiftparser/ParserTokenSpecSetFile.swift

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,15 @@ func tokenCaseMatch(_ caseName: TokenSyntax, experimentalFeature: ExperimentalFe
2727
}
2828

2929
let parserTokenSpecSetFile = SourceFileSyntax(leadingTrivia: copyrightHeader) {
30-
DeclSyntax("@_spi(RawSyntax) @_spi(ExperimentalLanguageFeatures) import SwiftSyntax")
30+
DeclSyntax(
31+
"""
32+
#if swift(>=6)
33+
@_spi(RawSyntax) @_spi(ExperimentalLanguageFeatures) public import SwiftSyntax
34+
#else
35+
@_spi(RawSyntax) @_spi(ExperimentalLanguageFeatures) import SwiftSyntax
36+
#endif
37+
"""
38+
)
3139

3240
for layoutNode in SYNTAX_NODES.compactMap(\.layoutNode) {
3341
for child in layoutNode.children {

CodeGeneration/Sources/generate-swift-syntax/templates/swiftsyntax/RawSyntaxValidationFile.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ let rawSyntaxValidationFile = try! SourceFileSyntax(leadingTrivia: copyrightHead
8484

8585
DeclSyntax(
8686
"""
87-
func verify<Node: RawSyntaxNodeProtocol>(_ raw: RawSyntax?, as _: Node.Type, file: StaticString = #file, line: UInt = #line) -> ValidationError? {
87+
func verify<Node: RawSyntaxNodeProtocol>(_ raw: RawSyntax?, as _: Node.Type, file: StaticString = #filePath, line: UInt = #line) -> ValidationError? {
8888
guard let raw = raw else {
8989
return .expectedNonNil(expectedKind: Node.self, file: file, line: line)
9090
}
@@ -98,7 +98,7 @@ let rawSyntaxValidationFile = try! SourceFileSyntax(leadingTrivia: copyrightHead
9898

9999
DeclSyntax(
100100
"""
101-
func verify<Node: RawSyntaxNodeProtocol>(_ raw: RawSyntax?, as _: Node?.Type, file: StaticString = #file, line: UInt = #line) -> ValidationError? {
101+
func verify<Node: RawSyntaxNodeProtocol>(_ raw: RawSyntax?, as _: Node?.Type, file: StaticString = #filePath, line: UInt = #line) -> ValidationError? {
102102
if raw != nil {
103103
return verify(raw, as: Node.self, file: file, line: line)
104104
}
@@ -109,7 +109,7 @@ let rawSyntaxValidationFile = try! SourceFileSyntax(leadingTrivia: copyrightHead
109109

110110
DeclSyntax(
111111
"""
112-
func verify(_ raw: RawSyntax?, as _: RawTokenSyntax?.Type, tokenChoices: [TokenChoice], file: StaticString = #file, line: UInt = #line) -> ValidationError? {
112+
func verify(_ raw: RawSyntax?, as _: RawTokenSyntax?.Type, tokenChoices: [TokenChoice], file: StaticString = #filePath, line: UInt = #line) -> ValidationError? {
113113
// Validation of token choice is currently causing assertion failures where
114114
// the list of expected token choices in the syntax tree doesn't match those
115115
// the parser generates. Disable the verification for now until all issues
@@ -124,7 +124,7 @@ let rawSyntaxValidationFile = try! SourceFileSyntax(leadingTrivia: copyrightHead
124124

125125
DeclSyntax(
126126
"""
127-
func verify(_ raw: RawSyntax?, as _: RawTokenSyntax.Type, tokenChoices: [TokenChoice], file: StaticString = #file, line: UInt = #line) -> ValidationError? {
127+
func verify(_ raw: RawSyntax?, as _: RawTokenSyntax.Type, tokenChoices: [TokenChoice], file: StaticString = #filePath, line: UInt = #line) -> ValidationError? {
128128
// Validation of token choice is currently causing assertion failures where
129129
// the list of expected token choices in the syntax tree doesn't match those
130130
// the parser generates. Disable the verification for now until all issues

CodeGeneration/Sources/generate-swift-syntax/templates/swiftsyntaxbuilder/BuildableNodesFile.swift

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,15 @@ import SyntaxSupport
1616
import Utils
1717

1818
let buildableNodesFile = SourceFileSyntax(leadingTrivia: copyrightHeader) {
19-
DeclSyntax("@_spi(ExperimentalLanguageFeatures) import SwiftSyntax")
19+
DeclSyntax(
20+
"""
21+
#if swift(>=6)
22+
@_spi(ExperimentalLanguageFeatures) public import SwiftSyntax
23+
#else
24+
@_spi(ExperimentalLanguageFeatures) import SwiftSyntax
25+
#endif
26+
"""
27+
)
2028

2129
for node in SYNTAX_NODES.compactMap(\.layoutNode) {
2230
let type = node.type

CodeGeneration/Sources/generate-swift-syntax/templates/swiftsyntaxbuilder/RenamedChildrenBuilderCompatibilityFile.swift

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,15 @@ import SyntaxSupport
1616
import Utils
1717

1818
let renamedChildrenBuilderCompatibilityFile = try! SourceFileSyntax(leadingTrivia: copyrightHeader) {
19-
DeclSyntax("import SwiftSyntax")
19+
DeclSyntax(
20+
"""
21+
#if swift(>=6)
22+
public import SwiftSyntax
23+
#else
24+
import SwiftSyntax
25+
#endif
26+
"""
27+
)
2028

2129
for layoutNode in SYNTAX_NODES.compactMap(\.layoutNode).filter({ $0.children.hasDeprecatedChild }) {
2230
if let convenienceInit = try layoutNode.createConvenienceBuilderInitializer(useDeprecatedChildName: true) {

CodeGeneration/Sources/generate-swift-syntax/templates/swiftsyntaxbuilder/ResultBuildersFile.swift

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,15 @@ import SyntaxSupport
1616
import Utils
1717

1818
let resultBuildersFile = SourceFileSyntax(leadingTrivia: copyrightHeader) {
19-
DeclSyntax("import SwiftSyntax")
19+
DeclSyntax(
20+
"""
21+
#if swift(>=6)
22+
public import SwiftSyntax
23+
#else
24+
import SwiftSyntax
25+
#endif
26+
"""
27+
)
2028

2129
for node in SYNTAX_NODES.compactMap(\.collectionNode) {
2230
let type = SyntaxBuildableType(kind: .node(kind: node.kind))

CodeGeneration/Sources/generate-swift-syntax/templates/swiftsyntaxbuilder/SyntaxExpressibleByStringInterpolationConformancesFile.swift

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,26 @@ import Utils
1818
let syntaxExpressibleByStringInterpolationConformancesFile = SourceFileSyntax(leadingTrivia: copyrightHeader) {
1919
DeclSyntax("import SwiftSyntax")
2020

21-
for node in SYNTAX_NODES where node.parserFunction != nil {
22-
DeclSyntax("extension \(node.kind.syntaxType): SyntaxExpressibleByStringInterpolation {}")
23-
}
21+
let typesExpressibleByStringInterpolation =
22+
SYNTAX_NODES
23+
.filter { $0.parserFunction != nil }
24+
.map { $0.kind.syntaxType }
25+
// `SyntaxParsable` conformance for collection nodes is hand-written.
26+
+ [
27+
"AccessorDeclListSyntax",
28+
"AttributeListSyntax",
29+
"CodeBlockItemListSyntax",
30+
"MemberBlockItemListSyntax",
31+
]
2432

25-
// `SyntaxParsable` conformance for collection nodes is hand-written.
26-
// We also need to hand-write the corresponding `SyntaxExpressibleByStringInterpolation` conformances.
27-
DeclSyntax("extension AccessorDeclListSyntax: SyntaxExpressibleByStringInterpolation {}")
28-
DeclSyntax("extension AttributeListSyntax: SyntaxExpressibleByStringInterpolation {}")
29-
DeclSyntax("extension CodeBlockItemListSyntax: SyntaxExpressibleByStringInterpolation {}")
30-
DeclSyntax("extension MemberBlockItemListSyntax: SyntaxExpressibleByStringInterpolation {}")
33+
for type in typesExpressibleByStringInterpolation {
34+
DeclSyntax("extension \(type): SyntaxExpressibleByStringInterpolation {}")
35+
DeclSyntax(
36+
"""
37+
#if compiler(>=6)
38+
extension \(type): @retroactive ExpressibleByStringInterpolation {}
39+
#endif
40+
"""
41+
)
42+
}
3143
}

CodeGeneration/Tests/ValidateSyntaxNodes/ValidateSyntaxNodes.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import XCTest
1616
fileprivate func assertNoFailures(
1717
_ failures: [ValidationFailure],
1818
message: String,
19-
file: StaticString = #file,
19+
file: StaticString = #filePath,
2020
line: UInt = #line
2121
) {
2222
if failures.isEmpty {
@@ -37,7 +37,7 @@ fileprivate func assertNoFailures(
3737
fileprivate func assertFailuresMatchXFails(
3838
_ failures: [ValidationFailure],
3939
expectedFailures: [ValidationFailure],
40-
file: StaticString = #file,
40+
file: StaticString = #filePath,
4141
line: UInt = #line
4242
) {
4343
let matchedXFails = expectedFailures.filter { failures.contains($0) }

Package.swift

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,8 @@ let package = Package(
7474

7575
.target(
7676
name: "SwiftCompilerPlugin",
77-
dependencies: ["SwiftCompilerPluginMessageHandling", "SwiftSyntaxMacros"]
77+
dependencies: ["SwiftCompilerPluginMessageHandling", "SwiftSyntaxMacros"],
78+
exclude: ["CMakeLists.txt"]
7879
),
7980

8081
.testTarget(
@@ -206,7 +207,6 @@ let package = Package(
206207
dependencies: ["SwiftSyntax"],
207208
exclude: ["CMakeLists.txt", "README.md"],
208209
swiftSettings: swiftParserSwiftSettings
209-
210210
),
211211

212212
.testTarget(
@@ -247,7 +247,8 @@ let package = Package(
247247

248248
.target(
249249
name: "SwiftRefactor",
250-
dependencies: ["SwiftBasicFormat", "SwiftParser", "SwiftSyntax", "SwiftSyntaxBuilder"]
250+
dependencies: ["SwiftBasicFormat", "SwiftParser", "SwiftSyntax", "SwiftSyntaxBuilder"],
251+
exclude: ["CMakeLists.txt"]
251252
),
252253

253254
.testTarget(

Release Notes/600.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,10 @@
126126
- Pull request: https://github.com/apple/swift-syntax/pull/1554
127127
- Migration steps: Add the new property `lexicalContext` to any `MacroExpansionContext`-conforming types. If implementing the host-to-plugin message protocol, add support for `lexicalContext`. For macro expansion operations going through `SyntaxProtocol.expand`, provide a context generator that creates a fresh context including the lexical context.
128128

129+
- `TriviaPiece.isBackslash` in `SwiftParserDiagnostics` removed
130+
- Description: `TriviaPiece.isBackslash` was not intended to be public API.
131+
- Pull request: xxx
132+
- Migration steps: Use `if case .backslash = triviaPiece` instead
129133

130134
## Template
131135

Sources/SwiftBasicFormat/BasicFormat.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13+
#if swift(>=6)
14+
@_spi(RawSyntax) public import SwiftSyntax
15+
#else
1316
@_spi(RawSyntax) import SwiftSyntax
17+
#endif
1418

1519
/// A rewriter that performs a "basic" format of the passed tree.
1620
///

Sources/SwiftBasicFormat/InferIndentation.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13+
#if swift(>=6)
14+
public import SwiftSyntax
15+
#else
1316
import SwiftSyntax
17+
#endif
1418

1519
extension BasicFormat {
1620
/// Uses heuristics to infer the indentation width used in the given syntax tree.

Sources/SwiftBasicFormat/Syntax+Extensions.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13+
#if swift(>=6)
14+
public import SwiftSyntax
15+
#else
1316
import SwiftSyntax
17+
#endif
1418

1519
extension TokenSyntax {
1620
/// The indentation of this token

Sources/SwiftBasicFormat/SyntaxProtocol+Formatted.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13+
#if swift(>=6)
14+
public import SwiftSyntax
15+
#else
1316
import SwiftSyntax
17+
#endif
1418

1519
public extension SyntaxProtocol {
1620
/// Build a syntax node from this `Buildable` and format it with the given format.

Sources/SwiftBasicFormat/Trivia+FormatExtensions.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13+
#if swift(>=6)
14+
public import SwiftSyntax
15+
#else
1316
import SwiftSyntax
17+
#endif
1418

1519
extension Trivia {
1620
/// Removes all whitespaces that is trailing before a newline trivia,

Sources/SwiftCompilerPlugin/CompilerPlugin.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,18 @@
1212
// NOTE: This basic plugin mechanism is mostly copied from
1313
// https://github.com/apple/swift-package-manager/blob/main/Sources/PackagePlugin/Plugin.swift
1414

15-
import SwiftSyntaxMacros
16-
17-
#if swift(>=5.11)
15+
#if swift(>=6.0)
16+
public import SwiftSyntaxMacros
1817
private import Foundation
1918
private import SwiftCompilerPluginMessageHandling
2019
#else
20+
import SwiftSyntaxMacros
2121
import Foundation
2222
import SwiftCompilerPluginMessageHandling
2323
#endif
2424

2525
#if os(Windows)
26-
#if swift(>=5.11)
26+
#if swift(>=6.0)
2727
private import ucrt
2828
#else
2929
import ucrt

Sources/SwiftCompilerPluginMessageHandling/CompilerPluginMessageHandler.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13+
#if swift(>=6)
14+
public import SwiftSyntaxMacros
15+
#else
1316
import SwiftSyntaxMacros
17+
#endif
1418

1519
/// Optional features.
1620
public enum PluginFeature: String {

Sources/SwiftDiagnostics/Convenience.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13+
#if swift(>=6)
14+
public import SwiftSyntax
15+
#else
1316
import SwiftSyntax
17+
#endif
1418

1519
extension Diagnostic {
1620
/// Construct a new diagnostic that has exactly one Fix-It.

Sources/SwiftDiagnostics/Diagnostic.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13+
#if swift(>=6)
14+
public import SwiftSyntax
15+
#else
1316
import SwiftSyntax
17+
#endif
1418

1519
public struct Diagnostic: CustomDebugStringConvertible, Sendable {
1620
/// The message that should be displayed to the user

Sources/SwiftDiagnostics/DiagnosticsFormatter.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13+
#if swift(>=6)
14+
public import SwiftSyntax
15+
#else
1316
import SwiftSyntax
17+
#endif
1418

1519
extension Sequence where Element == Range<Int> {
1620
/// Given a set of ranges that are sorted in order of nondecreasing lower

Sources/SwiftDiagnostics/FixIt.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13+
#if swift(>=6)
14+
public import SwiftSyntax
15+
#else
1316
import SwiftSyntax
17+
#endif
1418

1519
/// Types conforming to this protocol represent Fix-It messages that can be
1620
/// shown to the client.

Sources/SwiftDiagnostics/GroupedDiagnostics.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,12 @@
99
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
1010
//
1111
//===----------------------------------------------------------------------===//
12+
13+
#if swift(>=6)
14+
public import SwiftSyntax
15+
#else
1216
import SwiftSyntax
17+
#endif
1318

1419
public struct GroupedDiagnostics {
1520
/// A unique identifier for a source file.

Sources/SwiftDiagnostics/Note.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13+
#if swift(>=6)
14+
public import SwiftSyntax
15+
#else
1316
import SwiftSyntax
17+
#endif
1418

1519
/// Types conforming to this protocol represent note messages that can be
1620
/// shown to the client.

0 commit comments

Comments
 (0)