Skip to content

Commit eb04df4

Browse files
author
Dave Abrahams
committed
[stdlib] Eliminate over-constraint on StringProtocol
1 parent d0c0043 commit eb04df4

File tree

3 files changed

+38
-23
lines changed

3 files changed

+38
-23
lines changed

stdlib/public/SDK/Foundation/NSRange.swift

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -105,22 +105,20 @@ extension NSRange {
105105
//===----------------------------------------------------------------------===//
106106

107107
extension NSRange {
108-
public init<R: RangeExpression>(_ rangeExpression: R)
108+
public init<R: RangeExpression>(_ region: R)
109109
where R.Bound: FixedWidthInteger, R.Bound.Stride : SignedInteger {
110-
let range = rangeExpression.relative(to: 0..<R.Bound.max)
111-
let start: Int = numericCast(range.lowerBound)
112-
let end: Int = numericCast(range.upperBound)
113-
self = NSRange(location: start, length: end - start)
110+
let r = region.relative(to: 0..<R.Bound.max)
111+
location = numericCast(r.lowerBound)
112+
length = numericCast(r.count)
114113
}
115114

116-
public init<R: RangeExpression, S: StringProtocol>(_ rangeExpression: R, in string: S)
117-
where R.Bound == String.Index, S.Index == String.Index {
118-
let range = rangeExpression.relative(to: string)
119-
let start = range.lowerBound.samePosition(in: string.utf16)
120-
let end = range.upperBound.samePosition(in: string.utf16)
121-
let location = string.utf16.distance(from: string.utf16.startIndex, to: start)
122-
let length = string.utf16.distance(from: start, to: end)
123-
self = NSRange(location: location, length: length)
115+
public init<R: RangeExpression, S: StringProtocol>(_ region: R, in target: S)
116+
where R.Bound == S.Index, S.Index == String.Index {
117+
let r = region.relative(to: target)
118+
self = NSRange(
119+
location: r.lowerBound._utf16Index - target.startIndex._utf16Index,
120+
length: r.upperBound._utf16Index - r.lowerBound._utf16Index
121+
)
124122
}
125123

126124
@available(swift, deprecated: 4, renamed: "Range.init(_:)")

stdlib/public/core/String.swift

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,24 @@ import SwiftShims
1515
/// A type that can represent a string as a collection of characters.
1616
public protocol StringProtocol
1717
: RangeReplaceableCollection, BidirectionalCollection,
18-
CustomDebugStringConvertible,
19-
CustomReflectable, CustomPlaygroundQuickLookable,
2018
TextOutputStream, TextOutputStreamable,
2119
LosslessStringConvertible, ExpressibleByStringLiteral,
2220
Hashable, Comparable
2321
where Iterator.Element == Character {
2422

25-
associatedtype UTF8Index
26-
var utf8: String.UTF8View { get }
27-
associatedtype UTF16Index
28-
var utf16: String.UTF16View { get }
29-
associatedtype UnicodeScalarIndex
30-
var unicodeScalars: String.UnicodeScalarView { get }
31-
/*associatedtype CharacterIndex*/
32-
var characters: String.CharacterView { get }
23+
associatedtype UTF8View : /*Bidirectional*/Collection
24+
where UTF8View.Element == UInt8 // Unicode.UTF8.CodeUnit
25+
26+
associatedtype UTF16View : BidirectionalCollection
27+
where UTF16View.Element == UInt16 // Unicode.UTF16.CodeUnit
3328

29+
associatedtype UnicodeScalarView : BidirectionalCollection
30+
where UnicodeScalarView.Element == Unicode.Scalar
31+
32+
var utf8: UTF8View { get }
33+
var utf16: UTF16View { get }
34+
var unicodeScalars: UnicodeScalarView { get }
35+
3436
#if _runtime(_ObjC)
3537
func hasPrefix(_ prefix: String) -> Bool
3638
func hasSuffix(_ prefix: String) -> Bool
@@ -110,6 +112,18 @@ public protocol StringProtocol
110112
) rethrows -> Result
111113
}
112114

115+
extension StringProtocol {
116+
//@available(swift, deprecated: 3.2, obsoleted: 4.0, message: "Please use the StringProtocol itself")
117+
//public var characters: Self { return self }
118+
119+
@available(swift, deprecated: 3.2, obsoleted: 4.0, renamed: "UTF8View.Index")
120+
public typealias UTF8Index = UTF8View.Index
121+
@available(swift, deprecated: 3.2, obsoleted: 4.0, renamed: "UTF16View.Index")
122+
public typealias UTF16Index = UTF16View.Index
123+
@available(swift, deprecated: 3.2, obsoleted: 4.0, renamed: "UnicodeScalarView.Index")
124+
public typealias UnicodeScalarIndex = UnicodeScalarView.Index
125+
}
126+
113127
/// A protocol that provides fast access to a known representation of String.
114128
///
115129
/// Can be used to specialize generic functions that would otherwise end up

validation-test/compiler_crashers_2/0105-sr5050.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
// RUN: not --crash %target-swift-frontend %s -typecheck
22

3+
// See <rdar://32555384>
4+
// XFAIL: *
5+
36
protocol P {}
47

58
func bar(p: P?) {

0 commit comments

Comments
 (0)