Skip to content

Commit ce28cde

Browse files
authored
Update the error message when subscripting String with Int. (#27069)
Instead of referring to a doc comment that isn't really accessible to the developer, let's be accurate about what type they should be using here.
1 parent 4c05a35 commit ce28cde

File tree

4 files changed

+17
-17
lines changed

4 files changed

+17
-17
lines changed

stdlib/public/core/UnavailableStringAPIs.swift.gyb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,15 +55,15 @@ extension String {
5555
${stringSubscriptComment}
5656
@available(
5757
*, unavailable,
58-
message: "cannot subscript String with an Int, see the documentation comment for discussion")
58+
message: "cannot subscript String with an Int, use a String.Index instead.")
5959
public subscript(i: Int) -> Character {
6060
Builtin.unreachable()
6161
}
6262

6363
${stringSubscriptComment}
6464
@available(
6565
*, unavailable,
66-
message: "cannot subscript String with an integer range, see the documentation comment for discussion")
66+
message: "cannot subscript String with an integer range, use a String.Index range instead.")
6767
public subscript<R: RangeExpression>(bounds: R) -> String where R.Bound == Int {
6868
Builtin.unreachable()
6969
}

test/Constraints/diagnostics.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -683,7 +683,7 @@ func r23641896() {
683683
var g = "Hello World"
684684
g.replaceSubrange(0...2, with: "ce") // expected-error {{cannot convert value of type 'ClosedRange<Int>' to expected argument type 'Range<String.Index>'}}
685685

686-
_ = g[12] // expected-error {{'subscript(_:)' is unavailable: cannot subscript String with an Int, see the documentation comment for discussion}}
686+
_ = g[12] // expected-error {{'subscript(_:)' is unavailable: cannot subscript String with an Int, use a String.Index instead.}}
687687

688688
}
689689

test/stdlib/StringDiagnostics.swift

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,19 @@ import Foundation
77
// Common pitfall: trying to subscript a string with integers.
88
func testIntSubscripting(s: String, i: Int) {
99
// FIXME swift-3-indexing-model: test new overloads of ..<, ...
10-
_ = s[i] // expected-error{{'subscript(_:)' is unavailable: cannot subscript String with an Int, see the documentation comment for discussion}}
11-
_ = s[17] // expected-error{{'subscript(_:)' is unavailable: cannot subscript String with an Int, see the documentation comment for discussion}}
12-
_ = s[i...i] // expected-error{{'subscript(_:)' is unavailable: cannot subscript String with an integer range, see the documentation comment for discussion}}
13-
_ = s[17..<20] // expected-error{{'subscript(_:)' is unavailable: cannot subscript String with an integer range, see the documentation comment for discussion}}
14-
_ = s[17...20] // expected-error{{'subscript(_:)' is unavailable: cannot subscript String with an integer range, see the documentation comment for discussion}}
10+
_ = s[i] // expected-error{{'subscript(_:)' is unavailable: cannot subscript String with an Int, use a String.Index instead.}}
11+
_ = s[17] // expected-error{{'subscript(_:)' is unavailable: cannot subscript String with an Int, use a String.Index instead.}}
12+
_ = s[i...i] // expected-error{{'subscript(_:)' is unavailable: cannot subscript String with an integer range, use a String.Index range instead.}}
13+
_ = s[17..<20] // expected-error{{'subscript(_:)' is unavailable: cannot subscript String with an integer range, use a String.Index range instead.}}
14+
_ = s[17...20] // expected-error{{'subscript(_:)' is unavailable: cannot subscript String with an integer range, use a String.Index range instead.}}
1515

16-
_ = s[Range(i...i)] // expected-error{{'subscript(_:)' is unavailable: cannot subscript String with an integer range, see the documentation comment for discussion}}
17-
_ = s[Range(17..<20)] // expected-error{{'subscript(_:)' is unavailable: cannot subscript String with an integer range, see the documentation comment for discussion}}
18-
_ = s[Range(17...20)] // expected-error{{'subscript(_:)' is unavailable: cannot subscript String with an integer range, see the documentation comment for discussion}}
16+
_ = s[Range(i...i)] // expected-error{{'subscript(_:)' is unavailable: cannot subscript String with an integer range, use a String.Index range instead.}}
17+
_ = s[Range(17..<20)] // expected-error{{'subscript(_:)' is unavailable: cannot subscript String with an integer range, use a String.Index range instead.}}
18+
_ = s[Range(17...20)] // expected-error{{'subscript(_:)' is unavailable: cannot subscript String with an integer range, use a String.Index range instead.}}
1919

20-
_ = s[Range(i...i)] // expected-error{{'subscript(_:)' is unavailable: cannot subscript String with an integer range, see the documentation comment for discussion}}
21-
_ = s[Range(17..<20)] // expected-error{{'subscript(_:)' is unavailable: cannot subscript String with an integer range, see the documentation comment for discussion}}
22-
_ = s[Range(17...20)] // expected-error{{'subscript(_:)' is unavailable: cannot subscript String with an integer range, see the documentation comment for discussion}}
20+
_ = s[Range(i...i)] // expected-error{{'subscript(_:)' is unavailable: cannot subscript String with an integer range, use a String.Index range instead.}}
21+
_ = s[Range(17..<20)] // expected-error{{'subscript(_:)' is unavailable: cannot subscript String with an integer range, use a String.Index range instead.}}
22+
_ = s[Range(17...20)] // expected-error{{'subscript(_:)' is unavailable: cannot subscript String with an integer range, use a String.Index range instead.}}
2323
}
2424

2525
func testNonAmbiguousStringComparisons() {

test/stdlib/UnavailableStringAPIs.swift.gyb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ func test_StringSubscriptByInt(
88
r1: Range<Int>,
99
r2: ClosedRange<Int>
1010
) {
11-
_ = x[i] // expected-error {{'subscript(_:)' is unavailable: cannot subscript String with an Int, see the documentation comment for discussion}} {{none}}
12-
_ = x[r1] // expected-error {{'subscript(_:)' is unavailable: cannot subscript String with an integer range, see the documentation comment for discussion}} {{none}}
13-
_ = x[r2] // expected-error {{'subscript(_:)' is unavailable: cannot subscript String with an integer range, see the documentation comment for discussion}} {{none}}
11+
_ = x[i] // expected-error {{'subscript(_:)' is unavailable: cannot subscript String with an Int, use a String.Index instead.}} {{none}}
12+
_ = x[r1] // expected-error {{'subscript(_:)' is unavailable: cannot subscript String with an integer range, use a String.Index range instead.}} {{none}}
13+
_ = x[r2] // expected-error {{'subscript(_:)' is unavailable: cannot subscript String with an integer range, use a String.Index range instead.}} {{none}}
1414
}
1515

1616
func test_UTF8View(s: String.UTF8View, i: String.UTF8View.Index, d: Int) {

0 commit comments

Comments
 (0)