Skip to content

[stdlib] prevent MutableCollections from inappropriately inheriting a Slice<Self> subscript #38509

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 8 commits into from
Aug 2, 2021

Conversation

glessard
Copy link
Contributor

@glessard glessard commented Jul 20, 2021

  • adds a conditional protocol extension that defines MutableCollection.MutableCollection.subscript(bounds: Range<_>) -> Slice<Self> only when SubSequence == Slice<Self>.

  • attempts to mark the unconditional extension method MutableCollection.subscript(bounds: Range<_>) -> Slice<Self> as unavailable

  • MutableCollections where SubSequence == Slice<Self> still get the expected default implementation.

  • adds a default implementation of MutableCollection.subscript(bounds: Range<_>) -> SubSequence, marked unavailable, in order to prevent invalid conformances from compiling when SubSequence is not Slice<Self>.

Resolves SR-14850 / rdar://79898408 (and completes the fix to SR-14848).

glessard added 3 commits July 20, 2021 13:25
- add a default implementation of MutableCollection’s
  subscript(bounds: Range<_>) with the most general signature possible
- it is marked unavailable in order to prevent the
  infinite recursion bug reported in SR-14848
- add a conditionally-available subscript(bounds: Range<_>) -> Slice<Self>
  only when Subsequence is Slice<Self>. This will supersede
  the unconditional extension that provides the same signature.
@glessard
Copy link
Contributor Author

Is there a wizard of availability annotations to help figure out what's needed in the last of these commits?

@glessard glessard changed the title [wip][stdlib] prevent MutableCollections from always inheriting a Slice<Self> subscript [wip][stdlib] prevent MutableCollections from inappropriately inheriting a Slice<Self> subscript Jul 21, 2021
@glessard glessard changed the title [wip][stdlib] prevent MutableCollections from inappropriately inheriting a Slice<Self> subscript [stdlib] prevent MutableCollections from inappropriately inheriting a Slice<Self> subscript Jul 21, 2021
@glessard
Copy link
Contributor Author

@swift-ci please smoke test

@glessard glessard requested a review from slavapestov July 21, 2021 22:25
@glessard
Copy link
Contributor Author

glessard commented Jul 21, 2021

@slavapestov I'd like your opinion on availability annotation and ABI impact of these changes.

@glessard
Copy link
Contributor Author

@swift-ci please smoke test

2 similar comments
@glessard
Copy link
Contributor Author

@swift-ci please smoke test

@glessard
Copy link
Contributor Author

@swift-ci please smoke test

@glessard
Copy link
Contributor Author

@swift-ci please smoke test

@glessard
Copy link
Contributor Author

@swift-ci please smoke test macOS platform

@glessard
Copy link
Contributor Author

As it is, this change is tripping up the api-digester test. I believe that's the not-breaking-things test, so an adjustment is clearly needed.

@lorentey
Copy link
Member

lorentey commented Jul 28, 2021

The source/abi compatibility tests aren't perfect -- they sometimes report false positives. We also know that this is technically a source breaking change, so I'd expect we'll need to add at least a couple new expected differences.

Source compat diff

--- /Users/buildnode/jenkins/workspace/swift-PR-macos-smoke-test/branch-main/buildbot_incremental/swift-macosx-x86_64/test-macosx-x86_64/api-digester/Output/stability-stdlib-source.swift.tmp.tmp/stability-stdlib-source.swift.expected	2021-07-23 13:23:30.000000000 -0700
+++ /Users/buildnode/jenkins/workspace/swift-PR-macos-smoke-test/branch-main/buildbot_incremental/swift-macosx-x86_64/test-macosx-x86_64/api-digester/Output/stability-stdlib-source.swift.tmp.tmp/changes.txt.tmp	2021-07-23 13:23:30.000000000 -0700
@@ -1,4 +1,7 @@
+Accessor MutableCollection.subscript(_:).Get() has generic signature change from <Self where Self : Swift.MutableCollection> to <Self where Self : Swift.MutableCollection, Self.SubSequence == Swift.Slice<Self>>
+Accessor MutableCollection.subscript(_:).Set() has generic signature change from <Self where Self : Swift.MutableCollection> to <Self where Self : Swift.MutableCollection, Self.SubSequence == Swift.Slice<Self>>
+Subscript MutableCollection.subscript(_:) has generic signature change from <Self where Self : Swift.MutableCollection> to <Self where Self : Swift.MutableCollection, Self.SubSequence == Swift.Slice<Self>>

These are good -- the test correctly caught that we added an extra constraint on the Slice-returning subscript. It is a source compat break we've accepted -- so we'll just need to add these three lines to the expected failure list.

ABI diffs

--- /Users/buildnode/jenkins/workspace/swift-PR-macos-smoke-test/branch-main/buildbot_incremental/swift-macosx-x86_64/test-macosx-x86_64/api-digester/Output/stability-stdlib-abi-with-asserts.test.tmp.tmp/stability-stdlib-abi.swift.expected.sorted	2021-07-23 13:23:31.000000000 -0700
+++ /Users/buildnode/jenkins/workspace/swift-PR-macos-smoke-test/branch-main/buildbot_incremental/swift-macosx-x86_64/test-macosx-x86_64/api-digester/Output/stability-stdlib-abi-with-asserts.test.tmp.tmp/changes.txt.tmp	2021-07-23 13:23:31.000000000 -0700
@@ -1,3 +1,4 @@
+Accessor _SmallString.subscript(_:).Modify() is a new API without @available attribute

This one is interesting -- it looks like the compiler generated an implicit _modify accessor for the smol string subscript. I did not know the compiler did that!

If true, then this is indeed an ABI break that needs to be fixed. A quick patch is to add an explicit _modify with an @_alwaysEmitIntoClient attribute.

@glessard
Copy link
Contributor Author

@swift-ci please smoke test macOS platform

@glessard
Copy link
Contributor Author

@swift-ci please smoke test

@glessard
Copy link
Contributor Author

@swift-ci please smoke test

@glessard
Copy link
Contributor Author

@swift-ci please smoke test macOS platform

@glessard
Copy link
Contributor Author

@swift-ci please smoke test Linux platform

@glessard
Copy link
Contributor Author

@lorentey Thanks for confirming I wasn't breaking anything I shouldn't break!

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