Skip to content

Update the error message when subscripting String with Int. #27069

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 3 commits into from
Sep 10, 2019
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
4 changes: 2 additions & 2 deletions stdlib/public/core/UnavailableStringAPIs.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,15 @@ extension String {
${stringSubscriptComment}
@available(
*, unavailable,
message: "cannot subscript String with an Int, see the documentation comment for discussion")
message: "cannot subscript String with an Int, use a String.Index instead.")
public subscript(i: Int) -> Character {
Builtin.unreachable()
}

${stringSubscriptComment}
@available(
*, unavailable,
message: "cannot subscript String with an integer range, see the documentation comment for discussion")
message: "cannot subscript String with an integer range, use a String.Index range instead.")
public subscript<R: RangeExpression>(bounds: R) -> String where R.Bound == Int {
Builtin.unreachable()
}
Expand Down
2 changes: 1 addition & 1 deletion test/Constraints/diagnostics.swift
Original file line number Diff line number Diff line change
Expand Up @@ -683,7 +683,7 @@ func r23641896() {
var g = "Hello World"
g.replaceSubrange(0...2, with: "ce") // expected-error {{cannot convert value of type 'ClosedRange<Int>' to expected argument type 'Range<String.Index>'}}

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

}

Expand Down
22 changes: 11 additions & 11 deletions test/stdlib/StringDiagnostics.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,19 @@ import Foundation
// Common pitfall: trying to subscript a string with integers.
func testIntSubscripting(s: String, i: Int) {
// FIXME swift-3-indexing-model: test new overloads of ..<, ...
_ = s[i] // expected-error{{'subscript(_:)' is unavailable: cannot subscript String with an Int, see the documentation comment for discussion}}
_ = s[17] // expected-error{{'subscript(_:)' is unavailable: cannot subscript String with an Int, see the documentation comment for discussion}}
_ = s[i...i] // expected-error{{'subscript(_:)' is unavailable: cannot subscript String with an integer range, see the documentation comment for discussion}}
_ = s[17..<20] // expected-error{{'subscript(_:)' is unavailable: cannot subscript String with an integer range, see the documentation comment for discussion}}
_ = s[17...20] // expected-error{{'subscript(_:)' is unavailable: cannot subscript String with an integer range, see the documentation comment for discussion}}
_ = s[i] // expected-error{{'subscript(_:)' is unavailable: cannot subscript String with an Int, use a String.Index instead.}}
_ = s[17] // expected-error{{'subscript(_:)' is unavailable: cannot subscript String with an Int, use a String.Index instead.}}
_ = s[i...i] // expected-error{{'subscript(_:)' is unavailable: cannot subscript String with an integer range, use a String.Index range instead.}}
_ = s[17..<20] // expected-error{{'subscript(_:)' is unavailable: cannot subscript String with an integer range, use a String.Index range instead.}}
_ = s[17...20] // expected-error{{'subscript(_:)' is unavailable: cannot subscript String with an integer range, use a String.Index range instead.}}

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

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

func testNonAmbiguousStringComparisons() {
Expand Down
6 changes: 3 additions & 3 deletions test/stdlib/UnavailableStringAPIs.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ func test_StringSubscriptByInt(
r1: Range<Int>,
r2: ClosedRange<Int>
) {
_ = x[i] // expected-error {{'subscript(_:)' is unavailable: cannot subscript String with an Int, see the documentation comment for discussion}} {{none}}
_ = x[r1] // expected-error {{'subscript(_:)' is unavailable: cannot subscript String with an integer range, see the documentation comment for discussion}} {{none}}
_ = x[r2] // expected-error {{'subscript(_:)' is unavailable: cannot subscript String with an integer range, see the documentation comment for discussion}} {{none}}
_ = x[i] // expected-error {{'subscript(_:)' is unavailable: cannot subscript String with an Int, use a String.Index instead.}} {{none}}
_ = x[r1] // expected-error {{'subscript(_:)' is unavailable: cannot subscript String with an integer range, use a String.Index range instead.}} {{none}}
_ = x[r2] // expected-error {{'subscript(_:)' is unavailable: cannot subscript String with an integer range, use a String.Index range instead.}} {{none}}
}

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