Skip to content

Dependency analysis: treat member operators as top-level "provides". #3986

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 1 commit into from
Aug 4, 2016
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
36 changes: 30 additions & 6 deletions lib/FrontendTool/FrontendTool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,14 +125,31 @@ static bool emitMakeDependencies(DiagnosticEngine &diags,
return false;
}

static void findNominals(llvm::MapVector<const NominalTypeDecl *, bool> &found,
DeclRange members) {
static void findNominalsAndOperators(
llvm::MapVector<const NominalTypeDecl *, bool> &foundNominals,
llvm::SmallVectorImpl<const FuncDecl *> &foundOperators,
DeclRange members) {
for (const Decl *D : members) {
auto *VD = dyn_cast<ValueDecl>(D);
if (!VD)
continue;

if (VD->hasAccessibility() &&
VD->getFormalAccess() <= Accessibility::FilePrivate) {
continue;
}

if (VD->getFullName().isOperator()) {
foundOperators.push_back(cast<FuncDecl>(VD));
continue;
}

auto nominal = dyn_cast<NominalTypeDecl>(D);
if (!nominal)
continue;
found[nominal] |= true;
findNominals(found, nominal->getMembers());
foundNominals[nominal] |= true;
findNominalsAndOperators(foundNominals, foundOperators,
nominal->getMembers());
}
}

Expand Down Expand Up @@ -209,6 +226,7 @@ static bool emitReferenceDependencies(DiagnosticEngine &diags,
out << "### Swift dependencies file v0 ###\n";

llvm::MapVector<const NominalTypeDecl *, bool> extendedNominals;
llvm::SmallVector<const FuncDecl *, 8> memberOperatorDecls;
llvm::SmallVector<const ExtensionDecl *, 8> extensionsWithJustMembers;

out << "provides-top-level:\n";
Expand Down Expand Up @@ -243,7 +261,8 @@ static bool emitReferenceDependencies(DiagnosticEngine &diags,
}
}
extendedNominals[NTD] |= !justMembers;
findNominals(extendedNominals, ED->getMembers());
findNominalsAndOperators(extendedNominals, memberOperatorDecls,
ED->getMembers());
break;
}

Expand All @@ -270,7 +289,8 @@ static bool emitReferenceDependencies(DiagnosticEngine &diags,
}
out << "- \"" << escape(NTD->getName()) << "\"\n";
extendedNominals[NTD] |= true;
findNominals(extendedNominals, NTD->getMembers());
findNominalsAndOperators(extendedNominals, memberOperatorDecls,
NTD->getMembers());
break;
}

Expand Down Expand Up @@ -306,6 +326,10 @@ static bool emitReferenceDependencies(DiagnosticEngine &diags,
}
}

// This is also part of "provides-top-level".
for (auto *operatorFunction : memberOperatorDecls)
out << "- \"" << escape(operatorFunction->getName()) << "\"\n";

out << "provides-nominal:\n";
for (auto entry : extendedNominals) {
if (!entry.second)
Expand Down
16 changes: 16 additions & 0 deletions test/NameBinding/Inputs/reference-dependencies-helper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,22 @@ func getOtherFileIntArray() -> OtherFileIntArray { return OtherFileIntArray() }
typealias OtherFileAliasForSecret = OtherFileSecretTypeWrapper.SecretType

prefix operator *** {}
prefix operator ~~~~~ {}

prefix operator ****
infix operator *****
infix operator ******
protocol Starry {
static prefix func ****(arg: Self)
static func *****(lhs: Self, rhs: Int)
static func ******(lhs: Int, rhs: Self)
}
// Deliberately does not conform to Starry.
struct Flyswatter {
static prefix func ****(arg: Flyswatter) {}
static func *****(lhs: Flyswatter, rhs: Int) {}
static func ******(lhs: Int, rhs: Flyswatter) {}
}

typealias ExpressibleByOtherFileAliasForFloatLiteral = ExpressibleByFloatLiteral

Expand Down
41 changes: 41 additions & 0 deletions test/NameBinding/reference-dependencies.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,22 @@
// CHECK-NEXT: "ThreeTilde"
// CHECK-NEXT: "overloadedOnProto"
// CHECK-NEXT: "overloadedOnProto"
// CHECK-NEXT: "~~~~"
// CHECK-NEXT: "FourTilde"
// CHECK-NEXT: "FourTildeImpl"
// CHECK-NEXT: "FiveTildeImpl"
// CHECK-NEXT: "topLevelComputedProperty"
// CHECK-NEXT: "lookUpManyTopLevelNames"
// CHECK-NEXT: "testOperators"
// CHECK-NEXT: "TopLevelForMemberLookup"
// CHECK-NEXT: "lookUpMembers"
// CHECK-NEXT: "publicUseOfMember"
// CHECK-NEXT: "Outer"
// CHECK: "eof"
// CHECK-NEXT: "~~~"
// CHECK-NEXT: "~~~~"
// CHECK-NEXT: "~~~~"
// CHECK-NEXT: "~~~~~"

// CHECK-LABEL: {{^provides-nominal:$}}
// CHECK-NEXT: "V4main10IntWrapper"
Expand All @@ -46,6 +55,8 @@
// CHECK: - ["Ps25ExpressibleByArrayLiteral", "useless"]
// CHECK-NEXT: - ["Ps25ExpressibleByArrayLiteral", "useless2"]
// CHECK-NEXT: - ["Sb", "InnerToBool"]
// CHECK-NEXT: - ["{{.*[0-9]}}FourTildeImpl", "~~~~"]
// CHECK-NEXT: - ["{{.*[0-9]}}FiveTildeImpl", "~~~~~"]

// CHECK-LABEL: {{^depends-top-level:$}}

Expand Down Expand Up @@ -129,6 +140,23 @@ func overloadedOnProto<T: ThreeTilde>(_: T) {}
// CHECK-DAG: - "~~~"
private prefix func ~~~(_: ThreeTildeTypeImpl) {}

// CHECK-DAG: - "~~~~"
prefix operator ~~~~ {}
protocol FourTilde {
prefix static func ~~~~(arg: Self)
}
struct FourTildeImpl : FourTilde {}
extension FourTildeImpl {
prefix static func ~~~~(arg: FourTildeImpl) {}
}

// CHECK-DAG: - "~~~~~"
// ~~~~~ is declared in the other file.
struct FiveTildeImpl {}
extension FiveTildeImpl {
prefix static func ~~~~~(arg: FiveTildeImpl) {}
}

var topLevelComputedProperty: Bool {
return true
}
Expand Down Expand Up @@ -213,6 +241,19 @@ func lookUpManyTopLevelNames() {
overloadedOnProto(otherFileGetNonImpl())
}

func testOperators<T: Starry>(generic: T, specific: Flyswatter) {
// CHECK-DAG: !private "****"
// CHECK-DAG: !private "*****"
// CHECK-DAG: !private "******"
****generic
generic*****0
0******generic

****specific
specific*****0
0******specific
}

struct TopLevelForMemberLookup {
static func m1() {}
static func m2() {}
Expand Down