Skip to content

[AST] Reinstate "Fix excessive deserialization in GenericSignatureBuilder" #7530

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
4 changes: 2 additions & 2 deletions include/swift/AST/Decl.h
Original file line number Diff line number Diff line change
Expand Up @@ -2854,8 +2854,8 @@ class NominalTypeDecl : public GenericTypeDecl, public IterableDeclContext {
///
/// \param ignoreNewExtensions Whether to avoid loading any new extension.
/// Used by the module loader to break recursion.
ArrayRef<ValueDecl *> lookupDirect(DeclName name,
bool ignoreNewExtensions = false);
TinyPtrVector<ValueDecl *> lookupDirect(DeclName name,
bool ignoreNewExtensions = false);

/// Collect the set of protocols to which this type should implicitly
/// conform, such as AnyObject (for classes).
Expand Down
11 changes: 10 additions & 1 deletion lib/AST/GenericSignatureBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -554,7 +554,16 @@ auto GenericSignatureBuilder::PotentialArchetype::getNestedType(
SmallVector<std::pair<ProtocolDecl *, RequirementSource>, 4>
conformsTo(rep->ConformsTo.begin(), rep->ConformsTo.end());
for (auto &conforms : conformsTo) {
for (auto member : conforms.first->lookupDirect(nestedName)) {
// Make sure we don't trigger deserialization of extensions,
// since they can refer back to a protocol we're currently
// type checking.
//
// Note that typealiases in extensions won't matter here,
// because a typealias is never going to be a representative
// PA.
auto members = conforms.first->lookupDirect(nestedName,
/*ignoreNewExtensions=*/true);
for (auto member : members) {
PotentialArchetype *pa;

if (auto assocType = dyn_cast<AssociatedTypeDecl>(member)) {
Expand Down
7 changes: 4 additions & 3 deletions lib/AST/NameLookup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1183,8 +1183,9 @@ void NominalTypeDecl::makeMemberVisible(ValueDecl *member) {
LookupTable.getPointer()->addMember(member);
}

ArrayRef<ValueDecl *> NominalTypeDecl::lookupDirect(DeclName name,
bool ignoreNewExtensions) {
TinyPtrVector<ValueDecl *> NominalTypeDecl::lookupDirect(
DeclName name,
bool ignoreNewExtensions) {
// Make sure we have the complete list of members (in this nominal and in all
// extensions).
if (!ignoreNewExtensions) {
Expand All @@ -1202,7 +1203,7 @@ ArrayRef<ValueDecl *> NominalTypeDecl::lookupDirect(DeclName name,
return { };

// We found something; return it.
return { known->second.begin(), known->second.size() };
return known->second;
}

void ClassDecl::createObjCMethodLookup() {
Expand Down
9 changes: 4 additions & 5 deletions lib/Sema/TypeCheckDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -867,21 +867,20 @@ static void checkRedeclaration(TypeChecker &tc, ValueDecl *current) {
isCascading = (current->getFormalAccess() > Accessibility::FilePrivate);

// Find other potential definitions.
SmallVector<ValueDecl *, 4> otherDefinitionsVec;
ArrayRef<ValueDecl *> otherDefinitions;
SmallVector<ValueDecl *, 4> otherDefinitions;
if (currentDC->isTypeContext()) {
// Look within a type context.
if (auto nominal = currentDC->getAsNominalTypeOrNominalTypeExtensionContext()) {
otherDefinitions = nominal->lookupDirect(current->getBaseName());
auto found = nominal->lookupDirect(current->getBaseName());
otherDefinitions.append(found.begin(), found.end());
if (tracker)
tracker->addUsedMember({nominal, current->getName()}, isCascading);
}
} else {
// Look within a module context.
currentFile->getParentModule()->lookupValue({ }, current->getBaseName(),
NLKind::QualifiedLookup,
otherDefinitionsVec);
otherDefinitions = otherDefinitionsVec;
otherDefinitions);
if (tracker)
tracker->addTopLevelName(current->getName(), isCascading);
}
Expand Down
3 changes: 3 additions & 0 deletions test/Serialization/Inputs/circular-associated-type/a.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
public protocol A {
associatedtype T : B
}
7 changes: 7 additions & 0 deletions test/Serialization/Inputs/circular-associated-type/b.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
public protocol BB {
associatedtype T
}

public protocol B {
associatedtype T : BB
}
5 changes: 5 additions & 0 deletions test/Serialization/Inputs/circular-associated-type/c.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
extension B {
public init?<T : A>(_: T) where T.T == Self {
return nil
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// RUN: rm -rf %t && mkdir -p %t

// RUN: %target-swift-frontend -emit-module -module-name Multi -o %t/a.swiftmodule -primary-file %S/Inputs/circular-associated-type/a.swift %S/Inputs/circular-associated-type/b.swift %S/Inputs/circular-associated-type/c.swift
// RUN: %target-swift-frontend -emit-module -module-name Multi -o %t/b.swiftmodule -primary-file %S/Inputs/circular-associated-type/b.swift %S/Inputs/circular-associated-type/a.swift %S/Inputs/circular-associated-type/c.swift
// RUN: %target-swift-frontend -emit-module -module-name Multi -o %t/c.swiftmodule -primary-file %S/Inputs/circular-associated-type/c.swift %S/Inputs/circular-associated-type/a.swift %S/Inputs/circular-associated-type/b.swift

// RUN: %target-swift-frontend -parse-as-library -emit-module -module-name Multi %t/a.swiftmodule %t/b.swiftmodule %t/c.swiftmodule -o %t