Skip to content

Commit 7899343

Browse files
authored
Merge pull request #79732 from glessard/integers-are-safe
[stdlib] Integers are safe
2 parents dc3df46 + fab6589 commit 7899343

11 files changed

+30
-29
lines changed

stdlib/public/core/Hasher.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ extension Hasher {
217217

218218
// Load first unaligned partial word of data
219219
do {
220-
let start = unsafe UInt(bitPattern: data)
220+
let start = UInt(bitPattern: data)
221221
let end = _roundUp(start, toAlignment: MemoryLayout<UInt64>.alignment)
222222
let c = min(remaining, Int(end - start))
223223
if c > 0 {
@@ -227,7 +227,7 @@ extension Hasher {
227227
remaining -= c
228228
}
229229
}
230-
unsafe _internalInvariant(
230+
_internalInvariant(
231231
remaining == 0 ||
232232
Int(bitPattern: data) & (MemoryLayout<UInt64>.alignment - 1) == 0)
233233

stdlib/public/core/KeyPath.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public class AnyKeyPath: _AppendKeyPath {
9696
}
9797

9898
#if _pointerBitWidth(_64)
99-
let offset = unsafe (0 &- Int(bitPattern: _kvcKeyPathStringPtr)) &- 1
99+
let offset = (0 &- Int(bitPattern: _kvcKeyPathStringPtr)) &- 1
100100
guard _fastPath(offset >= 0) else {
101101
// This happens to be an actual _kvcKeyPathStringPtr, not an offset, if
102102
// we get here.
@@ -2046,7 +2046,7 @@ internal struct KeyPathBuffer {
20462046
internal mutating func pushRaw(size: Int, alignment: Int)
20472047
-> UnsafeMutableRawBufferPointer {
20482048
var baseAddress = unsafe buffer.baseAddress._unsafelyUnwrappedUnchecked
2049-
var misalign = unsafe Int(bitPattern: baseAddress) & (alignment - 1)
2049+
var misalign = Int(bitPattern: baseAddress) & (alignment - 1)
20502050
if misalign != 0 {
20512051
misalign = alignment - misalign
20522052
unsafe baseAddress = unsafe baseAddress.advanced(by: misalign)
@@ -2969,7 +2969,7 @@ internal func _getTypeByMangledNameInEnvironmentOrContext(
29692969
genericEnvironmentOrContext: UnsafeRawPointer?,
29702970
genericArguments: UnsafeRawPointer?)
29712971
-> Any.Type? {
2972-
let taggedPointer = unsafe UInt(bitPattern: genericEnvironmentOrContext)
2972+
let taggedPointer = UInt(bitPattern: genericEnvironmentOrContext)
29732973
if taggedPointer & 1 == 0 {
29742974
return unsafe _getTypeByMangledNameInEnvironment(name, nameLength,
29752975
genericEnvironment: genericEnvironmentOrContext,
@@ -2990,7 +2990,7 @@ internal func _resolveKeyPathGenericArgReference(
29902990
arguments: UnsafeRawPointer?)
29912991
-> UnsafeRawPointer {
29922992
// If the low bit is clear, it's a direct reference to the argument.
2993-
if unsafe (UInt(bitPattern: reference) & 0x01 == 0) {
2993+
if (UInt(bitPattern: reference) & 0x01 == 0) {
29942994
return unsafe reference
29952995
}
29962996

@@ -3682,7 +3682,7 @@ internal struct InstantiateKeyPathBuffer: KeyPathPatternVisitor {
36823682
) {
36833683
let alignment = MemoryLayout<T>.alignment
36843684
var baseAddress = unsafe destData.baseAddress._unsafelyUnwrappedUnchecked
3685-
var misalign = unsafe Int(bitPattern: baseAddress) & (alignment - 1)
3685+
var misalign = Int(bitPattern: baseAddress) & (alignment - 1)
36863686
if misalign != 0 {
36873687
misalign = alignment - misalign
36883688
unsafe baseAddress = unsafe baseAddress.advanced(by: misalign)

stdlib/public/core/MemoryLayout.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ extension MemoryLayout where T: ~Copyable {
276276
}
277277

278278
internal static func _roundingUpBaseToAlignment(_ value: UnsafeRawBufferPointer) -> UnsafeRawBufferPointer {
279-
let baseAddressBits = unsafe Int(bitPattern: value.baseAddress)
279+
let baseAddressBits = Int(bitPattern: value.baseAddress)
280280
var misalignment = baseAddressBits & _alignmentMask
281281
if misalignment != 0 {
282282
misalignment = _alignmentMask & -misalignment
@@ -288,7 +288,7 @@ extension MemoryLayout where T: ~Copyable {
288288
}
289289

290290
internal static func _roundingUpBaseToAlignment(_ value: UnsafeMutableRawBufferPointer) -> UnsafeMutableRawBufferPointer {
291-
let baseAddressBits = unsafe Int(bitPattern: value.baseAddress)
291+
let baseAddressBits = Int(bitPattern: value.baseAddress)
292292
var misalignment = baseAddressBits & _alignmentMask
293293
if misalignment != 0 {
294294
misalignment = _alignmentMask & -misalignment

stdlib/public/core/NativeDictionary.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -639,7 +639,7 @@ extension _NativeDictionary where Value: Equatable {
639639
@inlinable
640640
@inline(__always)
641641
func isEqual(to other: _NativeDictionary) -> Bool {
642-
if unsafe self._storage === other._storage { return true }
642+
if self._storage === other._storage { return true }
643643
if self.count != other.count { return false }
644644

645645
for (key, value) in self {

stdlib/public/core/NativeSet.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -487,7 +487,7 @@ extension _NativeSet {
487487
@inlinable
488488
@inline(__always)
489489
func isEqual(to other: _NativeSet) -> Bool {
490-
if unsafe self._storage === other._storage { return true }
490+
if self._storage === other._storage { return true }
491491
if self.count != other.count { return false }
492492

493493
for member in self {

stdlib/public/core/Pointer.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,7 @@ extension Int {
340340
///
341341
/// - Parameter pointer: The pointer to use as the source for the new
342342
/// integer.
343+
@safe
343344
@_transparent
344345
public init<P: _Pointer>(bitPattern pointer: P?) {
345346
if let pointer = pointer {
@@ -358,6 +359,7 @@ extension UInt {
358359
///
359360
/// - Parameter pointer: The pointer to use as the source for the new
360361
/// integer.
362+
@safe
361363
@_transparent
362364
public init<P: _Pointer>(bitPattern pointer: P?) {
363365
if let pointer = pointer {

stdlib/public/core/Runtime.swift

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -550,12 +550,11 @@ func _uint64ToString(
550550

551551
@inlinable
552552
internal func _rawPointerToString(_ value: Builtin.RawPointer) -> String {
553-
var result = unsafe _uint64ToString(
554-
UInt64(
555-
UInt(bitPattern: UnsafeRawPointer(value))),
556-
radix: 16,
557-
uppercase: false
558-
)
553+
var result = _uint64ToString(
554+
UInt64(UInt(bitPattern: UnsafeRawPointer(value))),
555+
radix: 16,
556+
uppercase: false
557+
)
559558
for _ in unsafe 0..<(2 * MemoryLayout<UnsafeRawPointer>.size - result.utf16.count) {
560559
result = "0" + result
561560
}

stdlib/public/core/Span/Span.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ extension Span where Element: ~Copyable {
102102
) {
103103
//FIXME: Workaround for https://github.com/swiftlang/swift/issues/77235
104104
let baseAddress = unsafe UnsafeRawPointer(buffer.baseAddress)
105-
unsafe _precondition(
105+
_precondition(
106106
((Int(bitPattern: baseAddress) &
107107
(MemoryLayout<Element>.alignment &- 1)) == 0),
108108
"baseAddress must be properly aligned to access Element"
@@ -224,7 +224,7 @@ extension Span where Element: BitwiseCopyable {
224224
) {
225225
//FIXME: Workaround for https://github.com/swiftlang/swift/issues/77235
226226
let baseAddress = buffer.baseAddress
227-
unsafe _precondition(
227+
_precondition(
228228
((Int(bitPattern: baseAddress) &
229229
(MemoryLayout<Element>.alignment &- 1)) == 0),
230230
"baseAddress must be properly aligned to access Element"

stdlib/public/core/UnsafePointer.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -451,7 +451,7 @@ extension UnsafePointer {
451451
) -> UnsafePointer<Property>? {
452452
guard let o = property._storedInlineOffset else { return nil }
453453
_internalInvariant(o >= 0)
454-
unsafe _debugPrecondition(
454+
_debugPrecondition(
455455
!UInt(bitPattern: self).addingReportingOverflow(UInt(bitPattern: o)).overflow,
456456
"Overflow in pointer arithmetic"
457457
)
@@ -1343,7 +1343,7 @@ extension UnsafeMutablePointer {
13431343
) -> UnsafePointer<Property>? {
13441344
guard let o = property._storedInlineOffset else { return nil }
13451345
_internalInvariant(o >= 0)
1346-
unsafe _debugPrecondition(
1346+
_debugPrecondition(
13471347
!UInt(bitPattern: self).addingReportingOverflow(UInt(bitPattern: o)).overflow,
13481348
"Overflow in pointer arithmetic"
13491349
)
@@ -1365,7 +1365,7 @@ extension UnsafeMutablePointer {
13651365
) -> UnsafeMutablePointer<Property>? {
13661366
guard let o = property._storedInlineOffset else { return nil }
13671367
_internalInvariant(o >= 0)
1368-
unsafe _debugPrecondition(
1368+
_debugPrecondition(
13691369
!UInt(bitPattern: self).addingReportingOverflow(UInt(bitPattern: o)).overflow,
13701370
"Overflow in pointer arithmetic"
13711371
)

stdlib/public/core/UnsafeRawBufferPointer.swift.gyb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -816,7 +816,7 @@ extension Unsafe${Mutable}RawBufferPointer {
816816
}
817817

818818
_debugPrecondition(
819-
unsafe Int(bitPattern: base) & (MemoryLayout<S.Element>.alignment-1) == 0,
819+
Int(bitPattern: base) & (MemoryLayout<S.Element>.alignment-1) == 0,
820820
"buffer base address must be properly aligned to access S.Element"
821821
)
822822

@@ -875,7 +875,7 @@ extension Unsafe${Mutable}RawBufferPointer {
875875
return unsafe .init(start: nil, count: 0)
876876
}
877877
_debugPrecondition(
878-
unsafe Int(bitPattern: baseAddress) & (MemoryLayout<C.Element>.alignment-1) == 0,
878+
Int(bitPattern: baseAddress) & (MemoryLayout<C.Element>.alignment-1) == 0,
879879
"buffer base address must be properly aligned to access C.Element"
880880
)
881881
_precondition(
@@ -900,7 +900,7 @@ extension Unsafe${Mutable}RawBufferPointer {
900900
}
901901
_internalInvariant(unsafe _end != nil)
902902
_debugPrecondition(
903-
unsafe Int(bitPattern: baseAddress) & (MemoryLayout<C.Element>.alignment-1) == 0,
903+
Int(bitPattern: baseAddress) & (MemoryLayout<C.Element>.alignment-1) == 0,
904904
"buffer base address must be properly aligned to access C.Element"
905905
)
906906
var iterator = source.makeIterator()
@@ -961,7 +961,7 @@ extension Unsafe${Mutable}RawBufferPointer {
961961
return unsafe .init(start: nil, count: 0)
962962
}
963963
_debugPrecondition(
964-
unsafe Int(bitPattern: baseAddress) & (MemoryLayout<T>.alignment-1) == 0,
964+
Int(bitPattern: baseAddress) & (MemoryLayout<T>.alignment-1) == 0,
965965
"buffer base address must be properly aligned to access T"
966966
)
967967
_precondition(
@@ -1103,7 +1103,7 @@ extension Unsafe${Mutable}RawBufferPointer {
11031103
return try unsafe body(.init(start: nil, count: 0))
11041104
}
11051105
_debugPrecondition(
1106-
unsafe Int(bitPattern: s) & (MemoryLayout<T>.alignment-1) == 0,
1106+
Int(bitPattern: s) & (MemoryLayout<T>.alignment-1) == 0,
11071107
"baseAddress must be a properly aligned pointer for type T"
11081108
)
11091109
// initializer ensures _end is nil only when _position is nil.

stdlib/public/core/UnsafeRawPointer.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,7 @@ extension UnsafeRawPointer {
399399
capacity count: Int,
400400
_ body: (_ pointer: UnsafePointer<T>) throws(E) -> Result
401401
) throws(E) -> Result {
402-
unsafe _debugPrecondition(
402+
_debugPrecondition(
403403
Int(bitPattern: self) & (MemoryLayout<T>.alignment-1) == 0,
404404
"self must be a properly aligned pointer for type T"
405405
)
@@ -1016,7 +1016,7 @@ extension UnsafeMutableRawPointer {
10161016
capacity count: Int,
10171017
_ body: (_ pointer: UnsafeMutablePointer<T>) throws(E) -> Result
10181018
) throws(E) -> Result {
1019-
unsafe _debugPrecondition(
1019+
_debugPrecondition(
10201020
Int(bitPattern: self) & (MemoryLayout<T>.alignment-1) == 0,
10211021
"self must be a properly aligned pointer for type T"
10221022
)

0 commit comments

Comments
 (0)