Skip to content

[stdlib] fix UnsafePointer.withMemoryRebound(to:capacity:) argument t… #4908

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 1 commit into from
Sep 22, 2016
Merged

[stdlib] fix UnsafePointer.withMemoryRebound(to:capacity:) argument t… #4908

merged 1 commit into from
Sep 22, 2016

Conversation

atrick
Copy link
Contributor

@atrick atrick commented Sep 21, 2016

SE-0107 states that UnsafePointer.withMemoryRebound(to:capacity:) should produce
a const UnsafePointer, but the implementation that I committed in Whitney
produces an UnsafeMutablePointer.

As a result Swift 3 accepts code, that we would like to reject:

func takesUInt(: UnsafeMutablePointer) {}
func takesConstUInt(
: UnsafePointer) {}

func foo(p: UnsafePointer) {
p.withMemoryRebound(to: UInt.self, capacity: 1) {
takesUInt($0) // <========= implicitly converts to a mutable pointer
takesConstUInt($0)
}
}

We would like to reject this in favor of:

func takesUInt(: UnsafeMutablePointer) {}
func takesConstUInt(
: UnsafePointer) {}

func foo(p: UnsafePointer) {
p.withMemoryRebound(to: UInt.self, capacity: 1) {
takesUInt(UnsafeMutablePointer(mutating: $0))
takesConstUInt($0)
}
}

This looks to me like an experimental change accidentally creeped onto my branch
and it was hard to spot in .gyb code. I needed to write the unit test
in terms of UnsafeMutablePointer in order to use expectType, so the original test didn't
catch this.

rdar://28409842 UnsafePointer.withMemoryRebound(to:capacity:) incorrectly produces a mutable pointer argument

…ype.

SE-0107 states that UnsafePointer.withMemoryRebound(to:capacity:) should produce
a const UnsafePointer, but the implementation that I committed in Whitney
produces an UnsafeMutablePointer.

As a result Swift 3 accepts code, that we would like to reject:

func takesUInt(_: UnsafeMutablePointer<UInt>) {}
func takesConstUInt(_: UnsafePointer<UInt>) {}

func foo(p: UnsafePointer<Int>) {
  p.withMemoryRebound(to: UInt.self, capacity: 1) {
    takesUInt($0) // <========= implicitly converts to a mutable pointer
    takesConstUInt($0)
  }
}

We would like to reject this in favor of:

func takesUInt(_: UnsafeMutablePointer<UInt>) {}
func takesConstUInt(_: UnsafePointer<UInt>) {}

func foo(p: UnsafePointer<Int>) {
  p.withMemoryRebound(to: UInt.self, capacity: 1) {
    takesUInt(UnsafeMutablePointer(mutating: $0))
    takesConstUInt($0)
  }
}

This looks to me like an experimental change accidentally creeped onto my branch
and it was hard to spot in .gyb code. I needed to write the unit test
in terms of UnsafeMutablePointer in order to use expectType, so didn't
catch this.

rdar://28409842 UnsafePointer.withMemoryRebound(to:capacity:) incorrectly produces a mutable pointer argument
@atrick
Copy link
Contributor Author

atrick commented Sep 21, 2016

CCC Info:
• Explanation: UnsafePointer implementation in Swift 3 does not match the proposal.
• Scope of Issue: The Swift 3 API is too lenient in one case. Rarely, users who already migrated to 3 may need to insert an additional cast after this fix.
• Origination: SE-0107: Swift 3.
• Risk: Potentially causes a compiler error in code that was already migrated.
• Nominated Reviewers: @dabrahams
• Testing: Unit test. Currently testing build-toolchain on swift-3.0-branch.
• Bug: rdar://28409842

@atrick
Copy link
Contributor Author

atrick commented Sep 21, 2016

@swift-ci test.

@dabrahams
Copy link
Contributor

LGTM!

@atrick atrick merged commit fe1fd2e into swiftlang:swift-3.0-branch Sep 22, 2016
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.

2 participants