Skip to content

Commit 524790e

Browse files
Suyash SrijanSuyash Srijan
authored andcommitted
[typechecker] remove call to getAllConformances() and add some new test cases
1 parent 952111b commit 524790e

File tree

2 files changed

+41
-7
lines changed

2 files changed

+41
-7
lines changed

lib/Sema/TypeCheckConstraints.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4040,22 +4040,21 @@ CheckedCastKind TypeChecker::typeCheckCheckedCast(Type fromType,
40404040
if (toType->isAnyObject() || fromType->isAnyObject())
40414041
return CheckedCastKind::ValueCast;
40424042

4043-
// A cast from an existential type to a concrete type does not succeed.
4043+
// A cast from an existential type to a concrete type does not succeed. For
4044+
// example:
40444045
//
40454046
// struct S {}
40464047
// enum FooError: Error { case bar }
4047-
// func foo() throws {
4048+
//
4049+
// func foo() {
40484050
// do {
40494051
// throw FooError.bar
4050-
// } catch is X {
4052+
// } catch is X { /* Will always fail */
40514053
// print("Caught bar error")
40524054
// }
40534055
// }
40544056
if (fromExistential) {
40554057
if (auto NTD = toType->getAnyNominal()) {
4056-
if (NTD->getAllConformances().empty()) {
4057-
return failed();
4058-
}
40594058

40604059
auto protocolDecl =
40614060
dyn_cast_or_null<ProtocolDecl>(fromType->getAnyNominal());

test/stmt/errors.swift

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,16 +175,51 @@ enum SR_6400_E: Error {
175175
struct SR_6400_S_1 {}
176176
struct SR_6400_S_2: Error {}
177177

178+
protocol SR_6400_FakeApplicationDelegate: AnyObject {}
179+
class SR_6400_FakeViewController {}
180+
178181
func sr_6400() throws {
179182
do {
180183
throw SR_6400_E.castError
181184
} catch is SR_6400_S_1 { // expected-warning {{cast from 'Error' to unrelated type 'SR_6400_S_1' always fails}}
182185
print("Caught error")
183186
}
184-
187+
185188
do {
186189
throw SR_6400_E.castError
187190
} catch is SR_6400_S_2 {
188191
print("Caught error") // Ok
189192
}
190193
}
194+
195+
func sr_6400_1<T>(error: Error, as type: T.Type) -> Bool {
196+
return (error as? T) != nil // Ok
197+
}
198+
199+
func sr_6400_2(error: Error) {
200+
_ = error as? (SR_6400_FakeViewController & Error) // Ok
201+
}
202+
func sr_6400_3(error: Error) {
203+
_ = error as? (Error & SR_6400_FakeApplicationDelegate) // Ok
204+
}
205+
206+
class SR_6400_A {}
207+
class SR_6400_B: SR_6400_FakeApplicationDelegate & Error {}
208+
209+
func sr_6400_4() {
210+
do {
211+
throw SR_6400_E.castError
212+
} catch let error as SR_6400_A { // expected-warning {{cast from 'Error' to unrelated type 'SR_6400_A' always fails}} // expected-warning {{immutable value 'error' was never used; consider replacing with '_' or removing it}}
213+
print("Foo")
214+
} catch {
215+
print("Bar")
216+
}
217+
218+
do {
219+
throw SR_6400_E.castError
220+
} catch let error as SR_6400_B { // expected-warning {{immutable value 'error' was never used; consider replacing with '_' or removing it}}
221+
print("Foo")
222+
} catch {
223+
print("Bar")
224+
}
225+
}

0 commit comments

Comments
 (0)