Skip to content

[stdlib] Remove overly-permissive UnsafePointer init #22288

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Feb 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 0 additions & 28 deletions stdlib/public/core/Pointer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -93,34 +93,6 @@ extension _Pointer {
guard let unwrapped = other else { return nil }
self.init(unwrapped._rawValue)
}

// all pointers are creatable from mutable pointers

/// Creates a new pointer from the given mutable pointer.
///
/// Use this initializer to explicitly convert `other` to an `UnsafeRawPointer`
/// instance. This initializer creates a new pointer to the same address as
/// `other` and performs no allocation or copying.
///
/// - Parameter other: The typed pointer to convert.
@_transparent
public init<T>(_ other: UnsafeMutablePointer<T>) {
self.init(other._rawValue)
}

/// Creates a new raw pointer from the given typed pointer.
///
/// Use this initializer to explicitly convert `other` to an `UnsafeRawPointer`
/// instance. This initializer creates a new pointer to the same address as
/// `other` and performs no allocation or copying.
///
/// - Parameter other: The typed pointer to convert. If `other` is `nil`, the
/// result is `nil`.
@_transparent
public init?<T>(_ other: UnsafeMutablePointer<T>?) {
guard let unwrapped = other else { return nil }
self.init(unwrapped)
}
}

// well, this is pretty annoying
Expand Down
6 changes: 4 additions & 2 deletions stdlib/public/core/StringBreadcrumbs.swift
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,8 @@ extension _StringGuts {
}

_internalInvariant(mutPtr.pointee != nil)
return UnsafePointer(mutPtr)
// assuming optional class reference and class reference can alias
return UnsafeRawPointer(mutPtr).assumingMemoryBound(to: _StringBreadcrumbs.self)
}

@inline(never) // slow-path
Expand All @@ -137,6 +138,7 @@ extension _StringGuts {
// Thread-safe compare-and-swap
let crumbs = _StringBreadcrumbs(String(self))
_stdlib_atomicInitializeARCRef(
object: UnsafeMutablePointer(mutPtr), desired: crumbs)
object: UnsafeMutableRawPointer(mutPtr).assumingMemoryBound(to: Optional<AnyObject>.self),
desired: crumbs)
}
}
2 changes: 2 additions & 0 deletions test/api-digester/Outputs/stability-stdlib-abi.swift.expected
Original file line number Diff line number Diff line change
Expand Up @@ -181,3 +181,5 @@ Subscript Substring.subscript(_:) has been removed
Func Collection.makeIterator() has self access kind changing from NonMutating to __Consuming

Func Sequence.count(where:) has been removed

Constructor _Pointer.init(_:) has been removed
17 changes: 16 additions & 1 deletion test/stdlib/UnsafePointerDiagnostics.swift
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ func unsafePointerConversionAvailability(
_ = UnsafePointer<Int>(mrp) // expected-error {{cannot convert value of type 'UnsafeMutableRawPointer' to expected argument type 'RawPointer'}}
_ = UnsafePointer<Int>(orp) // expected-error {{cannot convert value of type 'UnsafeRawPointer?' to expected argument type 'RawPointer'}}
_ = UnsafePointer<Int>(omrp) // expected-error {{cannot convert value of type 'UnsafeMutableRawPointer?' to expected argument type 'RawPointer'}}

_ = UnsafePointer<Int>(ups) // expected-error {{cannot convert value of type 'UnsafePointer<String>' to expected argument type 'RawPointer'}}
_ = UnsafeMutablePointer<Int>(umps) // expected-error {{cannot convert value of type 'UnsafeMutablePointer<String>' to expected argument type 'UnsafeMutablePointer<_>'}}
_ = UnsafePointer<String>(upi) // expected-error {{cannot convert value of type 'UnsafePointer<Int>' to expected argument type 'RawPointer'}}
_ = UnsafeMutablePointer<String>(umpi) // expected-error {{cannot convert value of type 'UnsafeMutablePointer<Int>' to expected argument type 'UnsafeMutablePointer<_>'}}
}

func unsafeRawBufferPointerConversions(
Expand Down Expand Up @@ -114,4 +119,14 @@ func unsafeRawBufferPointerConversions(
_ = UnsafeRawBufferPointer(start: omrp, count: 1)
_ = UnsafeMutableRawBufferPointer(start: orp, count: 1) // expected-error {{cannot convert value of type 'UnsafeRawPointer?' to expected argument type 'UnsafeMutableRawPointer?'}}
_ = UnsafeRawBufferPointer(start: orp, count: 1)
}
}


struct SR9800 {
func foo(_: UnsafePointer<CChar>) {}
func foo(_: UnsafePointer<UInt8>) {}

func ambiguityTest(buf: UnsafeMutablePointer<CChar>) {
_ = foo(UnsafePointer(buf)) // this call should be unambiguoius
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public func XUAllSubclassesOfClass<T: AnyObject>(_ aClass: T.Type) -> [T.Type] {
free(memory)
}

let classesPtr = memory.assumingMemoryBound(to: Optional<AnyClass>.self)
let classesPtr = memory.assumingMemoryBound(to: AnyClass.self)
let classes = AutoreleasingUnsafeMutablePointer<AnyClass>(classesPtr)
numClasses = objc_getClassList(classes, numClasses)

Expand Down