Skip to content

Commit c4c8cc3

Browse files
committed
[stdlib] Fixed typo in UnavailableStringAPIs
Added test cases for expected-errors and validating that suggested fix works.
1 parent 1d03d16 commit c4c8cc3

File tree

2 files changed

+84
-1
lines changed

2 files changed

+84
-1
lines changed

stdlib/public/core/UnavailableStringAPIs.swift.gyb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ ${stringSubscriptComment}
112112

113113
% for View in ['UTF8View', 'UTF16View', 'UnicodeScalarView', 'CharacterView']:
114114
% Index = 'String.%s.Index' % View
115-
% Distance = 'String.%s.Index' % View
115+
% Distance = 'String.%s.IndexDistance' % View
116116
extension ${Index} {
117117
@available(
118118
*, unavailable,
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
// RUN: rm -rf %t
2+
// RUN: mkdir -p %t
3+
// RUN: %gyb -D_runtime=%target-runtime %s -o %t/main.swift
4+
// RUN: %line-directive %t/main.swift -- %target-swift-frontend -parse -verify %t/main.swift
5+
6+
func test_StringSubscriptByInt(
7+
x: String,
8+
i: Int,
9+
r1: Range<Int>,
10+
r2: ClosedRange<Int>,
11+
r3: CountableRange<Int>,
12+
r4: CountableClosedRange<Int>
13+
) {
14+
_ = x[i] // expected-error {{'subscript' is unavailable: cannot subscript String with an Int, see the documentation comment for discussion}} {{none}}
15+
_ = x[r1] // expected-error {{'subscript' is unavailable: cannot subscript String with a Range<Int>, see the documentation comment for discussion}} {{none}}
16+
_ = x[r2] // expected-error {{'subscript' is unavailable: cannot subscript String with a ClosedRange<Int>, see the documentation comment for discussion}} {{none}}
17+
_ = x[r3] // expected-error {{'subscript' is unavailable: cannot subscript String with a CountableRange<Int>, see the documentation comment for discussion}} {{none}}
18+
_ = x[r4] // expected-error {{'subscript' is unavailable: cannot subscript String with a CountableClosedRange<Int>, see the documentation comment for discussion}} {{none}}
19+
_ = x.count // expected-error {{'count' is unavailable: there is no universally good answer, see the documentation comment for discussion}} {{none}}
20+
}
21+
22+
% if _runtime == 'objc':
23+
func test_UTF16ViewSubscriptByInt(x: String.UTF16View, i: Int, r: Range<Int>) {
24+
_ = x[i] // expected-error {{'subscript' is unavailable: Indexing a String's UTF16View requires a String.UTF16View.Index, which can be constructed from Int when Foundation is imported}} {{none}}
25+
_ = x[r] // expected-error {{'subscript' is unavailable: Slicing a String's UTF16View requires a Range<String.UTF16View.Index>, String.UTF16View.Index can be constructed from Int when Foundation is imported}} {{none}}
26+
}
27+
% end
28+
29+
func test_UTF8View(s: String.UTF8View, i: String.UTF8View.Index, d: Int) {
30+
_ = i.successor() // expected-error {{'successor()' is unavailable: To get the next index call 'index(after:)' on the UTF8View instance that produced the index.}} {{none}}
31+
_ = i.predecessor() // expected-error {{value of type 'String.UTF8View.Index' has no member 'predecessor'}} {{none}}
32+
_ = i.advancedBy(d) // expected-error {{'advancedBy' is unavailable: To advance an index by n steps call 'index(_:offsetBy:)' on the UTF8View instance that produced the index.}} {{none}}
33+
_ = i.advancedBy(d, limit: i) // expected-error {{'advancedBy(_:limit:)' is unavailable: To advance an index by n steps stopping at a given limit call 'index(_:offsetBy:limitedBy:)' on UTF8View instance that produced the index. Note that the Swift 3 API returns 'nil' when trying to advance past the limit; the Swift 2 API returned the limit.}} {{none}}
34+
_ = i.distanceTo(i) // expected-error {{'distanceTo' is unavailable: To find the distance between two indices call 'distance(from:to:)' on the UTF8View instance that produced the index.}} {{none}}
35+
36+
_ = s.index(after: i) // OK
37+
_ = s.index(before: i) // expected-error {{before:}}
38+
_ = s.index(i, offsetBy: d) // OK
39+
_ = s.index(i, offsetBy: d, limitedBy: i) // OK
40+
_ = s.distance(from: i, to: i) // OK
41+
}
42+
43+
func test_UTF16View(s: String.UTF16View, i: String.UTF16View.Index, d: Int) {
44+
_ = i.successor() // expected-error {{'successor()' is unavailable: To get the next index call 'index(after:)' on the UTF16View instance that produced the index.}} {{none}}
45+
_ = i.predecessor() // expected-error {{'predecessor()' is unavailable: To get the previous index call 'index(before:)' on the UTF16View instance that produced the index.}} {{none}}
46+
_ = i.advancedBy(d) // expected-error {{'advancedBy' is unavailable: To advance an index by n steps call 'index(_:offsetBy:)' on the UTF16View instance that produced the index.}} {{none}}
47+
_ = i.advancedBy(d, limit: i) // expected-error {{'advancedBy(_:limit:)' is unavailable: To advance an index by n steps stopping at a given limit call 'index(_:offsetBy:limitedBy:)' on UTF16View instance that produced the index. Note that the Swift 3 API returns 'nil' when trying to advance past the limit; the Swift 2 API returned the limit.}} {{none}}
48+
_ = i.distanceTo(i) // expected-error {{'distanceTo' is unavailable: To find the distance between two indices call 'distance(from:to:)' on the UTF16View instance that produced the index.}} {{none}}
49+
50+
_ = s.index(after: i) // OK
51+
_ = s.index(before: i) // OK
52+
_ = s.index(i, offsetBy: d) // OK
53+
_ = s.index(i, offsetBy: d, limitedBy: i) // OK
54+
_ = s.distance(from: i, to: i) // OK
55+
}
56+
57+
func test_UnicodeScalarView(s: String.UnicodeScalarView, i: String.UnicodeScalarView.Index, d: Int) {
58+
_ = i.successor() // expected-error {{'successor()' is unavailable: To get the next index call 'index(after:)' on the UnicodeScalarView instance that produced the index.}} {{none}}
59+
_ = i.predecessor() // expected-error {{'predecessor()' is unavailable: To get the previous index call 'index(before:)' on the UnicodeScalarView instance that produced the index.}} {{none}}
60+
_ = i.advancedBy(d) // expected-error {{'advancedBy' is unavailable: To advance an index by n steps call 'index(_:offsetBy:)' on the UnicodeScalarView instance that produced the index.}} {{none}}
61+
_ = i.advancedBy(d, limit: i) // expected-error {{'advancedBy(_:limit:)' is unavailable: To advance an index by n steps stopping at a given limit call 'index(_:offsetBy:limitedBy:)' on UnicodeScalarView instance that produced the index. Note that the Swift 3 API returns 'nil' when trying to advance past the limit; the Swift 2 API returned the limit.}} {{none}}
62+
_ = i.distanceTo(i) // expected-error {{'distanceTo' is unavailable: To find the distance between two indices call 'distance(from:to:)' on the UnicodeScalarView instance that produced the index.}} {{none}}
63+
64+
_ = s.index(after: i) // OK
65+
_ = s.index(before: i) // OK
66+
_ = s.index(i, offsetBy: d) // OK
67+
_ = s.index(i, offsetBy: d, limitedBy: i) // OK
68+
_ = s.distance(from: i, to: i) // OK
69+
}
70+
71+
func test_CharacterView(s: String.CharacterView, i: String.CharacterView.Index, d: Int) {
72+
_ = i.successor() // expected-error {{'successor()' is unavailable: To get the next index call 'index(after:)' on the CharacterView instance that produced the index.}} {{none}}
73+
_ = i.predecessor() // expected-error {{'predecessor()' is unavailable: To get the previous index call 'index(before:)' on the CharacterView instance that produced the index.}} {{none}}
74+
_ = i.advancedBy(d) // expected-error {{'advancedBy' is unavailable: To advance an index by n steps call 'index(_:offsetBy:)' on the CharacterView instance that produced the index.}} {{none}}
75+
_ = i.advancedBy(d, limit: i) // expected-error {{'advancedBy(_:limit:)' is unavailable: To advance an index by n steps stopping at a given limit call 'index(_:offsetBy:limitedBy:)' on CharacterView instance that produced the index. Note that the Swift 3 API returns 'nil' when trying to advance past the limit; the Swift 2 API returned the limit.}} {{none}}
76+
_ = i.distanceTo(i) // expected-error {{'distanceTo' is unavailable: To find the distance between two indices call 'distance(from:to:)' on the CharacterView instance that produced the index.}} {{none}}
77+
78+
_ = s.index(after: i) // OK
79+
_ = s.index(before: i) // OK
80+
_ = s.index(i, offsetBy: d) // OK
81+
_ = s.index(i, offsetBy: d, limitedBy: i) // OK
82+
_ = s.distance(from: i, to: i) // OK
83+
}

0 commit comments

Comments
 (0)