Skip to content

Commit 6bcb9a9

Browse files
[tests] Adding regression tests for SR-13088
1 parent addc6b8 commit 6bcb9a9

File tree

2 files changed

+72
-3
lines changed

2 files changed

+72
-3
lines changed

lib/Sema/TypeCheckConstraints.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3250,7 +3250,7 @@ CheckedCastKind TypeChecker::typeCheckCheckedCast(Type fromType,
32503250
auto checkElementCast = [&](Type fromElt, Type toElt,
32513251
CheckedCastKind castKind) -> CheckedCastKind {
32523252
// Let's not emit diagnostic when the element type is erased because
3253-
// we can't statically known about if element is convertible.
3253+
// we can't statically known if element is convertible.
32543254
if (fromElt->isAny() || toElt->isAny())
32553255
return castKind;
32563256

test/Constraints/casts.swift

Lines changed: 71 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -235,8 +235,8 @@ func test_tuple_casts_no_warn() {
235235
_ = arr as! [(Foo, Foo, Foo)] // expected-warning {{cast from '[(Any, Any)]' to unrelated type '[(Foo, Foo, Foo)]' always fails}}
236236
_ = tup as! (Foo, Foo, Foo) // expected-warning {{cast from '(Any, Any)' to unrelated type '(Foo, Foo, Foo)' always fails}}
237237

238-
_ = arr as! [(a: Foo, Foo)] // expected-warning {{cast from '[(Any, Any)]' to unrelated type '[(a: Foo, Foo)]' always fails}}
239-
_ = tup as! (a: Foo, Foo) // expected-warning {{cast from '(Any, Any)' to unrelated type '(a: Foo, Foo)' always fails}}
238+
_ = arr as! [(a: Foo, Foo)] // Ok
239+
_ = tup as! (a: Foo, Foo) // Ok
240240
}
241241

242242
infix operator ^^^
@@ -335,3 +335,72 @@ func test_compatibility_coercions(_ arr: [Int], _ optArr: [Int]?, _ dict: [Strin
335335
// The array can also be inferred to be [Any].
336336
_ = ([] ?? []) as Array // expected-warning {{left side of nil coalescing operator '??' has non-optional type '[Any]', so the right side is never used}}
337337
}
338+
339+
// SR-13088
340+
protocol JSON { }
341+
protocol JSONLeaf: JSON {}
342+
extension Int: JSONLeaf { }
343+
extension Array: JSON where Element: JSON { }
344+
345+
protocol SR13035Error: Error {}
346+
struct ChildError: SR13035Error {}
347+
348+
protocol AnyC {
349+
func foo()
350+
}
351+
352+
protocol AnyEvent {}
353+
354+
protocol A {
355+
associatedtype C: AnyC
356+
}
357+
358+
protocol EventA: A {
359+
associatedtype Event
360+
}
361+
362+
typealias Container<Namespace>
363+
= (event: Namespace.Event, c: Namespace.C) where Namespace: EventA
364+
365+
enum ConcreteA: EventA {
366+
struct C: AnyC {
367+
func foo() {}
368+
}
369+
370+
enum Event: AnyEvent {
371+
case test
372+
}
373+
}
374+
375+
func tests_SR13088_false_positive_always_fail_casts() {
376+
// SR-13081
377+
let x: JSON = [4] // [4]
378+
_ = x as? [Any] // Ok
379+
380+
// SR-7187
381+
let a: [Any] = [String?.some("hello") as Any, String?.none as Any]
382+
383+
_ = a is [String?] // Ok
384+
_ = a as? [String?] // Ok
385+
386+
// SR-13035
387+
func SR13035<SomeError: SR13035Error>(_ mockResult: Result<String, ChildError>, _: Result<String, SomeError>) {
388+
let _ = mockResult as? Result<String, SomeError> // Ok
389+
}
390+
391+
// SR-11434 and SR-12321
392+
func encodable(_ value: Encodable) {
393+
_ = value as! [String : Encodable] // Ok
394+
_ = value as? [String: Encodable] // Ok
395+
}
396+
397+
// SR-13025
398+
func coordinate(_ event: AnyEvent, from c: AnyC) {
399+
switch (event, c) {
400+
case let container as Container<ConcreteA>: // OK
401+
container.c.foo()
402+
default:
403+
break
404+
}
405+
}
406+
}

0 commit comments

Comments
 (0)