Skip to content

Commit 90d9094

Browse files
authored
Add explicit availability annotations (#2234)
Motivation: gRPC is moving from specifying the platforms in the package manifest to annotating code with availability annotations. Modifications: - Explicitly annotate public API - Add '-require-explicit-availability' to CI - Drop platforms Result: - gRPC can be used by packages which don't specify their platforms in the manifest
1 parent b18eb73 commit 90d9094

File tree

148 files changed

+562
-86
lines changed

Some content is hidden

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

148 files changed

+562
-86
lines changed

.github/workflows/main.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ jobs:
1313
with:
1414
linux_5_9_enabled: false
1515
linux_5_10_enabled: false
16-
linux_6_0_arguments_override: "--explicit-target-dependency-import-check error -Xswiftc -require-explicit-sendable -Xswiftc -warnings-as-errors"
17-
linux_6_1_arguments_override: "--explicit-target-dependency-import-check error -Xswiftc -require-explicit-sendable -Xswiftc -warnings-as-errors"
16+
linux_6_0_arguments_override: "--explicit-target-dependency-import-check error -Xswiftc -require-explicit-sendable -Xswiftc -warnings-as-errors -Xswiftc -require-explicit-availability"
17+
linux_6_1_arguments_override: "--explicit-target-dependency-import-check error -Xswiftc -require-explicit-sendable -Xswiftc -warnings-as-errors -Xswiftc -require-explicit-availability"
1818
linux_nightly_next_arguments_override: "--explicit-target-dependency-import-check error -Xswiftc -require-explicit-sendable"
1919
linux_nightly_main_arguments_override: "--explicit-target-dependency-import-check error -Xswiftc -require-explicit-sendable"
2020

.github/workflows/pull_request.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ jobs:
2222
with:
2323
linux_5_9_enabled: false
2424
linux_5_10_enabled: false
25-
linux_6_0_arguments_override: "--explicit-target-dependency-import-check error -Xswiftc -require-explicit-sendable -Xswiftc -warnings-as-errors"
26-
linux_6_1_arguments_override: "--explicit-target-dependency-import-check error -Xswiftc -require-explicit-sendable -Xswiftc -warnings-as-errors"
25+
linux_6_0_arguments_override: "--explicit-target-dependency-import-check error -Xswiftc -require-explicit-sendable -Xswiftc -warnings-as-errors -Xswiftc -require-explicit-availability"
26+
linux_6_1_arguments_override: "--explicit-target-dependency-import-check error -Xswiftc -require-explicit-sendable -Xswiftc -warnings-as-errors -Xswiftc -require-explicit-availability"
2727
linux_nightly_next_arguments_override: "--explicit-target-dependency-import-check error -Xswiftc -require-explicit-sendable"
2828
linux_nightly_main_arguments_override: "--explicit-target-dependency-import-check error -Xswiftc -require-explicit-sendable"
2929

Package.swift

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
* limitations under the License.
1616
*/
1717

18+
import CompilerPluginSupport
1819
import PackageDescription
1920

2021
let products: [Product] = [
@@ -45,12 +46,28 @@ let dependencies: [Package.Dependency] = [
4546
),
4647
]
4748

48-
let defaultSwiftSettings: [SwiftSetting] = [
49-
.swiftLanguageMode(.v6),
50-
.enableUpcomingFeature("ExistentialAny"),
51-
.enableUpcomingFeature("InternalImportsByDefault"),
52-
.enableUpcomingFeature("MemberImportVisibility"),
53-
]
49+
// -------------------------------------------------------------------------------------------------
50+
51+
// This adds some build settings which allow us to map "@available(gRPCSwift 2.x, *)" to
52+
// the appropriate OS platforms.
53+
let nextMinorVersion = 2
54+
let availabilitySettings: [SwiftSetting] = (0 ... nextMinorVersion).map { minor in
55+
let name = "gRPCSwift"
56+
let version = "2.\(minor)"
57+
let platforms = "macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0"
58+
let setting = "AvailabilityMacro=\(name) \(version):\(platforms)"
59+
return .enableExperimentalFeature(setting)
60+
}
61+
62+
let defaultSwiftSettings: [SwiftSetting] =
63+
availabilitySettings + [
64+
.swiftLanguageMode(.v6),
65+
.enableUpcomingFeature("ExistentialAny"),
66+
.enableUpcomingFeature("InternalImportsByDefault"),
67+
.enableUpcomingFeature("MemberImportVisibility"),
68+
]
69+
70+
// -------------------------------------------------------------------------------------------------
5471

5572
let targets: [Target] = [
5673
// Runtime serialization components
@@ -107,13 +124,6 @@ let targets: [Target] = [
107124

108125
let package = Package(
109126
name: "grpc-swift",
110-
platforms: [
111-
.macOS(.v15),
112-
.iOS(.v18),
113-
.tvOS(.v18),
114-
.watchOS(.v11),
115-
.visionOS(.v2),
116-
],
117127
products: products,
118128
dependencies: dependencies,
119129
targets: targets

Sources/GRPCCodeGen/CodeGenError.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616

1717
/// A error thrown by the ``SourceGenerator`` to signal errors in the ``CodeGenerationRequest`` object.
18+
@available(gRPCSwift 2.0, *)
1819
public struct CodeGenError: Error, Hashable, Sendable {
1920
/// The code indicating the domain of the error.
2021
public var code: Code
@@ -33,6 +34,7 @@ public struct CodeGenError: Error, Hashable, Sendable {
3334
}
3435
}
3536

37+
@available(gRPCSwift 2.0, *)
3638
extension CodeGenError {
3739
public struct Code: Hashable, Sendable {
3840
private enum Value {
@@ -63,6 +65,7 @@ extension CodeGenError {
6365
}
6466
}
6567

68+
@available(gRPCSwift 2.0, *)
6669
extension CodeGenError: CustomStringConvertible {
6770
public var description: String {
6871
return "\(self.code): \"\(self.message)\""

Sources/GRPCCodeGen/CodeGenerationRequest.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
/// Describes the services, dependencies and trivia from an IDL file,
1818
/// and the IDL itself through its specific serializer and deserializer.
19+
@available(gRPCSwift 2.0, *)
1920
public struct CodeGenerationRequest {
2021
/// The name of the source file containing the IDL, including the extension if applicable.
2122
public var fileName: String
@@ -81,6 +82,7 @@ public struct CodeGenerationRequest {
8182
}
8283
}
8384

85+
@available(gRPCSwift 2.0, *)
8486
extension CodeGenerationRequest {
8587
@available(*, deprecated, renamed: "makeSerializerSnippet")
8688
public var lookupSerializer: (_ messageType: String) -> String {
@@ -120,6 +122,7 @@ extension CodeGenerationRequest {
120122
}
121123

122124
/// Represents an import: a module or a specific item from a module.
125+
@available(gRPCSwift 2.0, *)
123126
public struct Dependency: Equatable {
124127
/// If the dependency is an item, the property's value is the item representation.
125128
/// If the dependency is a module, this property is nil.
@@ -261,6 +264,7 @@ public struct Dependency: Equatable {
261264
}
262265

263266
/// Represents a service described in an IDL file.
267+
@available(gRPCSwift 2.0, *)
264268
public struct ServiceDescriptor: Hashable {
265269
/// Documentation from comments above the IDL service description.
266270
/// It is already formatted, meaning it contains "///" and new lines.
@@ -285,6 +289,7 @@ public struct ServiceDescriptor: Hashable {
285289
}
286290
}
287291

292+
@available(gRPCSwift 2.0, *)
288293
extension ServiceDescriptor {
289294
@available(*, deprecated, renamed: "init(documentation:name:methods:)")
290295
public init(
@@ -317,6 +322,7 @@ extension ServiceDescriptor {
317322
}
318323

319324
/// Represents a method described in an IDL file.
325+
@available(gRPCSwift 2.0, *)
320326
public struct MethodDescriptor: Hashable {
321327
/// Documentation from comments above the IDL method description.
322328
/// It is already formatted, meaning it contains "///" and new lines.
@@ -357,6 +363,7 @@ public struct MethodDescriptor: Hashable {
357363
}
358364
}
359365

366+
@available(gRPCSwift 2.0, *)
360367
extension MethodDescriptor {
361368
@available(*, deprecated, message: "Use MethodName instead of Name")
362369
public init(
@@ -380,6 +387,7 @@ extension MethodDescriptor {
380387
}
381388
}
382389

390+
@available(gRPCSwift 2.0, *)
383391
public struct ServiceName: Hashable {
384392
/// The identifying name as used in the service/method descriptors including any namespace.
385393
///
@@ -414,6 +422,7 @@ public struct ServiceName: Hashable {
414422
}
415423
}
416424

425+
@available(gRPCSwift 2.0, *)
417426
public struct MethodName: Hashable {
418427
/// The identifying name as used in the service/method descriptors.
419428
///
@@ -445,6 +454,7 @@ public struct MethodName: Hashable {
445454

446455
/// Represents the name associated with a namespace, service or a method, in three different formats.
447456
@available(*, deprecated, message: "Use ServiceName/MethodName instead.")
457+
@available(gRPCSwift 2.0, *)
448458
public struct Name: Hashable {
449459
/// The base name is the name used for the namespace/service/method in the IDL file, so it should follow
450460
/// the specific casing of the IDL.
@@ -473,6 +483,7 @@ public struct Name: Hashable {
473483
}
474484

475485
@available(*, deprecated, message: "Use ServiceName/MethodName instead.")
486+
@available(gRPCSwift 2.0, *)
476487
extension Name {
477488
/// The base name replacing occurrences of "." with "_".
478489
///

Sources/GRPCCodeGen/CodeGenerator.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,12 @@
1515
*/
1616

1717
@available(*, deprecated, renamed: "CodeGenerator")
18+
@available(gRPCSwift 2.0, *)
1819
public typealias SourceGenerator = CodeGenerator
1920

2021
/// Generates ``SourceFile`` objects containing generated code for the RPCs represented
2122
/// in a ``CodeGenerationRequest`` object.
23+
@available(gRPCSwift 2.0, *)
2224
public struct CodeGenerator: Sendable {
2325
/// The options regarding the access level, indentation for the generated code
2426
/// and whether to generate server and client code.
@@ -41,8 +43,10 @@ public struct CodeGenerator: Sendable {
4143
/// Whether or not server code should be generated.
4244
public var server: Bool
4345
/// The name of the core gRPC module.
46+
@available(gRPCSwift 2.1, *)
4447
public var grpcCoreModuleName: String
4548
/// The availability annotations to use on the generated code.
49+
@available(gRPCSwift 2.2, *)
4650
public var availability: AvailabilityAnnotations = .default
4751

4852
/// Creates a new configuration.
@@ -88,6 +92,7 @@ public struct CodeGenerator: Sendable {
8892
}
8993

9094
// The availability that generated code is annotated with.
95+
@available(gRPCSwift 2.2, *)
9196
public struct AvailabilityAnnotations: Sendable, Hashable {
9297
public struct Platform: Sendable, Hashable {
9398
/// The name of the OS, e.g. 'macOS'.
@@ -156,6 +161,7 @@ public struct CodeGenerator: Sendable {
156161
}
157162
}
158163

164+
@available(gRPCSwift 2.0, *)
159165
extension AvailabilityDescription {
160166
init(_ availability: CodeGenerator.Config.AvailabilityAnnotations) throws {
161167
switch availability.wrapped {

Sources/GRPCCodeGen/Internal/Renderer/RendererProtocol.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
/// into Swift files.
3232
///
3333
/// Rendering is the last phase of the generator pipeline.
34+
@available(gRPCSwift 2.0, *)
3435
protocol RendererProtocol {
3536

3637
/// Renders the specified structured code into a raw Swift file.

Sources/GRPCCodeGen/Internal/Renderer/TextBasedRenderer.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ extension TextBasedRenderer: Sendable {}
111111

112112
/// A renderer that uses string interpolation and concatenation
113113
/// to convert the provided structure code into raw string form.
114+
@available(gRPCSwift 2.0, *)
114115
struct TextBasedRenderer: RendererProtocol {
115116

116117
func render(
@@ -1207,6 +1208,7 @@ extension String {
12071208
}
12081209
}
12091210

1211+
@available(gRPCSwift 2.0, *)
12101212
extension TextBasedRenderer {
12111213

12121214
/// Returns the provided expression rendered as a string.

Sources/GRPCCodeGen/Internal/StructuredSwift+Client.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@ extension FunctionDescription {
194194
}
195195
}
196196

197+
@available(gRPCSwift 2.0, *)
197198
extension ProtocolDescription {
198199
/// ```
199200
/// protocol <Name>: Sendable {
@@ -233,6 +234,7 @@ extension ProtocolDescription {
233234
}
234235
}
235236

237+
@available(gRPCSwift 2.0, *)
236238
extension ExtensionDescription {
237239
/// ```
238240
/// extension <Name> {
@@ -503,6 +505,7 @@ extension FunctionDescription {
503505
}
504506
}
505507

508+
@available(gRPCSwift 2.0, *)
506509
extension ExtensionDescription {
507510
/// ```
508511
/// extension <Name> {
@@ -662,6 +665,7 @@ extension FunctionDescription {
662665
}
663666
}
664667

668+
@available(gRPCSwift 2.0, *)
665669
extension StructDescription {
666670
/// ```
667671
/// struct <Name><Transport>: <ClientProtocol> where Transport: GRPCCore.ClientTransport {
@@ -752,6 +756,7 @@ extension StructDescription {
752756
}
753757
}
754758

759+
@available(gRPCSwift 2.0, *)
755760
private func docs(
756761
for method: MethodDescriptor,
757762
serializers includeSerializers: Bool = true
@@ -793,6 +798,7 @@ private func docs(
793798
return Docs.interposeDocs(method.documentation, between: summary, and: allParameters)
794799
}
795800

801+
@available(gRPCSwift 2.0, *)
796802
private func explodedDocs(for method: MethodDescriptor) -> String {
797803
let summary = "/// Call the \"\(method.name.identifyingName)\" method."
798804
var parameters = """

Sources/GRPCCodeGen/Internal/StructuredSwift+Server.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ extension FunctionSignatureDescription {
4848
}
4949
}
5050

51+
@available(gRPCSwift 2.0, *)
5152
extension ProtocolDescription {
5253
/// ```
5354
/// protocol <Name>: GRPCCore.RegistrableRPCService {
@@ -101,6 +102,7 @@ extension ProtocolDescription {
101102
}
102103
}
103104

105+
@available(gRPCSwift 2.0, *)
104106
extension ExtensionDescription {
105107
/// ```
106108
/// extension <ExtensionName> {
@@ -136,6 +138,7 @@ extension ExtensionDescription {
136138
}
137139
}
138140

141+
@available(gRPCSwift 2.0, *)
139142
extension ProtocolDescription {
140143
/// ```
141144
/// protocol <Name>: <StreamingProtocol> {
@@ -304,6 +307,7 @@ extension FunctionCallDescription {
304307
}
305308
}
306309

310+
@available(gRPCSwift 2.0, *)
307311
extension FunctionDescription {
308312
/// ```
309313
/// func registerMethods(with router: inout GRPCCore.RPCRouter) {
@@ -451,6 +455,7 @@ extension FunctionDescription {
451455
}
452456
}
453457

458+
@available(gRPCSwift 2.0, *)
454459
extension ExtensionDescription {
455460
/// ```
456461
/// extension <ExtensionName> {
@@ -548,6 +553,7 @@ extension FunctionSignatureDescription {
548553
}
549554
}
550555

556+
@available(gRPCSwift 2.0, *)
551557
extension ProtocolDescription {
552558
/// ```
553559
/// protocol SimpleServiceProtocol: <ServiceProtocol> {
@@ -763,6 +769,7 @@ extension FunctionDescription {
763769
}
764770
}
765771

772+
@available(gRPCSwift 2.0, *)
766773
extension ExtensionDescription {
767774
/// ```
768775
/// extension ServiceProtocol {

0 commit comments

Comments
 (0)