-
Notifications
You must be signed in to change notification settings - Fork 10.5k
Return nil on applying() failure instead of crashing #26575
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,8 +15,8 @@ | |
@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *) | ||
extension CollectionDifference { | ||
fileprivate func _fastEnumeratedApply( | ||
_ consume: (Change) -> Void | ||
) { | ||
_ consume: (Change) throws -> Void | ||
) rethrows { | ||
let totalRemoves = removals.count | ||
let totalInserts = insertions.count | ||
var enumeratedRemoves = 0 | ||
|
@@ -41,7 +41,7 @@ extension CollectionDifference { | |
preconditionFailure() | ||
} | ||
|
||
consume(change) | ||
try consume(change) | ||
|
||
switch change { | ||
case .remove(_, _, _): | ||
|
@@ -53,6 +53,9 @@ extension CollectionDifference { | |
} | ||
} | ||
|
||
// Error type allows the use of throw to unroll state on application failure | ||
private enum _ApplicationError : Error { case failed } | ||
|
||
extension RangeReplaceableCollection { | ||
/// Applies the given difference to this collection. | ||
/// | ||
|
@@ -66,45 +69,54 @@ extension RangeReplaceableCollection { | |
/// number of changes contained by the parameter. | ||
@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *) | ||
public func applying(_ difference: CollectionDifference<Element>) -> Self? { | ||
var result = Self() | ||
var enumeratedRemoves = 0 | ||
var enumeratedInserts = 0 | ||
var enumeratedOriginals = 0 | ||
var currentIndex = self.startIndex | ||
|
||
func append( | ||
into target: inout Self, | ||
contentsOf source: Self, | ||
from index: inout Self.Index, count: Int | ||
) { | ||
) throws { | ||
let start = index | ||
source.formIndex(&index, offsetBy: count) | ||
if !source.formIndex(&index, offsetBy: count, limitedBy: source.endIndex) { | ||
throw _ApplicationError.failed | ||
} | ||
target.append(contentsOf: source[start..<index]) | ||
} | ||
|
||
difference._fastEnumeratedApply { change in | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This hunk is all whitespace changes with the exception of the addition of a |
||
switch change { | ||
case .remove(offset: let offset, element: _, associatedWith: _): | ||
let origCount = offset - enumeratedOriginals | ||
append(into: &result, contentsOf: self, from: ¤tIndex, count: origCount) | ||
enumeratedOriginals += origCount + 1 // Removal consumes an original element | ||
currentIndex = self.index(after: currentIndex) | ||
enumeratedRemoves += 1 | ||
case .insert(offset: let offset, element: let element, associatedWith: _): | ||
let origCount = (offset + enumeratedRemoves - enumeratedInserts) - enumeratedOriginals | ||
append(into: &result, contentsOf: self, from: ¤tIndex, count: origCount) | ||
result.append(element) | ||
enumeratedOriginals += origCount | ||
enumeratedInserts += 1 | ||
var result = Self() | ||
do { | ||
var enumeratedRemoves = 0 | ||
var enumeratedInserts = 0 | ||
var enumeratedOriginals = 0 | ||
var currentIndex = self.startIndex | ||
try difference._fastEnumeratedApply { change in | ||
switch change { | ||
case .remove(offset: let offset, element: _, associatedWith: _): | ||
let origCount = offset - enumeratedOriginals | ||
try append(into: &result, contentsOf: self, from: ¤tIndex, count: origCount) | ||
if currentIndex == self.endIndex { | ||
// Removing nonexistent element off the end of the collection | ||
throw _ApplicationError.failed | ||
} | ||
enumeratedOriginals += origCount + 1 // Removal consumes an original element | ||
currentIndex = self.index(after: currentIndex) | ||
enumeratedRemoves += 1 | ||
case .insert(offset: let offset, element: let element, associatedWith: _): | ||
let origCount = (offset + enumeratedRemoves - enumeratedInserts) - enumeratedOriginals | ||
try append(into: &result, contentsOf: self, from: ¤tIndex, count: origCount) | ||
result.append(element) | ||
enumeratedOriginals += origCount | ||
enumeratedInserts += 1 | ||
} | ||
_internalInvariant(enumeratedOriginals <= self.count) | ||
} | ||
if currentIndex < self.endIndex { | ||
result.append(contentsOf: self[currentIndex...]) | ||
} | ||
_internalInvariant(enumeratedOriginals <= self.count) | ||
_internalInvariant(result.count == self.count + enumeratedInserts - enumeratedRemoves) | ||
} catch { | ||
return nil | ||
} | ||
let origCount = self.count - enumeratedOriginals | ||
append(into: &result, contentsOf: self, from: ¤tIndex, count: origCount) | ||
|
||
_internalInvariant(currentIndex == self.endIndex) | ||
_internalInvariant(enumeratedOriginals + origCount == self.count) | ||
_internalInvariant(result.count == self.count + enumeratedInserts - enumeratedRemoves) | ||
return result | ||
} | ||
} | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These throws may throw off people who break on all throws. We can fix this later if it proves annoying.