Skip to content

Commit 0586bc6

Browse files
authored
Merge pull request #6244 from slavapestov/ban-existential-nested-type-in-switch-swift4
Sema: Ban existentials with associated types from appearing in 'switch' patterns
2 parents d15c448 + 4b0a9e1 commit 0586bc6

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

lib/Sema/MiscDiagnostics.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3097,6 +3097,10 @@ static void checkSwitch(TypeChecker &TC, const SwitchStmt *stmt) {
30973097
// We want to warn about "case .Foo, .Bar where 1 != 100:" since the where
30983098
// clause only applies to the second case, and this is surprising.
30993099
for (auto cs : stmt->getCases()) {
3100+
// We forgot to do this in Swift 3
3101+
if (!TC.Context.isSwiftVersion3())
3102+
TC.checkUnsupportedProtocolType(cs);
3103+
31003104
// The case statement can have multiple case items, each can have a where.
31013105
// If we find a "where", and there is a preceding item without a where, and
31023106
// if they are on the same source line, then warn.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// RUN: %target-swift-frontend -typecheck -verify -swift-version 4 %s
2+
3+
// rdar://29605388 -- Swift 3 admitted opening an existential type with
4+
// associated types in one case.
5+
6+
// See test/IRGen/existential_nested_type.swift for the Swift 3 test.
7+
8+
protocol HasAssoc {
9+
associatedtype A
10+
}
11+
12+
enum MyError : Error {
13+
case bad(Any)
14+
}
15+
16+
func checkIt(_ js: Any) throws {
17+
switch js {
18+
case let dbl as HasAssoc: // expected-error {{protocol 'HasAssoc' can only be used as a generic constraint because it has Self or associated type requirements}}
19+
throw MyError.bad(dbl)
20+
21+
default:
22+
fatalError("wrong")
23+
}
24+
}

0 commit comments

Comments
 (0)