Skip to content

Commit 2010f02

Browse files
committed
Remove overly-permissive UnsafePointer init
1 parent 351c129 commit 2010f02

File tree

4 files changed

+6
-31
lines changed

4 files changed

+6
-31
lines changed

stdlib/public/core/Pointer.swift

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -93,34 +93,6 @@ extension _Pointer {
9393
guard let unwrapped = other else { return nil }
9494
self.init(unwrapped._rawValue)
9595
}
96-
97-
// all pointers are creatable from mutable pointers
98-
99-
/// Creates a new pointer from the given mutable pointer.
100-
///
101-
/// Use this initializer to explicitly convert `other` to an `UnsafeRawPointer`
102-
/// instance. This initializer creates a new pointer to the same address as
103-
/// `other` and performs no allocation or copying.
104-
///
105-
/// - Parameter other: The typed pointer to convert.
106-
@_transparent
107-
public init<T>(_ other: UnsafeMutablePointer<T>) {
108-
self.init(other._rawValue)
109-
}
110-
111-
/// Creates a new raw pointer from the given typed pointer.
112-
///
113-
/// Use this initializer to explicitly convert `other` to an `UnsafeRawPointer`
114-
/// instance. This initializer creates a new pointer to the same address as
115-
/// `other` and performs no allocation or copying.
116-
///
117-
/// - Parameter other: The typed pointer to convert. If `other` is `nil`, the
118-
/// result is `nil`.
119-
@_transparent
120-
public init?<T>(_ other: UnsafeMutablePointer<T>?) {
121-
guard let unwrapped = other else { return nil }
122-
self.init(unwrapped)
123-
}
12496
}
12597

12698
// well, this is pretty annoying

stdlib/public/core/StringBreadcrumbs.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,8 @@ extension _StringGuts {
126126
}
127127

128128
_internalInvariant(mutPtr.pointee != nil)
129-
return UnsafePointer(mutPtr)
129+
// assuming optional class reference and class reference can alias
130+
return UnsafeRawPointer(mutPtr).assumingMemoryBound(to: _StringBreadcrumbs.self)
130131
}
131132

132133
@inline(never) // slow-path
@@ -137,6 +138,6 @@ extension _StringGuts {
137138
// Thread-safe compare-and-swap
138139
let crumbs = _StringBreadcrumbs(String(self))
139140
_stdlib_atomicInitializeARCRef(
140-
object: UnsafeMutablePointer(mutPtr), desired: crumbs)
141+
object: UnsafeMutablePointer(mutating: UnsafeRawPointer(mutPtr).assumingMemoryBound(to: Optional<AnyObject>.self)), desired: crumbs)
141142
}
142143
}

test/api-digester/Outputs/stability-stdlib-abi.swift.expected

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,3 +85,5 @@ Subscript String.UnicodeScalarView.subscript(_:) has been removed
8585
Subscript Substring.subscript(_:) has been removed
8686

8787
Func Collection.makeIterator() has self access kind changing from NonMutating to __Consuming
88+
89+
Constructor _Pointer.init(_:) has been removed

validation-test/compiler_crashers_2_fixed/0060-sr2702.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public func XUAllSubclassesOfClass<T: AnyObject>(_ aClass: T.Type) -> [T.Type] {
4646
free(memory)
4747
}
4848

49-
let classesPtr = memory.assumingMemoryBound(to: Optional<AnyClass>.self)
49+
let classesPtr = memory.assumingMemoryBound(to: AnyClass.self)
5050
let classes = AutoreleasingUnsafeMutablePointer<AnyClass>(classesPtr)
5151
numClasses = objc_getClassList(classes, numClasses)
5252

0 commit comments

Comments
 (0)