Skip to content

Commit 8fd41c0

Browse files
authored
Merge pull request swiftlang#9178 from slavapestov/anyobject-removal-vol-2
AnyObject removal volume 2
2 parents 8752669 + c653b9e commit 8fd41c0

File tree

4 files changed

+47
-42
lines changed

4 files changed

+47
-42
lines changed

lib/AST/GenericSignatureBuilder.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2020,11 +2020,13 @@ Type GenericSignatureBuilder::PotentialArchetype::getTypeInContext(
20202020
Type superclass = representative->getSuperclass();
20212021
if (superclass && superclass->hasTypeParameter()) {
20222022
if (representative->RecursiveSuperclassType) {
2023-
superclass = ErrorType::get(superclass);
2023+
superclass = Type();
20242024
} else {
20252025
superclass = genericEnv->mapTypeIntoContext(
20262026
superclass,
20272027
builder.getLookupConformanceFn());
2028+
if (superclass->is<ErrorType>())
2029+
superclass = Type();
20282030

20292031
// We might have recursively recorded the archetype; if so, return early.
20302032
// FIXME: This should be detectable before we end up building archetypes.

lib/AST/Type.cpp

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -889,16 +889,20 @@ Type TypeBase::getRValueInstanceType() {
889889

890890
/// \brief Collect the protocols in the existential type T into the given
891891
/// vector.
892-
static void addProtocols(Type T, SmallVectorImpl<ProtocolDecl *> &Protocols,
893-
Type &Superclass) {
892+
static void addProtocols(Type T,
893+
SmallVectorImpl<ProtocolDecl *> &Protocols,
894+
Type &Superclass,
895+
bool &HasExplicitAnyObject) {
894896
if (auto Proto = T->getAs<ProtocolType>()) {
895897
Protocols.push_back(Proto->getDecl());
896898
return;
897899
}
898900

899901
if (auto PC = T->getAs<ProtocolCompositionType>()) {
902+
if (PC->hasExplicitAnyObject())
903+
HasExplicitAnyObject = true;
900904
for (auto P : PC->getMembers())
901-
addProtocols(P, Protocols, Superclass);
905+
addProtocols(P, Protocols, Superclass, HasExplicitAnyObject);
902906
return;
903907
}
904908

@@ -2554,6 +2558,8 @@ CanArchetypeType ArchetypeType::getNew(
25542558
SmallVectorImpl<ProtocolDecl *> &ConformsTo,
25552559
Type Superclass,
25562560
LayoutConstraint Layout) {
2561+
assert(!Superclass || Superclass->getClassOrBoundGenericClass());
2562+
25572563
// Gather the set of protocol declarations to which this archetype conforms.
25582564
ProtocolType::canonicalizeProtocols(ConformsTo);
25592565

@@ -2574,6 +2580,7 @@ ArchetypeType::getNew(const ASTContext &Ctx,
25742580
SmallVectorImpl<ProtocolDecl *> &ConformsTo,
25752581
Type Superclass,
25762582
LayoutConstraint Layout) {
2583+
assert(!Superclass || Superclass->getClassOrBoundGenericClass());
25772584
assert(genericEnvironment && "missing generic environment for archetype");
25782585

25792586
// Gather the set of protocol declarations to which this archetype conforms.
@@ -2780,12 +2787,16 @@ Type ProtocolCompositionType::get(const ASTContext &C,
27802787
Type Superclass;
27812788
SmallVector<ProtocolDecl *, 4> Protocols;
27822789
for (Type t : Members) {
2783-
addProtocols(t, Protocols, Superclass);
2790+
addProtocols(t, Protocols, Superclass, HasExplicitAnyObject);
27842791
}
27852792

27862793
// Minimize the set of protocols composed together.
27872794
ProtocolType::canonicalizeProtocols(Protocols);
27882795

2796+
// The presence of a superclass constraint makes AnyObject redundant.
2797+
if (Superclass)
2798+
HasExplicitAnyObject = false;
2799+
27892800
// If one protocol remains with no further constraints, its nominal
27902801
// type is the canonical type.
27912802
if (Protocols.size() == 1 && !Superclass && !HasExplicitAnyObject)
@@ -2803,8 +2814,7 @@ Type ProtocolCompositionType::get(const ASTContext &C,
28032814
});
28042815

28052816
// TODO: Canonicalize away HasExplicitAnyObject if it is implied
2806-
// by one of our member protocols or the presence of a superclass
2807-
// constraint.
2817+
// by one of our member protocols.
28082818
return build(C, CanTypes, HasExplicitAnyObject);
28092819
}
28102820

lib/PrintAsObjC/PrintAsObjC.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,7 @@ static bool isNSObjectOrAnyHashable(ASTContext &ctx, Type type) {
5353
classDecl->getModuleContext()->getName() == ctx.Id_ObjectiveC;
5454
}
5555
if (auto nomDecl = type->getAnyNominal()) {
56-
return nomDecl->getName() == ctx.getIdentifier("AnyHashable") &&
57-
nomDecl->getModuleContext() == ctx.getStdlibModule();
56+
return nomDecl == ctx.getAnyHashableDecl();
5857
}
5958

6059
return false;
@@ -1996,7 +1995,8 @@ class ModuleWriter {
19961995

19971996
if (otherModule == &M)
19981997
return false;
1999-
if (otherModule->isStdlibModule())
1998+
if (otherModule->isStdlibModule() ||
1999+
otherModule->isBuiltinModule())
20002000
return true;
20012001
// Don't need a module for SIMD types in C.
20022002
if (otherModule->getName() == M.getASTContext().Id_simd)

lib/Sema/MiscDiagnostics.cpp

Lines changed: 25 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1469,53 +1469,46 @@ bool TypeChecker::getDefaultGenericArgumentsString(
14691469
genericParamText << "<";
14701470

14711471
auto printGenericParamSummary =
1472-
[&](const GenericTypeParamType *genericParamTy) {
1472+
[&](GenericTypeParamType *genericParamTy) {
14731473
const GenericTypeParamDecl *genericParam = genericParamTy->getDecl();
14741474
if (Type result = getPreferredType(genericParam)) {
14751475
result.print(genericParamText);
14761476
return;
14771477
}
14781478

1479-
ArrayRef<ProtocolDecl *> protocols =
1480-
genericParam->getConformingProtocols();
1479+
auto contextTy = typeDecl->mapTypeIntoContext(genericParamTy);
1480+
if (auto archetypeTy = contextTy->getAs<ArchetypeType>()) {
1481+
SmallVector<Type, 2> members;
14811482

1482-
Type superclass = genericParam->getSuperclass();
1483-
if (superclass && !superclass->hasError()) {
1484-
if (protocols.empty()) {
1485-
superclass.print(genericParamText);
1486-
return;
1483+
bool hasExplicitAnyObject = archetypeTy->requiresClass();
1484+
if (auto superclass = archetypeTy->getSuperclass()) {
1485+
hasExplicitAnyObject = false;
1486+
members.push_back(superclass);
14871487
}
14881488

1489-
genericParamText << "<#" << genericParam->getName() << ": ";
1490-
superclass.print(genericParamText);
1491-
for (const ProtocolDecl *proto : protocols) {
1492-
if (proto->isSpecificProtocol(KnownProtocolKind::AnyObject))
1493-
continue;
1494-
genericParamText << " & " << proto->getName();
1489+
for (auto proto : archetypeTy->getConformsTo()) {
1490+
members.push_back(proto->getDeclaredType());
1491+
if (proto->requiresClass())
1492+
hasExplicitAnyObject = false;
14951493
}
1496-
genericParamText << "#>";
1497-
return;
1498-
}
14991494

1500-
if (protocols.empty()) {
1501-
genericParamText << Context.Id_Any;
1502-
return;
1503-
}
1495+
if (hasExplicitAnyObject)
1496+
members.push_back(typeDecl->getASTContext().getAnyObjectType());
1497+
1498+
auto type = ProtocolCompositionType::get(typeDecl->getASTContext(),
1499+
members, hasExplicitAnyObject);
1500+
1501+
if (type->isObjCExistentialType() || type->isAny()) {
1502+
genericParamText << type;
1503+
return;
1504+
}
15041505

1505-
if (protocols.size() == 1 &&
1506-
(protocols.front()->isObjC() ||
1507-
protocols.front()->isSpecificProtocol(KnownProtocolKind::AnyObject))) {
1508-
genericParamText << protocols.front()->getName();
1506+
genericParamText << "<#" << genericParam->getName() << ": ";
1507+
genericParamText << type << "#>";
15091508
return;
15101509
}
15111510

1512-
genericParamText << "<#" << genericParam->getName() << ": ";
1513-
interleave(protocols,
1514-
[&](const ProtocolDecl *proto) {
1515-
genericParamText << proto->getName();
1516-
},
1517-
[&] { genericParamText << " & "; });
1518-
genericParamText << "#>";
1511+
genericParamText << contextTy;
15191512
};
15201513

15211514
interleave(typeDecl->getInnermostGenericParamTypes(),

0 commit comments

Comments
 (0)