Repository: apple/swift-syntax (swift)

Issue:
Getting warnings about `@_implementationOnly` when building a package dependent on SwiftSyntax
### Description

When building swift-testing, I get a bunch of warnings along these lines while SwiftSyntax builds:

```
/Volumes/Dev/Source/swift-testing/.build/checkouts/swift-syntax/Sources/SwiftSyntax/SyntaxText.swift:15:22: warning: using '@_implementationOnly' without enabling library evolution for 'SwiftSyntax' may lead to instability during execution
@_implementationOnly import Darwin
                     ^
/Volumes/Dev/Source/swift-testing/.build/checkouts/swift-syntax/Sources/SwiftCompilerPlugin/CompilerPlugin.swift:16:22: warning: using '@_implementationOnly' without enabling library evolution for 'SwiftCompilerPlugin' may lead to instability during execution
@_implementationOnly import SwiftCompilerPluginMessageHandling
                     ^
```

This then pollutes the output of the downstream package (swift-testing) that's using SwiftSyntax. :(

### Steps to Reproduce

Using toolchain swift-DEVELOPMENT-SNAPSHOT-2023-09-20-a and SwiftSyntax 509.0.0.

To repro:

1. Check out https://github.com/apple/swift-testing
2. `export TOOLCHAINS=swift` (if needed)
3. `swift test`

Pull request description:
These annotations produce warnings when compiling swift-syntax without library evolution using Swift ≥5.10.

Replace them by `private import` when compiling using Swift ≥5.11.

Also mark the import of XCTest from `SwiftSyntaxMacrosTestSupport` as `private`, fixing rdar://119517162.

Fixes https://github.com/apple/swift-syntax/issues/2228
rdar://119517162
rdar://115910675

Patch:
diff --git a/Sources/SwiftCompilerPlugin/CompilerPlugin.swift b/Sources/SwiftCompilerPlugin/CompilerPlugin.swift
index c16a8390c85..b4efd0d1258 100644
--- a/Sources/SwiftCompilerPlugin/CompilerPlugin.swift
+++ b/Sources/SwiftCompilerPlugin/CompilerPlugin.swift
@@ -12,12 +12,22 @@
 // NOTE: This basic plugin mechanism is mostly copied from
 // https://github.com/apple/swift-package-manager/blob/main/Sources/PackagePlugin/Plugin.swift
 
-@_implementationOnly import Foundation
-@_implementationOnly import SwiftCompilerPluginMessageHandling
 import SwiftSyntaxMacros
 
+#if swift(>=5.11)
+private import Foundation
+private import SwiftCompilerPluginMessageHandling
+#else
+import Foundation
+import SwiftCompilerPluginMessageHandling
+#endif
+
 #if os(Windows)
-@_implementationOnly import ucrt
+#if swift(>=5.11)
+private import ucrt
+#else
+import ucrt
+#endif
 #endif
 
 //
@@ -167,8 +177,8 @@ extension CompilerPlugin {
 }
 
 internal struct PluginHostConnection: MessageConnection {
-  let inputStream: FileHandle
-  let outputStream: FileHandle
+  fileprivate let inputStream: FileHandle
+  fileprivate let outputStream: FileHandle
 
   func sendMessage<TX: Encodable>(_ message: TX) throws {
     // Encode the message as JSON.
diff --git a/Sources/SwiftSyntax/SyntaxText.swift b/Sources/SwiftSyntax/SyntaxText.swift
index c5068a76aa4..a15ef1a7b15 100644
--- a/Sources/SwiftSyntax/SyntaxText.swift
+++ b/Sources/SwiftSyntax/SyntaxText.swift
@@ -10,12 +10,22 @@
 //
 //===----------------------------------------------------------------------===//
 
+#if swift(>=5.11)
 #if canImport(Darwin)
-@_implementationOnly import Darwin
+private import Darwin
 #elseif canImport(Glibc)
-@_implementationOnly import Glibc
+private import Glibc
 #elseif canImport(Musl)
-@_implementationOnly import Musl
+private import Musl
+#endif
+#else
+#if canImport(Darwin)
+import Darwin
+#elseif canImport(Glibc)
+import Glibc
+#elseif canImport(Musl)
+import Musl
+#endif
 #endif
 
 /// Represent a string.


Test patch:
diff --git a/Sources/SwiftSyntaxMacrosTestSupport/Assertions.swift b/Sources/SwiftSyntaxMacrosTestSupport/Assertions.swift
index 5a3fcdbd2c8..7f7d2f40dac 100644
--- a/Sources/SwiftSyntaxMacrosTestSupport/Assertions.swift
+++ b/Sources/SwiftSyntaxMacrosTestSupport/Assertions.swift
@@ -17,9 +17,14 @@ import SwiftParserDiagnostics
 import SwiftSyntax
 import SwiftSyntaxMacroExpansion
 import SwiftSyntaxMacros
-import XCTest
 import _SwiftSyntaxTestSupport
 
+#if swift(>=5.11)
+private import XCTest
+#else
+import XCTest
+#endif
+
 // MARK: - Note
 
 /// Describes a diagnostic note that tests expect to be created by a macro expansion.