Skip to content

[stdlib] fix another accidental infinite-recursion bug #38950

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 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions stdlib/public/core/RangeReplaceableCollection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -757,6 +757,23 @@ extension RangeReplaceableCollection {
self.replaceSubrange(subrange.relative(to: self), with: newElements)
}

// This unavailable default implementation of
// `replaceSubrange<C: Collection>(_: Range<Index>, with: C)` prevents
// incomplete RangeReplaceableCollection implementations from satisfying
// the protocol through the use of the generic convenience implementation
// `replaceSubrange<C: Collection, R: RangeExpression>(_: R, with: C)`,
// If that were the case, at runtime the implementation generic over
// `RangeExpression` would call itself in an infinite recursion
// due to the absence of a better option.
@available(*, unavailable)
@_alwaysEmitIntoClient
public mutating func replaceSubrange<C>(
_ subrange: Range<Index>,
with newElements: C
) where C: Collection, C.Element == Element {
fatalError()
}

/// Removes the elements in the specified subrange from the collection.
///
/// All the elements following the specified position are moved to close the
Expand Down
12 changes: 12 additions & 0 deletions validation-test/stdlib/CollectionDiagnostics.swift
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,18 @@ func subscriptMutableCollectionIgnored() {
ds[3..<5] = goodSlice
}

// expected-error@+2 {{type 'IncompleteRangeReplaceableCollection' does not conform to protocol 'RangeReplaceableCollection'}}
// expected-error@+1 {{unavailable instance method 'replaceSubrange(_:with:)' was used to satisfy a requirement of protocol 'RangeReplaceableCollection'}}
struct IncompleteRangeReplaceableCollection: RangeReplaceableCollection {
var startIndex: Int
var endIndex: Int

func index(after i: Int) -> Int { i+1 }
subscript(position: Int) -> Int { position }

init() { startIndex = 0; endIndex = 0 }
}

// FIXME: Remove -verify-ignore-unknown.
// <unknown>:0: error: unexpected note produced: possibly intended match
// <unknown>:0: error: unexpected note produced: possibly intended match