Skip to content

Commit

Permalink
Enable strict concurrency (#686)
Browse files Browse the repository at this point in the history
### Motivation:

Catch potential data races at build time.

### Modifications:

- Enabled unconditional strict concurrency complete checking.
- Enabled warnings-as-errors on Swift 6 Linux pipelines.
- Made a few fixes to be strict concurrency-clean.

### Result:

Fewer potential data races can sneak in.

### Test Plan

Ran tests locally, did not see any more warnings or errors.
  • Loading branch information
czechboy0 authored Dec 2, 2024
1 parent cdc23b5 commit 1524989
Show file tree
Hide file tree
Showing 14 changed files with 18 additions and 29 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
with:
linux_5_9_arguments_override: "--explicit-target-dependency-import-check error"
linux_5_10_arguments_override: "--explicit-target-dependency-import-check error"
linux_6_0_arguments_override: "--explicit-target-dependency-import-check error"
linux_6_0_arguments_override: "-Xswiftc -warnings-as-errors --explicit-target-dependency-import-check error"
linux_nightly_6_0_arguments_override: "--explicit-target-dependency-import-check error"
linux_nightly_main_arguments_override: "--explicit-target-dependency-import-check error"

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/pull_request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
with:
linux_5_9_arguments_override: "--explicit-target-dependency-import-check error"
linux_5_10_arguments_override: "--explicit-target-dependency-import-check error"
linux_6_0_arguments_override: "--explicit-target-dependency-import-check error"
linux_6_0_arguments_override: "-Xswiftc -warnings-as-errors --explicit-target-dependency-import-check error"
linux_nightly_6_0_arguments_override: "--explicit-target-dependency-import-check error"
linux_nightly_main_enabled: false

Expand Down
9 changes: 1 addition & 8 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,9 @@ import PackageDescription
var swiftSettings: [SwiftSetting] = [
// https://github.com/apple/swift-evolution/blob/main/proposals/0335-existential-any.md
// Require `any` for existential types.
.enableUpcomingFeature("ExistentialAny")
.enableUpcomingFeature("ExistentialAny"), .enableExperimentalFeature("StrictConcurrency=complete"),
]

// Strict concurrency is enabled in CI; use this environment variable to enable it locally.
if ProcessInfo.processInfo.environment["SWIFT_OPENAPI_STRICT_CONCURRENCY"].flatMap(Bool.init) ?? false {
swiftSettings.append(contentsOf: [
.define("SWIFT_OPENAPI_STRICT_CONCURRENCY"), .enableExperimentalFeature("StrictConcurrency"),
])
}

let package = Package(
name: "swift-openapi-generator",
platforms: [
Expand Down
8 changes: 5 additions & 3 deletions Sources/_OpenAPIGeneratorCore/Diagnostics.swift
Original file line number Diff line number Diff line change
Expand Up @@ -174,15 +174,17 @@ public protocol DiagnosticCollector {
/// It receives diagnostics and forwards them to an upstream `DiagnosticCollector`.
///
/// If a diagnostic with a severity of `.error` is emitted, this collector will throw the diagnostic as an error.
public struct ErrorThrowingDiagnosticCollector: DiagnosticCollector {
let upstream: any DiagnosticCollector
public struct ErrorThrowingDiagnosticCollector: DiagnosticCollector, Sendable {

/// The `DiagnosticCollector` to which this collector will forward diagnostics.
internal let upstream: any DiagnosticCollector & Sendable

/// Initializes a new `ErrorThrowingDiagnosticCollector` with an upstream `DiagnosticCollector`.
///
/// The upstream collector is where this collector will forward all received diagnostics.
///
/// - Parameter upstream: The `DiagnosticCollector` to which this collector will forward diagnostics.
public init(upstream: any DiagnosticCollector) { self.upstream = upstream }
public init(upstream: any DiagnosticCollector & Sendable) { self.upstream = upstream }

/// Emits a diagnostic to the collector.
///
Expand Down
4 changes: 0 additions & 4 deletions Sources/_OpenAPIGeneratorCore/PlatformChecks.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,3 @@
"_OpenAPIGeneratorCore is only to be used by swift-openapi-generator itself—your target should not link this library or the command line tool directly."
)
#endif

#if SWIFT_OPENAPI_STRICT_CONCURRENCY
#warning("Compiling with Strict Concurrency")
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
import OpenAPIKit
@preconcurrency import OpenAPIKit

/// The strategy for handling the additional properties key in a multipart schema.
enum MultipartAdditionalPropertiesStrategy: Equatable {
enum MultipartAdditionalPropertiesStrategy: Equatable, Sendable {

/// A strategy where additional properties are explicitly disallowed.
case disallowed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ extension FileTranslator {
return nil
}
let finalContentTypeSource: MultipartPartInfo.ContentTypeSource
if let encoding, let contentType = encoding.contentType {
if let encoding, let contentType = encoding.contentTypes.first, encoding.contentTypes.count == 1 {
finalContentTypeSource = try .explicit(contentType.asGeneratorContentType)
} else {
finalContentTypeSource = candidateSource
Expand Down
4 changes: 1 addition & 3 deletions Sources/swift-openapi-generator/Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,11 @@ import Yams

#if $RetroactiveAttribute
extension URL: @retroactive ExpressibleByArgument {}
extension GeneratorMode: @retroactive ExpressibleByArgument {}
extension FeatureFlag: @retroactive ExpressibleByArgument {}
#else
extension URL: ExpressibleByArgument {}
#endif
extension GeneratorMode: ExpressibleByArgument {}
extension FeatureFlag: ExpressibleByArgument {}
#endif

extension URL {

Expand Down
2 changes: 1 addition & 1 deletion Sources/swift-openapi-generator/FilterCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import Yams
import OpenAPIKit

struct _FilterCommand: AsyncParsableCommand {
static var configuration = CommandConfiguration(
static let configuration = CommandConfiguration(
commandName: "filter",
abstract: "Filter an OpenAPI document",
discussion: """
Expand Down
2 changes: 1 addition & 1 deletion Sources/swift-openapi-generator/GenerateCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import Foundation
import _OpenAPIGeneratorCore

struct _GenerateCommand: AsyncParsableCommand {
static var configuration: CommandConfiguration = .init(
static let configuration: CommandConfiguration = .init(
commandName: "generate",
abstract: "Generate Swift files from an OpenAPI document",
discussion: """
Expand Down
2 changes: 1 addition & 1 deletion Sources/swift-openapi-generator/Tool.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import ArgumentParser

@main struct _Tool: AsyncParsableCommand {
static var configuration: CommandConfiguration = .init(
static let configuration: CommandConfiguration = .init(
commandName: "swift-openapi-generator",
abstract: "Generate Swift client and server code from an OpenAPI document",
subcommands: [_FilterCommand.self, _GenerateCommand.self]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
//
//===----------------------------------------------------------------------===//
import XCTest
import OpenAPIKit
@preconcurrency import OpenAPIKit
@testable import _OpenAPIGeneratorCore

class Test_MultipartAdditionalProperties: XCTestCase {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
//
//===----------------------------------------------------------------------===//
import XCTest
import OpenAPIKit
@preconcurrency import OpenAPIKit
@testable import _OpenAPIGeneratorCore

final class Test_TypeMatcher: Test_Core {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
//
//===----------------------------------------------------------------------===//
import XCTest
import OpenAPIKit
@preconcurrency import OpenAPIKit
@testable import _OpenAPIGeneratorCore

class Test_isSchemaSupported: XCTestCase {
Expand Down

0 comments on commit 1524989

Please sign in to comment.