Skip to content

Commit 4619e2e

Browse files
authored
Merge pull request swiftlang#31199 from brentdax/pathfinder
Fix layering of cross-import and clang overlays
2 parents 767c7d5 + f384748 commit 4619e2e

File tree

12 files changed

+110
-2
lines changed

12 files changed

+110
-2
lines changed

include/swift/AST/SourceFile.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,8 @@ class SourceFile final : public FileUnit {
389389
overlays.append(value.begin(), value.end());
390390
}
391391

392+
SWIFT_DEBUG_DUMPER(dumpSeparatelyImportedOverlays());
393+
392394
void cacheVisibleDecls(SmallVectorImpl<ValueDecl *> &&globals) const;
393395
const SmallVectorImpl<ValueDecl *> &getCachedVisibleDecls() const;
394396

lib/AST/ASTDumper.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1258,6 +1258,10 @@ namespace {
12581258

12591259
void visitModuleDecl(ModuleDecl *MD) {
12601260
printCommon(MD, "module");
1261+
1262+
if (MD->isNonSwiftModule())
1263+
OS << " non_swift";
1264+
12611265
PrintWithColorRAII(OS, ParenthesisColor) << ')';
12621266
}
12631267

lib/AST/Module.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1426,6 +1426,22 @@ SourceFile::getImportedModules(SmallVectorImpl<ModuleDecl::ImportedModule> &modu
14261426
}
14271427
}
14281428

1429+
void SourceFile::dumpSeparatelyImportedOverlays() const {
1430+
for (auto &pair : separatelyImportedOverlays) {
1431+
auto &underlying = std::get<0>(pair);
1432+
auto &overlays = std::get<1>(pair);
1433+
1434+
llvm::errs() << (void*)underlying << " ";
1435+
underlying->dump(llvm::errs());
1436+
1437+
for (auto overlay : overlays) {
1438+
llvm::errs() << "- ";
1439+
llvm::errs() << (void*)overlay << " ";
1440+
overlay->dump(llvm::errs());
1441+
}
1442+
}
1443+
}
1444+
14291445
void ModuleDecl::getImportedModulesForLookup(
14301446
SmallVectorImpl<ImportedModule> &modules) const {
14311447
FORWARD(getImportedModulesForLookup, (modules));

lib/Sema/ImportResolution.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -896,9 +896,17 @@ void ImportResolver::crossImport(ModuleDecl *M, UnboundImport &I) {
896896
if (!SF.shouldCrossImport())
897897
return;
898898

899-
if (I.getUnderlyingModule())
899+
if (I.getUnderlyingModule()) {
900+
auto underlying = I.getUnderlyingModule().get();
901+
902+
// If this is a clang module, and it has a clang overlay, we want the
903+
// separately-imported overlay to sit on top of the clang overlay.
904+
if (underlying->isNonSwiftModule())
905+
underlying = underlying->getTopLevelModule(true);
906+
900907
// FIXME: Should we warn if M doesn't reexport underlyingModule?
901-
SF.addSeparatelyImportedOverlay(M, I.getUnderlyingModule().get());
908+
SF.addSeparatelyImportedOverlay(M, underlying);
909+
}
902910

903911
auto newImports = crossImportableModules.getArrayRef()
904912
.drop_front(nextModuleToCrossImport);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
%YAML 1.2
2+
---
3+
version: 1
4+
modules:
5+
- name: _ClangFramework_BystandingLibrary
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
%YAML 1.2
2+
---
3+
version: 1
4+
modules:
5+
- name: _OverlaidClangFramework_BystandingLibrary
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
%YAML 1.2
2+
---
3+
version: 1
4+
modules:
5+
- name: _SwiftFramework_BystandingLibrary
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// swift-interface-format-version: 1.0
2+
// swift-module-flags: -swift-version 5 -enable-library-evolution -module-name _ClangFramework_BystandingLibrary
3+
4+
import Swift
5+
@_exported import ClangFramework
6+
7+
public func fromClangFrameworkCrossImport()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// swift-interface-format-version: 1.0
2+
// swift-module-flags: -swift-version 5 -enable-library-evolution -module-name _OverlaidClangFramework_BystandingLibrary
3+
4+
import Swift
5+
@_exported import OverlaidClangFramework
6+
7+
public func fromOverlaidClangFrameworkCrossImport()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// swift-interface-format-version: 1.0
2+
// swift-module-flags: -swift-version 5 -enable-library-evolution -module-name _SwiftFramework_BystandingLibrary
3+
4+
import Swift
5+
@_exported import SwiftFramework
6+
7+
public func fromSwiftFrameworkCrossImport()

test/CrossImport/Inputs/lib-templates/lib/swift/OverlaidClangFramework.swiftmodule/module-triple-here.swiftinterface

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,5 @@
33

44
import Swift
55
@_exported import OverlaidClangFramework
6+
7+
public func fromOverlaidClangFrameworkOverlay()

test/CrossImport/common-case.swift

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// This explicitly tests "common" cases with well-constructed cross-import
2+
// overlays. Some behaviors here are poorly covered by other tests.
3+
4+
// RUN: %empty-directory(%t)
5+
// RUN: cp -r %S/Inputs/lib-templates/* %t/
6+
// RUN: %{python} %S/Inputs/rewrite-module-triples.py %t %module-target-triple
7+
8+
// RUN: %target-typecheck-verify-swift -enable-cross-import-overlays -I %t/include -I %t/lib/swift -F %t/Frameworks
9+
10+
// Each framework has a cross-import overlay with this library:
11+
import BystandingLibrary
12+
13+
// 1. A Swift framework
14+
15+
import SwiftFramework
16+
17+
fromSwiftFramework()
18+
fromSwiftFrameworkCrossImport()
19+
SwiftFramework.fromSwiftFramework()
20+
SwiftFramework.fromSwiftFrameworkCrossImport()
21+
22+
// 2. A Clang framework
23+
24+
import ClangFramework
25+
26+
fromClangFramework()
27+
fromClangFrameworkCrossImport()
28+
ClangFramework.fromClangFramework()
29+
ClangFramework.fromClangFrameworkCrossImport()
30+
31+
// 3. A Swift-overlaid Clang framework
32+
33+
import OverlaidClangFramework
34+
35+
fromOverlaidClangFramework()
36+
fromOverlaidClangFrameworkOverlay()
37+
fromOverlaidClangFrameworkCrossImport()
38+
OverlaidClangFramework.fromOverlaidClangFramework()
39+
OverlaidClangFramework.fromOverlaidClangFrameworkOverlay()
40+
OverlaidClangFramework.fromOverlaidClangFrameworkCrossImport()

0 commit comments

Comments
 (0)