Skip to content

Commit 889e430

Browse files
author
Harlan Haskins
committed
[ModuleInterface] Allow falling back to prefer TypeReprs
There are still cases (a module with a type that's the same name as the module) where we cannot fully qualify all types. In those cases, allow them to remain unqualified with a flag, `-Xfrontend -preserve-types-as-written-in-module-interface`.
1 parent a480404 commit 889e430

File tree

7 files changed

+57
-4
lines changed

7 files changed

+57
-4
lines changed

include/swift/AST/PrintOptions.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -494,7 +494,7 @@ struct PrintOptions {
494494
/// consistent and well-formed.
495495
///
496496
/// \see swift::emitParseableInterface
497-
static PrintOptions printParseableInterfaceFile();
497+
static PrintOptions printParseableInterfaceFile(bool preferTypeRepr);
498498

499499
static PrintOptions printModuleInterface();
500500
static PrintOptions printTypeInterface(Type T);

include/swift/Frontend/ParseableInterfaceSupport.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ class ModuleDecl;
2727

2828
/// Options for controlling the generation of the .swiftinterface output.
2929
struct ParseableInterfaceOptions {
30+
/// Should we prefer printing TypeReprs when writing out types in a module
31+
/// interface, or should we fully-qualify them?
32+
bool PreserveTypesAsWrittenInModuleInterface = false;
33+
3034
/// Copy of all the command-line flags passed at .swiftinterface
3135
/// generation time, re-applied to CompilerInvocation when reading
3236
/// back .swiftinterface and reconstructing .swiftmodule.

include/swift/Option/FrontendOptions.td

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,11 @@ def build_module_from_parseable_interface :
535535
Alias<compile_module_from_interface>,
536536
ModeOpt;
537537

538+
def preserve_types_as_written_in_module_interface :
539+
Flag<["-"], "preserve-types-as-written-in-module-interface">,
540+
HelpText<"When emitting a module interface, preserve the types as they were "
541+
"written in the source, rather than fully-qualifying them.">;
542+
538543
def prebuilt_module_cache_path :
539544
Separate<["-"], "prebuilt-module-cache-path">,
540545
HelpText<"Directory of prebuilt modules for loading module interfaces">;

lib/AST/ASTPrinter.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ static bool contributesToParentTypeStorage(const AbstractStorageDecl *ASD) {
9494
return !ND->isResilient() && ASD->hasStorage() && !ASD->isStatic();
9595
}
9696

97-
PrintOptions PrintOptions::printParseableInterfaceFile() {
97+
PrintOptions PrintOptions::printParseableInterfaceFile(bool preferTypeRepr) {
9898
PrintOptions result;
9999
result.PrintLongAttrsOnSeparateLines = true;
100100
result.TypeDefinitions = true;
@@ -110,7 +110,7 @@ PrintOptions PrintOptions::printParseableInterfaceFile() {
110110
result.EnumRawValues = EnumRawValueMode::PrintObjCOnly;
111111
result.OpaqueReturnTypePrinting =
112112
OpaqueReturnTypePrintingMode::StableReference;
113-
result.PreferTypeRepr = false;
113+
result.PreferTypeRepr = preferTypeRepr;
114114

115115
// We should print __consuming, __owned, etc for the module interface file.
116116
result.SkipUnderscoredKeywords = false;

lib/Frontend/CompilerInvocation.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,14 @@ static void PrintArg(raw_ostream &OS, const char *Arg, StringRef TempDir) {
197197
OS << '"';
198198
}
199199

200+
static void ParseParseableInterfaceArgs(ParseableInterfaceOptions &Opts,
201+
ArgList &Args) {
202+
using namespace options;
203+
204+
Opts.PreserveTypesAsWrittenInModuleInterface |=
205+
Args.hasArg(OPT_preserve_types_as_written_in_module_interface);
206+
}
207+
200208
/// Save a copy of any flags marked as ModuleInterfaceOption, if running
201209
/// in a mode that is going to emit a .swiftinterface file.
202210
static void SaveParseableInterfaceArgs(ParseableInterfaceOptions &Opts,
@@ -1305,6 +1313,7 @@ bool CompilerInvocation::parseArgs(
13051313
return true;
13061314
}
13071315

1316+
ParseParseableInterfaceArgs(ParseableInterfaceOpts, ParsedArgs);
13081317
SaveParseableInterfaceArgs(ParseableInterfaceOpts, FrontendOpts,
13091318
ParsedArgs, Diags);
13101319

lib/Frontend/ParseableInterfaceSupport.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,8 @@ bool swift::emitParseableInterface(raw_ostream &out,
411411
printToolVersionAndFlagsComment(out, Opts, M);
412412
printImports(out, M);
413413

414-
const PrintOptions printOptions = PrintOptions::printParseableInterfaceFile();
414+
const PrintOptions printOptions = PrintOptions::printParseableInterfaceFile(
415+
Opts.PreserveTypesAsWrittenInModuleInterface);
415416
InheritedProtocolCollector::PerTypeMap inheritedProtocolMap;
416417

417418
SmallVector<Decl *, 16> topLevelDecls;
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// RUN: %target-swift-frontend -typecheck -emit-module-interface-path - %s -enable-library-evolution -module-name PreferTypeRepr -preserve-types-as-written-in-module-interface | %FileCheck %s --check-prefix PREFER
2+
// RUN: %target-swift-frontend -typecheck -emit-module-interface-path - %s -enable-library-evolution -module-name PreferTypeRepr | %FileCheck %s --check-prefix DONTPREFER
3+
4+
public protocol Pet {}
5+
6+
// PREFER: public struct Parrot : Pet {
7+
// DONTPREFER: public struct Parrot : PreferTypeRepr.Pet {
8+
// CHECK-NEXT: }
9+
public struct Parrot: Pet {}
10+
11+
// CHECK: public struct Ex<T> where T : PreferTypeRepr.Pet {
12+
public struct Ex<T: Pet> {
13+
// PREFER: public var hasCeasedToBe: Bool {
14+
// DONTPREFER: public var hasCeasedToBe: Swift.Bool {
15+
// CHECK: get
16+
// CHECK-NEXT: }
17+
public var hasCeasedToBe: Bool { false }
18+
19+
// CHECK-NEXT: }
20+
}
21+
22+
// CHECK: public struct My<T> {
23+
// CHECK-NEXT: }
24+
public struct My<T> {}
25+
26+
// CHECK: extension My where T : PreserveTypeRepr.Pet
27+
extension My where T: Pet {
28+
// CHECK: public func isPushingUpDaisies() -> Swift.String
29+
public func isPushingUpDaisies() -> String { "" }
30+
}
31+
32+
// PREFER: public func isNoMore(_ pet: Ex<Parrot>) -> Bool
33+
// DONTPREFER: public func isNoMore(_ pet: PreferTypeRepr.Ex<PreferTypeRepr.Parrot>) -> Swift.Bool
34+
public func isNoMore(_ pet: Ex<Parrot>) -> Bool {}

0 commit comments

Comments
 (0)