Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Kill some usages of ArchetypeType::getProtocolSelf() #4646

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 0 additions & 11 deletions include/swift/AST/Types.h
Expand Up @@ -3757,17 +3757,6 @@ class ArchetypeType final : public SubstitutableType,
return AssocTypeOrProto.dyn_cast<ProtocolDecl *>();
}

/// True if this is the 'Self' parameter of a protocol or an associated type
/// of 'Self'.
bool isSelfDerived() {
ArchetypeType *t = getPrimary();

if (t && t->getSelfProtocol())
return true;

return false;
}

/// getConformsTo - Retrieve the set of protocols to which this substitutable
/// type shall conform.
ArrayRef<ProtocolDecl *> getConformsTo() const { return ConformsTo; }
Expand Down
1 change: 0 additions & 1 deletion include/swift/Basic/DemangleNodes.def
Expand Up @@ -131,7 +131,6 @@ NODE(ReabstractionThunk)
NODE(ReabstractionThunkHelper)
NODE(ReturnType)
NODE(SILBoxType)
NODE(SelfTypeRef)
CONTEXT_NODE(Setter)
NODE(SpecializationPassID)
NODE(SpecializationIsFragile)
Expand Down
2 changes: 1 addition & 1 deletion lib/AST/ArchetypeBuilder.cpp
Expand Up @@ -437,7 +437,7 @@ auto ArchetypeBuilder::PotentialArchetype::getNestedType(
if (!containingProtocol) continue;

// Go up archetype parents until we find our containing protocol.
while (archetype->getSelfProtocol() != containingProtocol) {
while (!archetype->isEqual(containingProtocol->getSelfTypeInContext())) {
identifiers.push_back(archetype->getName());
archetype = archetype->getParent();
if (!archetype)
Expand Down
11 changes: 1 addition & 10 deletions lib/AST/Mangle.cpp
Expand Up @@ -1090,16 +1090,7 @@ void Mangler::mangleType(Type type, unsigned uncurryLevel) {
addSubstitution(archetype);
return;
}

// associated-type ::= 'Q' protocol-context
// Mangle the Self archetype of a protocol.
if (auto proto = archetype->getSelfProtocol()) {
Buffer << 'P';
mangleProtocolName(proto);
addSubstitution(archetype);
return;
}


// archetype ::= 'Q' <index> # archetype with depth=0, index=N
// archetype ::= 'Qd' <index> <index> # archetype with depth=M+1, index=N
// Mangle generic parameter archetypes.
Expand Down
7 changes: 0 additions & 7 deletions lib/AST/Type.cpp
Expand Up @@ -2795,13 +2795,6 @@ static Type getMemberForBaseType(Module *module,
if (archetypeParent->hasNestedType(name))
return archetypeParent->getNestedTypeValue(name);

if (auto parent = archetypeParent->getParent()) {
// If the archetype doesn't have the requested type and the parent is not
// self derived, error out
return parent->isSelfDerived() ? parent->getNestedTypeValue(name)
: ErrorType::get(module->getASTContext());
}

// If looking for an associated type and the archetype is constrained to a
// class, continue to the default associated type lookup
if (!assocType || !archetypeParent->getSuperclass()) {
Expand Down
23 changes: 1 addition & 22 deletions lib/Basic/Demangle.cpp
Expand Up @@ -1708,13 +1708,6 @@ class Demangler {
}

NodePointer demangleArchetypeType() {
auto makeSelfType = [&](NodePointer proto) -> NodePointer {
auto selfType = NodeFactory::create(Node::Kind::SelfTypeRef);
selfType->addChild(proto);
Substitutions.push_back(selfType);
return selfType;
};

auto makeAssociatedType = [&](NodePointer root) -> NodePointer {
NodePointer name = demangleIdentifier();
if (!name) return nullptr;
Expand All @@ -1725,12 +1718,6 @@ class Demangler {
return assocType;
};

if (Mangled.nextIf('P')) {
NodePointer proto = demangleProtocolName();
if (!proto) return nullptr;
return makeSelfType(proto);
}

if (Mangled.nextIf('Q')) {
NodePointer root = demangleArchetypeType();
if (!root) return nullptr;
Expand All @@ -1739,10 +1726,7 @@ class Demangler {
if (Mangled.nextIf('S')) {
NodePointer sub = demangleSubstitutionIndex();
if (!sub) return nullptr;
if (sub->getKind() == Node::Kind::Protocol)
return makeSelfType(sub);
else
return makeAssociatedType(sub);
return makeAssociatedType(sub);
}
if (Mangled.nextIf('s')) {
NodePointer stdlib = NodeFactory::create(Node::Kind::Module, STDLIB_NAME);
Expand Down Expand Up @@ -2402,7 +2386,6 @@ class NodePrinter {
case Node::Kind::Protocol:
case Node::Kind::QualifiedArchetype:
case Node::Kind::ReturnType:
case Node::Kind::SelfTypeRef:
case Node::Kind::SILBoxType:
case Node::Kind::Structure:
case Node::Kind::TupleElementName:
Expand Down Expand Up @@ -3460,10 +3443,6 @@ void NodePrinter::print(NodePointer pointer, bool asContext, bool suppressType)
print(pointer->getChild(0));
Printer << '.' << pointer->getChild(1)->getText();
return;
case Node::Kind::SelfTypeRef:
print(pointer->getChild(0));
Printer << ".Self";
return;
case Node::Kind::ProtocolList: {
NodePointer type_list = pointer->getChild(0);
if (!type_list)
Expand Down
9 changes: 0 additions & 9 deletions lib/Basic/Remangle.cpp
Expand Up @@ -1374,15 +1374,6 @@ void Remangler::mangleAssociatedType(Node *node) {
}
}

void Remangler::mangleSelfTypeRef(Node *node) {
SubstitutionEntry entry;
if (trySubstitution(node, entry)) return;
Out << "QP";
assert(node->getNumChildren() == 1);
mangleProtocolWithoutPrefix(node->begin()[0].get());
addSubstitution(entry);
}

void Remangler::mangleArchetypeRef(Node *node) {
Node::IndexType relativeDepth = node->getChild(0)->getIndex();
Node::IndexType index = node->getChild(1)->getIndex();
Expand Down
37 changes: 0 additions & 37 deletions lib/IDE/TypeReconstruction.cpp
Expand Up @@ -1947,39 +1947,6 @@ static void VisitNodeQualifiedArchetype(
}
}

static void VisitNodeSelfTypeRef(
ASTContext *ast, std::vector<Demangle::NodePointer> &nodes,
Demangle::NodePointer &cur_node, VisitNodeResult &result,
const VisitNodeResult &generic_context) { // set by GenericType case
nodes.push_back(cur_node->getFirstChild());
VisitNodeResult type_result;
VisitNode(ast, nodes, type_result, generic_context);
if (type_result.HasSingleType()) {
Type supposed_protocol_type(type_result.GetFirstType());
ProtocolType *protocol_type = supposed_protocol_type->getAs<ProtocolType>();
ProtocolDecl *protocol_decl =
protocol_type ? protocol_type->getDecl() : nullptr;
if (protocol_decl) {
ArchetypeType::AssocTypeOrProtocolType assoc_protocol_type(protocol_decl);
if (ast) {
CanTypeWrapper<ArchetypeType> self_type = ArchetypeType::getNew(
*ast, nullptr, assoc_protocol_type, ast->getIdentifier("Self"),
{supposed_protocol_type}, Type(), false);
if (self_type.getPointer())
result._types.push_back(Type(self_type));
else
result._error = "referent type cannot be made into an archetype";
} else {
result._error = "invalid ASTContext";
}
} else {
result._error = "referent type does not resolve to a protocol";
}
} else {
result._error = "couldn't resolve referent type";
}
}

static void VisitNodeTupleElement(
ASTContext *ast, std::vector<Demangle::NodePointer> &nodes,
Demangle::NodePointer &cur_node, VisitNodeResult &result,
Expand Down Expand Up @@ -2243,10 +2210,6 @@ static void visitNodeImpl(
VisitNodeQualifiedArchetype(ast, nodes, node, result, genericContext);
break;

case Demangle::Node::Kind::SelfTypeRef:
VisitNodeSelfTypeRef(ast, nodes, node, result, genericContext);
break;

case Demangle::Node::Kind::TupleElement:
VisitNodeTupleElement(ast, nodes, node, result, genericContext);
break;
Expand Down
38 changes: 20 additions & 18 deletions lib/SILGen/SILGenDecl.cpp
Expand Up @@ -1701,6 +1701,13 @@ static void addConformanceToSubstitutionMap(SILModule &M,
});
}

static bool isSelfDerived(Type selfTy, Type depTy) {
while (auto memberTy = depTy->getAs<DependentMemberType>())
depTy = memberTy->getBase();

return depTy->isEqual(selfTy);
}

/// Substitute the `Self` type from a protocol conformance into a protocol
/// requirement's type to get the type of the witness.
static CanAnyFunctionType
Expand All @@ -1714,15 +1721,15 @@ substSelfTypeIntoProtocolRequirementType(SILModule &M,
}

// Build a substitution map to replace `self` and its associated types.
auto &C = M.getASTContext();
CanType selfParamTy = CanGenericTypeParamType::get(0, 0, C);
auto selfTy = conformance->getProtocol()->getSelfInterfaceType()
->getCanonicalType();

TypeSubstitutionMap subs;
subs.insert({selfParamTy.getPointer(), conformance->getInterfaceType()
->getCanonicalType()});
subs.insert({selfTy.getPointer(), conformance->getInterfaceType()
->getCanonicalType()});
addConformanceToSubstitutionMap(M, subs,
conformance->getGenericEnvironment(),
selfParamTy, conformance);
selfTy, conformance);

ArchetypeBuilder builder(*M.getSwiftModule(),
M.getASTContext().Diags);
Expand All @@ -1743,17 +1750,10 @@ substSelfTypeIntoProtocolRequirementType(SILModule &M,
builder.addGenericParameter(param);
}

auto rootedInSelf = [&](Type t) -> bool {
while (auto dmt = t->getAs<DependentMemberType>()) {
t = dmt->getBase();
}
return t->isEqual(selfParamTy);
};

RequirementSource source(RequirementSource::Explicit, SourceLoc());

for (auto &reqt : reqtTy->getRequirements()) {
if (rootedInSelf(reqt.getFirstType()))
if (isSelfDerived(selfTy, reqt.getFirstType()))
continue;

switch (reqt.getKind()) {
Expand All @@ -1764,7 +1764,7 @@ substSelfTypeIntoProtocolRequirementType(SILModule &M,
break;

case RequirementKind::SameType: {
if (rootedInSelf(reqt.getSecondType()))
if (isSelfDerived(selfTy, reqt.getSecondType()))
continue;

// Substitute the constrained types.
Expand Down Expand Up @@ -1822,6 +1822,9 @@ getSubstitutedGenericEnvironment(SILModule &M,

TypeSubstitutionMap witnessContextParams;

auto selfTy = conformance->getProtocol()->getSelfInterfaceType()
->getCanonicalType();

// Outer generic parameters come from the generic context of
// the conformance (which might not be the same as the generic
// context of the witness, if the witness is defined in a
Expand All @@ -1833,9 +1836,8 @@ getSubstitutedGenericEnvironment(SILModule &M,
// also map to the archetypes of the requirement.
for (auto pair : reqtEnv->getInterfaceToArchetypeMap()) {
// Skip the 'Self' parameter and friends.
if (auto *archetypeTy = pair.second->getAs<ArchetypeType>())
if (archetypeTy->isSelfDerived())
continue;
if (isSelfDerived(selfTy, pair.first))
continue;

auto result = witnessContextParams.insert(pair);
assert(result.second);
Expand Down
4 changes: 1 addition & 3 deletions lib/Sema/ConstraintSystem.cpp
Expand Up @@ -446,10 +446,8 @@ namespace {
if (implArchetype->hasNestedType(member->getName())) {
nestedType = implArchetype->getNestedType(member->getName());
archetype = nestedType.getValue()->getAs<ArchetypeType>();
} else if (implArchetype->isSelfDerived()) {
archetype = implArchetype;
}

ConstraintLocator *locator;
if (archetype) {
locator = CS.getConstraintLocator(
Expand Down