Skip to content

Add implicit conversions and casts from T:Hashable <-> AnyHashable. #4022

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

rjmccall
Copy link
Contributor

@rjmccall rjmccall commented Aug 4, 2016

What's in this pull request?

Resolved bug number: (SR-)


Before merging this pull request to apple/swift repository:

  • Test pull request on Swift continuous integration.

Triggering Swift CI

The swift-ci is triggered by writing a comment on this PR addressed to the GitHub user @swift-ci. Different tests will run depending on the specific comment that you use. The currently available comments are:

Smoke Testing

Platform Comment
All supported platforms @swift-ci Please smoke test
All supported platforms @swift-ci Please smoke test and merge
OS X platform @swift-ci Please smoke test OS X platform
Linux platform @swift-ci Please smoke test Linux platform

A smoke test on macOS does the following:

  1. Builds the compiler incrementally.
  2. Builds the standard library only for macOS. Simulator standard libraries and
    device standard libraries are not built.
  3. lldb is not built.
  4. The test and validation-test targets are run only for macOS. The optimized
    version of these tests are not run.

A smoke test on Linux does the following:

  1. Builds the compiler incrementally.
  2. Builds the standard library incrementally.
  3. lldb is built incrementally.
  4. The swift test and validation-test targets are run. The optimized version of these
    tests are not run.
  5. lldb is tested.

Validation Testing

Platform Comment
All supported platforms @swift-ci Please test
All supported platforms @swift-ci Please test and merge
OS X platform @swift-ci Please test OS X platform
OS X platform @swift-ci Please benchmark
Linux platform @swift-ci Please test Linux platform

Lint Testing

Language Comment
Python @swift-ci Please Python lint

Note: Only members of the Apple organization can trigger swift-ci.

rdar://27615802

@rjmccall
Copy link
Contributor Author

rjmccall commented Aug 4, 2016

@swift-ci Please smoke test and merge.

@rjmccall
Copy link
Contributor Author

rjmccall commented Aug 4, 2016

@jckarter Mind reviewing the SILGen and runtime parts of this?
@DougGregor Mind reviewing the type-checker parts of this?

@rjmccall
Copy link
Contributor Author

rjmccall commented Aug 4, 2016

Passed tests on a separate Linux build.

@DougGregor
Copy link
Member

Type checker parts look good!

@rjmccall rjmccall force-pushed the anyhashable-conversions branch from eb875a0 to 5d479e7 Compare August 5, 2016 00:56
@rjmccall
Copy link
Contributor Author

rjmccall commented Aug 5, 2016

@swift-ci Please smoke test.

// CHECK-NEXT: Optional(AnyHashable(5))

print("End")
// CHECK-NEXT: End
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you use StdlibUnittest?

Copy link
Contributor

@gribozavr gribozavr Aug 5, 2016

Choose a reason for hiding this comment

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

Could you add a test for a cast from an existential that contains AnyHashable to an unboxed type?

Also, for a cast from an AnyHashable to a protocol existential of the unwrapped value?

Copy link
Contributor

Choose a reason for hiding this comment

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

It would be good to do something like the logic in test/1_stdlib/BridgeIdAsAny.swift.gyb, to test all the possible cases of compounded existential/AnyHashable erasure:

https://github.com/apple/swift/blob/master/test/1_stdlib/BridgeIdAsAny.swift.gyb#L177

@gribozavr
Copy link
Contributor

Does the cast optimizer need to be adjusted for this change?

func _anyHashableDownCastConditionalIndirect<T>(
_ value: UnsafePointer<AnyHashable>,
_ target: UnsafeMutablePointer<T>) -> Bool {
return value.pointee._downCastConditional(into: target)
Copy link
Contributor

Choose a reason for hiding this comment

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

Why not use public entry points:

if let result = value.pointee.base as? T {
  target.pointee.initialize(to: result)
  return true
}
return false

Copy link
Contributor

Choose a reason for hiding this comment

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

I see, this way you are potentially saving a heap box allocation for the container returned from value.pointee. If that is the important part here, I would appreciate a comment about this!

@rjmccall
Copy link
Contributor Author

rjmccall commented Aug 5, 2016

The cast optimizer uses the logic in DynamicCasts.cpp.

@jckarter
Copy link
Contributor

jckarter commented Aug 5, 2016

Looks good, thanks for taking care of this @rjmccall! I agree with @gribozavr that it'd be good to have more exhaustive tests for all the casting stuff.

@gottesmm
Copy link
Contributor

gottesmm commented Aug 5, 2016

Hey John. If the cast optimizer uses logic in DynamicCasts.cpp, please add specific sil tests that exercise the code in the cast optimizer. We have run into many issues with the cast optimizer not being updated properly in the face of these sorts of changes.

@DougGregor
Copy link
Member

@swift-ci please smoke test Linux

@rjmccall
Copy link
Contributor Author

rjmccall commented Aug 5, 2016

@gottesmm The test is run under -O as well as not, but I'll also add specific cast-optimizer tests.

@rjmccall
Copy link
Contributor Author

rjmccall commented Aug 5, 2016

To be clear, the existing test exercises the code in DynamicCasts.cpp and fails if it's wrong.

@DougGregor
Copy link
Member

The Linux failure is the same one that's been failing for a while in LLDB (which I just reverted), so let's give it one more go... I'd like to get this in so we can more easily test the combination of this change with my inference of existential element types for collection literals.

@DougGregor
Copy link
Member

@swift-ci please smoke test Linux

@rjmccall rjmccall force-pushed the anyhashable-conversions branch from 5d479e7 to f5d941b Compare August 5, 2016 06:00
@rjmccall
Copy link
Contributor Author

rjmccall commented Aug 5, 2016

Okay, addressed code review comments.

@swift-ci Please smoke test and merge.

@rjmccall
Copy link
Contributor Author

rjmccall commented Aug 5, 2016

Oh, I forgot to add specific cast-optimizer tests; I'll write that up separately.

@rjmccall rjmccall force-pushed the anyhashable-conversions branch from f5d941b to a6e1e87 Compare August 5, 2016 06:13
@rjmccall
Copy link
Contributor Author

rjmccall commented Aug 5, 2016

Nah, added them to this one. Done for now.

@swift-ci Please smoke test and merge.

@DougGregor
Copy link
Member

Awesome! Once this is in, I'll write some integration tests to make sure dictionary literals work properly with AnyHashable.

@swift-ci swift-ci merged commit 6fd6781 into swiftlang:master Aug 5, 2016
@rjmccall rjmccall deleted the anyhashable-conversions branch August 5, 2016 18:35
@gottesmm
Copy link
Contributor

gottesmm commented Aug 5, 2016

@rjmccall Thanks!

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