Skip to content

Commit f14a8eb

Browse files
committed
[ModuleInterface] Always use the real name for modules not imported directly
In AliasModuleNames, avoid wrongfully printing aliased names for modules that were not aliased. This can happen in the case of modules indirectly imported via a reexport. rdar://102262019
1 parent 81c19b5 commit f14a8eb

File tree

4 files changed

+47
-11
lines changed

4 files changed

+47
-11
lines changed

include/swift/AST/PrintOptions.h

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "swift/AST/TypeOrExtensionDecl.h"
2020
#include "llvm/ADT/Optional.h"
2121
#include "llvm/ADT/DenseMap.h"
22+
#include "llvm/ADT/SmallSet.h"
2223
#include <limits.h>
2324
#include <vector>
2425

@@ -470,6 +471,14 @@ struct PrintOptions {
470471
/// with types sharing a name with a module.
471472
bool AliasModuleNames = false;
472473

474+
/// Name of the modules that have been aliased in AliasModuleNames mode.
475+
/// Ideally we would use something other than a string to identify a module,
476+
/// but since one alias can apply to more than one module, strings happen
477+
/// to be pretty reliable. That is, unless there's an unexpected name
478+
/// collision between two modules, which isn't supported by this workaround
479+
/// yet.
480+
llvm::SmallSet<StringRef, 4> *AliasModuleNamesTargets = nullptr;
481+
473482
/// When printing an Optional<T>, rather than printing 'T?', print
474483
/// 'T!'. Used as a modifier only when we know we're printing
475484
/// something that was declared as an implicitly unwrapped optional
@@ -641,7 +650,10 @@ struct PrintOptions {
641650
bool preferTypeRepr,
642651
bool printFullConvention,
643652
bool printSPIs,
644-
bool aliasModuleNames);
653+
bool aliasModuleNames,
654+
llvm::SmallSet<StringRef, 4>
655+
*aliasModuleNamesTargets
656+
);
645657

646658
/// Retrieve the set of options suitable for "Generated Interfaces", which
647659
/// are a prettified representation of the public API of a module, to be

lib/AST/ASTPrinter.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,10 @@ PrintOptions PrintOptions::printSwiftInterfaceFile(ModuleDecl *ModuleToPrint,
132132
bool preferTypeRepr,
133133
bool printFullConvention,
134134
bool printSPIs,
135-
bool aliasModuleNames) {
135+
bool aliasModuleNames,
136+
llvm::SmallSet<StringRef, 4>
137+
*aliasModuleNamesTargets
138+
) {
136139
PrintOptions result;
137140
result.IsForSwiftInterface = true;
138141
result.PrintLongAttrsOnSeparateLines = true;
@@ -154,6 +157,7 @@ PrintOptions PrintOptions::printSwiftInterfaceFile(ModuleDecl *ModuleToPrint,
154157
OpaqueReturnTypePrintingMode::StableReference;
155158
result.PreferTypeRepr = preferTypeRepr;
156159
result.AliasModuleNames = aliasModuleNames;
160+
result.AliasModuleNamesTargets = aliasModuleNamesTargets;
157161
if (printFullConvention)
158162
result.PrintFunctionRepresentationAttrs =
159163
PrintOptions::FunctionRepresentationMode::Full;
@@ -5393,7 +5397,8 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
53935397
}
53945398
}
53955399

5396-
if (Options.AliasModuleNames) {
5400+
if (Options.AliasModuleNames && Options.AliasModuleNamesTargets &&
5401+
Options.AliasModuleNamesTargets->contains(Name.str())) {
53975402
auto nameTwine = MODULE_DISAMBIGUATING_PREFIX + Name.str();
53985403
Name = Mod->getASTContext().getIdentifier(nameTwine.str());
53995404
}

lib/Frontend/ModuleInterfaceSupport.cpp

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,9 @@ version::Version swift::InterfaceFormatVersion({1, 0});
4949
/// construct \p M.
5050
static void printToolVersionAndFlagsComment(raw_ostream &out,
5151
ModuleInterfaceOptions const &Opts,
52-
ModuleDecl *M) {
52+
ModuleDecl *M,
53+
llvm::SmallSet<StringRef, 4>
54+
&AliasModuleNamesTargets) {
5355
auto &Ctx = M->getASTContext();
5456
auto ToolsVersion =
5557
getSwiftInterfaceCompilerVersionForCurrentCompiler(Ctx);
@@ -62,9 +64,8 @@ static void printToolVersionAndFlagsComment(raw_ostream &out,
6264

6365
// Insert additional -module-alias flags
6466
if (Opts.AliasModuleNames) {
65-
llvm::SmallSet<StringRef, 2> aliasTargets;
6667
StringRef moduleName = M->getNameStr();
67-
aliasTargets.insert(M->getNameStr());
68+
AliasModuleNamesTargets.insert(M->getNameStr());
6869
out << " -module-alias " << MODULE_DISAMBIGUATING_PREFIX <<
6970
moduleName << "=" << moduleName;
7071

@@ -77,7 +78,7 @@ static void printToolVersionAndFlagsComment(raw_ostream &out,
7778
M->getMissingImportedModules(imports);
7879
for (ImportedModule import: imports) {
7980
StringRef importedName = import.importedModule->getNameStr();
80-
if (aliasTargets.insert(importedName).second) {
81+
if (AliasModuleNamesTargets.insert(importedName).second) {
8182
out << " -module-alias " << MODULE_DISAMBIGUATING_PREFIX <<
8283
importedName << "=" << importedName;
8384
}
@@ -782,12 +783,14 @@ bool swift::emitSwiftInterface(raw_ostream &out,
782783
ModuleDecl *M) {
783784
assert(M);
784785

785-
printToolVersionAndFlagsComment(out, Opts, M);
786+
llvm::SmallSet<StringRef, 4> aliasModuleNamesTargets;
787+
printToolVersionAndFlagsComment(out, Opts, M, aliasModuleNamesTargets);
788+
786789
printImports(out, Opts, M);
787790

788791
const PrintOptions printOptions = PrintOptions::printSwiftInterfaceFile(
789792
M, Opts.PreserveTypesAsWritten, Opts.PrintFullConvention, Opts.PrintSPIs,
790-
Opts.AliasModuleNames);
793+
Opts.AliasModuleNames, &aliasModuleNamesTargets);
791794
InheritedProtocolCollector::PerTypeMap inheritedProtocolMap;
792795

793796
SmallVector<Decl *, 16> topLevelDecls;

test/ModuleInterface/ambiguous-aliases-workaround.swift

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,19 @@
33
// RUN: %empty-directory(%t)
44
// RUN: split-file %s %t
55

6+
// RUN: %target-swift-frontend -emit-module -module-name ExportedLib \
7+
// RUN: -swift-version 5 -enable-library-evolution \
8+
// RUN: -o %t/ExportedLib.swiftmodule \
9+
// RUN: -emit-module-interface-path %t/ExportedLib.swiftinterface \
10+
// RUN: %t/ExportedLib.swift
11+
// RUN: %target-swift-typecheck-module-from-interface(%t/ExportedLib.swiftinterface)
12+
613
// RUN: %target-swift-frontend -emit-module -module-name AmbiguousLib \
714
// RUN: -swift-version 5 -enable-library-evolution \
815
// RUN: -o %t/AmbiguousLib.swiftmodule \
916
// RUN: -emit-module-interface-path %t/AmbiguousLib.swiftinterface \
10-
// RUN: %t/AmbiguousLib.swift
11-
// RUN: %target-swift-typecheck-module-from-interface(%t/AmbiguousLib.swiftinterface)
17+
// RUN: %t/AmbiguousLib.swift -I%t
18+
// RUN: %target-swift-typecheck-module-from-interface(%t/AmbiguousLib.swiftinterface) -I%t
1219

1320
// RUN: %target-swift-frontend -emit-module -module-name AmbiguousClientName \
1421
// RUN: -swift-version 5 -enable-library-evolution \
@@ -40,10 +47,17 @@ struct UnderlyingType {};
4047
void underlyingFunc() {}
4148

4249
//--- SomeClangModule.h
50+
4351
struct CType {};
4452

53+
//--- ExportedLib.swift
54+
55+
public struct RexportedStruct {}
56+
4557
//--- AmbiguousLib.swift
4658

59+
@_exported import ExportedLib
60+
4761
// 1. AmbiguousLib defined a type named AmbiguousLib
4862
public struct AmbiguousLib {
4963
public struct Nested {}
@@ -73,6 +87,8 @@ public func refToStdlib(_ a: Swift.Int) {}
7387
public func refToUnderlying(_ a: UnderlyingType) {}
7488
public func refToC(_ a: CType) {}
7589

90+
public func refToReexport(_ a: RexportedStruct) {}
91+
7692
//--- OverlayClient.swift
7793

7894
import AmbiguousClientName

0 commit comments

Comments
 (0)