Skip to content

Commit

Permalink
Compile with -strict-concurrency=complete (#5320)
Browse files Browse the repository at this point in the history
* Compile with `-strict-concurrency=complete`

Only in Bazel for now, because this is considered an unsafe flag in
SwiftPM which would lead to warnings for downstream consumers of
SwiftLint using SwiftPM.

Some imports of SwiftSyntax need the `@preconcurrency` annotation until
swiftlang/swift-syntax#2322 is available in a
release.

The following SwiftLint libraries have `-strict-concurrency=complete`
applied:

* SwiftLintCoreMacros
* SwiftLintBuiltInRules
* SwiftLintExtraRules

The following SwiftLint libraries don't have the flag applied and need
to be migrated:

* SwiftLintCore
* swiftlint (CLI target)

So really the rules and macros are now being compiled with
`-strict-concurrency=complete`, but the core infrastructure of SwiftLint
is not.

Still, given that Swift 6 will eventually make these warnings errors by
default, it's good to prevent issues from creeping in earlier rather
than later.

* Add CI job to build with strict concurrency
  • Loading branch information
jpsim authored Nov 1, 2023
1 parent 55c18c5 commit 3eb3772
Show file tree
Hide file tree
Showing 17 changed files with 56 additions and 21 deletions.
8 changes: 8 additions & 0 deletions .buildkite/pipeline.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@ steps:
- bazel build :swiftlint
- echo "+++ Test"
- bazel test --test_output=errors //Tests/...
- label: "Build With Strict Concurrency"
commands:
- echo "+++ Add @preconcurrency imports"
- ./tools/add-preconcurrency-imports.sh
- echo "+++ Build"
- bazel build --define strict_concurrency_builtin_rules=true :swiftlint
- echo "--- Clean up"
- git reset --hard
- label: "SwiftPM"
commands:
- echo "+++ Test"
Expand Down
28 changes: 21 additions & 7 deletions BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,27 @@ load(
"swift_library",
)

config_setting(
name = "strict_concurrency_builtin_rules",
values = {"define": "strict_concurrency_builtin_rules=true"},
)

copts = [
"-enable-upcoming-feature",
"ExistentialAny",
]

strict_concurrency_copts = [
"-Xfrontend",
"-strict-concurrency=complete",
]

# Targets

swift_library(
name = "SwiftLintCoreMacrosLib",
srcs = glob(["Source/SwiftLintCoreMacros/*.swift"]),
copts = copts,
copts = copts + strict_concurrency_copts,
module_name = "SwiftLintCoreMacros",
visibility = ["//visibility:public"],
deps = [
Expand All @@ -28,7 +38,7 @@ swift_library(
swift_compiler_plugin(
name = "SwiftLintCoreMacros",
srcs = glob(["Source/SwiftLintCoreMacros/*.swift"]),
copts = copts,
copts = copts + strict_concurrency_copts,
deps = [
"@SwiftSyntax//:SwiftCompilerPlugin_opt",
"@SwiftSyntax//:SwiftSyntaxMacros_opt",
Expand All @@ -38,7 +48,7 @@ swift_compiler_plugin(
swift_library(
name = "SwiftLintCore",
srcs = glob(["Source/SwiftLintCore/**/*.swift"]),
copts = copts,
copts = copts, # TODO: strict_concurrency_copts
module_name = "SwiftLintCore",
plugins = [
":SwiftLintCoreMacros",
Expand All @@ -62,7 +72,10 @@ swift_library(
swift_library(
name = "SwiftLintBuiltInRules",
srcs = glob(["Source/SwiftLintBuiltInRules/**/*.swift"]),
copts = copts,
copts = copts + select({
":strict_concurrency_builtin_rules": strict_concurrency_copts,
"//conditions:default": [],
}),
module_name = "SwiftLintBuiltInRules",
visibility = ["//visibility:public"],
deps = [
Expand All @@ -76,6 +89,7 @@ swift_library(
"Source/SwiftLintExtraRules/Exports.swift",
"@swiftlint_extra_rules//:extra_rules",
],
copts = copts + strict_concurrency_copts,
module_name = "SwiftLintExtraRules",
visibility = ["//visibility:public"],
deps = [
Expand All @@ -88,7 +102,7 @@ swift_library(
srcs = glob(
["Source/SwiftLintFramework/**/*.swift"],
),
copts = copts,
copts = copts + strict_concurrency_copts,
module_name = "SwiftLintFramework",
visibility = ["//visibility:public"],
deps = [
Expand All @@ -101,7 +115,7 @@ swift_library(
swift_library(
name = "swiftlint.library",
srcs = glob(["Source/swiftlint/**/*.swift"]),
copts = copts,
copts = copts, # TODO: strict_concurrency_copts
module_name = "swiftlint",
visibility = ["//visibility:public"],
deps = [
Expand All @@ -114,7 +128,7 @@ swift_library(

swift_binary(
name = "swiftlint",
copts = copts,
copts = copts + strict_concurrency_copts,
visibility = ["//visibility:public"],
deps = [
":swiftlint.library",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import SwiftSyntax
struct DiscouragedNoneNameRule: OptInRule {
var configuration = SeverityConfiguration<Self>(.warning)

static var description = RuleDescription(
static let description = RuleDescription(
identifier: "discouraged_none_name",
name: "Discouraged None Name",
description: "Enum cases and static members named `none` are discouraged as they can conflict with " +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import SwiftSyntax
struct LegacyRandomRule: Rule {
var configuration = SeverityConfiguration<Self>(.warning)

static var description = RuleDescription(
static let description = RuleDescription(
identifier: "legacy_random",
name: "Legacy Random",
description: "Prefer using `type.random(in:)` over legacy functions",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import SwiftSyntax
struct ShorthandOptionalBindingRule: OptInRule {
var configuration = SeverityConfiguration<Self>(.warning)

static var description = RuleDescription(
static let description = RuleDescription(
identifier: "shorthand_optional_binding",
name: "Shorthand Optional Binding",
description: "Use shorthand syntax for optional binding",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import SwiftSyntax
struct DuplicatedKeyInDictionaryLiteralRule: Rule {
var configuration = SeverityConfiguration<Self>(.warning)

static var description = RuleDescription(
static let description = RuleDescription(
identifier: "duplicated_key_in_dictionary_literal",
name: "Duplicated Key in Dictionary Literal",
description: "Dictionary literals with duplicated keys will crash at runtime",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ private func warnDeprecatedOnce() {
struct UnusedCaptureListRule: SwiftSyntaxRule, OptInRule {
var configuration = SeverityConfiguration<Self>(.warning)

static var description = RuleDescription(
static let description = RuleDescription(
identifier: "unused_capture_list",
name: "Unused Capture List",
description: "Unused reference in a capture list should be removed",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import SwiftSyntax
@preconcurrency import SwiftSyntax
import SwiftSyntaxBuilder

@SwiftSyntaxRule(explicitRewriter: true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ struct ValidIBInspectableRule: Rule {
]
)

fileprivate static var supportedTypes: Set<String> = {
fileprivate static let supportedTypes: Set<String> = {
// "You can add the IBInspectable attribute to any property in a class declaration,
// class extension, or category of type: boolean, integer or floating point number, string,
// localized string, rectangle, point, size, color, range, and nil."
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import SwiftSyntax
struct ReduceIntoRule: OptInRule {
var configuration = SeverityConfiguration<Self>(.warning)

static var description = RuleDescription(
static let description = RuleDescription(
identifier: "reduce_into",
name: "Reduce into",
description: "Prefer `reduce(into:_:)` over `reduce(_:_:)` for copy-on-write types",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import SwiftSyntax
struct CollectionAlignmentRule: OptInRule {
var configuration = CollectionAlignmentConfiguration()

static var description = RuleDescription(
static let description = RuleDescription(
identifier: "collection_alignment",
name: "Collection Element Alignment",
description: "All elements in a collection literal should be vertically aligned",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import SwiftSyntax
struct DirectReturnRule: OptInRule {
var configuration = SeverityConfiguration<Self>(.warning)

static var description = RuleDescription(
static let description = RuleDescription(
identifier: "direct_return",
name: "Direct Return",
description: "Directly return the expression instead of assigning it to a variable first",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import SwiftSyntax
struct NonOverridableClassDeclarationRule: SwiftSyntaxCorrectableRule, OptInRule {
var configuration = NonOverridableClassDeclarationConfiguration()

static var description = RuleDescription(
static let description = RuleDescription(
identifier: "non_overridable_class_declaration",
name: "Class Declaration in Final Class",
description: """
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import SwiftSyntax
struct PreferSelfInStaticReferencesRule: SwiftSyntaxCorrectableRule, OptInRule {
var configuration = SeverityConfiguration<Self>(.warning)

static var description = RuleDescription(
static let description = RuleDescription(
identifier: "prefer_self_in_static_references",
name: "Prefer Self in Static References",
description: "Use `Self` to refer to the surrounding type name",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import SwiftSyntax
struct RedundantSelfInClosureRule: SwiftSyntaxCorrectableRule, OptInRule {
var configuration = SeverityConfiguration<Self>(.warning)

static var description = RuleDescription(
static let description = RuleDescription(
identifier: "redundant_self_in_closure",
name: "Redundant Self in Closure",
description: "Explicit use of 'self' is not required",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import SwiftSyntax
struct SuperfluousElseRule: OptInRule {
var configuration = SeverityConfiguration<Self>(.warning)

static var description = RuleDescription(
static let description = RuleDescription(
identifier: "superfluous_else",
name: "Superfluous Else",
description: "Else branches should be avoided when the previous if-block exits the current scope",
Expand Down
13 changes: 13 additions & 0 deletions tools/add-preconcurrency-imports.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/bin/bash

files=(
"Source/SwiftLintBuiltInRules/Rules/Idiomatic/PrivateOverFilePrivateRule.swift"
"Source/SwiftLintBuiltInRules/Rules/Idiomatic/ToggleBoolRule.swift"
"Source/SwiftLintBuiltInRules/Rules/Style/EmptyEnumArgumentsRule.swift"
"Source/SwiftLintBuiltInRules/Rules/Style/OptionalEnumCaseMatchingRule.swift"
"Source/SwiftLintBuiltInRules/Rules/Style/TrailingCommaRule.swift"
)

for file in "${files[@]}"; do
sed -i '' -e 's/import SwiftSyntax$/@preconcurrency import SwiftSyntax/g' "$file"
done

0 comments on commit 3eb3772

Please sign in to comment.