Skip to content

Conversation

@atrick
Copy link
Contributor

@atrick atrick commented Jul 9, 2019

Remove this additional note when attempting to incorrectly initialize UnsafePointer:

Pointer conversion restricted: use '.assumingMemoryBound(to:)' or
'.bindMemory(to:capacity:)' to view memory as a type.

The note was added for Swift 3 migration. The idea was to get projects
building as quickly as possible and effectively tag bad behavior so it
could be corrected later. That worked, but now it's been working
against us for the past couple of releases. Too much new code is using
these two APIs and the vast majority of uses are not only incorrect,
but much more simply expressed without them. Instead, programmers need
to be made aware that the UnsafeRawBufferPointer API already does what
they want... but we can work on that problem as separate task.

Most uses of these APIs are something like:

return rawptr.assumingMemoryBound(to: T.self).pointee

or

UnsafeRawPointer(ptr).bindMemory(to: UInt16.self, capacity: 2).pointee

Instead of simply

return rawptr.load(as: T.self)

I've even seen programmers do this:

extension UnsafeRawPointer {
var uint8: UInt8 {
let uint8Ptr = self.bindMemory( to: UInt8.self, capacity: 1 )
return uint8Ptr[0]
}
}

...instead of simply using either UnsafeRawPointer.load or UnsafeRawBufferPointer's subscript.

Replace this paragraph with a description of your changes and rationale. Provide links to external references/discussions if appropriate.

Resolves SR-NNNN.

Remove this additional note when attempting to incorrectly initialize UnsafePointer:

  Pointer conversion restricted: use '.assumingMemoryBound(to:)' or
  '.bindMemory(to:capacity:)' to view memory as a type.

The note was added for Swift 3 migration. The idea was to get projects
building as quickly as possible and effectively tag bad behavior so it
could be corrected later. That worked, but now it's been working
against us for the past couple of releases. Too much new code is using
these two APIs and the vast majority of uses are not only incorrect,
but much more simply expressed without them. Instead, programmers need
to be made aware that the UnsafeRawBufferPointer API already does what
they want... but we can work on that problem as separate task.

Most uses of these APIs are something like:

  return rawptr.assumingMemoryBound(to: T.self).pointee

or

  UnsafeRawPointer(ptr).bindMemory(to: UInt16.self, capacity: 2).pointee

Instead of simply

  return rawptr.load(as: T.self)

I've even seen programmers do this:

  extension UnsafeRawPointer {
    var uint8: UInt8 {
      let uint8Ptr = self.bindMemory( to: UInt8.self, capacity: 1 )
      return uint8Ptr[0]
    }
  }

...instead of simply using either UnsafeRawPointer.load or UnsafeRawBufferPointer's subscript.
@atrick
Copy link
Contributor Author

atrick commented Jul 9, 2019

@swift-ci test

Copy link
Contributor

@xedin xedin left a comment

Choose a reason for hiding this comment

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

👍

@ravikandhadai
Copy link
Contributor

Looks good to me.

@atrick atrick merged commit c8a16d3 into swiftlang:master Jul 30, 2019
@atrick atrick deleted the remove-ptrcast-diag branch July 30, 2019 23:42
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.

4 participants