Skip to content

Commit 30bc5b9

Browse files
authored
Introduce Availability Macros for FoundationPreview (#279)
1 parent 0fe5c5f commit 30bc5b9

14 files changed

+74
-51
lines changed

Package.swift

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,14 @@
44
import PackageDescription
55
import CompilerPluginSupport
66

7+
// Availability Macros
8+
let availabilityMacros: [SwiftSetting] = [
9+
"FoundationPreview 0.1:macOS 9999, iOS 9999, tvOS 9999, watchOS 9999",
10+
"FoundationPreview 0.2:macOS 9999, iOS 9999, tvOS 9999, watchOS 9999",
11+
"FoundationPreview 0.3:macOS 9999, iOS 9999, tvOS 9999, watchOS 9999",
12+
"FoundationPreview 0.4:macOS 9999, iOS 9999, tvOS 9999, watchOS 9999",
13+
].map { .enableExperimentalFeature("AvailabilityMacro=\($0)") }
14+
715
let package = Package(
816
name: "FoundationPreview",
917
platforms: [.macOS("13.3"), .iOS("16.4"), .tvOS("16.4"), .watchOS("9.4")],
@@ -43,7 +51,7 @@ let package = Package(
4351
.target(name: "TestSupport", dependencies: [
4452
"FoundationEssentials",
4553
"FoundationInternationalization",
46-
]),
54+
], swiftSettings: availabilityMacros),
4755

4856
// FoundationEssentials
4957
.target(
@@ -55,12 +63,12 @@ let package = Package(
5563
swiftSettings: [
5664
.enableExperimentalFeature("VariadicGenerics"),
5765
.enableExperimentalFeature("AccessLevelOnImport")
58-
]
66+
] + availabilityMacros
5967
),
6068
.testTarget(name: "FoundationEssentialsTests", dependencies: [
6169
"TestSupport",
6270
"FoundationEssentials"
63-
]),
71+
], swiftSettings: availabilityMacros),
6472

6573
// FoundationInternationalization
6674
.target(
@@ -72,7 +80,7 @@ let package = Package(
7280
],
7381
swiftSettings: [
7482
.enableExperimentalFeature("AccessLevelOnImport")
75-
]
83+
] + availabilityMacros
7684
),
7785

7886
// FoundationMacros
@@ -88,14 +96,15 @@ let package = Package(
8896
],
8997
swiftSettings: [
9098
.enableExperimentalFeature("AccessLevelOnImport")
91-
]
99+
] + availabilityMacros
92100
),
93101
.testTarget(
94102
name: "FoundationMacrosTests",
95103
dependencies: [
96104
"FoundationMacros",
97105
"TestSupport"
98-
]
106+
],
107+
swiftSettings: availabilityMacros
99108
)
100109
]
101110
)
@@ -105,7 +114,7 @@ package.targets.append(contentsOf: [
105114
.testTarget(name: "FoundationInternationalizationTests", dependencies: [
106115
"TestSupport",
107116
"FoundationInternationalization"
108-
]),
117+
], swiftSettings: availabilityMacros),
109118
])
110119
#endif
111120

Sources/FoundationEssentials/AttributedString/AttributedString+CharacterView.swift

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -113,13 +113,15 @@ extension AttributedString.CharacterView: BidirectionalCollection {
113113

114114
@_alwaysEmitIntoClient
115115
public var count: Int {
116+
#if FOUNDATION_FRAMEWORK
116117
if #available(macOS 14, iOS 17, tvOS 17, watchOS 10, *) {
117118
return _count
118119
}
120+
#endif
119121
return _defaultCount
120122
}
121123

122-
@available(macOS 14, iOS 17, tvOS 17, watchOS 10, *)
124+
@available(FoundationPreview 0.1, *)
123125
@usableFromInline
124126
internal var _count: Int {
125127
_characters.count
@@ -141,13 +143,15 @@ extension AttributedString.CharacterView: BidirectionalCollection {
141143

142144
@_alwaysEmitIntoClient
143145
public func index(_ i: AttributedString.Index, offsetBy distance: Int) -> AttributedString.Index {
146+
#if FOUNDATION_FRAMEWORK
144147
if #available(macOS 14, iOS 17, tvOS 17, watchOS 10, *) {
145148
return _index(i, offsetBy: distance)
146149
}
150+
#endif
147151
return _defaultIndex(i, offsetBy: distance)
148152
}
149153

150-
@available(macOS 14, iOS 17, tvOS 17, watchOS 10, *)
154+
@available(FoundationPreview 0.1, *)
151155
@usableFromInline
152156
internal func _index(_ i: AttributedString.Index, offsetBy distance: Int) -> AttributedString.Index {
153157
precondition(i >= startIndex && i <= endIndex, "AttributedString index out of bounds")
@@ -162,13 +166,15 @@ extension AttributedString.CharacterView: BidirectionalCollection {
162166
offsetBy distance: Int,
163167
limitedBy limit: AttributedString.Index
164168
) -> AttributedString.Index? {
169+
#if FOUNDATION_FRAMEWORK
165170
if #available(macOS 14, iOS 17, tvOS 17, watchOS 10, *) {
166171
return _index(i, offsetBy: distance, limitedBy: limit)
167172
}
173+
#endif
168174
return _defaultIndex(i, offsetBy: distance, limitedBy: limit)
169175
}
170176

171-
@available(macOS 14, iOS 17, tvOS 17, watchOS 10, *)
177+
@available(FoundationPreview 0.1, *)
172178
@usableFromInline
173179
internal func _index(
174180
_ i: AttributedString.Index,
@@ -189,15 +195,17 @@ extension AttributedString.CharacterView: BidirectionalCollection {
189195

190196
@_alwaysEmitIntoClient
191197
public func distance(from start: AttributedString.Index, to end: AttributedString.Index) -> Int {
198+
#if FOUNDATION_FRAMEWORK
192199
if #available(macOS 14, iOS 17, tvOS 17, watchOS 10, *) {
193200
return _distance(from: start, to: end)
194201
}
202+
#endif
195203
precondition(start >= startIndex && start <= endIndex, "AttributedString index out of bounds")
196204
precondition(end >= startIndex && end <= endIndex, "AttributedString index out of bounds")
197205
return _defaultDistance(from: start, to: end)
198206
}
199207

200-
@available(macOS 14, iOS 17, tvOS 17, watchOS 10, *)
208+
@available(FoundationPreview 0.1, *)
201209
@usableFromInline
202210
internal func _distance(from start: AttributedString.Index, to end: AttributedString.Index) -> Int {
203211
precondition(start >= startIndex && start <= endIndex, "AttributedString index out of bounds")

Sources/FoundationEssentials/AttributedString/AttributedString+Runs.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,13 +236,15 @@ extension AttributedString.Runs: BidirectionalCollection {
236236

237237
@_alwaysEmitIntoClient
238238
public func index(_ i: Index, offsetBy distance: Int) -> Index {
239+
#if FOUNDATION_FRAMEWORK
239240
if #available(macOS 14, iOS 17, tvOS 17, watchOS 10, *) {
240241
return _index(i, offsetBy: distance)
241242
}
243+
#endif
242244
return i.advanced(by: distance)
243245
}
244246

245-
@available(macOS 14, iOS 17, tvOS 17, watchOS 10, *)
247+
@available(FoundationPreview 0.1, *)
246248
@usableFromInline
247249
internal func _index(_ index: Index, offsetBy distance: Int) -> Index {
248250
let i = _guts.runs.index(_resolveRun(index), offsetBy: distance)

Sources/FoundationEssentials/AttributedString/AttributedString+UnicodeScalarView.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,13 +114,15 @@ extension AttributedString.UnicodeScalarView: BidirectionalCollection {
114114

115115
@_alwaysEmitIntoClient
116116
public var count: Int {
117+
#if FOUNDATION_FRAMEWORK
117118
if #available(macOS 14, iOS 17, tvOS 17, watchOS 10, *) {
118119
return _count
119120
}
121+
#endif
120122
return _defaultCount
121123
}
122124

123-
@available(macOS 14, iOS 17, tvOS 17, watchOS 10, *)
125+
@available(FoundationPreview 0.1, *)
124126
@usableFromInline
125127
internal var _count: Int {
126128
_unicodeScalars.count
@@ -153,13 +155,15 @@ extension AttributedString.UnicodeScalarView: BidirectionalCollection {
153155
offsetBy distance: Int,
154156
limitedBy limit: AttributedString.Index
155157
) -> AttributedString.Index? {
158+
#if FOUNDATION_FRAMEWORK
156159
if #available(macOS 14, iOS 17, tvOS 17, watchOS 10, *) {
157160
return _index(i, offsetBy: distance, limitedBy: limit)
158161
}
162+
#endif
159163
return _defaultIndex(i, offsetBy: distance, limitedBy: limit)
160164
}
161165

162-
@available(macOS 14, iOS 17, tvOS 17, watchOS 10, *)
166+
@available(FoundationPreview 0.1, *)
163167
@usableFromInline
164168
internal func _index(
165169
_ i: AttributedString.Index,

Sources/FoundationEssentials/AttributedString/Conversion.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ extension String {
3939
}
4040
#endif
4141

42-
@available(macOS 14, iOS 17, tvOS 17, watchOS 10, *)
42+
@available(FoundationPreview 0.1, *)
4343
@usableFromInline
4444
internal init(_characters: AttributedString.CharacterView) {
4545
self.init(_characters._characters)

Sources/FoundationEssentials/AttributedString/FoundationAttributes.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,11 @@ extension AttributeScopes {
4040
public let durationField: DurationFieldAttribute
4141

4242
#if FOUNDATION_FRAMEWORK
43-
@available(macOS 14, iOS 17, tvOS 17, watchOS 10, *)
43+
@available(FoundationPreview 0.1, *)
4444
public let agreementConcept: AgreementConceptAttribute
45-
@available(macOS 14, iOS 17, tvOS 17, watchOS 10, *)
45+
@available(FoundationPreview 0.1, *)
4646
public let agreementArgument: AgreementArgumentAttribute
47-
@available(macOS 14, iOS 17, tvOS 17, watchOS 10, *)
47+
@available(FoundationPreview 0.1, *)
4848
public let referentConcept: ReferentConceptAttribute
4949

5050
// TODO: Support AttributedString markdown in FoundationPreview: https://github.com/apple/swift-foundation/issues/44
@@ -103,23 +103,23 @@ extension AttributeScopes.FoundationAttributes {
103103
#if FOUNDATION_FRAMEWORK
104104

105105
@frozen @_nonSendable
106-
@available(macOS 14, iOS 17, tvOS 17, watchOS 10, *)
106+
@available(FoundationPreview 0.1, *)
107107
public enum ReferentConceptAttribute : CodableAttributedStringKey, MarkdownDecodableAttributedStringKey {
108108
public typealias Value = Int
109109
public static let name = NSAttributedString.Key.referentConcept.rawValue
110110
public static let markdownName = "referentConcept"
111111
}
112112

113113
@frozen @_nonSendable
114-
@available(macOS 14, iOS 17, tvOS 17, watchOS 10, *)
114+
@available(FoundationPreview 0.1, *)
115115
public enum AgreementConceptAttribute : CodableAttributedStringKey, MarkdownDecodableAttributedStringKey {
116116
public typealias Value = Int
117117
public static let name = NSAttributedString.Key.agreeWithConcept.rawValue
118118
public static let markdownName = "agreeWithConcept"
119119
}
120120

121121
@frozen @_nonSendable
122-
@available(macOS 14, iOS 17, tvOS 17, watchOS 10, *)
122+
@available(FoundationPreview 0.1, *)
123123
public enum AgreementArgumentAttribute : CodableAttributedStringKey, MarkdownDecodableAttributedStringKey {
124124
public typealias Value = Int
125125
public static let name = NSAttributedString.Key.agreeWithArgument.rawValue

Sources/FoundationEssentials/Calendar/Calendar.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ public struct Calendar : Hashable, Equatable, Sendable {
194194
if contains(.calendar) { result.insert(.calendar) }
195195
if contains(.timeZone) { result.insert(.timeZone) }
196196
if contains(.isLeapMonth) {
197-
if #available(macOS 14, iOS 17, tvOS 17, watchOS 10, *) {
197+
if #available(FoundationPreview 0.1, *) {
198198
result.insert(.isLeapMonth)
199199
}
200200
}
@@ -218,7 +218,7 @@ public struct Calendar : Hashable, Equatable, Sendable {
218218
if self.contains(.nanosecond) { return .nanosecond }
219219

220220
// The algorithms that call this function assume that isLeapMonth counts as a 'highest unit set', but the order is after nanosecond.
221-
if #available(macOS 14, iOS 17, tvOS 17, watchOS 10, *) {
221+
if #available(FoundationPreview 0.1, *) {
222222
if self.contains(.isLeapMonth) { return .isLeapMonth }
223223
}
224224

@@ -249,7 +249,7 @@ public struct Calendar : Hashable, Equatable, Sendable {
249249
case nanosecond
250250
case calendar
251251
case timeZone
252-
@available(macOS 14, iOS 17, tvOS 17, watchOS 10, *)
252+
@available(FoundationPreview 0.1, *)
253253
case isLeapMonth
254254

255255
fileprivate var componentSetValue: ComponentSet.RawValue {

Sources/FoundationEssentials/Data/Data.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ internal final class __DataStorage : @unchecked Sendable {
354354
}
355355

356356
#if FOUNDATION_FRAMEWORK
357-
@available(macOS 14, iOS 17, watchOS 10, tvOS 17, *)
357+
@available(FoundationPreview 0.1, *)
358358
#endif
359359
@usableFromInline // This is not @inlinable as it is a non-trivial, non-generic function.
360360
func replaceBytes(in range_: Range<Int>, with replacementBytes: UnsafeRawPointer?, length replacementLength: Int) {

Sources/FoundationEssentials/JSON/JSONDecoder.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -350,14 +350,14 @@ open class JSONDecoder {
350350
}, from: data)
351351
}
352352

353-
@available(macOS 14, iOS 17, tvOS 17, watchOS 10, *)
353+
@available(FoundationPreview 0.1, *)
354354
open func decode<T: DecodableWithConfiguration>(_ type: T.Type, from data: Data, configuration: T.DecodingConfiguration) throws -> T {
355355
try _decode({
356356
try $0.unwrap($1, as: type, configuration: configuration, for: .root, _JSONKey?.none)
357357
}, from: data)
358358
}
359359

360-
@available(macOS 14, iOS 17, tvOS 17, watchOS 10, *)
360+
@available(FoundationPreview 0.1, *)
361361
open func decode<T, C>(_ type: T.Type, from data: Data, configuration: C.Type) throws -> T where T : DecodableWithConfiguration, C : DecodingConfigurationProviding, T.DecodingConfiguration == C.DecodingConfiguration {
362362
try decode(type, from: data, configuration: C.decodingConfiguration)
363363
}

Sources/FoundationEssentials/JSON/JSONEncoder.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -356,14 +356,14 @@ open class JSONEncoder {
356356
}, value: value)
357357
}
358358

359-
@available(macOS 14, iOS 17, tvOS 17, watchOS 10, *)
359+
@available(FoundationPreview 0.1, *)
360360
open func encode<T : EncodableWithConfiguration>(_ value: T, configuration: T.EncodingConfiguration) throws -> Data {
361361
try _encode({
362362
try $0.wrapGeneric(value, configuration: configuration, for: .root)
363363
}, value: value)
364364
}
365365

366-
@available(macOS 14, iOS 17, tvOS 17, watchOS 10, *)
366+
@available(FoundationPreview 0.1, *)
367367
open func encode<T, C>(_ value: T, configuration: C.Type) throws -> Data where T : EncodableWithConfiguration, C : EncodingConfigurationProviding, T.EncodingConfiguration == C.EncodingConfiguration {
368368
try encode(value, configuration: C.encodingConfiguration)
369369
}

0 commit comments

Comments
 (0)