Skip to content

RequirementMachine: Don't consider protocol typealiases with UnboundGenericType #41919

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 23 additions & 15 deletions lib/AST/RequirementMachine/RequirementLowering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -705,14 +705,16 @@ StructuralRequirementsRequest::evaluate(Evaluator &evaluator,
// underlying type of the typealias.
if (auto *typeAliasDecl = dyn_cast<TypeAliasDecl>(decl)) {
if (!typeAliasDecl->isGeneric()) {
// Ignore the typealias if we have an associated type with the same anme
// Ignore the typealias if we have an associated type with the same name
// in the same protocol. This is invalid anyway, but it's just here to
// ensure that we produce the same requirement signature on some tests
// with -requirement-machine-protocol-signatures=verify.
if (assocTypes.contains(typeAliasDecl->getName()))
continue;

auto underlyingType = typeAliasDecl->getStructuralType();
if (underlyingType->is<UnboundGenericType>())
continue;

auto subjectType = DependentMemberType::get(
selfTy, typeAliasDecl->getName());
Expand Down Expand Up @@ -752,6 +754,20 @@ TypeAliasRequirementsRequest::evaluate(Evaluator &evaluator,
(ctx.LangOpts.RequirementMachineProtocolSignatures ==
RequirementMachineMode::Enabled);

auto getStructuralType = [](TypeDecl *typeDecl) -> Type {
if (auto typealias = dyn_cast<TypeAliasDecl>(typeDecl)) {
if (typealias->getUnderlyingTypeRepr() != nullptr) {
auto type = typealias->getStructuralType();
if (auto *aliasTy = cast<TypeAliasType>(type.getPointer()))
return aliasTy->getSinglyDesugaredType();
return type;
}
return typealias->getUnderlyingType();
}

return typeDecl->getDeclaredInterfaceType();
};

// Collect all typealiases from inherited protocols recursively.
llvm::MapVector<Identifier, TinyPtrVector<TypeDecl *>> inheritedTypeDecls;
for (auto *inheritedProto : ctx.getRewriteContext().getInheritedProtocols(proto)) {
Expand All @@ -762,25 +778,17 @@ TypeAliasRequirementsRequest::evaluate(Evaluator &evaluator,
if (genReq->getGenericParams())
continue;

// Ignore typealiases with UnboundGenericType, since they
// are like generic typealiases.
if (auto *typeAlias = dyn_cast<TypeAliasDecl>(req))
if (getStructuralType(typeAlias)->is<UnboundGenericType>())
continue;

inheritedTypeDecls[typeReq->getName()].push_back(typeReq);
}
}
}

auto getStructuralType = [](TypeDecl *typeDecl) -> Type {
if (auto typealias = dyn_cast<TypeAliasDecl>(typeDecl)) {
if (typealias->getUnderlyingTypeRepr() != nullptr) {
auto type = typealias->getStructuralType();
if (auto *aliasTy = cast<TypeAliasType>(type.getPointer()))
return aliasTy->getSinglyDesugaredType();
return type;
}
return typealias->getUnderlyingType();
}

return typeDecl->getDeclaredInterfaceType();
};

// An inferred same-type requirement between the two type declarations
// within this protocol or a protocol it inherits.
auto recordInheritedTypeRequirement = [&](TypeDecl *first, TypeDecl *second) {
Expand Down
1 change: 1 addition & 0 deletions lib/AST/RequirementMachine/Symbol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ struct Symbol::Storage final
Storage(Symbol::Kind kind, CanType type, ArrayRef<Term> substitutions) {
assert(kind == Symbol::Kind::Superclass ||
kind == Symbol::Kind::ConcreteType);
assert(!type->hasUnboundGenericType());
assert(!type->hasTypeVariable());
assert(type->hasTypeParameter() != substitutions.empty());

Expand Down
7 changes: 7 additions & 0 deletions test/Generics/protocol_typealias_unbound_generic.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// RUN: %target-typecheck-verify-swift

protocol P {
typealias A = Array

associatedtype X where X == A // expected-error {{reference to generic type 'Self.A' (aka 'Array') requires arguments in <...>}}
}