Skip to content

Commit b442d31

Browse files
authored
Merge pull request #80740 from glessard/rdar137710901-addressable-span-properties
[6.2, SE-0456, -0467] enable span properties for inline-storage types
2 parents cdde103 + 803fafb commit b442d31

File tree

5 files changed

+75
-12
lines changed

5 files changed

+75
-12
lines changed

stdlib/public/core/CollectionOfOne.swift

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,22 @@ extension CollectionOfOne {
166166
@lifetime(borrow self)
167167
@_alwaysEmitIntoClient
168168
get {
169-
fatalError("Span over CollectionOfOne is not supported yet.")
169+
let pointer = unsafe UnsafePointer<Element>(Builtin.addressOfBorrow(self))
170+
let span = unsafe Span(_unsafeStart: pointer, count: 1)
171+
return unsafe _overrideLifetime(span, borrowing: self)
172+
}
173+
}
174+
175+
@available(SwiftStdlib 6.2, *)
176+
public var mutableSpan: MutableSpan<Element> {
177+
@lifetime(&self)
178+
@_alwaysEmitIntoClient
179+
mutating get {
180+
let pointer = unsafe UnsafeMutablePointer<Element>(
181+
Builtin.addressOfBorrow(self)
182+
)
183+
let span = unsafe MutableSpan(_unsafeStart: pointer, count: 1)
184+
return unsafe _overrideLifetime(span, mutating: &self)
170185
}
171186
}
172187
}

stdlib/public/core/InlineArray.swift

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,20 @@ extension InlineArray where Element: ~Copyable {
468468
@lifetime(borrow self)
469469
@_alwaysEmitIntoClient
470470
borrowing get {
471-
fatalError("Span over InlineArray is not supported yet.")
471+
let pointer = unsafe _address
472+
let span = unsafe Span(_unsafeStart: pointer, count: count)
473+
return unsafe _overrideLifetime(span, borrowing: self)
474+
}
475+
}
476+
477+
@available(SwiftStdlib 6.2, *)
478+
public var mutableSpan: MutableSpan<Element> {
479+
@lifetime(&self)
480+
@_alwaysEmitIntoClient
481+
mutating get {
482+
let pointer = unsafe _mutableAddress
483+
let span = unsafe MutableSpan(_unsafeStart: pointer, count: count)
484+
return unsafe _overrideLifetime(span, mutating: &self)
472485
}
473486
}
474487
}

test/abi/macOS/arm64/stdlib.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -973,6 +973,8 @@ Added: _$sSs8UTF8ViewV4spans4SpanVys5UInt8VGvpMV
973973
Added: _$sSa11mutableSpans07MutableB0VyxGvr
974974
Added: _$ss10ArraySliceV11mutableSpans07MutableD0VyxGvr
975975
Added: _$ss15ContiguousArrayV11mutableSpans07MutableD0VyxGvr
976+
Added: _$ss11InlineArrayVsRi__rlE11mutableSpans07MutableD0Vyq_Gvr
977+
Added: _$ss15CollectionOfOneV11mutableSpans07MutableE0VyxGvr
976978

977979
// _SwiftifyInfo enum for _SwiftifyImports macro
978980
Added: _$ss13_SwiftifyExprO5paramyABSicABmFWC

test/abi/macOS/x86_64/stdlib.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -974,6 +974,8 @@ Added: _$sSs8UTF8ViewV4spans4SpanVys5UInt8VGvpMV
974974
Added: _$sSa11mutableSpans07MutableB0VyxGvr
975975
Added: _$ss10ArraySliceV11mutableSpans07MutableD0VyxGvr
976976
Added: _$ss15ContiguousArrayV11mutableSpans07MutableD0VyxGvr
977+
Added: _$ss11InlineArrayVsRi__rlE11mutableSpans07MutableD0Vyq_Gvr
978+
Added: _$ss15CollectionOfOneV11mutableSpans07MutableE0VyxGvr
977979

978980
// _SwiftifyInfo enum for _SwiftifyImports macro
979981
Added: _$ss13_SwiftifyExprO5paramyABSicABmFWC

test/stdlib/Span/InlineSpanProperties.swift

Lines changed: 41 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ var suite = TestSuite("Span properties backed by inline storage")
2323
defer { runAllTests() }
2424

2525
suite.test("CollectionOfOne.span property")
26-
.skip(.wasiAny(reason: "Trap tests aren't supported on WASI."))
2726
.skip(.custom(
2827
{ if #available(SwiftStdlib 6.2, *) { false } else { true } },
2928
reason: "Requires Swift 6.2's standard library"
@@ -35,15 +34,13 @@ suite.test("CollectionOfOne.span property")
3534
let u = Array(s.utf8)
3635
let c = CollectionOfOne(consume s)
3736
s = ""
38-
expectCrashLater()
3937
let span = c.span
4038
expectEqual(span.count, 1)
4139
let v = Array(span[0].utf8)
4240
expectEqual(u, v)
4341
}
4442

4543
suite.test("CollectionOfOne.span property (simple)")
46-
.skip(.wasiAny(reason: "Trap tests aren't supported on WASI."))
4744
.skip(.custom(
4845
{ if #available(SwiftStdlib 6.2, *) { false } else { true } },
4946
reason: "Requires Swift 6.2's standard library"
@@ -52,7 +49,6 @@ suite.test("CollectionOfOne.span property (simple)")
5249
guard #available(SwiftStdlib 6.2, *) else { return }
5350

5451
let c = CollectionOfOne(Int.random(in: 0..<100000))
55-
expectCrashLater()
5652
let span = c.span
5753
expectEqual(span.count, c.indices.count)
5854
expectEqual(span[0], c[0])
@@ -63,7 +59,6 @@ struct Padded: BitwiseCopyable {
6359
}
6460

6561
suite.test("CollectionOfOne.span stride test")
66-
.skip(.wasiAny(reason: "Trap tests aren't supported on WASI."))
6762
.skip(.custom(
6863
{ if #available(SwiftStdlib 6.2, *) { false } else { true } },
6964
reason: "Requires Swift 6.2's standard library"
@@ -72,14 +67,25 @@ suite.test("CollectionOfOne.span stride test")
7267
guard #available(SwiftStdlib 6.2, *) else { return }
7368

7469
let c = CollectionOfOne(Padded(storage: (-1, 1)))
75-
expectCrashLater()
7670
let span = c.span
7771
let bytes = span.bytes
7872
expectEqual(bytes.byteCount, MemoryLayout.size(ofValue: c))
7973
}
8074

75+
suite.test("CollectionOfOne.mutableSpan property (simple)")
76+
.require(.stdlib_6_2).code {
77+
guard #available(SwiftStdlib 6.2, *) else { return }
78+
79+
var c = CollectionOfOne(Int.random(in: 0..<100000))
80+
expectEqual(c.count, 1)
81+
var span = c.mutableSpan
82+
expectEqual(span.count, 1)
83+
span[0] = Int.random(in: .min..<0)
84+
let r = span[0]
85+
expectEqual(c[0], r)
86+
}
87+
8188
suite.test("InlineArray.span property")
82-
.skip(.wasiAny(reason: "Trap tests aren't supported on WASI."))
8389
.skip(.custom(
8490
{ if #available(SwiftStdlib 6.2, *) { false } else { true } },
8591
reason: "Requires Swift 6.2's standard library"
@@ -89,7 +95,6 @@ suite.test("InlineArray.span property")
8995

9096
var s = InlineArray<5, Int>(repeating: 0)
9197
s[3] = .random(in: 0..<1000)
92-
expectCrashLater()
9398
let span = s.span
9499
expectEqual(span.count, s.count)
95100
for i in s.indices {
@@ -98,7 +103,6 @@ suite.test("InlineArray.span property")
98103
}
99104

100105
suite.test("InlineArray.span property (String)")
101-
.skip(.wasiAny(reason: "Trap tests aren't supported on WASI."))
102106
.skip(.custom(
103107
{ if #available(SwiftStdlib 6.2, *) { false } else { true } },
104108
reason: "Requires Swift 6.2's standard library"
@@ -108,10 +112,37 @@ suite.test("InlineArray.span property (String)")
108112

109113
var s = InlineArray<5, String>(repeating: "0")
110114
s[3] = String(Int.random(in: 0..<1000))
111-
expectCrashLater()
112115
let span = s.span
113116
expectEqual(span.count, s.count)
114117
for i in s.indices {
115118
expectEqual(span[i], s[i])
116119
}
117120
}
121+
122+
suite.test("InlineArray.mutableSpan property")
123+
.require(.stdlib_6_2).code
124+
{
125+
guard #available(SwiftStdlib 6.2, *) else { return }
126+
127+
var v = InlineArray<5, Int>(repeating: 0)
128+
let c = v.count
129+
var span = v.mutableSpan
130+
expectEqual(span.count, c)
131+
span[3] = Int.random(in: .min..<0)
132+
let r = span[3]
133+
expectEqual(v[3], r)
134+
}
135+
136+
suite.test("InlineArray.mutableSpan property (String)")
137+
.require(.stdlib_6_2).code
138+
{
139+
guard #available(SwiftStdlib 6.2, *) else { return }
140+
141+
var v = InlineArray<5, String>(repeating: "0")
142+
let c = v.count
143+
var span = v.mutableSpan
144+
expectEqual(span.count, c)
145+
span[3] = String(repeating: "0", count: Int.random(in: 100..<500))
146+
let s = span[3]
147+
expectTrue(s._isIdentical(to: v[3]))
148+
}

0 commit comments

Comments
 (0)