Skip to content

[4.1][stdlib] Eliminate IndexDistance #13344

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

Conversation

airspeedswift
Copy link
Member

  • Explanation: Eliminate the IndexDistance associated type, implementing SE-191.
  • Scope: This touches a large number of types in the standard library, simplifying them to replace all occurrences of IndexDistance with Int. It also introduces some compatibility shims (an IndexDistance typealias equal to Int, and generic conversions for calls using non-Int index distances).
  • Issue: rdar://problem/35940300
  • Reviewed by: @moiseev
  • Risk: Low; this is a pervasive change on all collections but the changes are fairly mechanical and easily reviewed. However, there are no known uses where Int is not already used, and the majority of changes are simplifications of the code to no longer need to perform casts between different distance types.
  • Testing: The existing collection implementations in the standard library are extensively tested. Additionally this passes the compatibility suite, and also passes tests of other collections implemented in Foundation and SwiftPM.

* Eradicate IndexDistance associated type, replacing with Int everywhere

* Consistently use Int for ExistentialCollection’s IndexDistance type.

* Fix test for IndexDistance removal

* Remove a handful of no-longer-needed explicit types

* Add compatibility shims for non-Int index distances

* Test compatibility shim

* Move IndexDistance typealias into the Collection protocol
@airspeedswift airspeedswift requested a review from moiseev December 8, 2017 20:11
@airspeedswift
Copy link
Member Author

@swift-ci please test

@airspeedswift
Copy link
Member Author

@swift-ci please benchmark

@airspeedswift
Copy link
Member Author

@swift-ci please test source compatibility

@airspeedswift
Copy link
Member Author

@swift-ci please smoke compiler performance

@@ -782,6 +776,9 @@ public protocol Collection: Sequence where SubSequence: Collection {
/// - Parameter i: A valid index of the collection. `i` must be less than
/// `endIndex`.
func formIndex(after i: inout Index)

@available(swift, deprecated, message: "all index distances are now of type Int")
typealias IndexDistance = Int
Copy link
Contributor

@moiseev moiseev Dec 8, 2017

Choose a reason for hiding this comment

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

public.

UPD: Oh wait, it's a protocol. Nevermind.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yup. I moved it back and forth between the protocol and the extension multiple times and forgot this every. single. time.

}
*/
@available(*, deprecated, message: "all index distances are now of type Int")
public func formIndex<T: BinaryInteger>(_ i: inout Index, offsetBy n: T, limitedBy limit: Index) -> Bool {
Copy link
Contributor

Choose a reason for hiding this comment

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

I think formIndex should have a @discardableResult attribute, otherwise it will issue 2 warnings.

Copy link
Member Author

Choose a reason for hiding this comment

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

eh, they're both legit warnings though

Copy link
Contributor

Choose a reason for hiding this comment

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

Take that back. Turns out we do not have @discardableResult on this method anywhere...

return formIndex(&i, offsetBy: Int(n), limitedBy: limit)
}
@available(*, deprecated, message: "all index distances are now of type Int")
public func distance<T: BinaryInteger>(from start: Index, to end: Index) -> T {
Copy link
Contributor

Choose a reason for hiding this comment

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

Surprised it does not result in ambiguity in expressions like let d = xs.distance(from: someIndex, to: someOtherIndex).

Copy link
Member Author

Choose a reason for hiding this comment

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

The other distance method isn't generic (T is Int) so it wins.

Copy link
Member Author

Choose a reason for hiding this comment

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

Isn't generic other than on the protocol, that is...

@@ -2898,6 +2898,9 @@ public struct ${Self}
: FixedWidthInteger, ${Unsigned}Integer,
_ExpressibleByBuiltinIntegerLiteral {

public typealias IntegerLiteralType = ${Self}
Copy link
Contributor

Choose a reason for hiding this comment

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

How is this related?

Copy link
Member Author

Choose a reason for hiding this comment

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

Hum. It isn't. I probably meant to do this on my clean-up branch. But since this is a cherry-pick from master where it's merged, probably should just leave it in.

@airspeedswift
Copy link
Member Author

@swift-ci please nominate

@swift-ci
Copy link
Contributor

swift-ci commented Dec 8, 2017

Build comment file:

Build failed before running benchmark.


@airspeedswift airspeedswift merged commit 080e6f1 into swiftlang:swift-4.1-branch Dec 8, 2017
tayloraswift pushed a commit to tayloraswift/swift-evolution that referenced this pull request Oct 24, 2018
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.

3 participants