Skip to content

Commit 2371e5c

Browse files
committed
Strip TypeLoc from EnumElementPattern
1 parent fc9070c commit 2371e5c

12 files changed

+159
-126
lines changed

include/swift/AST/Pattern.h

+22-37
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
#include "swift/Basic/LLVM.h"
2525
#include "swift/AST/Type.h"
2626
#include "swift/AST/Types.h"
27-
#include "swift/AST/TypeLoc.h"
2827
#include "swift/AST/TypeAlignments.h"
2928
#include "swift/Basic/InlineBitfield.h"
3029
#include "swift/Basic/OptionSet.h"
@@ -35,6 +34,7 @@ namespace swift {
3534
class Expr;
3635
enum class CheckedCastKind : unsigned;
3736
class TypeExpr;
37+
class TypeLoc;
3838

3939
/// PatternKind - The classification of different kinds of
4040
/// value-matching pattern.
@@ -503,35 +503,31 @@ class IsPattern : public Pattern {
503503
/// case, then the value is extracted. If there is a subpattern, it is then
504504
/// matched against the associated value for the case.
505505
class EnumElementPattern : public Pattern {
506-
TypeLoc ParentType;
506+
TypeExpr *ParentType;
507507
SourceLoc DotLoc;
508508
DeclNameLoc NameLoc;
509509
DeclNameRef Name;
510510
PointerUnion<EnumElementDecl *, Expr*> ElementDeclOrUnresolvedOriginalExpr;
511511
Pattern /*nullable*/ *SubPattern;
512512

513513
public:
514-
EnumElementPattern(TypeLoc ParentType, SourceLoc DotLoc, DeclNameLoc NameLoc,
515-
DeclNameRef Name, EnumElementDecl *Element,
516-
Pattern *SubPattern)
517-
: Pattern(PatternKind::EnumElement),
518-
ParentType(ParentType), DotLoc(DotLoc), NameLoc(NameLoc), Name(Name),
519-
ElementDeclOrUnresolvedOriginalExpr(Element),
520-
SubPattern(SubPattern) { }
514+
EnumElementPattern(TypeExpr *ParentType, SourceLoc DotLoc,
515+
DeclNameLoc NameLoc, DeclNameRef Name,
516+
EnumElementDecl *Element, Pattern *SubPattern)
517+
: Pattern(PatternKind::EnumElement), ParentType(ParentType),
518+
DotLoc(DotLoc), NameLoc(NameLoc), Name(Name),
519+
ElementDeclOrUnresolvedOriginalExpr(Element), SubPattern(SubPattern) {
520+
assert(ParentType && "Missing parent type?");
521+
}
521522

522523
/// Create an unresolved EnumElementPattern for a `.foo` pattern relying on
523524
/// contextual type.
524-
EnumElementPattern(SourceLoc DotLoc,
525-
DeclNameLoc NameLoc,
526-
DeclNameRef Name,
527-
Pattern *SubPattern,
528-
Expr *UnresolvedOriginalExpr)
529-
: Pattern(PatternKind::EnumElement),
530-
ParentType(), DotLoc(DotLoc), NameLoc(NameLoc), Name(Name),
531-
ElementDeclOrUnresolvedOriginalExpr(UnresolvedOriginalExpr),
532-
SubPattern(SubPattern) {
533-
534-
}
525+
EnumElementPattern(SourceLoc DotLoc, DeclNameLoc NameLoc, DeclNameRef Name,
526+
Pattern *SubPattern, Expr *UnresolvedOriginalExpr)
527+
: Pattern(PatternKind::EnumElement), ParentType(nullptr), DotLoc(DotLoc),
528+
NameLoc(NameLoc), Name(Name),
529+
ElementDeclOrUnresolvedOriginalExpr(UnresolvedOriginalExpr),
530+
SubPattern(SubPattern) {}
535531

536532
bool hasSubPattern() const { return SubPattern; }
537533

@@ -543,10 +539,6 @@ class EnumElementPattern : public Pattern {
543539
return SubPattern;
544540
}
545541

546-
bool isParentTypeImplicit() {
547-
return !ParentType.hasLocation();
548-
}
549-
550542
void setSubPattern(Pattern *p) { SubPattern = p; }
551543

552544
DeclNameRef getName() const { return Name; }
@@ -567,21 +559,14 @@ class EnumElementPattern : public Pattern {
567559

568560
DeclNameLoc getNameLoc() const { return NameLoc; }
569561
SourceLoc getLoc() const { return NameLoc.getBaseNameLoc(); }
570-
SourceLoc getStartLoc() const {
571-
return ParentType.hasLocation() ? ParentType.getSourceRange().Start :
572-
DotLoc.isValid() ? DotLoc
573-
: NameLoc.getBaseNameLoc();
574-
}
575-
SourceLoc getEndLoc() const {
576-
if (SubPattern && SubPattern->getSourceRange().isValid()) {
577-
return SubPattern->getSourceRange().End;
578-
}
579-
return NameLoc.getEndLoc();
580-
}
562+
SourceLoc getStartLoc() const;
563+
SourceLoc getEndLoc() const;
581564
SourceRange getSourceRange() const { return {getStartLoc(), getEndLoc()}; }
582565

583-
TypeLoc &getParentType() { return ParentType; }
584-
TypeLoc getParentType() const { return ParentType; }
566+
TypeRepr *getParentTypeRepr() const;
567+
568+
void setParentType(Type ty);
569+
Type getParentType() const;
585570

586571
static bool classof(const Pattern *P) {
587572
return P->getKind() == PatternKind::EnumElement;

lib/AST/ASTDumper.cpp

+1-2
Original file line numberDiff line numberDiff line change
@@ -514,8 +514,7 @@ namespace {
514514
void visitEnumElementPattern(EnumElementPattern *P) {
515515
printCommon(P, "pattern_enum_element");
516516
OS << ' ';
517-
P->getParentType().getType().print(
518-
PrintWithColorRAII(OS, TypeColor).getOS());
517+
P->getParentType().print(PrintWithColorRAII(OS, TypeColor).getOS());
519518
PrintWithColorRAII(OS, IdentifierColor) << '.' << P->getName();
520519
if (P->hasSubPattern()) {
521520
OS << '\n';

lib/AST/ASTWalker.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -1698,8 +1698,8 @@ Pattern *Traversal::visitIsPattern(IsPattern *P) {
16981698
}
16991699

17001700
Pattern *Traversal::visitEnumElementPattern(EnumElementPattern *P) {
1701-
if (!P->isParentTypeImplicit())
1702-
if (doIt(P->getParentType()))
1701+
if (auto *TR = P->getParentTypeRepr())
1702+
if (doIt(TR))
17031703
return nullptr;
17041704

17051705
if (!P->hasSubPattern())

lib/AST/Pattern.cpp

+35
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,7 @@ SourceRange IsPattern::getSourceRange() const {
466466

467467
Type IsPattern::getCastType() const { return CastType->getInstanceType(); }
468468
void IsPattern::setCastType(Type type) {
469+
assert(type);
469470
CastType->setType(MetatypeType::get(type));
470471
}
471472

@@ -479,6 +480,40 @@ ExprPattern::ExprPattern(Expr *e, bool isResolved, Expr *matchExpr,
479480
assert(!matchExpr || e->isImplicit() == matchExpr->isImplicit());
480481
}
481482

483+
SourceLoc EnumElementPattern::getStartLoc() const {
484+
return (ParentType && !ParentType->isImplicit())
485+
? ParentType->getSourceRange().Start
486+
: DotLoc.isValid() ? DotLoc : NameLoc.getBaseNameLoc();
487+
}
488+
489+
SourceLoc EnumElementPattern::getEndLoc() const {
490+
if (SubPattern && SubPattern->getSourceRange().isValid()) {
491+
return SubPattern->getSourceRange().End;
492+
}
493+
return NameLoc.getEndLoc();
494+
}
495+
496+
TypeRepr *EnumElementPattern::getParentTypeRepr() const {
497+
if (!ParentType)
498+
return nullptr;
499+
return ParentType->getTypeRepr();
500+
}
501+
502+
Type EnumElementPattern::getParentType() const {
503+
if (!ParentType)
504+
return Type();
505+
return ParentType->getInstanceType();
506+
}
507+
508+
void EnumElementPattern::setParentType(Type type) {
509+
assert(type);
510+
if (ParentType) {
511+
ParentType->setType(MetatypeType::get(type));
512+
} else {
513+
ParentType = TypeExpr::createImplicit(type, type->getASTContext());
514+
}
515+
}
516+
482517
SourceLoc ExprPattern::getLoc() const {
483518
return getSubExpr()->getLoc();
484519
}

lib/Sema/CSGen.cpp

+9-8
Original file line numberDiff line numberDiff line change
@@ -1481,11 +1481,6 @@ namespace {
14811481
Type resolveTypeReferenceInExpression(TypeRepr *repr,
14821482
TypeResolverContext resCtx) {
14831483
TypeLoc loc(repr);
1484-
return resolveTypeReferenceInExpression(loc, resCtx);
1485-
}
1486-
1487-
Type resolveTypeReferenceInExpression(TypeLoc &loc,
1488-
TypeResolverContext resCtx) {
14891484
TypeResolutionOptions options(resCtx);
14901485
options |= TypeResolutionFlags::AllowUnboundGenerics;
14911486
bool hadError = TypeChecker::validateType(
@@ -2581,10 +2576,16 @@ namespace {
25812576
CS.getConstraintLocator(locator),
25822577
TVO_CanBindToLValue | TVO_CanBindToNoEscape);
25832578
FunctionRefKind functionRefKind = FunctionRefKind::Compound;
2584-
if (!enumPattern->getParentType().isNull()) {
2579+
if (enumPattern->getParentType() || enumPattern->getParentTypeRepr()) {
25852580
// Resolve the parent type.
2586-
Type parentType = resolveTypeReferenceInExpression(
2587-
enumPattern->getParentType(), TypeResolverContext::InExpression);
2581+
Type parentType = [&]() -> Type {
2582+
if (auto preTy = enumPattern->getParentType()) {
2583+
return preTy;
2584+
}
2585+
return resolveTypeReferenceInExpression(
2586+
enumPattern->getParentTypeRepr(),
2587+
TypeResolverContext::InExpression);
2588+
}();
25882589

25892590
if (!parentType)
25902591
return Type();

lib/Sema/DerivedConformanceCodingKey.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -212,8 +212,8 @@ deriveBodyCodingKey_enum_stringValue(AbstractFunctionDecl *strValDecl, void *) {
212212
} else {
213213
SmallVector<ASTNode, 4> cases;
214214
for (auto *elt : elements) {
215-
auto *pat = new (C) EnumElementPattern(TypeLoc::withoutLoc(enumType),
216-
SourceLoc(), DeclNameLoc(),
215+
auto *baseTE = TypeExpr::createImplicit(enumType, C);
216+
auto *pat = new (C) EnumElementPattern(baseTE, SourceLoc(), DeclNameLoc(),
217217
DeclNameRef(), elt, nullptr);
218218
pat->setImplicit();
219219

lib/Sema/DerivedConformanceComparable.cpp

+8-8
Original file line numberDiff line numberDiff line change
@@ -137,20 +137,20 @@ deriveBodyComparable_enum_hasAssociatedValues_lt(AbstractFunctionDecl *ltDecl, v
137137
SmallVector<VarDecl*, 4> lhsPayloadVars;
138138
auto lhsSubpattern = DerivedConformance::enumElementPayloadSubpattern(elt, 'l', ltDecl,
139139
lhsPayloadVars);
140-
auto lhsElemPat = new (C) EnumElementPattern(TypeLoc::withoutLoc(enumType),
141-
SourceLoc(), DeclNameLoc(),
142-
DeclNameRef(), elt,
143-
lhsSubpattern);
140+
auto *lhsBaseTE = TypeExpr::createImplicit(enumType, C);
141+
auto lhsElemPat =
142+
new (C) EnumElementPattern(lhsBaseTE, SourceLoc(), DeclNameLoc(),
143+
DeclNameRef(), elt, lhsSubpattern);
144144
lhsElemPat->setImplicit();
145145

146146
// .<elt>(let r0, let r1, ...)
147147
SmallVector<VarDecl*, 4> rhsPayloadVars;
148148
auto rhsSubpattern = DerivedConformance::enumElementPayloadSubpattern(elt, 'r', ltDecl,
149149
rhsPayloadVars);
150-
auto rhsElemPat = new (C) EnumElementPattern(TypeLoc::withoutLoc(enumType),
151-
SourceLoc(), DeclNameLoc(),
152-
DeclNameRef(), elt,
153-
rhsSubpattern);
150+
auto *rhsBaseTE = TypeExpr::createImplicit(enumType, C);
151+
auto rhsElemPat =
152+
new (C) EnumElementPattern(rhsBaseTE, SourceLoc(), DeclNameLoc(),
153+
DeclNameRef(), elt, rhsSubpattern);
154154
rhsElemPat->setImplicit();
155155

156156
auto hasBoundDecls = !lhsPayloadVars.empty();

lib/Sema/DerivedConformanceEquatableHashable.cpp

+14-15
Original file line numberDiff line numberDiff line change
@@ -259,20 +259,20 @@ deriveBodyEquatable_enum_hasAssociatedValues_eq(AbstractFunctionDecl *eqDecl,
259259
SmallVector<VarDecl*, 3> lhsPayloadVars;
260260
auto lhsSubpattern = DerivedConformance::enumElementPayloadSubpattern(elt, 'l', eqDecl,
261261
lhsPayloadVars);
262-
auto lhsElemPat = new (C) EnumElementPattern(TypeLoc::withoutLoc(enumType),
263-
SourceLoc(), DeclNameLoc(),
264-
DeclNameRef(), elt,
265-
lhsSubpattern);
262+
auto *lhsBaseTE = TypeExpr::createImplicit(enumType, C);
263+
auto lhsElemPat =
264+
new (C) EnumElementPattern(lhsBaseTE, SourceLoc(), DeclNameLoc(),
265+
DeclNameRef(), elt, lhsSubpattern);
266266
lhsElemPat->setImplicit();
267267

268268
// .<elt>(let r0, let r1, ...)
269269
SmallVector<VarDecl*, 3> rhsPayloadVars;
270270
auto rhsSubpattern = DerivedConformance::enumElementPayloadSubpattern(elt, 'r', eqDecl,
271271
rhsPayloadVars);
272-
auto rhsElemPat = new (C) EnumElementPattern(TypeLoc::withoutLoc(enumType),
273-
SourceLoc(), DeclNameLoc(),
274-
DeclNameRef(), elt,
275-
rhsSubpattern);
272+
auto *rhsBaseTE = TypeExpr::createImplicit(enumType, C);
273+
auto rhsElemPat =
274+
new (C) EnumElementPattern(rhsBaseTE, SourceLoc(), DeclNameLoc(),
275+
DeclNameRef(), elt, rhsSubpattern);
276276
rhsElemPat->setImplicit();
277277

278278
auto hasBoundDecls = !lhsPayloadVars.empty();
@@ -748,12 +748,12 @@ deriveBodyHashable_enum_hasAssociatedValues_hashInto(
748748
// case A, B(Int), C(String, Int)
749749
// @derived func hash(into hasher: inout Hasher) {
750750
// switch self {
751-
// case A:
751+
// case .A:
752752
// hasher.combine(0)
753-
// case B(let a0):
753+
// case .B(let a0):
754754
// hasher.combine(1)
755755
// hasher.combine(a0)
756-
// case C(let a0, let a1):
756+
// case .C(let a0, let a1):
757757
// hasher.combine(2)
758758
// hasher.combine(a0)
759759
// hasher.combine(a1)
@@ -783,10 +783,9 @@ deriveBodyHashable_enum_hasAssociatedValues_hashInto(
783783

784784
auto payloadPattern = DerivedConformance::enumElementPayloadSubpattern(elt, 'a', hashIntoDecl,
785785
payloadVars);
786-
auto pat = new (C) EnumElementPattern(TypeLoc::withoutLoc(enumType),
787-
SourceLoc(), DeclNameLoc(),
788-
DeclNameRef(elt->getBaseIdentifier()),
789-
elt, payloadPattern);
786+
auto pat = new (C) EnumElementPattern(
787+
TypeExpr::createImplicit(enumType, C), SourceLoc(), DeclNameLoc(),
788+
DeclNameRef(elt->getBaseIdentifier()), elt, payloadPattern);
790789
pat->setImplicit();
791790

792791
auto labelItem = CaseLabelItem(pat);

lib/Sema/DerivedConformanceRawRepresentable.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,9 @@ deriveBodyRawRepresentable_raw(AbstractFunctionDecl *toRawDecl, void *) {
108108

109109
SmallVector<ASTNode, 4> cases;
110110
for (auto elt : enumDecl->getAllElements()) {
111-
auto pat = new (C) EnumElementPattern(TypeLoc::withoutLoc(enumType),
112-
SourceLoc(), DeclNameLoc(),
113-
DeclNameRef(), elt, nullptr);
111+
auto pat = new (C)
112+
EnumElementPattern(TypeExpr::createImplicit(enumType, C), SourceLoc(),
113+
DeclNameLoc(), DeclNameRef(), elt, nullptr);
114114
pat->setImplicit();
115115

116116
auto labelItem = CaseLabelItem(pat);

lib/Sema/DerivedConformances.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -548,9 +548,9 @@ DeclRefExpr *DerivedConformance::convertEnumToIndex(SmallVectorImpl<ASTNode> &st
548548
SmallVector<ASTNode, 4> cases;
549549
for (auto elt : enumDecl->getAllElements()) {
550550
// generate: case .<Case>:
551-
auto pat = new (C) EnumElementPattern(TypeLoc::withoutLoc(enumType),
552-
SourceLoc(), DeclNameLoc(),
553-
DeclNameRef(), elt, nullptr);
551+
auto pat = new (C)
552+
EnumElementPattern(TypeExpr::createImplicit(enumType, C), SourceLoc(),
553+
DeclNameLoc(), DeclNameRef(), elt, nullptr);
554554
pat->setImplicit();
555555
pat->setType(enumType);
556556

0 commit comments

Comments
 (0)