Skip to content

[4.2 4/30][Sema] SpaceEngine: Improve handling of empty/non-reduced spaces in c… #16316

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
33 changes: 28 additions & 5 deletions lib/Sema/TypeCheckSwitchStmt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -231,11 +231,18 @@ namespace {
return Space(C);
}
static Space forDisjunct(ArrayRef<Space> SP) {
if (SP.empty())
SmallVector<Space, 4> spaces(SP.begin(), SP.end());
spaces.erase(
std::remove_if(spaces.begin(), spaces.end(),
[](const Space &space) { return space.isEmpty(); }),
spaces.end());

if (spaces.empty())
return Space();
if (SP.size() == 1)
return SP.front();
return Space(SP);
if (spaces.size() == 1)
return spaces.front();

return Space(spaces);
}

bool operator==(const Space &other) const {
Expand Down Expand Up @@ -805,7 +812,23 @@ namespace {
// into each parameter.
SmallVector<Space, 4> copyParams(this->getSpaces().begin(),
this->getSpaces().end());
copyParams[idx] = s1.minus(s2, TC, DC);

auto reducedSpace = s1.minus(s2, TC, DC);
// If one of the constructor parameters is empty it means
// the whole constructor space is empty as well, so we can
// safely skip it.
if (reducedSpace.isEmpty())
continue;

// If reduced produced the same space as original one, we
// should return it directly instead of trying to create
// a disjunction of its sub-spaces because nothing got reduced.
// This is especially helpful when dealing with `unknown` case
// in parameter positions.
if (s1 == reducedSpace)
return *this;

copyParams[idx] = reducedSpace;
Space CS = Space::forConstructor(this->getType(), this->getHead(),
this->canDowngradeToWarning(),
copyParams);
Expand Down
61 changes: 61 additions & 0 deletions test/Sema/rdar39710335.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// RUN: %target-typecheck-verify-swift

enum E1 {
case a1
case b1
case c1
case d1
case e1
case f1
}

enum E2 {
case a2, b2, c2, d2
}

func foo(s: E1, style: E2) {
switch (s, style) {
case (.a1, .a2),
(.a1, .d2),
(.c1, .a2),
(.c1, .d2),
(.c1, .c2),
(.a1, .c2):
break

case (.a1, .b2),
(.b1, .b2),
(.c1, .b2):
break

case (.b1, .a2),
(.b1, .d2),
(.b1, .c2):
break

case (.e1, .a2),
(.e1, .d2),
(.e1, .c2):
break

case (.e1, .b2):
break

case (.d1, .a2),
(.d1, .d2):

break

case (.d1, .b2):
break

case (.d1, .c2):
break

case (.f1, .a2),
(.f1, .b2),
(.f1, .c2),
(.f1, .d2):
break
}
}