Skip to content

Commit b7a732e

Browse files
committed
[Diagnostics] NFC: Add a couple of examples of requirement failures related to generic subscripts
1 parent 88c39db commit b7a732e

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

test/Constraints/subscript.swift

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,3 +177,29 @@ func rdar_45825806() {
177177
let s = S()
178178
_ = s[takesPtr: [1, 2, 3]] // Ok
179179
}
180+
181+
func test_generic_subscript_requirements_mismatch_diagnostics() {
182+
struct S {
183+
subscript<T: StringProtocol>(_: T) -> T { // expected-note {{where 'T' = 'Int'}}
184+
fatalError()
185+
}
186+
187+
subscript<T, U: Collection>(v v: [T]) -> U where U.Element == T {
188+
fatalError()
189+
}
190+
191+
subscript<T: Collection>(number num: T) -> Int where T.Element: BinaryInteger {
192+
// expected-note@-1 {{'T.Element' = 'String'}}
193+
return 42
194+
}
195+
}
196+
197+
var s = S()
198+
_ = s[42] // expected-error {{subscript 'subscript(_:)' requires that 'Int' conform to 'StringProtocol'}}
199+
200+
var arr: [Int] = []
201+
let _: [String] = s[v: arr] // expected-error {{cannot convert value of type '[Int]' to expected argument type '[String]'}}
202+
// expected-note@-1 {{arguments to generic parameter 'Element' ('Int' and 'String') are expected to be equal}}
203+
204+
s[number: ["hello"]] // expected-error {{subscript 'subscript(number:)' requires that 'String' conform to 'BinaryInteger'}}
205+
}

0 commit comments

Comments
 (0)