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

Deprecate cString on non-Darwin (#3885) #5190

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

parkera
Copy link
Contributor

@parkera parkera commented Mar 17, 2025

The extension on String to provide a C string works well enough on Darwin, when used with an autoreleasepool { }. 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

@@ -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")
Copy link
Member

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

Copy link
Member

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.

Copy link
Contributor Author

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.

@parkera
Copy link
Contributor Author

parkera commented Mar 17, 2025

@swift-ci test

1 similar comment
@parkera
Copy link
Contributor Author

parkera commented Mar 18, 2025

@swift-ci test

@@ -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")
Copy link
Contributor

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?

Copy link
Contributor Author

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.

Copy link
Contributor

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.

@parkera
Copy link
Contributor Author

parkera commented Mar 18, 2025

@swift-ci test

@parkera
Copy link
Contributor Author

parkera commented Mar 19, 2025

@swift-ci test windows

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.

5 participants