Skip to content

Commit d78136f

Browse files
authored
Merge pull request #14539 from gregomni/6975
2 parents 0fa8706 + bb6f27d commit d78136f

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

lib/Sema/TypeCheckSwitchStmt.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1346,11 +1346,19 @@ namespace {
13461346
auto *IP = cast<IsPattern>(item);
13471347
switch (IP->getCastKind()) {
13481348
case CheckedCastKind::Coercion:
1349-
case CheckedCastKind::BridgingCoercion:
1349+
case CheckedCastKind::BridgingCoercion: {
1350+
// If the pattern and subpattern types are identical than this is a
1351+
// non-useful cast that we've already warned about, but it also means
1352+
// this pattern itself is a no-op and we should examine the subpattern.
1353+
auto *subPattern = IP->getSubPattern();
1354+
if (subPattern && IP->getType()->isEqual(subPattern->getType()))
1355+
return projectPattern(TC, subPattern, sawDowngradablePattern);
1356+
13501357
// These coercions are irrefutable. Project with the original type
13511358
// instead of the cast's target type to maintain consistency with the
13521359
// scrutinee's type.
13531360
return Space(IP->getType(), Identifier());
1361+
}
13541362
case CheckedCastKind::Unresolved:
13551363
case CheckedCastKind::ValueCast:
13561364
case CheckedCastKind::ArrayDowncast:

test/Sema/exhaustive_switch.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -786,3 +786,19 @@ func checkLiteralTuples() {
786786
default: break
787787
}
788788
}
789+
790+
func sr6975() {
791+
enum E {
792+
case a, b
793+
}
794+
795+
let e = E.b
796+
switch e {
797+
case .a as E: // expected-warning {{'as' test is always true}}
798+
print("a")
799+
case .b: // Valid!
800+
print("b")
801+
case .a: // expected-warning {{case is already handled by previous patterns; consider removing it}}
802+
print("second a")
803+
}
804+
}

0 commit comments

Comments
 (0)