Skip to content

Treat irrefutable casts as irrefutable patterns. #9449

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 10, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion lib/Sema/TypeCheckSwitchStmt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1071,8 +1071,25 @@ namespace {
auto *BP = cast<BoolPattern>(item);
return Space(BP->getValue());
}
case PatternKind::Is: {
auto *IP = cast<IsPattern>(item);
switch (IP->getCastKind()) {
case CheckedCastKind::Coercion:
case CheckedCastKind::BridgingCoercion:
// These coercions are irrefutable. Project with the original type
// instead of the cast's target type to maintain consistency with the
// scrutinee's type.
return Space(IP->getType());
case CheckedCastKind::Unresolved:
case CheckedCastKind::ValueCast:
case CheckedCastKind::ArrayDowncast:
case CheckedCastKind::DictionaryDowncast:
case CheckedCastKind::SetDowncast:
case CheckedCastKind::Swift3BridgingDowncast:
return Space();
}
}
case PatternKind::Typed:
case PatternKind::Is:
case PatternKind::Expr:
return Space();
case PatternKind::Var: {
Expand Down
23 changes: 23 additions & 0 deletions test/Sema/exhaustive_switch_objc.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk) -typecheck -verify %s
// REQUIRES: objc_interop

import Foundation

// Treat irrefutable casts as irrefutable patterns.
enum Castbah {
case shareef(NSInteger)
case dont(NSString)
case like(Int)
case it(Error)
}

func rock(the c : Castbah) {
switch (c, c, c) {
case (.shareef(let rock as NSObject), .dont(let the as String), .like(let castbah as Any)):
print(rock, the, castbah)
case (.it(let e as NSError), _, _):
print(e)
case let obj as Any:
print(obj)
}
}
3 changes: 2 additions & 1 deletion test/stmt/statements.swift
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,8 @@ func test_is_as_patterns() {
switch 4 {
case is Int: break // expected-warning {{'is' test is always true}}
case _ as Int: break // expected-warning {{'as' test is always true}}
case _: break
// expected-warning@-1 {{case is already handled by previous patterns; consider removing it}}
case _: break // expected-warning {{case is already handled by previous patterns; consider removing it}}
}
}

Expand Down