[ModuleInterface] Don't print typealiases that match generic params

These are synthesized to satisfy associated type requirements, but
they're not needed in source, and they look like self-referential
definitions (`typealias X = X`).
This commit is contained in:
Jordan Rose
2018-08-16 16:55:44 -07:00
parent d6c8ca016b
commit f061dff9b7
2 changed files with 42 additions and 2 deletions

View File

@@ -72,8 +72,9 @@ PrintOptions PrintOptions::printTextualInterfaceFile() {
result.FullyQualifiedTypes = true;
result.SkipImports = true;
class UsableFromInlineOnly : public ShouldPrintChecker {
class ShouldPrintForTextualInterface : public ShouldPrintChecker {
bool shouldPrint(const Decl *D, const PrintOptions &options) override {
// Skip anything that isn't 'public' or '@usableFromInline'.
if (auto *VD = dyn_cast<ValueDecl>(D)) {
AccessScope accessScope =
VD->getFormalAccessScope(/*useDC*/nullptr,
@@ -81,10 +82,29 @@ PrintOptions PrintOptions::printTextualInterfaceFile() {
if (!accessScope.isPublic())
return false;
}
// Skip typealiases that just redeclare generic parameters.
if (auto *alias = dyn_cast<TypeAliasDecl>(D)) {
if (alias->isImplicit()) {
const Decl *parent =
D->getDeclContext()->getAsDeclOrDeclExtensionContext();
if (auto *genericCtx = parent->getAsGenericContext()) {
bool matchesGenericParam =
llvm::any_of(genericCtx->getInnermostGenericParamTypes(),
[alias](const GenericTypeParamType *param) {
return param->getName() == alias->getName();
});
if (matchesGenericParam)
return false;
}
}
}
return ShouldPrintChecker::shouldPrint(D, options);
}
};
result.CurrentPrintabilityChecker = std::make_shared<UsableFromInlineOnly>();
result.CurrentPrintabilityChecker =
std::make_shared<ShouldPrintForTextualInterface>();
// FIXME: We don't really need 'public' on everything; we could just change
// the default to 'public' and mark the 'internal' things.

View File

@@ -0,0 +1,20 @@
// RUN: %target-swift-frontend -emit-interface-path %t.swiftinterface -emit-module -o /dev/null %s
// RUN: %FileCheck %s < %t.swiftinterface
// RUN: %FileCheck -check-prefix NEGATIVE %s < %t.swiftinterface
// CHECK-LABEL: public protocol SimpleProto {
public protocol SimpleProto {
// CHECK: associatedtype Element
associatedtype Element
// CHECK: associatedtype Inferred
associatedtype Inferred
func inference(_: Inferred)
} // CHECK: {{^}$}}
// CHECK-LABEL: public struct SimplImpl<Element> : SimpleProto {
public struct SimplImpl<Element>: SimpleProto {
// NEGATIVE-NOT: typealias Element =
// CHECK: public func inference(_: Int){{$}}
public func inference(_: Int) {}
// CHECK: public typealias Inferred = Swift.Int
} // CHECK: {{^}$}}