Skip to content

[4.0] Treat irrefutable casts as irrefutable patterns. #9469

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 2 commits into from
May 14, 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
34 changes: 33 additions & 1 deletion lib/Sema/TypeCheckSwitchStmt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -251,10 +251,25 @@ namespace {

// Special Case: A constructor pattern may include the head but not
// the payload patterns. In that case the space is covered.
// This also acts to short-circuit comparisons with payload-less
// constructors.
if (other.getSpaces().empty()) {
return true;
}

// If 'this' constructor pattern has no payload and the other space
// does, then 'this' covers more of the space only if the other
// constructor isn't the explicit form.
//
// .case <= .case(_, _, _, ...)
if (this->getSpaces().empty()) {
return std::accumulate(other.getSpaces().begin(),
other.getSpaces().end(),
true, [](bool acc, const Space sp){
return acc && sp.getKind() == SpaceKind::Type;
});
}

// H(a1, ..., an) <= H(b1, ..., bn) iff a1 <= b1 && ... && an <= bn
auto i = this->getSpaces().begin();
auto j = other.getSpaces().begin();
Expand Down Expand Up @@ -1071,8 +1086,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.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// RUN: %target-typecheck-verify-swift

func foo(a: Int?, b: Int?) -> Int {
switch (a, b) {
case (.none, _): return 1
Expand Down Expand Up @@ -286,3 +287,25 @@ func switcheroo(a: XX, b: XX) -> Int {
return 13
}
}

enum PatternCasts {
case one(Any)
case two
}

func checkPatternCasts() {
// Pattern casts with this structure shouldn't warn about duplicate cases.
let x: PatternCasts = .one("One")
switch x {
case .one(let s as String): print(s)
case .one: break
case .two: break
}

// But should warn here.
switch x {
case .one(_): print(s)
case .one: break // expected-warning {{case is already handled by previous patterns; consider removing it}}
case .two: break
}
}
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