Skip to content

Sema: Ban existentials with associated types from appearing in 'switch' patterns #6244

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
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
4 changes: 4 additions & 0 deletions lib/Sema/MiscDiagnostics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3097,6 +3097,10 @@ static void checkSwitch(TypeChecker &TC, const SwitchStmt *stmt) {
// We want to warn about "case .Foo, .Bar where 1 != 100:" since the where
// clause only applies to the second case, and this is surprising.
for (auto cs : stmt->getCases()) {
// We forgot to do this in Swift 3
if (!TC.Context.isSwiftVersion3())
TC.checkUnsupportedProtocolType(cs);

// The case statement can have multiple case items, each can have a where.
// If we find a "where", and there is a preceding item without a where, and
// if they are on the same source line, then warn.
Expand Down
24 changes: 24 additions & 0 deletions test/Sema/existential_nested_type.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// RUN: %target-swift-frontend -typecheck -verify -swift-version 4 %s

// rdar://29605388 -- Swift 3 admitted opening an existential type with
// associated types in one case.

// See test/IRGen/existential_nested_type.swift for the Swift 3 test.

protocol HasAssoc {
associatedtype A
}

enum MyError : Error {
case bad(Any)
}

func checkIt(_ js: Any) throws {
switch js {
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}}
throw MyError.bad(dbl)

default:
fatalError("wrong")
}
}