Skip to content

[SR-12881] DefaultIndices dynamic dispatch #32019

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 6 commits into from
Jul 14, 2020
Merged

Conversation

NevinBR
Copy link
Contributor

@NevinBR NevinBR commented May 26, 2020

As seen in SR-12881, DefaultIndices was not properly dispatching certain Collection requirements through conditional conformances to BidirectionalCollection and RandomAccessCollection. This should fix that by adding implementations for these methods at the point where DefaultIndices conforms to Collection:

index(_:offsetBy:)
index(_:offsetBy:limitedBy:)
distance(from:to:)

As seen in SR-12881, `DefaultIndices` was not properly dispatching certain `Collection` requirements through conditional conformances to `BidirectionalCollection` and `RandomAccessCollection`.  This should fix that.
@theblixguy theblixguy requested a review from airspeedswift May 26, 2020 15:48
Copy link
Member

@airspeedswift airspeedswift left a comment

Choose a reason for hiding this comment

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

This is a good change in principle, but the impact on ABI stability needs to be considered. These maybe need to be always emitted into client, or need availability annotations.

Copy link
Member

@lorentey lorentey left a comment

Choose a reason for hiding this comment

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

This looks good — thanks! 👍
Could you please also add unit tests for the correct behavior?

@NevinBR
Copy link
Contributor Author

NevinBR commented May 26, 2020

This looks good — thanks! 👍
Could you please also add unit tests for the correct behavior?

I’d be happy to, but I’ve never added unit tests to Swift before: where should they go, and is there a particular form or style that they should adopt?

@lorentey
Copy link
Member

I’d be happy to, but I’ve never added unit tests to Swift before: where should they go, and is there a particular form or style that they should adopt?

Most stdlib tests live under test/stdlib/. It looks like we don't have dedicated tests for DefaultIndices yet at all (oops!), so the best course of action is to add a new file test/stdlib/DefaultIndices.swift. You can use the existing tests in the same directory to learn by example -- the template to follow is this:

// RUN: %target-run-stdlib-swift
// REQUIRES: executable_test

import StdlibUnittest

var suite = TestSuite("DefaultIndices")
defer { runAllTests() }

suite.test("Some name") {
  expectEqual(4, 2 + 2)
}

For the most part, the assertion syntax is equivalent to XCTest, with the XCTAssert prefix replaced by expect.

The StdlibCollectionUnittest module contains a bunch of very handy collection validation test generators, including addRandomAccessCollectionTests -- unfortunately they are geared towards collections that can hold arbitrary data, so they aren't directly helpful here. So the most straightforward thing to do is to simply write a few tests that exercise the original issue.

@NevinBR
Copy link
Contributor Author

NevinBR commented May 28, 2020

@airspeedswift, I changed @inlinable to @_alwaysEmitIntoClient per @lorentey’s suggestion. Are there other ABI-related changes that are needed here?

@lorentey, I added unit tests to exercise SR-12881, do they look acceptable?

@airspeedswift
Copy link
Member

@swift-ci please test

@swift-ci
Copy link
Contributor

swift-ci commented Jun 5, 2020

Build failed
Swift Test Linux Platform
Git Sha - e146d3f

@swift-ci
Copy link
Contributor

swift-ci commented Jun 5, 2020

Build failed
Swift Test OS X Platform
Git Sha - e146d3f

@lorentey
Copy link
Member

Curiously, these requirements are declared @_nonoverride in BidirectionalCollection, which means they have two different protocol witnesses. (In fact, they have three because RandomAccessCollection does the same.)

So, IIUC, any conforming type that wants to perfectly forward implementations of these to another, user-supplied, collection needs to do this three different times for the (unlikely but not impossible) case that the target collection distinguishes between Collection.index(_:offsetBy:), BidirectionalCollection.index(_:offsetBy:) and RandomAccessCollection.index(_:offsetBy:).

This change still looks right to me. We are currently lacking stdlib tests for this rarely exercised language facility; if it turns out we need to add more code, we'll be able to do it later.

Cc @DougGregor

@lorentey
Copy link
Member

@swift-ci test and merge

@lorentey
Copy link
Member

17:14:59 FAILED: source/Target/CMakeFiles/lldbTarget.dir/SwiftLanguageRuntimeDynamicTypeResolution.cpp.o 
17:14:59 /usr/local/bin/sccache /Applications/Xcode-beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang++  -DGTEST_HAS_RTTI=0 -DHAVE_ROUND -DLLDB_CONFIGURATION_RELEASE -DLLDB_USE_OS_LOG -D_DEBUG -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS -Isource/Target -I/Users/buildnode/jenkins/workspace/swift-PR-osx/branch-master/llvm-project/lldb/source/Target -Isource -I/Users/buildnode/jenkins/workspace/swift-PR-osx/branch-master/llvm-project/lldb/include -Iinclude -I/Users/buildnode/jenkins/workspace/swift-PR-osx/branch-master/llvm-project/llvm/include -I/Users/buildnode/jenkins/workspace/swift-PR-osx/branch-master/buildbot_incremental/llvm-macosx-x86_64/include -I/Users/buildnode/jenkins/workspace/swift-PR-osx/branch-master/llvm-project/clang/include -I/Users/buildnode/jenkins/workspace/swift-PR-osx/branch-master/buildbot_incremental/llvm-macosx-x86_64/tools/clang/include -I/Users/buildnode/jenkins/workspace/swift-PR-osx/branch-master/buildbot_incremental/swift-macosx-x86_64/include -I/Users/buildnode/jenkins/workspace/swift-PR-osx/branch-master/swift/include -I/Users/buildnode/jenkins/workspace/swift-PR-osx/branch-master/llvm-project/lldb/source -I/Applications/Xcode-beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.16.sdk/usr/include/python2.7 -I/Users/buildnode/jenkins/workspace/swift-PR-osx/branch-master/llvm-project/lldb/tools/clang/include -I../clang/include -I/Applications/Xcode-beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.16.sdk/usr/include/libxml2 -I/Users/buildnode/jenkins/workspace/swift-PR-osx/branch-master/llvm-project/lldb/source/. -Wno-unknown-warning-option -Werror=unguarded-availability-new -arch x86_64  -fno-stack-protector -fPIC -fvisibility-inlines-hidden -Werror=date-time -Werror=unguarded-availability-new -w -fdiagnostics-color -Wno-deprecated-declarations -Wno-unknown-pragmas -Wno-strict-aliasing -Wno-deprecated-register -Wno-vla-extension -O3  -isysroot /Applications/Xcode-beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.16.sdk -mmacosx-version-min=10.11   -UNDEBUG  -fno-exceptions -fno-rtti -std=c++14 -MD -MT source/Target/CMakeFiles/lldbTarget.dir/SwiftLanguageRuntimeDynamicTypeResolution.cpp.o -MF source/Target/CMakeFiles/lldbTarget.dir/SwiftLanguageRuntimeDynamicTypeResolution.cpp.o.d -o source/Target/CMakeFiles/lldbTarget.dir/SwiftLanguageRuntimeDynamicTypeResolution.cpp.o -c /Users/buildnode/jenkins/workspace/swift-PR-osx/branch-master/llvm-project/lldb/source/Target/SwiftLanguageRuntimeDynamicTypeResolution.cpp
17:14:59 
/Users/buildnode/jenkins/workspace/swift-PR-osx/branch-master/llvm-project/lldb/source/Target/SwiftLanguageRuntimeDynamicTypeResolution.cpp:642:31: error: no viable conversion from 'llvm::Optional<SwiftASTContextReader>' to 'lldb_private::SwiftASTContextReader'
17:14:59     if (SwiftASTContextReader reader = instance->GetScratchSwiftASTContext())
17:14:59                               ^        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
17:14:59 /Users/buildnode/jenkins/workspace/swift-PR-osx/branch-master/llvm-project/lldb/include/lldb/Core/SwiftASTContextReader.h:103:3: note: candidate constructor not viable: no known conversion from 'llvm::Optional<SwiftASTContextReader>' to 'const lldb_private::SwiftASTContextReader &' for 1st argument
17:14:59   SwiftASTContextReader(const SwiftASTContextReader &copy)
17:14:59   ^
17:14:59 1 error generated.

@lorentey
Copy link
Member

@vedantk It looks like swiftlang/llvm-project#1442 got automerged to llvm-project/master, but it doesn't work there.

@lorentey
Copy link
Member

This will be resolved by swiftlang/llvm-project#1446.

@lorentey
Copy link
Member

@swift-ci test

1 similar comment
@lorentey
Copy link
Member

@swift-ci test

@vedantk
Copy link
Contributor

vedantk commented Jul 14, 2020

@lorentey sorry about that, I wasn't aware of the fact that swift/release/5.3 auto-merges into swift/master for llvm-project. I'll keep this in mind going forward.

@lorentey lorentey merged commit 40d7da4 into swiftlang:master Jul 14, 2020
@NevinBR NevinBR deleted the patch-1 branch July 26, 2020 06:06
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