Skip to content

Commit b49f02c

Browse files
committed
[AST][Sema] Add ValueDecl::hasParameterList
1 parent 2872210 commit b49f02c

File tree

5 files changed

+26
-52
lines changed

5 files changed

+26
-52
lines changed

include/swift/AST/Decl.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2698,6 +2698,12 @@ class ValueDecl : public Decl {
26982698
/// curried self parameter.
26992699
bool hasCurriedSelf() const;
27002700

2701+
/// Returns true if the declaration has a parameter list associated with it.
2702+
///
2703+
/// Note that not all declarations with function interface types have
2704+
/// parameter lists, for example an enum element without associated values.
2705+
bool hasParameterList() const;
2706+
27012707
/// Get the decl for this value's opaque result type, if it has one.
27022708
OpaqueTypeDecl *getOpaqueResultTypeDecl() const;
27032709

@@ -7152,6 +7158,12 @@ inline bool ValueDecl::hasCurriedSelf() const {
71527158
return false;
71537159
}
71547160

7161+
inline bool ValueDecl::hasParameterList() const {
7162+
if (auto *eed = dyn_cast<EnumElementDecl>(this))
7163+
return eed->hasAssociatedValues();
7164+
return isa<AbstractFunctionDecl>(this) || isa<SubscriptDecl>(this);
7165+
}
7166+
71557167
inline bool Decl::isPotentiallyOverridable() const {
71567168
if (isa<VarDecl>(this) ||
71577169
isa<SubscriptDecl>(this) ||

lib/AST/DeclContext.cpp

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -317,14 +317,8 @@ ResilienceExpansion DeclContext::getResilienceExpansion() const {
317317
if (isa<DefaultArgumentInitializer>(dc)) {
318318
dc = dc->getParent();
319319

320-
const ValueDecl *VD;
321-
if (auto *FD = dyn_cast<AbstractFunctionDecl>(dc)) {
322-
VD = FD;
323-
} else if (auto *EED = dyn_cast<EnumElementDecl>(dc)) {
324-
VD = EED;
325-
} else {
326-
VD = cast<SubscriptDecl>(dc);
327-
}
320+
auto *VD = cast<ValueDecl>(dc->getAsDecl());
321+
assert(VD->hasParameterList());
328322

329323
auto access =
330324
VD->getFormalAccessScope(/*useDC=*/nullptr,

lib/AST/Type.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -757,8 +757,10 @@ swift::computeDefaultMap(ArrayRef<AnyFunctionType::Param> params,
757757

758758
// No parameter list means no default arguments - hand back the zeroed
759759
// bitvector.
760-
if (!paramList)
760+
if (!paramList) {
761+
assert(!paramOwner->hasParameterList());
761762
return resultVector;
763+
}
762764

763765
switch (params.size()) {
764766
case 0:

lib/Sema/CSRanking.cpp

Lines changed: 8 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -471,29 +471,10 @@ static bool isDeclAsSpecializedAs(TypeChecker &tc, DeclContext *dc,
471471
Type type1 = decl1->getInterfaceType();
472472
Type type2 = decl2->getInterfaceType();
473473

474-
/// What part of the type should we check?
475-
enum {
476-
CheckAll,
477-
CheckInput,
478-
} checkKind;
479-
if (isa<AbstractFunctionDecl>(decl1) || isa<EnumElementDecl>(decl1)) {
480-
// Nothing to do: these have the curried 'self' already.
481-
if (auto elt = dyn_cast<EnumElementDecl>(decl1)) {
482-
checkKind = elt->hasAssociatedValues() ? CheckInput : CheckAll;
483-
} else {
484-
checkKind = CheckInput;
485-
}
486-
} else {
487-
// Add a curried 'self' type.
474+
// Add curried 'self' types if necessary.
475+
if (!decl1->hasCurriedSelf()) {
488476
type1 = type1->addCurriedSelfType(outerDC1);
489477
type2 = type2->addCurriedSelfType(outerDC2);
490-
491-
// For a subscript declaration, only look at the input type (i.e., the
492-
// indices).
493-
if (isa<SubscriptDecl>(decl1))
494-
checkKind = CheckInput;
495-
else
496-
checkKind = CheckAll;
497478
}
498479

499480
// Construct a constraint system to compare the two declarations.
@@ -613,18 +594,16 @@ static bool isDeclAsSpecializedAs(TypeChecker &tc, DeclContext *dc,
613594
}
614595

615596
bool fewerEffectiveParameters = false;
616-
switch (checkKind) {
617-
case CheckAll:
618-
// Check whether the first type is a subtype of the second.
597+
if (!decl1->hasParameterList()) {
598+
// If the decl doesn't have a parameter list, simply check whether the
599+
// first type is a subtype of the second.
619600
cs.addConstraint(ConstraintKind::Subtype,
620601
openedType1,
621602
openedType2,
622603
locator);
623-
break;
624-
625-
case CheckInput: {
626-
// Check whether the first function type's input is a subtype of the
627-
// second type's inputs, i.e., can we forward the arguments?
604+
} else {
605+
// Otherwise, check whether the first function type's input is a subtype
606+
// of the second type's inputs, i.e., can we forward the arguments?
628607
auto funcTy1 = openedType1->castTo<FunctionType>();
629608
auto funcTy2 = openedType2->castTo<FunctionType>();
630609
auto params1 = funcTy1->getParams();
@@ -698,9 +677,6 @@ static bool isDeclAsSpecializedAs(TypeChecker &tc, DeclContext *dc,
698677
if (compareTrailingClosureParamsSeparately)
699678
if (!maybeAddSubtypeConstraint(params1.back(), params2.back()))
700679
knownNonSubtype = true;
701-
702-
break;
703-
}
704680
}
705681

706682
if (!knownNonSubtype) {

lib/Sema/CSSimplify.cpp

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -144,18 +144,8 @@ bool constraints::areConservativelyCompatibleArgumentLabels(
144144
return true;
145145
}
146146

147-
148-
const AnyFunctionType *fTy;
149-
150-
if (auto fn = dyn_cast<AbstractFunctionDecl>(decl)) {
151-
fTy = fn->getInterfaceType()->castTo<AnyFunctionType>();
152-
} else if (auto subscript = dyn_cast<SubscriptDecl>(decl)) {
153-
fTy = subscript->getInterfaceType()->castTo<AnyFunctionType>();
154-
} else if (auto enumElement = dyn_cast<EnumElementDecl>(decl)) {
155-
fTy = enumElement->getInterfaceType()->castTo<AnyFunctionType>();
156-
} else {
147+
if (!decl->hasParameterList())
157148
return true;
158-
}
159149

160150
SmallVector<AnyFunctionType::Param, 8> argInfos;
161151
for (auto argLabel : labels) {

0 commit comments

Comments
 (0)