Skip to content

Commit 7405394

Browse files
committed
Space Engine: check for empty spaces in Space::forConstructor
This is the last optimization performed in Space::simplify, so now we can remove that as well. No intended functionality change.
1 parent 84f17e1 commit 7405394

File tree

1 file changed

+15
-61
lines changed

1 file changed

+15
-61
lines changed

lib/Sema/TypeCheckSwitchStmt.cpp

Lines changed: 15 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -221,10 +221,17 @@ namespace {
221221
}
222222
static Space forConstructor(Type T, Identifier H, bool downgrade,
223223
ArrayRef<Space> SP) {
224+
if (llvm::any_of(SP, std::mem_fn(&Space::isEmpty))) {
225+
// A constructor with an unconstructible parameter can never actually
226+
// be used.
227+
return Space();
228+
}
224229
return Space(T, H, downgrade, SP);
225230
}
226231
static Space forConstructor(Type T, Identifier H, bool downgrade,
227232
std::forward_list<Space> SP) {
233+
// No need to filter SP here; this is only used to copy other
234+
// Constructor spaces.
228235
return Space(T, H, downgrade, SP);
229236
}
230237
static Space forBool(bool C) {
@@ -493,9 +500,11 @@ namespace {
493500
}
494501
}
495502

503+
/// Convenience declaration to make the intersection operation look more
504+
/// symmetric.
496505
static Space intersect(const Space &a, const Space &b, TypeChecker &TC,
497506
const DeclContext *DC) {
498-
return a.intersect(b, TC, DC).simplify(TC, DC);
507+
return a.intersect(b, TC, DC);
499508
}
500509

501510
// Returns the intersection of this space with another. The intersection
@@ -736,13 +745,12 @@ namespace {
736745
PAIRCASE (SpaceKind::Constructor, SpaceKind::UnknownCase): {
737746
SmallVector<Space, 4> newSubSpaces;
738747
for (auto subSpace : this->getSpaces()) {
739-
auto diff = subSpace.minus(other, TC, DC, minusCount);
740-
if (!diff)
748+
auto nextSpace = subSpace.minus(other, TC, DC, minusCount);
749+
if (!nextSpace)
741750
return None;
742-
auto nextSpace = diff->simplify(TC, DC);
743-
if (nextSpace.isEmpty())
751+
if (nextSpace.getValue().isEmpty())
744752
return Space();
745-
newSubSpaces.push_back(nextSpace);
753+
newSubSpaces.push_back(nextSpace.getValue());
746754
}
747755
return Space::forConstructor(this->getType(), this->getHead(),
748756
this->canDowngradeToWarning(),
@@ -947,59 +955,6 @@ namespace {
947955
}
948956
}
949957

950-
// For optimization, attempt to simplify a space by removing any empty
951-
// cases and unpacking empty or singular disjunctions where possible.
952-
Space simplify(TypeChecker &TC, const DeclContext *DC) const {
953-
switch (getKind()) {
954-
case SpaceKind::Constructor: {
955-
// If a constructor has no spaces it is an enum without a payload and
956-
// cannot be optimized further.
957-
if (getSpaces().empty()) {
958-
return *this;
959-
}
960-
961-
// Simplify each component subspace. If, after simplification, any
962-
// subspace contains an empty, then the whole space is empty.
963-
SmallVector<Space, 4> simplifiedSpaces;
964-
for (const auto &space : Spaces) {
965-
auto simplified = space.simplify(TC, DC);
966-
if (simplified.isEmpty())
967-
return Space();
968-
969-
simplifiedSpaces.push_back(simplified);
970-
}
971-
972-
return Space::forConstructor(getType(), Head, canDowngradeToWarning(),
973-
simplifiedSpaces);
974-
}
975-
case SpaceKind::Type: {
976-
// If the decomposition of a space is empty, the space is empty.
977-
if (canDecompose(this->getType(), DC)) {
978-
SmallVector<Space, 4> ss;
979-
decompose(TC, DC, this->getType(), ss);
980-
if (ss.empty()) {
981-
return Space();
982-
}
983-
return *this;
984-
} else {
985-
return *this;
986-
}
987-
}
988-
case SpaceKind::Disjunct: {
989-
// Simplify each disjunct.
990-
SmallVector<Space, 4> simplifiedSpaces;
991-
std::transform(Spaces.begin(), Spaces.end(),
992-
std::back_inserter(simplifiedSpaces),
993-
[&](const Space &el){
994-
return el.simplify(TC, DC);
995-
});
996-
return Space::forDisjunct(simplifiedSpaces);
997-
}
998-
default:
999-
return *this;
1000-
}
1001-
}
1002-
1003958
static bool isSwift3DowngradeExhaustivityCase(TypeChecker &TC,
1004959
const EnumElementDecl *eed){
1005960
if (TC.getLangOpts().isSwiftVersionAtLeast(4))
@@ -1268,7 +1223,7 @@ namespace {
12681223
return;
12691224
}
12701225

1271-
auto uncovered = diff->simplify(TC, DC);
1226+
auto uncovered = diff.getValue();
12721227
if (unknownCase && uncovered.isEmpty()) {
12731228
TC.diagnose(unknownCase->getLoc(), diag::redundant_particular_case)
12741229
.highlight(unknownCase->getSourceRange());
@@ -1279,7 +1234,6 @@ namespace {
12791234
// that are implicitly frozen.
12801235
uncovered = *uncovered.minus(Space::forUnknown(unknownCase == nullptr),
12811236
TC, DC, /*&minusCount*/ nullptr);
1282-
uncovered = uncovered.simplify(TC, DC);
12831237

12841238
if (uncovered.isEmpty())
12851239
return;

0 commit comments

Comments
 (0)