Skip to content
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

Update examples in 0458-strict-memory-safety.md #2661

Closed
wants to merge 1 commit into from
Closed
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
5 changes: 2 additions & 3 deletions proposals/0458-strict-memory-safety.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ To suppress these warnings, the expressions involving unsafe code must be marked
extension Array<Int> {
func sum() -> Int {
withUnsafeBufferPointer { buffer in
// warning: use of unsafe function 'sumIntBuffer' and unsafe property 'baseAddress'
unsafe sumIntBuffer(buffer.baseAddress, buffer.count, 0)
}
}
Expand Down Expand Up @@ -122,10 +121,10 @@ The operation `UnsafeMutableBufferPointer.swapAt` swaps the values at the given

```swift
extension UnsafeMutableBufferPointer {
@unsafe public func swapAt(_ i: Element, _ j: Element) {
@unsafe public func swapAt(_ i: Index, _ j: Index) {
guard i != j else { return }
precondition(i >= 0 && j >= 0)
precondition(unsafe i < endIndex && j < endIndex)
precondition(i < endIndex && j < endIndex)
Copy link
Member

Choose a reason for hiding this comment

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

According to the current proposal, the unsafe is needed with the endIndex because of the implicit self being of an unsafe type. There is discussion on the review thread about how we might avoid that.

let pi = unsafe (baseAddress! + i)
let pj = unsafe (baseAddress! + j)
let tmp = unsafe pi.move()
Expand Down