Skip to content

Commit 10598af

Browse files
authored
Merge pull request #6063 from slavapestov/more-generics-gardening
More generics gardening
2 parents 9197174 + aaaded5 commit 10598af

31 files changed

+182
-332
lines changed

include/swift/AST/Decl.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4580,10 +4580,6 @@ class AbstractFunctionDecl : public ValueDecl, public DeclContext {
45804580
// FIXME: Hack that provides names with keyword arguments for accessors.
45814581
DeclName getEffectiveFullName() const;
45824582

4583-
/// \brief If this is a method in a type extension for some type,
4584-
/// return that type, otherwise return Type().
4585-
Type getExtensionType() const;
4586-
45874583
/// Returns true if the function has a body written in the source file.
45884584
///
45894585
/// Note that a true return value does not imply that the body was actually

include/swift/AST/DiagnosticsParse.def

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -508,7 +508,8 @@ ERROR(sil_member_lookup_bad_type,none,
508508
ERROR(sil_member_decl_type_mismatch,none,
509509
"member defined with mismatching type %0 (expected %1)", (Type, Type))
510510
ERROR(sil_substitution_mismatch,none,
511-
"substitution conformances don't match archetype", ())
511+
"substitution replacement type %0 does not conform to protocol %1",
512+
(Type, DeclName))
512513
ERROR(sil_missing_substitutions,none,
513514
"missing substitutions", ())
514515
ERROR(sil_too_many_substitutions,none,

include/swift/AST/Types.h

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -916,9 +916,6 @@ class alignas(1 << TypeAlignInBits) TypeBase {
916916

917917
/// Whether this is the AnyObject type.
918918
bool isAnyObject();
919-
920-
/// Whether this is an empty existential composition ("{}").
921-
bool isEmptyExistentialComposition();
922919

923920
/// Whether this is an existential composition containing
924921
/// Error.
@@ -938,16 +935,6 @@ class alignas(1 << TypeAlignInBits) TypeBase {
938935
/// Return whether this type is or can be substituted for a bridgeable
939936
/// object type.
940937
TypeTraitResult canBeClass();
941-
942-
/// Returns true if the type conforms to protocols using witnesses from the
943-
/// environment or from within the value. Generic parameters and existentials
944-
/// meet this criteria. In these cases we represent the
945-
/// conformance as a null ProtocolConformance* pointer, because there is no
946-
/// static conformance associated with the conforming type.
947-
bool hasDependentProtocolConformances();
948-
949-
/// Retrieve the type declaration directly referenced by this type, if any.
950-
TypeDecl *getDirectlyReferencedTypeDecl() const;
951938

952939
private:
953940
// Make vanilla new/delete illegal for Types.
@@ -4618,11 +4605,6 @@ inline CanType Type::getCanonicalTypeOrNull() const {
46184605
return isNull() ? CanType() : getPointer()->getCanonicalType();
46194606
}
46204607

4621-
inline bool TypeBase::hasDependentProtocolConformances() {
4622-
return is<SubstitutableType>() || is<GenericTypeParamType>()
4623-
|| isAnyExistentialType();
4624-
}
4625-
46264608
#define TYPE(id, parent)
46274609
#define SUGARED_TYPE(id, parent) \
46284610
template <> \

lib/AST/ASTVerifier.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2005,7 +2005,7 @@ struct ASTNodeBase {};
20052005
void verifyChecked(ConstructorDecl *CD) {
20062006
PrettyStackTraceDecl debugStack("verifying ConstructorDecl", CD);
20072007

2008-
auto *ND = CD->getExtensionType()->getNominalOrBoundGenericNominal();
2008+
auto *ND = CD->getDeclContext()->getAsNominalTypeOrNominalTypeExtensionContext();
20092009
if (!isa<ClassDecl>(ND) && !isa<StructDecl>(ND) && !isa<EnumDecl>(ND) &&
20102010
!isa<ProtocolDecl>(ND) && !CD->isInvalid()) {
20112011
Out << "ConstructorDecls outside structs, classes or enums "
@@ -2188,7 +2188,7 @@ struct ASTNodeBase {};
21882188
void verifyChecked(DestructorDecl *DD) {
21892189
PrettyStackTraceDecl debugStack("verifying DestructorDecl", DD);
21902190

2191-
auto *ND = DD->getExtensionType()->getNominalOrBoundGenericNominal();
2191+
auto *ND = DD->getDeclContext()->getAsNominalTypeOrNominalTypeExtensionContext();
21922192
if (!isa<ClassDecl>(ND) && !DD->isInvalid()) {
21932193
Out << "DestructorDecls outside classes should be marked invalid";
21942194
abort();

lib/AST/Decl.cpp

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2935,7 +2935,7 @@ bool AbstractStorageDecl::isGetterMutating() const {
29352935
/// called even on an immutable base value.
29362936
bool AbstractStorageDecl::isSetterNonMutating() const {
29372937
// Setters declared in reference type contexts are never mutating.
2938-
if (auto contextType = getDeclContext()->getDeclaredTypeInContext()) {
2938+
if (auto contextType = getDeclContext()->getDeclaredInterfaceType()) {
29392939
if (contextType->hasReferenceSemantics())
29402940
return true;
29412941
}
@@ -3608,7 +3608,7 @@ void VarDecl::emitLetToVarNoteIfSimple(DeclContext *UseDC) const {
36083608
// a property in a non-mutating method.
36093609
auto FD = dyn_cast_or_null<FuncDecl>(UseDC->getInnermostMethodContext());
36103610
if (FD && !FD->isMutating() && !FD->isImplicit() && FD->isInstanceMember()&&
3611-
!FD->getDeclContext()->getDeclaredTypeInContext()
3611+
!FD->getDeclContext()->getDeclaredInterfaceType()
36123612
->hasReferenceSemantics()) {
36133613
auto &d = getASTContext().Diags;
36143614
d.diagnose(FD->getFuncLoc(), diag::change_to_mutating, FD->isAccessor())
@@ -4030,10 +4030,6 @@ ParamDecl *AbstractFunctionDecl::getImplicitSelfDecl() {
40304030
return nullptr;
40314031
}
40324032

4033-
Type AbstractFunctionDecl::getExtensionType() const {
4034-
return getDeclContext()->getDeclaredTypeInContext();
4035-
}
4036-
40374033
std::pair<DefaultArgumentKind, Type>
40384034
AbstractFunctionDecl::getDefaultArg(unsigned Index) const {
40394035
auto paramLists = getParameterLists();
@@ -4323,7 +4319,7 @@ bool FuncDecl::isExplicitNonMutating() const {
43234319
return !isMutating() &&
43244320
isAccessor() && !isGetter() &&
43254321
isInstanceMember() &&
4326-
!getDeclContext()->getDeclaredTypeInContext()->hasReferenceSemantics();
4322+
!getDeclContext()->getDeclaredInterfaceType()->hasReferenceSemantics();
43274323
}
43284324

43294325
void FuncDecl::setDeserializedSignature(ArrayRef<ParameterList *> BodyParams,

lib/AST/LookupVisibleDecls.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -827,14 +827,11 @@ void swift::lookupVisibleDecls(VisibleDeclConsumer &Consumer,
827827
Consumer.foundDecl(const_cast<ParamDecl*>(AFD->getImplicitSelfDecl()),
828828
DeclVisibilityKind::FunctionParameter);
829829

830-
if (AFD->getExtensionType()) {
831-
ExtendedType = AFD->getExtensionType();
830+
if (AFD->getDeclContext()->isTypeContext()) {
831+
ExtendedType = AFD->getDeclContext()->getSelfTypeInContext();
832832
BaseDecl = AFD->getImplicitSelfDecl();
833833
DC = DC->getParent();
834834

835-
if (DC->getAsProtocolExtensionContext())
836-
ExtendedType = DC->getSelfTypeInContext();
837-
838835
if (auto *FD = dyn_cast<FuncDecl>(AFD))
839836
if (FD->isStatic())
840837
ExtendedType = MetatypeType::get(ExtendedType);

lib/AST/NameLookup.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,8 @@ bool swift::removeShadowedDecls(SmallVectorImpl<ValueDecl*> &decls,
197197
if (decl->hasClangNode()) {
198198
if (auto ctor = dyn_cast<ConstructorDecl>(decl)) {
199199
auto ctorSignature
200-
= std::make_pair(ctor->getExtensionType()->getCanonicalType(),
200+
= std::make_pair(ctor->getDeclContext()->getDeclaredInterfaceType()
201+
->getCanonicalType(),
201202
decl->getFullName());
202203
auto &knownCtors = ObjCCollidingConstructors[ctorSignature];
203204
if (!knownCtors.empty())
@@ -722,18 +723,17 @@ UnqualifiedLookup::UnqualifiedLookup(DeclName Name, DeclContext *DC,
722723
if (!isCascadingUse.hasValue() || isCascadingUse.getValue())
723724
isCascadingUse = AFD->isCascadingContextForLookup(false);
724725

725-
if (AFD->getExtensionType()) {
726-
if (AFD->getDeclContext()->getAsProtocolOrProtocolExtensionContext()) {
727-
ExtendedType = AFD->getDeclContext()->getSelfTypeInContext();
726+
if (AFD->getDeclContext()->isTypeContext()) {
727+
ExtendedType = AFD->getDeclContext()->getSelfTypeInContext();
728+
// FIXME: Hack to deal with missing 'Self' archetypes.
729+
if (!ExtendedType)
730+
if (auto *PD = AFD->getDeclContext()
731+
->getAsProtocolOrProtocolExtensionContext())
732+
ExtendedType = PD->getDeclaredType();
728733

729-
// Fallback path.
730-
if (!ExtendedType)
731-
ExtendedType = AFD->getExtensionType();
732-
} else {
733-
ExtendedType = AFD->getExtensionType();
734-
}
735734
BaseDecl = AFD->getImplicitSelfDecl();
736-
MetaBaseDecl = AFD->getExtensionType()->getAnyNominal();
735+
MetaBaseDecl = AFD->getDeclContext()
736+
->getAsNominalTypeOrNominalTypeExtensionContext();
737737
DC = DC->getParent();
738738

739739
if (auto *FD = dyn_cast<FuncDecl>(AFD))

lib/AST/Substitution.cpp

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -92,15 +92,10 @@ Substitution Substitution::subst(Module *module,
9292
conformance = ProtocolConformanceRef(lookupResults.front());
9393
}
9494

95-
if (conformance) {
96-
if (conformance->isConcrete())
97-
conformancesChanged = true;
98-
substConformances.push_back(*conformance);
99-
} else {
100-
assert(substReplacement->hasDependentProtocolConformances() &&
101-
"couldn't find concrete conformance for concrete type?");
102-
substConformances.push_back(ProtocolConformanceRef(proto));
103-
}
95+
assert(conformance);
96+
if (conformance->isConcrete())
97+
conformancesChanged = true;
98+
substConformances.push_back(*conformance);
10499
}
105100
assert(substConformances.size() == Conformance.size());
106101

lib/AST/Type.cpp

Lines changed: 0 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -659,17 +659,6 @@ bool TypeBase::isAnyObject() {
659659
return false;
660660
}
661661

662-
bool TypeBase::isEmptyExistentialComposition() {
663-
if (auto emtType = ExistentialMetatypeType::get(this)) {
664-
if (auto pcType = emtType->getInstanceType()->
665-
getAs<ProtocolCompositionType>()) {
666-
return pcType->getProtocols().empty();
667-
}
668-
}
669-
670-
return false;
671-
}
672-
673662
bool TypeBase::isExistentialWithError() {
674663
// FIXME: Compute this as a bit in TypeBase so this operation isn't
675664
// overly expensive.
@@ -955,38 +944,6 @@ Type TypeBase::getRValueInstanceType() {
955944
return type->getInOutObjectType();
956945
}
957946

958-
TypeDecl *TypeBase::getDirectlyReferencedTypeDecl() const {
959-
if (auto module = dyn_cast<ModuleType>(this))
960-
return module->getModule();
961-
962-
if (auto nominal = dyn_cast<NominalType>(this))
963-
return nominal->getDecl();
964-
965-
if (auto bound = dyn_cast<BoundGenericType>(this))
966-
return bound->getDecl();
967-
968-
if (auto unbound = dyn_cast<UnboundGenericType>(this))
969-
return unbound->getDecl();
970-
971-
if (auto alias = dyn_cast<NameAliasType>(this))
972-
return alias->getDecl();
973-
974-
if (auto gp = dyn_cast<GenericTypeParamType>(this))
975-
return gp->getDecl();
976-
977-
if (auto depMem = dyn_cast<DependentMemberType>(this))
978-
return depMem->getAssocType();
979-
980-
if (auto archetype = dyn_cast<ArchetypeType>(this)) {
981-
if (auto assoc = archetype->getAssocType())
982-
return assoc;
983-
984-
return nullptr;
985-
}
986-
987-
return nullptr;
988-
}
989-
990947
/// \brief Collect the protocols in the existential type T into the given
991948
/// vector.
992949
static void addProtocols(Type T, SmallVectorImpl<ProtocolDecl *> &Protocols) {

lib/ClangImporter/ImportDecl.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5414,7 +5414,8 @@ ConstructorDecl *SwiftDeclConverter::importConstructor(
54145414

54155415
void SwiftDeclConverter::recordObjCOverride(AbstractFunctionDecl *decl) {
54165416
// Figure out the class in which this method occurs.
5417-
auto classTy = decl->getExtensionType()->getAs<ClassType>();
5417+
auto classTy = decl->getDeclContext()->getDeclaredInterfaceType()
5418+
->getAs<ClassType>();
54185419
if (!classTy)
54195420
return;
54205421
auto superTy = classTy->getSuperclass(nullptr);

lib/Index/Index.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -575,8 +575,7 @@ bool IndexSwiftASTWalker::passRelatedType(const TypeLoc &Ty) {
575575
}
576576

577577
if (Ty.getType()) {
578-
if (auto nominal = dyn_cast_or_null<NominalTypeDecl>(
579-
Ty.getType()->getDirectlyReferencedTypeDecl()))
578+
if (auto nominal = Ty.getType()->getAnyNominal())
580579
if (!passRelated(nominal, Ty.getLoc()))
581580
return false;
582581
}
@@ -644,8 +643,7 @@ bool IndexSwiftASTWalker::reportPseudoAccessor(AbstractStorageDecl *D,
644643
NominalTypeDecl *
645644
IndexSwiftASTWalker::getTypeLocAsNominalTypeDecl(const TypeLoc &Ty) {
646645
if (Type T = Ty.getType())
647-
return dyn_cast_or_null<NominalTypeDecl>(
648-
T->getDirectlyReferencedTypeDecl());
646+
return T->getAnyNominal();
649647
if (IdentTypeRepr *T = dyn_cast_or_null<IdentTypeRepr>(Ty.getTypeRepr())) {
650648
auto Comp = T->getComponentRange().back();
651649
if (auto NTD = dyn_cast_or_null<NominalTypeDecl>(Comp->getBoundDecl()))

0 commit comments

Comments
 (0)