Skip to content

Commit 0366343

Browse files
committed
[ParseableInterface] Honor "exported module names"
(as described in the previous commit) When printing an interface that has to be stable, we need to use the module name that identifies where declarations should be searched for, just like we do with serialization. rdar://problem/49114811
1 parent 93616cd commit 0366343

File tree

9 files changed

+57
-2
lines changed

9 files changed

+57
-2
lines changed

include/swift/AST/PrintOptions.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,12 @@ struct PrintOptions {
185185
/// type might be ambiguous.
186186
bool FullyQualifiedTypesIfAmbiguous = false;
187187

188+
/// If true, printed module names will use the "exported" name, which may be
189+
/// different from the regular name.
190+
///
191+
/// \see FileUnit::getExportedModuleName
192+
bool UseExportedModuleNames = false;
193+
188194
/// Print Swift.Array and Swift.Optional with sugared syntax
189195
/// ([] and ?), even if there are no sugar type nodes.
190196
bool SynthesizeSugarOnTypes = false;

lib/AST/ASTPrinter.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ PrintOptions PrintOptions::printParseableInterfaceFile() {
9999
result.TypeDefinitions = true;
100100
result.PrintIfConfig = false;
101101
result.FullyQualifiedTypes = true;
102+
result.UseExportedModuleNames = true;
102103
result.AllowNullTypes = false;
103104
result.SkipImports = true;
104105
result.OmitNameOfInaccessibleProperties = true;
@@ -3342,8 +3343,14 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
33423343

33433344
template <typename T>
33443345
void printModuleContext(T *Ty) {
3345-
ModuleDecl *Mod = Ty->getDecl()->getModuleContext();
3346-
Printer.printModuleRef(Mod, Mod->getName());
3346+
FileUnit *File = cast<FileUnit>(Ty->getDecl()->getModuleScopeContext());
3347+
ModuleDecl *Mod = File->getParentModule();
3348+
3349+
Identifier Name = Mod->getName();
3350+
if (Options.UseExportedModuleNames)
3351+
Name = Mod->getASTContext().getIdentifier(File->getExportedModuleName());
3352+
3353+
Printer.printModuleRef(Mod, Name);
33473354
Printer << ".";
33483355
}
33493356

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#import <ExportAsCoreKit.h>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
struct CKThing {
2+
long value;
3+
};
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module CoreKit {
2+
header "CoreKit.h"
3+
header "ExportAsCoreKit.h"
4+
export *
5+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#import <ExportAsCoreKit.h>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
struct CKThing {
2+
long value;
3+
};
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
module CoreKit {
2+
header "CoreKit.h"
3+
export *
4+
}
5+
6+
module ExportAsCoreKit_BAD {
7+
header "ExportAsCoreKit.h"
8+
export_as CoreKit
9+
export *
10+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: %target-swift-frontend -typecheck -emit-parseable-module-interface-path %t/CoreKitClient.swiftinterface -module-name CoreKitClient -I %S/Inputs/exported-module-name-before %s
3+
// RUN: %FileCheck -implicit-check-not BAD %s < %t/CoreKitClient.swiftinterface
4+
5+
// Test that we can rebuild it even when the "export as" module goes away.
6+
// RUN: %target-swift-frontend -build-module-from-parseable-interface -o %t/CoreKitClient.swiftmodule -I %S/Inputs/exported-module-name-after %t/CoreKitClient.swiftinterface
7+
8+
// CHECK: import CoreKit
9+
import CoreKit
10+
11+
// CHECK-LABEL: public struct CKThingWrapper : RawRepresentable {
12+
public struct CKThingWrapper: RawRepresentable {
13+
public var rawValue: CKThing
14+
public init(rawValue: CKThing) {
15+
self.rawValue = rawValue
16+
}
17+
// Note that this is CoreKit.CKThing, not ExportAsCoreKit.CKThing
18+
// CHECK: public typealias RawValue = CoreKit.CKThing
19+
} // CHECK: {{^}$}}

0 commit comments

Comments
 (0)