Skip to content

Commit 700d9fd

Browse files
committed
[IDE] Avoid printing some Swift extensions twice in mixed source frameworks
`printModuleInterfaceDecl` printes extensions right after the type they are associated with is printed. Extensions associated with a type that appears in the "target" module shouldn't be added to `SwiftDecls` because that would lead to double printing them.
1 parent 62a9178 commit 700d9fd

File tree

5 files changed

+50
-11
lines changed

5 files changed

+50
-11
lines changed

lib/IDE/ModuleInterfacePrinting.cpp

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -145,17 +145,6 @@ static bool printModuleInterfaceDecl(Decl *D,
145145
Printer.callAvoidPrintDeclPost(D);
146146
return false;
147147
}
148-
if (auto Ext = dyn_cast<ExtensionDecl>(D)) {
149-
// Clang extensions (categories) are always printed in source order.
150-
// Swift extensions are printed with their associated type unless it's
151-
// a cross-module extension.
152-
if (!extensionHasClangNode(Ext)) {
153-
auto ExtendedNominal = Ext->getExtendedNominal();
154-
if (!ExtendedNominal ||
155-
Ext->getModuleContext() == ExtendedNominal->getModuleContext())
156-
return false;
157-
}
158-
}
159148

160149
// It'd be nice to avoid cloning the options here, but that would require
161150
// SynthesizedExtensionAnalyzer to promise to stay within the lifetime of
@@ -677,6 +666,14 @@ void swift::ide::printModuleInterface(
677666
addToClangDecls(Ext, extensionGetClangNode(Ext));
678667
continue;
679668
}
669+
670+
// Swift extensions are printed with their associated type unless it's
671+
// a cross-module extension.
672+
if (auto extendedTy = Ext->getExtendedNominal()) {
673+
if (TargetMod->isSameModuleLookingThroughOverlays(
674+
extendedTy->getModuleContext()))
675+
continue;
676+
}
680677
}
681678

682679
if (!IsSubmodule) {
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
@interface MyType
2+
@end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// swift-interface-format-version: 1.0
2+
// swift-compiler-version: Apple Swift version 6.2
3+
// swift-module-flags: -target arm64-apple-macos10.13 -enable-library-evolution -swift-version 5 -module-name TestExt
4+
5+
@_exported import TestExt
6+
7+
extension MyType {
8+
public struct MyInnerType {
9+
public func test() {}
10+
}
11+
}
12+
13+
extension Int {
14+
public struct OtherInnerType {
15+
}
16+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
framework module TestExt {
2+
header "MyType.h"
3+
export *
4+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// RUN: %target-swift-synthesize-interface -module-name TestExt -F %S/Inputs/Frameworks -o - | %FileCheck %s
2+
3+
// REQUIRES: OS=macosx && CPU=arm64
4+
5+
// CHECK: open class MyType
6+
7+
// CHECK: extension MyType {
8+
// CHECK: public struct MyInnerType {
9+
// CHECK: public func test()
10+
// CHECK: }
11+
// CHECK: }
12+
13+
// CHECK-NOT: public struct MyInnerType
14+
15+
// CHECK: extension Int {
16+
// CHECK: public struct OtherInnerType {
17+
// CHECK: }
18+
// CHECK: }
19+
20+
// CHECK-NOT: public struct MyInnerType

0 commit comments

Comments
 (0)