Skip to content

Commit a4339a0

Browse files
committed
Treat irrefutable casts as irrefutable patterns.
1 parent 154e07c commit a4339a0

File tree

3 files changed

+43
-2
lines changed

3 files changed

+43
-2
lines changed

lib/Sema/TypeCheckSwitchStmt.cpp

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1071,8 +1071,25 @@ namespace {
10711071
auto *BP = cast<BoolPattern>(item);
10721072
return Space(BP->getValue());
10731073
}
1074+
case PatternKind::Is: {
1075+
auto *IP = cast<IsPattern>(item);
1076+
switch (IP->getCastKind()) {
1077+
case CheckedCastKind::Coercion:
1078+
case CheckedCastKind::BridgingCoercion:
1079+
// These coercions are irrefutable. Project with the original type
1080+
// instead of the cast's target type to maintain consistency with the
1081+
// scrutinee's type.
1082+
return Space(IP->getType());
1083+
case CheckedCastKind::Unresolved:
1084+
case CheckedCastKind::ValueCast:
1085+
case CheckedCastKind::ArrayDowncast:
1086+
case CheckedCastKind::DictionaryDowncast:
1087+
case CheckedCastKind::SetDowncast:
1088+
case CheckedCastKind::Swift3BridgingDowncast:
1089+
return Space();
1090+
}
1091+
}
10741092
case PatternKind::Typed:
1075-
case PatternKind::Is:
10761093
case PatternKind::Expr:
10771094
return Space();
10781095
case PatternKind::Var: {
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk) -typecheck -verify %s
2+
// REQUIRES: objc_interop
3+
4+
import Foundation
5+
6+
// Treat irrefutable casts as irrefutable patterns.
7+
enum Castbah {
8+
case shareef(NSInteger)
9+
case dont(NSString)
10+
case like(Int)
11+
case it(Error)
12+
}
13+
14+
func rock(the c : Castbah) {
15+
switch (c, c, c) {
16+
case (.shareef(let rock as NSObject), .dont(let the as String), .like(let castbah as Any)):
17+
print(rock, the, castbah)
18+
case (.it(let e as NSError), _, _):
19+
print(e)
20+
case let obj as Any:
21+
print(obj)
22+
}
23+
}

test/stmt/statements.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,8 @@ func test_is_as_patterns() {
384384
switch 4 {
385385
case is Int: break // expected-warning {{'is' test is always true}}
386386
case _ as Int: break // expected-warning {{'as' test is always true}}
387-
case _: break
387+
// expected-warning@-1 {{case is already handled by previous patterns; consider removing it}}
388+
case _: break // expected-warning {{case is already handled by previous patterns; consider removing it}}
388389
}
389390
}
390391

0 commit comments

Comments
 (0)