Skip to content

Make bridged String and collection types conform to CVarArg. #3974

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
Aug 5, 2016

Conversation

jckarter
Copy link
Contributor

@jckarter jckarter commented Aug 3, 2016

This allows String, Array, Dictionary, and Set to be passed as variadic arguments to Cocoa APIs like NSLog, NSPredicate, stringWithFormat:, etc. rdar://problem/27651717

@jckarter
Copy link
Contributor Author

jckarter commented Aug 3, 2016

@swift-ci Please smoke test

@jckarter
Copy link
Contributor Author

jckarter commented Aug 3, 2016

@gribozavr Do you mind reviewing?

@jrose-apple
Copy link
Contributor

How about the Foundation-level bridged types? What about Linux? cc @phausler

@phausler
Copy link
Contributor

phausler commented Aug 3, 2016

NSLog is omitted since print is preferred.
NSString has the method:

public convenience init(format: String, arguments argList: CVaListPointer) {
        let str = CFStringCreateWithFormatAndArguments(kCFAllocatorSystemDefault, nil, format._cfObject, argList)!
        self.init(str._swiftObject)
    }

which defers to CF, but there is no adoption else wise.

@jckarter
Copy link
Contributor Author

jckarter commented Aug 3, 2016

@jrose-apple We can add more conformances easily enough. String is by far the most important. This has no impact on Linux, since it would already have not worked before, but maybe corelibs could add the same conformances for parity.

@jckarter
Copy link
Contributor Author

jckarter commented Aug 3, 2016

@shahmishal Linux build seems to be broken by an LLVM change:

ninja: error: '/home/buildnode/jenkins/workspace/swift-PR-Linux-smoke-test/buildbot_linux/llvm-linux-x86_64/lib/libLLVMGlobalISel.a', needed by 'bin/swift', missing and no known rule to make it

@rjmccall
Copy link
Contributor

rjmccall commented Aug 3, 2016

Is this a good idea for String? It's not obvious that converting to an NSString is better than, say, converting to const char*.

@jckarter
Copy link
Contributor Author

jckarter commented Aug 3, 2016

@rjmccall It mirrors our Swift 2 behavior, where strings in CVarArg context would implicitly convert to NSString. The CVarArg protocol doesn't provide any safe way to get a char* out of a String that I can think of either.

@rjmccall
Copy link
Contributor

rjmccall commented Aug 3, 2016

Alright, fair enough.

@gribozavr
Copy link
Contributor

I'm not sure this is the right thing to do. Do people actually expect that String is passed as NSString, or do they prefer char *? Especially on Linux.

Also, please add tests for all new APIs and new usecases (e.g., passing an Array as a vararg).

@jckarter
Copy link
Contributor Author

jckarter commented Aug 4, 2016

@gribozavr This preserves the Swift 2 behavior for Darwin, and this never worked for Linux. I don't see how we can realistically support %s at all without a major redesign of CVarArg, and I doubt that's ever in the cards, since the affected APIs aren't particularly idiomatic to begin with.

@jckarter
Copy link
Contributor Author

jckarter commented Aug 4, 2016

@gribozavr The behavior should also already be exercised by existing tests where I've rolled back as conversions that were made necessary by the original implementation of SE-0072.

@jckarter jckarter force-pushed the cvararg-post-0072 branch from 1a23cb2 to 384c880 Compare August 4, 2016 00:56
@gottesmm
Copy link
Contributor

gottesmm commented Aug 4, 2016

@swift-ci Please smoke test

@jckarter
Copy link
Contributor Author

jckarter commented Aug 4, 2016

Looks like we never really exercised (NS)Array or Dictionary varargs. Added a test for that.

@gottesmm
Copy link
Contributor

gottesmm commented Aug 4, 2016

(Just curious if the linux smoke test is fixed, Mishal cleared the workspace)

@gribozavr
Copy link
Contributor

gribozavr commented Aug 4, 2016

The behavior should also already be exercised by existing tests where I've rolled back as conversions that were made necessary by the original implementation of SE-0072.

We have a policy in the library that we don't add APIs without directed tests for them. Incidental tests can come and go as those files are refactored. We can't rely on them.

@gribozavr
Copy link
Contributor

@gribozavr This preserves the Swift 2 behavior for Darwin, and this never worked for Linux.

I understand, but we are trying to do the right thing, and not just preserve the behavior. If we were trying to preserve the behavior, we would not do id-as-Any.

// CHECK-NEXT: ) {
// CHECK-NEXT: y = z;
// CHECK-NEXT: }
print(NSString(format: "%@ %@", ["x"], ["y": "z"]))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a better, more descriptive test or at least a comment about what is important about this line. It is not clear what this line is testing.

This allows String, Array, Dictionary, and Set to be passed as variadic arguments to Cocoa APIs like NSLog, NSPredicate, stringWithFormat:, etc. rdar://problem/27651717
@jckarter jckarter force-pushed the cvararg-post-0072 branch from 384c880 to 7535acc Compare August 4, 2016 14:18
@jckarter
Copy link
Contributor Author

jckarter commented Aug 4, 2016

@gribozavr OK, I added comments to the tests.

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.

6 participants