Skip to content

Commit c93163c

Browse files
committed
Add _assumeNonNegative to OutputSpan's count as well
1 parent f0839be commit c93163c

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

stdlib/public/core/Span/OutputSpan.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ extension OutputSpan where Element: ~Copyable {
8383
/// The number of initialized elements in this span.
8484
@_alwaysEmitIntoClient
8585
@_semantics("fixed_storage.get_count")
86-
public var count: Int { _count }
86+
public var count: Int { _assumeNonNegative(_count) }
8787

8888
/// The number of additional elements that can be added to this span.
8989
@_alwaysEmitIntoClient
@@ -191,7 +191,7 @@ extension OutputSpan where Element: ~Copyable {
191191
/// The range of initialized positions for this `OutputSpan`.
192192
@_alwaysEmitIntoClient
193193
public var indices: Range<Index> {
194-
unsafe Range(_uncheckedBounds: (0, _count))
194+
unsafe Range(_uncheckedBounds: (0, count))
195195
}
196196

197197
// SILOptimizer looks for fixed_storage.check_index semantics for bounds check optimizations.

test/SILOptimizer/mutable_span_bounds_check_tests.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,3 +171,12 @@ public func sorted(_ span: borrowing MutableSpan<Int>) -> Bool {
171171
return true
172172
}
173173

174+
// CHECK-SIL-LABEL: sil @$s31mutable_span_bounds_check_tests22outputspan_get_elementySis10OutputSpanVySiG_SitF :
175+
// CHECK-SIL: [[REG1:%.*]] = struct_extract %0, #OutputSpan._count
176+
// CHECK-SIL: [[REG2:%.*]] = struct_extract [[REG1]], #Int._value
177+
// CHECK-SIL: [[REG3:%.*]] = builtin "assumeNonNegative_Int64"([[REG2]]) : $Builtin.Int64
178+
// CHECK-SIL-LABEL:} // end sil function '$s31mutable_span_bounds_check_tests22outputspan_get_elementySis10OutputSpanVySiG_SitF'
179+
public func outputspan_get_element(_ v: borrowing OutputSpan<Int>, _ i: Int) -> Int {
180+
return v[i]
181+
}
182+

0 commit comments

Comments
 (0)