Navigation Menu

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-10175] Data.withUnsafe(Mutable)Bytes ambiguous with deprecated call #52577

Open
swift-ci opened this issue Mar 26, 2019 · 7 comments
Open
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself diagnostics QoI Bug: Diagnostics Quality of Implementation type checker Area → compiler: Semantic analysis

Comments

@swift-ci
Copy link
Collaborator

Previous ID SR-10175
Radar None
Original Reporter eaigner (JIRA User)
Type Bug
Environment

10.14.4 (18E226)

Version 10.2 (10E125)

Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug, DiagnosticsQoI, TypeChecker
Assignee None
Priority Medium

md5: 5e43a1a592c837fcf75a13f51c3d33a3

Issue Description:

Swift 5 deprecated the old method signature of

Data.withUnsafe(Mutable)Bytes 

In foundation they are defined like this:

@available(swift, deprecated: 5, message: "use `withUnsafeMutableBytes<R>(_: (UnsafeMutableRawBufferPointer) throws -> R) rethrows -> R` instead")
public mutating func withUnsafeMutableBytes<ResultType, ContentType>(_ body: (UnsafeMutablePointer<ContentType>) throws -> ResultType) rethrows -> ResultType
@inlinable public mutating func withUnsafeMutableBytes<ResultType>(_ body: (UnsafeMutableRawBufferPointer) throws -> ResultType) rethrows -> ResultType 

Unfortunately there is no way to silence this warning, since the compiler always thinks that I am using the dprecated version.

If I change the types to be explicitly

data.withUnsafeMutableBytes { (ptr: UnsafeMutableRawBufferPointer) -> Int32 in
 // do stuff
}

The compiler errors out with

'UnsafeMutableRawBufferPointer' is not convertible to 'UnsafeMutablePointer<_>'

trying to conform to the deprecated version

@stephentyrone
Copy link
Member

You should be able to do something like:

var bound = ptr.bindMemory(to: ContentType.self).baseAddress!

to get an UnsafeMutablePointer<ContentType> from the UnsafeMutableRawBufferPointer that you have.

@swift-ci
Copy link
Collaborator Author

Comment by Erik Aigner (JIRA)

If it would compile. The new version does not compile at all!

@swift-ci
Copy link
Collaborator Author

Comment by Erik Aigner (JIRA)

Writing

data.withUnsafeMutableBytes { ptr in
    // do stuff
}

will generate a deprecation warning.

Explicitly writing (the new method signature)

data.withUnsafeMutableBytes { (ptr: UnsafeMutableRawBufferPointer) -> Int32 in
    // do stuff
}

will generate a compile error.

@stephentyrone
Copy link
Member

Right. It's generating a compile error because you've changed the type of ptr such that it no longer matches the code you have in // do stuff. I'm giving you a way that you can convert it to the type that does match your code.

@swift-ci
Copy link
Collaborator Author

Comment by Erik Aigner (JIRA)

Oh, I see what's going on.

Xcode did output the error on the wrong line, so i thought the type mismatch was referring
to the closure parameter, not to passing the pointer to the call inside the closure.

@belkadan
Copy link
Contributor

Retagging this as a diagnostics bug, then. cc @xedin

@stephentyrone
Copy link
Member

Yeah, we should figure out some way to provide more sophisticated fixits for cases like this (in the short term, even a long comment explaining what to do would be a big help).

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself diagnostics QoI Bug: Diagnostics Quality of Implementation type checker Area → compiler: Semantic analysis
Projects
None yet
Development

No branches or pull requests

3 participants