Skip to content

Commit f15892c

Browse files
Merge pull request #22288 from airspeedswift/safe-conversion
[stdlib] Remove overly-permissive UnsafePointer init
2 parents a2896c4 + 0f339d3 commit f15892c

File tree

5 files changed

+23
-32
lines changed

5 files changed

+23
-32
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: 4 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,7 @@ 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: UnsafeMutableRawPointer(mutPtr).assumingMemoryBound(to: Optional<AnyObject>.self),
142+
desired: crumbs)
141143
}
142144
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,3 +181,5 @@ Subscript Substring.subscript(_:) has been removed
181181
Func Collection.makeIterator() has self access kind changing from NonMutating to __Consuming
182182

183183
Func Sequence.count(where:) has been removed
184+
185+
Constructor _Pointer.init(_:) has been removed

test/stdlib/UnsafePointerDiagnostics.swift

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,11 @@ func unsafePointerConversionAvailability(
8585
_ = UnsafePointer<Int>(mrp) // expected-error {{cannot convert value of type 'UnsafeMutableRawPointer' to expected argument type 'RawPointer'}}
8686
_ = UnsafePointer<Int>(orp) // expected-error {{cannot convert value of type 'UnsafeRawPointer?' to expected argument type 'RawPointer'}}
8787
_ = UnsafePointer<Int>(omrp) // expected-error {{cannot convert value of type 'UnsafeMutableRawPointer?' to expected argument type 'RawPointer'}}
88+
89+
_ = UnsafePointer<Int>(ups) // expected-error {{cannot convert value of type 'UnsafePointer<String>' to expected argument type 'RawPointer'}}
90+
_ = UnsafeMutablePointer<Int>(umps) // expected-error {{cannot convert value of type 'UnsafeMutablePointer<String>' to expected argument type 'UnsafeMutablePointer<_>'}}
91+
_ = UnsafePointer<String>(upi) // expected-error {{cannot convert value of type 'UnsafePointer<Int>' to expected argument type 'RawPointer'}}
92+
_ = UnsafeMutablePointer<String>(umpi) // expected-error {{cannot convert value of type 'UnsafeMutablePointer<Int>' to expected argument type 'UnsafeMutablePointer<_>'}}
8893
}
8994

9095
func unsafeRawBufferPointerConversions(
@@ -114,4 +119,14 @@ func unsafeRawBufferPointerConversions(
114119
_ = UnsafeRawBufferPointer(start: omrp, count: 1)
115120
_ = UnsafeMutableRawBufferPointer(start: orp, count: 1) // expected-error {{cannot convert value of type 'UnsafeRawPointer?' to expected argument type 'UnsafeMutableRawPointer?'}}
116121
_ = UnsafeRawBufferPointer(start: orp, count: 1)
117-
}
122+
}
123+
124+
125+
struct SR9800 {
126+
func foo(_: UnsafePointer<CChar>) {}
127+
func foo(_: UnsafePointer<UInt8>) {}
128+
129+
func ambiguityTest(buf: UnsafeMutablePointer<CChar>) {
130+
_ = foo(UnsafePointer(buf)) // this call should be unambiguoius
131+
}
132+
}

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)