-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Deprecate cString on non-Darwin (#3885) #5190
base: main
Are you sure you want to change the base?
Conversation
Sources/Foundation/NSStringAPI.swift
Outdated
@@ -632,6 +632,7 @@ extension StringProtocol { | |||
|
|||
/// Returns a representation of the string as a C string | |||
/// using a given encoding. | |||
@available(*, deprecated, message: "On platforms without Objective-C autorelease pools, use withCString instead") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's an NSString.cString(using:)
that has the same issue, FWIW
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that we should refer people to use String.withCString(encodedAs:_)
instead of this message.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure. There is one place we use this API in keyed archiver that will have to get a follow-up.
@swift-ci test |
1 similar comment
@swift-ci test |
Sources/Foundation/NSStringAPI.swift
Outdated
@@ -632,6 +632,7 @@ extension StringProtocol { | |||
|
|||
/// Returns a representation of the string as a C string | |||
/// using a given encoding. | |||
@available(*, deprecated, message: "On platforms without Objective-C autorelease pools, use withCString(encodedAs:_) instead") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This one is actually safe to use though, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not unless we reimplement it to use a lifetime-limited version of the NSString
cString(using:)
function. Which I suppose we could do.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't know if it's worth it, but it might mean less churn.
@swift-ci test |
@swift-ci test windows |
The extension on
String
to provide a C string works well enough on Darwin, when used with anautoreleasepool { }
. However, on other platforms where we have no autorelease pools, it simply leaks. Even though this API is useful on Darwin, we should deprecate it in swift-corelibs-foundation because it has repeatedly been the source of some confusing behavior.#3885