Skip to content

Commit 1dfab98

Browse files
committed
Look through parens to the semantic pattern below
1 parent 39494b2 commit 1dfab98

File tree

1 file changed

+26
-28
lines changed

1 file changed

+26
-28
lines changed

lib/Sema/TypeCheckSwitchStmt.cpp

Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ namespace {
4444
Type = 1 << 1,
4545
Constructor = 1 << 2,
4646
Disjunct = 1 << 3,
47-
BooleanConstant = 1 << 5,
47+
BooleanConstant = 1 << 4,
4848
};
4949

5050
/// A data structure for conveniently pattern-matching on the kinds of
@@ -75,27 +75,26 @@ namespace {
7575
class Space final {
7676
private:
7777
SpaceKind Kind;
78-
Type Ty;
78+
llvm::PointerIntPair<Type, 1, bool> TypeAndVal;
7979
Identifier Head;
8080
std::forward_list<Space> Spaces;
81-
bool Val;
8281

8382
public:
8483
explicit Space(Type T)
85-
: Kind(SpaceKind::Type), Ty(T), Head(Identifier()),
86-
Spaces({}), Val(false) {}
84+
: Kind(SpaceKind::Type), TypeAndVal(T, false), Head(Identifier()),
85+
Spaces({}){}
8786
explicit Space(Type T, Identifier H, SmallVectorImpl<Space> &SP)
88-
: Kind(SpaceKind::Constructor), Ty(T), Head(H),
89-
Spaces(SP.begin(), SP.end()), Val(false) {}
87+
: Kind(SpaceKind::Constructor), TypeAndVal(T, false), Head(H),
88+
Spaces(SP.begin(), SP.end()) {}
9089
explicit Space(SmallVectorImpl<Space> &SP)
91-
: Kind(SpaceKind::Disjunct), Ty(Type()), Head(Identifier()),
92-
Spaces(SP.begin(), SP.end()), Val(false) {}
90+
: Kind(SpaceKind::Disjunct), TypeAndVal(Type(), false),
91+
Head(Identifier()), Spaces(SP.begin(), SP.end()) {}
9392
explicit Space()
94-
: Kind(SpaceKind::Empty), Ty(Type()), Head(Identifier()),
95-
Spaces({}), Val(false) {}
93+
: Kind(SpaceKind::Empty), TypeAndVal(Type(), false), Head(Identifier()),
94+
Spaces({}) {}
9695
explicit Space(bool C)
97-
: Kind(SpaceKind::BooleanConstant), Ty(Type()), Head(Identifier()),
98-
Spaces({}), Val(C) {}
96+
: Kind(SpaceKind::BooleanConstant), TypeAndVal(Type(), C),
97+
Head(Identifier()), Spaces({}) {}
9998

10099
SpaceKind getKind() const { return Kind; }
101100

@@ -107,7 +106,7 @@ namespace {
107106
assert((getKind() == SpaceKind::Type
108107
|| getKind() == SpaceKind::Constructor)
109108
&& "Wrong kind of space tried to access space type");
110-
return Ty;
109+
return TypeAndVal.getPointer();
111110
}
112111

113112
Identifier getHead() const {
@@ -126,7 +125,7 @@ namespace {
126125
bool getBoolValue() const {
127126
assert(getKind() == SpaceKind::BooleanConstant
128127
&& "Wrong kind of space tried to access bool value");
129-
return Val;
128+
return TypeAndVal.getInt();
130129
}
131130

132131
// An optimization that computes if the difference of this space and
@@ -237,11 +236,8 @@ namespace {
237236
return true;
238237
}
239238
PAIRCASE(SpaceKind::Constructor, SpaceKind::Disjunct):
240-
/// H(p1, ..., pn) <= (S1 | ... | Sn) iff (this - other) == [EMPTY]
241-
return this->minus(other).simplify().isEmpty();
242-
243239
PAIRCASE (SpaceKind::BooleanConstant, SpaceKind::Disjunct): {
244-
// Bool <= (S1 | ... | Sn) iff Bool <= S1 || ... || Bool <= Sn
240+
// S <= (S1 | ... | Sn) <= S iff (S <= S1) || ... || (S <= Sn)
245241
for (auto &param : other.getSpaces()) {
246242
if (this->isSubspace(param)) {
247243
return true;
@@ -617,7 +613,7 @@ namespace {
617613
}
618614
break;
619615
case SpaceKind::BooleanConstant:
620-
buffer << ((Val) ? "true" : "false");
616+
buffer << ((getBoolValue()) ? "true" : "false");
621617
break;
622618
case SpaceKind::Constructor: {
623619
if (!Head.empty()) {
@@ -649,7 +645,7 @@ namespace {
649645
break;
650646
case SpaceKind::Type:
651647
if (!normalize) {
652-
Ty->print(buffer);
648+
getType()->print(buffer);
653649
}
654650
buffer << "_";
655651
break;
@@ -680,13 +676,13 @@ namespace {
680676
return Space();
681677
}
682678
}
683-
return Space(Ty, Head, simplifiedSpaces);
679+
return Space(getType(), Head, simplifiedSpaces);
684680
}
685681
case SpaceKind::Type: {
686682
// If the decomposition of a space is empty, the space is empty.
687-
if (canDecompose(Ty)) {
683+
if (canDecompose(this->getType())) {
688684
SmallVector<Space, 4> ss;
689-
decompose(Ty, ss);
685+
decompose(this->getType(), ss);
690686
if (ss.empty()) {
691687
return Space();
692688
}
@@ -811,11 +807,11 @@ namespace {
811807
continue;
812808

813809
auto projection = projectPattern(Ctx, caseItem.getPattern());
814-
spaces.push_back(projection);
815810

816811
// Space is trivially covered with a default clause.
817812
if (caseItem.isDefault())
818813
return;
814+
spaces.push_back(projection);
819815
}
820816
}
821817

@@ -1027,12 +1023,14 @@ namespace {
10271023
}
10281024
case PatternKind::Paren: {
10291025
auto *PP = dyn_cast<ParenPattern>(SP);
1030-
auto *SP = PP->getSubPattern();
1026+
auto *SP = PP->getSemanticsProvidingPattern();
1027+
10311028
// Special Case: A constructor pattern may have all of its payload
10321029
// matched by a single var pattern. Project it like the tuple it
10331030
// really is.
1034-
if (SP->getKind() == PatternKind::Var
1035-
|| SP->getKind() == PatternKind::Any) {
1031+
if (SP->getKind() == PatternKind::Named
1032+
|| SP->getKind() == PatternKind::Any
1033+
|| SP->getKind() == PatternKind::Tuple) {
10361034
if (auto *TTy = SP->getType()->getAs<TupleType>()) {
10371035
for (auto ty : TTy->getElements()) {
10381036
conArgSpace.push_back(Space(ty.getType()));

0 commit comments

Comments
 (0)