Skip to content

Fix more warnings for deprecation and unsafeBitCast() #953

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
May 30, 2017

Conversation

spevans
Copy link
Contributor

@spevans spevans commented Apr 18, 2017

Convert unsafeBitCast() to bindMemory()/withMemoryRebound()

Convert deprecated initialize(from:) to UnsafeMutableBufferPointer.initialize(from:)

@@ -156,7 +156,10 @@ public struct DateInterval : ReferenceConvertible, Comparable, Hashable {
public var hashValue: Int {
var buf: (UInt, UInt) = (UInt(start.timeIntervalSinceReferenceDate), UInt(end.timeIntervalSinceReferenceDate))
return withUnsafeMutablePointer(to: &buf) {
return Int(bitPattern: CFHashBytes(unsafeBitCast($0, to: UnsafeMutablePointer<UInt8>.self), CFIndex(MemoryLayout<UInt>.size * 2)))
let count = MemoryLayout<UInt>.size * 2
return $0.withMemoryRebound(to: UInt8.self, capacity: count) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Repeated use of implicit arguments makes me nervous - if nothing else about readability. Can you give these names?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@CodaFi I've added some names

@@ -130,7 +130,7 @@ open class NSArray : NSObject, NSCopying, NSMutableCopying, NSSecureCoding, NSCo
// }
let cnt = array.count
let buffer = UnsafeMutablePointer<AnyObject>.allocate(capacity: cnt)
buffer.initialize(from: optionalArray)
_ = UnsafeMutableBufferPointer(start: buffer, count: cnt).initialize(from: optionalArray)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems like programming via side-effect. Is there some more explicit way we could express this?

- Use UnsafeMutablePointer.initialize(from:count:) as a better
  alternative to UnsafeMutableBufferPointer.initialize(from:)
@parkera
Copy link
Contributor

parkera commented Apr 18, 2017

That looks much better, thanks. cc @phausler

@spevans
Copy link
Contributor Author

spevans commented Apr 18, 2017

Agreed about the _ = UnsafeMutableBufferPointer...
I found a better alternative to UnsafeMutablePointer.initalize(from:) which is UnsafeMutablePointer.initalize(from:count:). Not sure why the deprecation warning didnt list it as its not marked at deprecated.

@spevans
Copy link
Contributor Author

spevans commented May 22, 2017

@pushkarnk Could you run a test on this PR please

@alblue
Copy link
Contributor

alblue commented May 22, 2017

@swift-ci please test

1 similar comment
@aciidgh
Copy link

aciidgh commented May 23, 2017

@swift-ci please test

@spevans
Copy link
Contributor Author

spevans commented May 25, 2017

@parkera Can this be merged now?

@spevans
Copy link
Contributor Author

spevans commented May 27, 2017

@phausler Could you have a look at this PR? It fixes up quite a few warnings

Copy link
Contributor

@phausler phausler left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall looks fine to me, provided the rebind of the memory does not cause an allocation I don't see an issue with this.

return Int(bitPattern: CFHashBytes(unsafeBitCast($0, to: UnsafeMutablePointer<UInt8>.self), CFIndex(MemoryLayout<UInt>.size * 2)))
return withUnsafeMutablePointer(to: &buf) { bufferPtr in
let count = MemoryLayout<UInt>.size * 2
return bufferPtr.withMemoryRebound(to: UInt8.self, capacity: count) { bufferBytes in
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does this potentially allocate?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nothing here seems to indicate an allocation, unless there is something Im not seeing

@@ -155,8 +155,11 @@ public struct DateInterval : ReferenceConvertible, Comparable, Hashable {

public var hashValue: Int {
var buf: (UInt, UInt) = (UInt(start.timeIntervalSinceReferenceDate), UInt(end.timeIntervalSinceReferenceDate))
return withUnsafeMutablePointer(to: &buf) {
return Int(bitPattern: CFHashBytes(unsafeBitCast($0, to: UnsafeMutablePointer<UInt8>.self), CFIndex(MemoryLayout<UInt>.size * 2)))
return withUnsafeMutablePointer(to: &buf) { bufferPtr in
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should consider this for the overlay as well

@parkera parkera merged commit 124680b into swiftlang:master May 30, 2017
@parkera
Copy link
Contributor

parkera commented May 30, 2017

Thanks!

@spevans spevans deleted the pr_warning_fixes_2 branch August 9, 2017 06:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants