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

[SR-4649] Don't require & to pass value to UnsafeRawPointer #47226

Closed
swift-ci opened this issue Apr 20, 2017 · 3 comments
Closed

[SR-4649] Don't require & to pass value to UnsafeRawPointer #47226

swift-ci opened this issue Apr 20, 2017 · 3 comments
Labels
compiler The Swift compiler in itself feature A feature request or implementation improvement

Comments

@swift-ci
Copy link
Collaborator

Previous ID SR-4649
Radar None
Original Reporter smartgo (JIRA User)
Type Improvement

Attachment: Download

Additional Detail from JIRA
Votes 1
Component/s Compiler
Labels Improvement, LanguageFeatureRequest
Assignee None
Priority Medium

md5: 0988f63ad6ccc74f00f9fa5fc89b1c7a

relates to:

  • SR-1956 withUnsafePointer shouldn't take its argument as inout
  • SR-4929 Support non-inout homogeneous tuple argument conversion to UnsafePointer

Issue Description:

The following code shows the inconsistency in passing values to UnsafeRawPointer:

func get(_ pointer: UnsafeRawPointer, at index: Int) -> Int {
let a = pointer.bindMemory(to: Int.self, capacity: 6)
return a[index]
}

var varArray = [Int](repeating: 0, count: 6)
var varTuple = (0, 0, 0, 0, 0, 0)

let letArray = [Int](repeating: 0, count: 6)
let letTuple = (0, 0, 0, 0, 0, 0)

// Array can be passed directly, automatically takes address.
_ = get(varArray, at: 2) // okay
_ = get(letArray, at: 2) // okay

// When explicitly taking address, can only pass mutable variables.
_ = get(&varArray, at: 2) // okay, but seems inconsistent
_ = get(&letArray, at: 2) // fails: & not allowed

// Passing tuple instead of array fails.
_ = get(varTuple, at: 2) // fails (wrong type)
_ = get(letTuple, at: 2) // fails (wrong type)

// Adding & to pass a tuple only works for mutable values. Having to
// pass the address using & means that any methods calling this must
// be mutating, even though they don't mutate.
_ = get(&varTuple, at: 2) // okay, but forces mutating
_ = get(&letTuple, at: 2) // fails: cannot pass immutable value

Getting an UnsafeRawPointer should not require use of &, as that forces all code calling it to be mutating. Having a special case for array also doesn't make sense, as the idea of UnsafeRawPointer is that we're interpreting that memory content as whatever we want. This needs to be fixed. This is related to SR-4542, as a C array is imported as tuples, and thus read-only access is not possible without being mutating.

Proposal:

  • Never require & when passing value to UnsafeRawPointer.

  • Always require & when passing value to UnsafeMutableRawPointer.

@belkadan
Copy link
Contributor

This would need to go through the Swift Evolution Process (though I did see you bring it up on swift-evolution already).

@swift-ci
Copy link
Collaborator Author

Comment by Anders Kierulf (JIRA)

Added a sample project that shows the workaround of using an extra copy is not a viable solution, as the extra copy doesn't get optimized away.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
@atrick
Copy link
Member

atrick commented Apr 26, 2022

@atrick atrick closed this as completed Apr 26, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler The Swift compiler in itself feature A feature request or implementation improvement
Projects
None yet
Development

No branches or pull requests

3 participants