Skip to content

Commit e180ee9

Browse files
committed
[NFC] MemberTypeRepr: Tolerate zero member components in factory constructors
1 parent 943d1b8 commit e180ee9

File tree

4 files changed

+16
-27
lines changed

4 files changed

+16
-27
lines changed

include/swift/AST/TypeRepr.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -442,11 +442,11 @@ class MemberTypeRepr final : public DeclRefTypeRepr,
442442
}
443443

444444
public:
445-
static MemberTypeRepr *create(const ASTContext &Ctx, TypeRepr *Base,
446-
ArrayRef<IdentTypeRepr *> MemberComponents);
445+
static TypeRepr *create(const ASTContext &Ctx, TypeRepr *Base,
446+
ArrayRef<IdentTypeRepr *> MemberComponents);
447447

448-
static MemberTypeRepr *create(const ASTContext &Ctx,
449-
ArrayRef<IdentTypeRepr *> Components);
448+
static DeclRefTypeRepr *create(const ASTContext &Ctx,
449+
ArrayRef<IdentTypeRepr *> Components);
450450

451451
TypeRepr *getBaseComponent() const { return Base; }
452452

lib/AST/TypeRepr.cpp

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -384,17 +384,20 @@ GenericIdentTypeRepr *GenericIdentTypeRepr::create(const ASTContext &C,
384384
return new (mem) GenericIdentTypeRepr(Loc, Id, GenericArgs, AngleBrackets);
385385
}
386386

387-
MemberTypeRepr *
388-
MemberTypeRepr::create(const ASTContext &C, TypeRepr *Base,
389-
ArrayRef<IdentTypeRepr *> MemberComponents) {
387+
TypeRepr *MemberTypeRepr::create(const ASTContext &C, TypeRepr *Base,
388+
ArrayRef<IdentTypeRepr *> MemberComponents) {
389+
if (MemberComponents.empty())
390+
return Base;
391+
390392
auto size = totalSizeToAlloc<IdentTypeRepr *>(MemberComponents.size());
391393
auto mem = C.Allocate(size, alignof(MemberTypeRepr));
392394
return new (mem) MemberTypeRepr(Base, MemberComponents);
393395
}
394396

395-
MemberTypeRepr *MemberTypeRepr::create(const ASTContext &Ctx,
396-
ArrayRef<IdentTypeRepr *> Components) {
397-
return create(Ctx, Components.front(), Components.drop_front());
397+
DeclRefTypeRepr *MemberTypeRepr::create(const ASTContext &Ctx,
398+
ArrayRef<IdentTypeRepr *> Components) {
399+
return cast<DeclRefTypeRepr>(
400+
create(Ctx, Components.front(), Components.drop_front()));
398401
}
399402

400403
PackTypeRepr::PackTypeRepr(SourceLoc keywordLoc, SourceRange braceLocs,

lib/Parse/ParseType.cpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -793,11 +793,7 @@ Parser::parseTypeIdentifier(bool isParsingQualifiedDeclBaseType) {
793793

794794
DeclRefTypeRepr *DeclRefTR = nullptr;
795795
if (!ComponentsR.empty()) {
796-
if (ComponentsR.size() == 1) {
797-
DeclRefTR = ComponentsR.front();
798-
} else {
799-
DeclRefTR = MemberTypeRepr::create(Context, ComponentsR);
800-
}
796+
DeclRefTR = MemberTypeRepr::create(Context, ComponentsR);
801797
}
802798

803799
if (Status.hasCodeCompletion()) {

lib/Sema/TypeCheckPattern.cpp

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -482,12 +482,7 @@ class ResolvePattern : public ASTVisitor<ResolvePattern,
482482
const auto options =
483483
TypeResolutionOptions(None) | TypeResolutionFlags::SilenceErrors;
484484

485-
DeclRefTypeRepr *repr = nullptr;
486-
if (components.size() == 1) {
487-
repr = components.front();
488-
} else {
489-
repr = MemberTypeRepr::create(Context, components);
490-
}
485+
DeclRefTypeRepr *repr = MemberTypeRepr::create(Context, components);
491486

492487
// See if the repr resolves to a type.
493488
const auto ty = TypeResolution::resolveContextualType(
@@ -609,12 +604,7 @@ class ResolvePattern : public ASTVisitor<ResolvePattern,
609604

610605
// Otherwise, see whether we had an enum type as the penultimate
611606
// component, and look up an element inside it.
612-
DeclRefTypeRepr *prefixRepr = nullptr;
613-
if (components.size() == 1) {
614-
prefixRepr = components.front();
615-
} else {
616-
prefixRepr = MemberTypeRepr::create(Context, components);
617-
}
607+
DeclRefTypeRepr *prefixRepr = MemberTypeRepr::create(Context, components);
618608

619609
// See first if the entire repr resolves to a type.
620610
const Type enumTy = TypeResolution::resolveContextualType(

0 commit comments

Comments
 (0)