Skip to content

Fix issue with expression patterns in switch exhaustivity checking #23804

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
Apr 5, 2019
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
9 changes: 9 additions & 0 deletions lib/Sema/TypeCheckSwitchStmt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1380,6 +1380,7 @@ namespace {
}
}
case PatternKind::Typed:
llvm_unreachable("cannot appear in case patterns");
case PatternKind::Expr:
return Space();
case PatternKind::Var: {
Expand Down Expand Up @@ -1428,6 +1429,8 @@ namespace {
conArgSpace);
}
case PatternKind::Paren: {
// If we've got an extra level of parens, we need to flatten that into
// the enum payload.
auto *PP = dyn_cast<ParenPattern>(SP);
auto *SP = PP->getSemanticsProvidingPattern();

Expand All @@ -1448,6 +1451,12 @@ namespace {
}
} else if (SP->getKind() == PatternKind::Tuple) {
Space argTupleSpace = projectPattern(TC, SP);
// Tuples are modeled as if they are enums with a single, nameless
// case, which means argTupleSpace will either be a Constructor or
// Empty space. If it's empty (i.e. it contributes nothing to the
// overall exhaustiveness), the entire enum case space is empty.
if (argTupleSpace.isEmpty())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Short comment here please.

return Space();
assert(argTupleSpace.getKind() == SpaceKind::Constructor);
conArgSpace.insert(conArgSpace.end(),
argTupleSpace.getSpaces().begin(),
Expand Down
40 changes: 40 additions & 0 deletions test/Compatibility/exhaustive_switch.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1176,3 +1176,43 @@ extension Result where T == NoError {
}
}
}

enum SR10301<T,E> {
case value(T)
case error(E)
}
enum SR10301Error: Error {
case bad
}

func sr10301(_ foo: SR10301<String,(Int,Error)>) {
switch foo {
case .value: return
case .error((_, SR10301Error.bad)): return
case .error((_, let err)):
_ = err
return
}
}

func sr10301_is(_ foo: SR10301<String,(Int,Error)>) {
switch foo {
case .value: return
case .error((_, is SR10301Error)): return
case .error((_, let err)):
_ = err
return
}
}

func sr10301_as(_ foo: SR10301<String,(Int,Error)>) {
switch foo {
case .value: return
case .error((_, let err as SR10301Error)):
_ = err
return
case .error((_, let err)):
_ = err
return
}
}
40 changes: 40 additions & 0 deletions test/Sema/exhaustive_switch.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1135,3 +1135,43 @@ extension Result where T == NoError {
}
}
}

enum SR10301<T,E> {
case value(T)
case error(E)
}
enum SR10301Error: Error {
case bad
}

func sr10301(_ foo: SR10301<String,(Int,Error)>) {
switch foo {
case .value: return
case .error((_, SR10301Error.bad)): return
case .error((_, let err)):
_ = err
return
}
}

func sr10301_is(_ foo: SR10301<String,(Int,Error)>) {
switch foo {
case .value: return
case .error((_, is SR10301Error)): return
case .error((_, let err)):
_ = err
return
}
}

func sr10301_as(_ foo: SR10301<String,(Int,Error)>) {
switch foo {
case .value: return
case .error((_, let err as SR10301Error)):
_ = err
return
case .error((_, let err)):
_ = err
return
}
}