Skip to content

[Name lookup] Deduplicate nominal declarations found via resolveTypeDeclsToNominal #26174

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
13 changes: 9 additions & 4 deletions lib/AST/NameLookup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1734,12 +1734,17 @@ resolveTypeDeclsToNominal(Evaluator &evaluator,
SmallVectorImpl<ModuleDecl *> &modulesFound,
bool &anyObject,
llvm::SmallPtrSetImpl<TypeAliasDecl *> &typealiases) {
SmallPtrSet<NominalTypeDecl *, 4> knownNominalDecls;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would SetVector<NominalTypeDecl *, TinyPtrVector<NominalTypeDecl *>, SmallPtrSet<NominalTypeDecl *, 4>> make this simpler or more complicated?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's clever to simplify it. Then I can use takeVector to steal its vector.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sigh.

error: no type named 'size_type' in 'llvm::TinyPtrVector<swift::NominalTypeDecl *>'

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sigh.

TinyPtrVector<NominalTypeDecl *> nominalDecls;
auto addNominalDecl = [&](NominalTypeDecl *nominal) {
if (knownNominalDecls.insert(nominal).second)
nominalDecls.push_back(nominal);
};

for (auto typeDecl : typeDecls) {
// Nominal type declarations get copied directly.
if (auto nominalDecl = dyn_cast<NominalTypeDecl>(typeDecl)) {
nominalDecls.push_back(nominalDecl);
addNominalDecl(nominalDecl);
continue;
}

Expand All @@ -1756,9 +1761,9 @@ resolveTypeDeclsToNominal(Evaluator &evaluator,
auto underlyingNominalReferences
= resolveTypeDeclsToNominal(evaluator, ctx, underlyingTypeReferences,
modulesFound, anyObject, typealiases);
nominalDecls.insert(nominalDecls.end(),
underlyingNominalReferences.begin(),
underlyingNominalReferences.end());
std::for_each(underlyingNominalReferences.begin(),
underlyingNominalReferences.end(),
addNominalDecl);

// Recognize Swift.AnyObject directly.
if (typealias->getName().is("AnyObject")) {
Expand Down
8 changes: 8 additions & 0 deletions test/NameBinding/Inputs/property_wrappers_A.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
@propertyWrapper
public struct Wrapper<Value> {
public var wrappedValue: Value
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Funny indentation

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ugh, annoying. I'll clean it up in a follow-up


public init(wrappedValue: Value) {
self.wrappedValue = wrappedValue
}
}
3 changes: 3 additions & 0 deletions test/NameBinding/Inputs/property_wrappers_B.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import property_wrappers_A

public typealias Wrapper = property_wrappers_A.Wrapper
10 changes: 10 additions & 0 deletions test/NameBinding/property_wrappers_ambig.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// RUN: %empty-directory(%t)
// RUN: %target-swift-frontend -emit-module -I %t -o %t %S/Inputs/property_wrappers_A.swift
// RUN: %target-swift-frontend -emit-module -I %t -o %t %S/Inputs/property_wrappers_B.swift
// RUN: %target-swift-frontend -typecheck -verify -I %t %s
import property_wrappers_A
import property_wrappers_B

struct Test {
@Wrapper var x: Int = 17
}