From 823d3d42716ac12771690ae83ce7c40cd5431a2e Mon Sep 17 00:00:00 2001 From: Brent Royal-Gordon Date: Tue, 21 Apr 2020 23:35:02 -0700 Subject: [PATCH 1/3] [NFC] Indicate foreign modules in AST dumps --- lib/AST/ASTDumper.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/AST/ASTDumper.cpp b/lib/AST/ASTDumper.cpp index a2d864c93da..b5e39aadcb8 100644 --- a/lib/AST/ASTDumper.cpp +++ b/lib/AST/ASTDumper.cpp @@ -1259,6 +1259,10 @@ namespace { void visitModuleDecl(ModuleDecl *MD) { printCommon(MD, "module"); + + if (MD->isNonSwiftModule()) + OS << " non_swift"; + PrintWithColorRAII(OS, ParenthesisColor) << ')'; } From d4eabeb4b124ff040adb7b9d5c152819bc84e84d Mon Sep 17 00:00:00 2001 From: Brent Royal-Gordon Date: Tue, 21 Apr 2020 23:35:25 -0700 Subject: [PATCH 2/3] [NFC] Add dumper for separately-imported overlays --- include/swift/AST/SourceFile.h | 2 ++ lib/AST/Module.cpp | 16 ++++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/include/swift/AST/SourceFile.h b/include/swift/AST/SourceFile.h index c7e0eabe650..e9905607d6f 100644 --- a/include/swift/AST/SourceFile.h +++ b/include/swift/AST/SourceFile.h @@ -389,6 +389,8 @@ public: overlays.append(value.begin(), value.end()); } + SWIFT_DEBUG_DUMPER(dumpSeparatelyImportedOverlays()); + void cacheVisibleDecls(SmallVectorImpl &&globals) const; const SmallVectorImpl &getCachedVisibleDecls() const; diff --git a/lib/AST/Module.cpp b/lib/AST/Module.cpp index c8a06289faf..93982ddb396 100644 --- a/lib/AST/Module.cpp +++ b/lib/AST/Module.cpp @@ -1430,6 +1430,22 @@ SourceFile::getImportedModules(SmallVectorImpl &modu } } +void SourceFile::dumpSeparatelyImportedOverlays() const { + for (auto &pair : separatelyImportedOverlays) { + auto &underlying = std::get<0>(pair); + auto &overlays = std::get<1>(pair); + + llvm::errs() << (void*)underlying << " "; + underlying->dump(llvm::errs()); + + for (auto overlay : overlays) { + llvm::errs() << "- "; + llvm::errs() << (void*)overlay << " "; + overlay->dump(llvm::errs()); + } + } +} + void ModuleDecl::getImportedModulesForLookup( SmallVectorImpl &modules) const { FORWARD(getImportedModulesForLookup, (modules)); From f38474896d18492465bd509b386a08409e8b160a Mon Sep 17 00:00:00 2001 From: Brent Royal-Gordon Date: Tue, 21 Apr 2020 23:38:07 -0700 Subject: [PATCH 3/3] Fix layering of cross-import and clang overlays If a clang module declares a cross-import overlay, but it also has a traditional overlay, we want the cross-import overlay to be registered with the SourceFile as sitting atop the traditional overlay. Otherwise module-qualified name lookups will bypass the cross-import overlay. Fixes rdar://62139656. --- lib/Sema/ImportResolution.cpp | 12 +++++- .../BystandingLibrary.swiftoverlay | 5 +++ .../BystandingLibrary.swiftoverlay | 5 +++ .../BystandingLibrary.swiftoverlay | 5 +++ .../module-triple-here.swiftinterface | 7 ++++ .../module-triple-here.swiftinterface | 7 ++++ .../module-triple-here.swiftinterface | 7 ++++ .../module-triple-here.swiftinterface | 2 + test/CrossImport/common-case.swift | 40 +++++++++++++++++++ 9 files changed, 88 insertions(+), 2 deletions(-) create mode 100644 test/CrossImport/Inputs/lib-templates/Frameworks/ClangFramework.framework/Modules/ClangFramework.swiftcrossimport/BystandingLibrary.swiftoverlay create mode 100644 test/CrossImport/Inputs/lib-templates/Frameworks/OverlaidClangFramework.framework/Modules/OverlaidClangFramework.swiftcrossimport/BystandingLibrary.swiftoverlay create mode 100644 test/CrossImport/Inputs/lib-templates/Frameworks/SwiftFramework.framework/Modules/SwiftFramework.swiftcrossimport/BystandingLibrary.swiftoverlay create mode 100644 test/CrossImport/Inputs/lib-templates/Frameworks/_ClangFramework_BystandingLibrary.framework/Modules/_ClangFramework_BystandingLibrary.swiftmodule/module-triple-here.swiftinterface create mode 100644 test/CrossImport/Inputs/lib-templates/Frameworks/_OverlaidClangFramework_BystandingLibrary.framework/Modules/_OverlaidClangFramework_BystandingLibrary.swiftmodule/module-triple-here.swiftinterface create mode 100644 test/CrossImport/Inputs/lib-templates/Frameworks/_SwiftFramework_BystandingLibrary.framework/Modules/_SwiftFramework_BystandingLibrary.swiftmodule/module-triple-here.swiftinterface create mode 100644 test/CrossImport/common-case.swift diff --git a/lib/Sema/ImportResolution.cpp b/lib/Sema/ImportResolution.cpp index 34a283ed345..a9f463dee43 100644 --- a/lib/Sema/ImportResolution.cpp +++ b/lib/Sema/ImportResolution.cpp @@ -896,9 +896,17 @@ void ImportResolver::crossImport(ModuleDecl *M, UnboundImport &I) { if (!SF.shouldCrossImport()) return; - if (I.getUnderlyingModule()) + if (I.getUnderlyingModule()) { + auto underlying = I.getUnderlyingModule().get(); + + // If this is a clang module, and it has a clang overlay, we want the + // separately-imported overlay to sit on top of the clang overlay. + if (underlying->isNonSwiftModule()) + underlying = underlying->getTopLevelModule(true); + // FIXME: Should we warn if M doesn't reexport underlyingModule? - SF.addSeparatelyImportedOverlay(M, I.getUnderlyingModule().get()); + SF.addSeparatelyImportedOverlay(M, underlying); + } auto newImports = crossImportableModules.getArrayRef() .drop_front(nextModuleToCrossImport); diff --git a/test/CrossImport/Inputs/lib-templates/Frameworks/ClangFramework.framework/Modules/ClangFramework.swiftcrossimport/BystandingLibrary.swiftoverlay b/test/CrossImport/Inputs/lib-templates/Frameworks/ClangFramework.framework/Modules/ClangFramework.swiftcrossimport/BystandingLibrary.swiftoverlay new file mode 100644 index 00000000000..211a35e6188 --- /dev/null +++ b/test/CrossImport/Inputs/lib-templates/Frameworks/ClangFramework.framework/Modules/ClangFramework.swiftcrossimport/BystandingLibrary.swiftoverlay @@ -0,0 +1,5 @@ +%YAML 1.2 +--- +version: 1 +modules: + - name: _ClangFramework_BystandingLibrary diff --git a/test/CrossImport/Inputs/lib-templates/Frameworks/OverlaidClangFramework.framework/Modules/OverlaidClangFramework.swiftcrossimport/BystandingLibrary.swiftoverlay b/test/CrossImport/Inputs/lib-templates/Frameworks/OverlaidClangFramework.framework/Modules/OverlaidClangFramework.swiftcrossimport/BystandingLibrary.swiftoverlay new file mode 100644 index 00000000000..e5b788d0e10 --- /dev/null +++ b/test/CrossImport/Inputs/lib-templates/Frameworks/OverlaidClangFramework.framework/Modules/OverlaidClangFramework.swiftcrossimport/BystandingLibrary.swiftoverlay @@ -0,0 +1,5 @@ +%YAML 1.2 +--- +version: 1 +modules: + - name: _OverlaidClangFramework_BystandingLibrary diff --git a/test/CrossImport/Inputs/lib-templates/Frameworks/SwiftFramework.framework/Modules/SwiftFramework.swiftcrossimport/BystandingLibrary.swiftoverlay b/test/CrossImport/Inputs/lib-templates/Frameworks/SwiftFramework.framework/Modules/SwiftFramework.swiftcrossimport/BystandingLibrary.swiftoverlay new file mode 100644 index 00000000000..b989c183f5e --- /dev/null +++ b/test/CrossImport/Inputs/lib-templates/Frameworks/SwiftFramework.framework/Modules/SwiftFramework.swiftcrossimport/BystandingLibrary.swiftoverlay @@ -0,0 +1,5 @@ +%YAML 1.2 +--- +version: 1 +modules: + - name: _SwiftFramework_BystandingLibrary diff --git a/test/CrossImport/Inputs/lib-templates/Frameworks/_ClangFramework_BystandingLibrary.framework/Modules/_ClangFramework_BystandingLibrary.swiftmodule/module-triple-here.swiftinterface b/test/CrossImport/Inputs/lib-templates/Frameworks/_ClangFramework_BystandingLibrary.framework/Modules/_ClangFramework_BystandingLibrary.swiftmodule/module-triple-here.swiftinterface new file mode 100644 index 00000000000..031fbf64987 --- /dev/null +++ b/test/CrossImport/Inputs/lib-templates/Frameworks/_ClangFramework_BystandingLibrary.framework/Modules/_ClangFramework_BystandingLibrary.swiftmodule/module-triple-here.swiftinterface @@ -0,0 +1,7 @@ +// swift-interface-format-version: 1.0 +// swift-module-flags: -swift-version 5 -enable-library-evolution -module-name _ClangFramework_BystandingLibrary + +import Swift +@_exported import ClangFramework + +public func fromClangFrameworkCrossImport() diff --git a/test/CrossImport/Inputs/lib-templates/Frameworks/_OverlaidClangFramework_BystandingLibrary.framework/Modules/_OverlaidClangFramework_BystandingLibrary.swiftmodule/module-triple-here.swiftinterface b/test/CrossImport/Inputs/lib-templates/Frameworks/_OverlaidClangFramework_BystandingLibrary.framework/Modules/_OverlaidClangFramework_BystandingLibrary.swiftmodule/module-triple-here.swiftinterface new file mode 100644 index 00000000000..98765c3c5d2 --- /dev/null +++ b/test/CrossImport/Inputs/lib-templates/Frameworks/_OverlaidClangFramework_BystandingLibrary.framework/Modules/_OverlaidClangFramework_BystandingLibrary.swiftmodule/module-triple-here.swiftinterface @@ -0,0 +1,7 @@ +// swift-interface-format-version: 1.0 +// swift-module-flags: -swift-version 5 -enable-library-evolution -module-name _OverlaidClangFramework_BystandingLibrary + +import Swift +@_exported import OverlaidClangFramework + +public func fromOverlaidClangFrameworkCrossImport() diff --git a/test/CrossImport/Inputs/lib-templates/Frameworks/_SwiftFramework_BystandingLibrary.framework/Modules/_SwiftFramework_BystandingLibrary.swiftmodule/module-triple-here.swiftinterface b/test/CrossImport/Inputs/lib-templates/Frameworks/_SwiftFramework_BystandingLibrary.framework/Modules/_SwiftFramework_BystandingLibrary.swiftmodule/module-triple-here.swiftinterface new file mode 100644 index 00000000000..036396fb0cc --- /dev/null +++ b/test/CrossImport/Inputs/lib-templates/Frameworks/_SwiftFramework_BystandingLibrary.framework/Modules/_SwiftFramework_BystandingLibrary.swiftmodule/module-triple-here.swiftinterface @@ -0,0 +1,7 @@ +// swift-interface-format-version: 1.0 +// swift-module-flags: -swift-version 5 -enable-library-evolution -module-name _SwiftFramework_BystandingLibrary + +import Swift +@_exported import SwiftFramework + +public func fromSwiftFrameworkCrossImport() diff --git a/test/CrossImport/Inputs/lib-templates/lib/swift/OverlaidClangFramework.swiftmodule/module-triple-here.swiftinterface b/test/CrossImport/Inputs/lib-templates/lib/swift/OverlaidClangFramework.swiftmodule/module-triple-here.swiftinterface index 3dd48ec578d..2997e45b97c 100644 --- a/test/CrossImport/Inputs/lib-templates/lib/swift/OverlaidClangFramework.swiftmodule/module-triple-here.swiftinterface +++ b/test/CrossImport/Inputs/lib-templates/lib/swift/OverlaidClangFramework.swiftmodule/module-triple-here.swiftinterface @@ -3,3 +3,5 @@ import Swift @_exported import OverlaidClangFramework + +public func fromOverlaidClangFrameworkOverlay() diff --git a/test/CrossImport/common-case.swift b/test/CrossImport/common-case.swift new file mode 100644 index 00000000000..f29a547e600 --- /dev/null +++ b/test/CrossImport/common-case.swift @@ -0,0 +1,40 @@ +// This explicitly tests "common" cases with well-constructed cross-import +// overlays. Some behaviors here are poorly covered by other tests. + +// RUN: %empty-directory(%t) +// RUN: cp -r %S/Inputs/lib-templates/* %t/ +// RUN: %{python} %S/Inputs/rewrite-module-triples.py %t %module-target-triple + +// RUN: %target-typecheck-verify-swift -enable-cross-import-overlays -I %t/include -I %t/lib/swift -F %t/Frameworks + +// Each framework has a cross-import overlay with this library: +import BystandingLibrary + +// 1. A Swift framework + +import SwiftFramework + +fromSwiftFramework() +fromSwiftFrameworkCrossImport() +SwiftFramework.fromSwiftFramework() +SwiftFramework.fromSwiftFrameworkCrossImport() + +// 2. A Clang framework + +import ClangFramework + +fromClangFramework() +fromClangFrameworkCrossImport() +ClangFramework.fromClangFramework() +ClangFramework.fromClangFrameworkCrossImport() + +// 3. A Swift-overlaid Clang framework + +import OverlaidClangFramework + +fromOverlaidClangFramework() +fromOverlaidClangFrameworkOverlay() +fromOverlaidClangFrameworkCrossImport() +OverlaidClangFramework.fromOverlaidClangFramework() +OverlaidClangFramework.fromOverlaidClangFrameworkOverlay() +OverlaidClangFramework.fromOverlaidClangFrameworkCrossImport()