Skip to content

Fix build warnings #1603

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 5 commits into from
Aug 1, 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 @@ -11,6 +11,7 @@
//===----------------------------------------------------------------------===//

import BuildServerProtocol
import Foundation
import LanguageServerProtocol
import LanguageServerProtocolJSONRPC
import SKLogging
Expand All @@ -26,13 +27,6 @@ import var TSCBasic.localFileSystem
import func TSCBasic.lookupExecutablePath
import func TSCBasic.resolveSymlinks

#if canImport(Darwin)
import Foundation
#else
// FIMXE: (async-workaround) @preconcurrency needed because Pipe is not marked as Sendable on Linux rdar://132378792
@preconcurrency import Foundation
#endif

enum BuildServerTestError: Error {
case executableNotFound(String)
}
Expand Down
8 changes: 6 additions & 2 deletions Sources/Diagnose/DiagnoseCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,9 @@ package struct DiagnoseCommand: AsyncParsableCommand {
#if os(macOS)
reportProgress(.collectingLogMessages(progress: 0), message: "Collecting log messages")
let outputFileUrl = bundlePath.appendingPathComponent("log.txt")
FileManager.default.createFile(atPath: outputFileUrl.path, contents: nil)
guard FileManager.default.createFile(atPath: outputFileUrl.path, contents: nil) else {
throw ReductionError("Failed to create log.txt")
}
let fileHandle = try FileHandle(forWritingTo: outputFileUrl)
var bytesCollected = 0
// 50 MB is an average log size collected by sourcekit-lsp diagnose.
Expand Down Expand Up @@ -304,7 +306,9 @@ package struct DiagnoseCommand: AsyncParsableCommand {
@MainActor
private func addSwiftVersion(toBundle bundlePath: URL) async throws {
let outputFileUrl = bundlePath.appendingPathComponent("swift-versions.txt")
FileManager.default.createFile(atPath: outputFileUrl.path, contents: nil)
guard FileManager.default.createFile(atPath: outputFileUrl.path, contents: nil) else {
throw ReductionError("Failed to create file at \(outputFileUrl)")
}
let fileHandle = try FileHandle(forWritingTo: outputFileUrl)

let toolchains = try await toolchainRegistry.toolchains
Expand Down
2 changes: 1 addition & 1 deletion Sources/Diagnose/IndexCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -121,4 +121,4 @@ fileprivate extension SourceKitLSPServer {
}
}

extension ExperimentalFeature: ExpressibleByArgument {}
extension ExperimentalFeature: ArgumentParser.ExpressibleByArgument {}
3 changes: 2 additions & 1 deletion Sources/Diagnose/MergeSwiftFiles.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ extension RequestInfo {
progressUpdate: (_ progress: Double, _ message: String) -> Void
) async throws -> RequestInfo? {
let swiftFilePaths = compilerArgs.filter { $0.hasSuffix(".swift") }
let mergedFile = try swiftFilePaths.map { try String(contentsOfFile: $0) }.joined(separator: "\n\n\n\n")
let mergedFile = try swiftFilePaths.map { try String(contentsOfFile: $0, encoding: .utf8) }
.joined(separator: "\n\n\n\n")

progressUpdate(0, "Merging all .swift files into a single file")

Expand Down
2 changes: 1 addition & 1 deletion Sources/Diagnose/ReduceCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ package struct ReduceCommand: AsyncParsableCommand {

let progressBar = PercentProgressAnimation(stream: stderrStreamConcurrencySafe, header: "Reducing sourcekitd issue")

let request = try String(contentsOfFile: sourcekitdRequestPath)
let request = try String(contentsOfFile: sourcekitdRequestPath, encoding: .utf8)
let requestInfo = try RequestInfo(request: request)

let executor = OutOfProcessSourceKitRequestExecutor(
Expand Down
4 changes: 2 additions & 2 deletions Sources/Diagnose/RequestInfo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ package struct RequestInfo: Sendable {

self.requestTemplate = requestTemplate

fileContents = try String(contentsOf: URL(fileURLWithPath: sourceFilePath))
fileContents = try String(contentsOf: URL(fileURLWithPath: sourceFilePath), encoding: .utf8)
}

/// Create a `RequestInfo` that is used to reduce a `swift-frontend issue`
Expand All @@ -126,7 +126,7 @@ package struct RequestInfo: Sendable {
guard let fileList = iterator.next() else {
throw ReductionError("Expected file path after -filelist command line argument")
}
frontendArgsWithFilelistInlined += try String(contentsOfFile: fileList)
frontendArgsWithFilelistInlined += try String(contentsOfFile: fileList, encoding: .utf8)
.split(separator: "\n")
.map { String($0) }
default:
Expand Down
2 changes: 1 addition & 1 deletion Sources/Diagnose/RunSourcekitdRequestCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ package struct RunSourceKitdRequestCommand: AsyncParsableCommand {
package init() {}

package func run() async throws {
var requestString = try String(contentsOf: URL(fileURLWithPath: sourcekitdRequestPath))
var requestString = try String(contentsOf: URL(fileURLWithPath: sourcekitdRequestPath), encoding: .utf8)

let installPath = try AbsolutePath(validating: Bundle.main.bundlePath)
let sourcekitdPath =
Expand Down
3 changes: 1 addition & 2 deletions Sources/SKLogging/NonDarwinLogging.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ import SwiftExtensions
#if canImport(Darwin)
import Foundation
#else
// FIMXE: (async-workaround) @preconcurrency needed because DateFormatter and stderr are not marked as Sendable on Linux
// rdar://125578486, rdar://132378589
// TODO: @preconcurrency needed because stderr is not sendable on Linux https://github.com/swiftlang/swift/issues/75601
@preconcurrency import Foundation
#endif

Expand Down
3 changes: 1 addition & 2 deletions Sources/SKLogging/SetGlobalLogFileHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ import RegexBuilder
#if canImport(Darwin)
import Foundation
#else
// FIMXE: (async-workaround) @preconcurrency needed because DateFormatter and stderr are not marked as Sendable on Linux
// rdar://125578486, rdar://132378589
// TODO: @preconcurrency needed because stderr is not sendable on Linux https://github.com/swiftlang/swift/issues/75601
@preconcurrency import Foundation
#endif

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ extension DocumentURI {
return "<DocumentURI length=\(description.count) hash=\(description.hashForLogging)>"
}
}
extension DocumentURI: CustomLogStringConvertible {}
extension DocumentURI: SKLogging.CustomLogStringConvertible {}
5 changes: 4 additions & 1 deletion Sources/SKTestSupport/SkipUnless.swift
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,10 @@ package actor SkipUnless {
.appending(component: "swift-frontend")
return try await withTestScratchDir { scratchDirectory in
let input = scratchDirectory.appending(component: "Input.swift")
FileManager.default.createFile(atPath: input.pathString, contents: nil)
guard FileManager.default.createFile(atPath: input.pathString, contents: nil) else {
struct FailedToCrateInputFileError: Error {}
throw FailedToCrateInputFileError()
}
// If we can't compile for wasm, this fails complaining that it can't find the stdlib for wasm.
let process = Process(
args: swiftFrontend.pathString,
Expand Down
7 changes: 1 addition & 6 deletions Sources/SKTestSupport/TestJSONRPCConnection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,7 @@ import SKSupport
import SwiftExtensions
import XCTest

#if canImport(Darwin)
import class Foundation.Pipe
#else
// FIMXE: (async-workaround) @preconcurrency needed because Pipe is not marked as Sendable on Linux rdar://132378792
@preconcurrency import class Foundation.Pipe
#endif

package final class TestJSONRPCConnection: Sendable {
package let clientToServer: Pipe = Pipe()
Expand Down Expand Up @@ -207,7 +202,7 @@ private let testMessageRegistry = MessageRegistry(
notifications: [EchoNotification.self, ShowMessageNotification.self]
)

extension String: ResponseType {}
extension String: LanguageServerProtocol.ResponseType {}

package struct EchoRequest: RequestType {
package static let method: String = "test_server/echo"
Expand Down
12 changes: 1 addition & 11 deletions Sources/SourceKitLSP/Clang/ClangLanguageService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
//===----------------------------------------------------------------------===//

import BuildSystemIntegration
import Foundation
import LanguageServerProtocol
import LanguageServerProtocolJSONRPC
import SKLogging
Expand All @@ -22,21 +23,10 @@ import ToolchainRegistry

import struct TSCBasic.AbsolutePath

#if canImport(Darwin)
import Foundation
#else
// FIMXE: (async-workaround) @preconcurrency needed because Pipe is not marked as Sendable on Linux
@preconcurrency import Foundation
#endif

#if os(Windows)
import WinSDK
#endif

#if !canImport(Darwin)
extension Process: @unchecked Sendable {}
#endif

/// A thin wrapper over a connection to a clangd server providing build setting handling.
///
/// In addition, it also intercepts notifications and replies from clangd in order to do things
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ package extension DocumentSnapshot {
///
/// Throws an error if the file could not be read.
init(withContentsFromDisk url: URL, language: Language) throws {
let contents = try String(contentsOf: url)
let contents = try String(contentsOf: url, encoding: .utf8)
self.init(uri: DocumentURI(url), language: language, version: 0, lineTable: LineTable(contents))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,4 @@ extension UncheckedIndex {
}
}

extension UncheckedIndex: MainFilesProvider {}
extension UncheckedIndex: BuildSystemIntegration.MainFilesProvider {}
6 changes: 0 additions & 6 deletions Sources/SourceKitLSP/Swift/MacroExpansion.swift
Original file line number Diff line number Diff line change
Expand Up @@ -180,12 +180,6 @@ extension SwiftLanguageService {
}

func expandMacro(macroExpansionURLData: MacroExpansionReferenceDocumentURLData) async throws -> String {
guard let sourceKitLSPServer = self.sourceKitLSPServer else {
// `SourceKitLSPServer` has been destructed. We are tearing down the
// language server. Nothing left to do.
throw ResponseError.unknown("Connection to the editor closed")
}

let expandMacroCommand = ExpandMacroCommand(
positionRange: macroExpansionURLData.selectionRange,
textDocument: TextDocumentIdentifier(macroExpansionURLData.primaryFile)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,12 @@ package struct MacroExpansionReferenceDocumentURLData {
}

package init(displayName: String, queryItems: [URLQueryItem]) throws {
guard let primaryFilePath = queryItems.last { $0.name == Parameters.primaryFilePath }?.value,
let fromLine = Int(queryItems.last { $0.name == Parameters.fromLine }?.value ?? ""),
let fromColumn = Int(queryItems.last { $0.name == Parameters.fromColumn }?.value ?? ""),
let toLine = Int(queryItems.last { $0.name == Parameters.toLine }?.value ?? ""),
let toColumn = Int(queryItems.last { $0.name == Parameters.toColumn }?.value ?? ""),
let bufferName = queryItems.last { $0.name == Parameters.bufferName }?.value
guard let primaryFilePath = queryItems.last(where: { $0.name == Parameters.primaryFilePath })?.value,
let fromLine = Int(queryItems.last(where: { $0.name == Parameters.fromLine })?.value ?? ""),
let fromColumn = Int(queryItems.last(where: { $0.name == Parameters.fromColumn })?.value ?? ""),
let toLine = Int(queryItems.last(where: { $0.name == Parameters.toLine })?.value ?? ""),
let toColumn = Int(queryItems.last(where: { $0.name == Parameters.toColumn })?.value ?? ""),
let bufferName = queryItems.last(where: { $0.name == Parameters.bufferName })?.value
else {
throw ReferenceDocumentURLError(description: "Invalid queryItems for macro expansion reference document url")
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/SourceKitLSP/Swift/ReferenceDocumentURL.swift
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ package enum ReferenceDocumentURL {
throw ReferenceDocumentURLError(
description: "Bad URL for reference document: \(url)"
)
default:
case let documentType?:
throw ReferenceDocumentURLError(
description: "Invalid document type in URL for reference document: \(documentType)"
)
Expand Down
14 changes: 4 additions & 10 deletions Sources/sourcekit-lsp/SourceKitLSP.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import BuildSystemIntegration
import Csourcekitd // Not needed here, but fixes debugging...
import Diagnose
import Dispatch
import Foundation
import LanguageServerProtocol
import LanguageServerProtocolJSONRPC
import SKLogging
Expand All @@ -28,13 +29,6 @@ import struct TSCBasic.AbsolutePath
import struct TSCBasic.RelativePath
import var TSCBasic.localFileSystem

#if canImport(Darwin)
import Foundation
#else
// FIMXE: (async-workaround) @preconcurrency needed because FileHandle is not marked as Sendable on Linux rdar://132378985
@preconcurrency import Foundation
#endif

extension AbsolutePath {
public init?(argument: String) {
let path: AbsolutePath?
Expand Down Expand Up @@ -90,11 +84,11 @@ extension PathPrefixMapping {
)
}
}
extension PathPrefixMapping: ExpressibleByArgument {}
extension PathPrefixMapping: ArgumentParser.ExpressibleByArgument {}

extension SKOptions.BuildConfiguration: ExpressibleByArgument {}
extension SKOptions.BuildConfiguration: ArgumentParser.ExpressibleByArgument {}

extension SKOptions.WorkspaceType: ExpressibleByArgument {}
extension SKOptions.WorkspaceType: ArgumentParser.ExpressibleByArgument {}

@main
struct SourceKitLSP: AsyncParsableCommand {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,13 @@

import BuildServerProtocol
import BuildSystemIntegration
import Foundation
import ISDBTestSupport
import LanguageServerProtocol
import SKTestSupport
import TSCBasic
import XCTest

#if canImport(Darwin)
import Foundation
#else
// FIMXE: (async-workaround) @preconcurrency needed because Pipe is not marked as Sendable on Linux rdar://132378792
@preconcurrency import Foundation
#endif

/// The path to the INPUTS directory of shared test projects.
private let skTestSupportInputsDirectory: URL = {
#if os(macOS)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,7 @@ import LanguageServerProtocol
import SKTestSupport
import XCTest

#if canImport(Darwin)
import class Foundation.Pipe
#else
// FIMXE: (async-workaround) @preconcurrency needed because Pipe is not marked as Sendable on Linux rdar://132378792
@preconcurrency import class Foundation.Pipe
#endif

#if os(Windows)
import WinSDK
Expand Down
6 changes: 3 additions & 3 deletions Tests/SourceKitLSPTests/BackgroundIndexingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1199,7 +1199,7 @@ final class BackgroundIndexingTests: XCTestCase {
)
let packageResolvedURL = project.scratchDirectory.appendingPathComponent("Package.resolved")

let originalPackageResolvedContents = try String(contentsOf: packageResolvedURL)
let originalPackageResolvedContents = try String(contentsOf: packageResolvedURL, encoding: .utf8)

// First check our setup to see that we get the expected hover response before changing the dependency project.
let (uri, positions) = try project.openDocument("Test.swift")
Expand Down Expand Up @@ -1234,7 +1234,7 @@ final class BackgroundIndexingTests: XCTestCase {
])
)
try await project.testClient.send(PollIndexRequest())
XCTAssertEqual(try String(contentsOf: packageResolvedURL), originalPackageResolvedContents)
XCTAssertEqual(try String(contentsOf: packageResolvedURL, encoding: .utf8), originalPackageResolvedContents)

// Simulate a package update which goes as follows:
// - The user runs `swift package update`
Expand All @@ -1248,7 +1248,7 @@ final class BackgroundIndexingTests: XCTestCase {
],
workingDirectory: nil
)
XCTAssertNotEqual(try String(contentsOf: packageResolvedURL), originalPackageResolvedContents)
XCTAssertNotEqual(try String(contentsOf: packageResolvedURL, encoding: .utf8), originalPackageResolvedContents)
project.testClient.send(
DidChangeWatchedFilesNotification(changes: [
FileEvent(uri: DocumentURI(project.scratchDirectory.appendingPathComponent("Package.resolved")), type: .changed)
Expand Down