Skip to content

Commit e5d2475

Browse files
authored
Merge pull request #62539 from artemcm/ExplicitVersionedCanImport
[Explicit Module Builds] Add support for versioned 'canImport' to the explicit module loader
2 parents d32c027 + e790a21 commit e5d2475

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

lib/Frontend/ModuleInterfaceLoader.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1993,6 +1993,24 @@ bool ExplicitSwiftModuleLoader::canImportModule(
19931993
if (it == Impl.ExplicitModuleMap.end()) {
19941994
return false;
19951995
}
1996+
1997+
// If the caller doesn't want version info we're done.
1998+
if (!versionInfo)
1999+
return true;
2000+
2001+
// Open .swiftmodule file and read out the version
2002+
auto &fs = *Ctx.SourceMgr.getFileSystem();
2003+
auto moduleBuf = fs.getBufferForFile(it->second.modulePath);
2004+
if (!moduleBuf) {
2005+
Ctx.Diags.diagnose(SourceLoc(), diag::error_opening_explicit_module_file,
2006+
it->second.modulePath);
2007+
return false;
2008+
}
2009+
auto metaData = serialization::validateSerializedAST(
2010+
(*moduleBuf)->getBuffer(), Ctx.SILOpts.EnableOSSAModules,
2011+
Ctx.LangOpts.SDKName, !Ctx.LangOpts.DebuggerSupport);
2012+
versionInfo->setVersion(metaData.userModuleVersion,
2013+
ModuleVersionSourceKind::SwiftBinaryModule);
19962014
return true;
19972015
}
19982016

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: mkdir -p %t/clang-module-cache
3+
// RUN: mkdir -p %t/inputs
4+
// RUN: mkdir -p %t/barinputs
5+
// RUN: echo "public func foo() {}" >> %t/foo.swift
6+
// RUN: %target-swift-frontend -emit-module -emit-module-path %t/inputs/Foo.swiftmodule -emit-module-doc-path %t/inputs/Foo.swiftdoc -emit-module-source-info -emit-module-source-info-path %t/inputs/Foo.swiftsourceinfo -module-cache-path %t.module-cache %t/foo.swift -module-name Foo -user-module-version 9001
7+
// RUN: echo "public func bar() {}" >> %t/bar.swift
8+
// RUN: %target-swift-frontend -emit-module -emit-module-path %t/barinputs/Bar.swiftmodule -emit-module-doc-path %t/barinputs/Bar.swiftdoc -emit-module-source-info -emit-module-source-info-path %t/barinputs/Bar.swiftsourceinfo -module-cache-path %t.module-cache %t/bar.swift -module-name Bar
9+
10+
// RUN: echo "[{" > %/t/inputs/map.json
11+
// RUN: echo "\"moduleName\": \"Foo\"," >> %/t/inputs/map.json
12+
// RUN: echo "\"modulePath\": \"%/t/inputs/Foo.swiftmodule\"," >> %/t/inputs/map.json
13+
// RUN: echo "\"docPath\": \"%/t/inputs/Foo.swiftdoc\"," >> %/t/inputs/map.json
14+
// RUN: echo "\"sourceInfoPath\": \"%/t/inputs/Foo.swiftsourceinfo\"," >> %/t/inputs/map.json
15+
// RUN: echo "\"isFramework\": false" >> %/t/inputs/map.json
16+
// RUN: echo "}]" >> %/t/inputs/map.json
17+
18+
// RUN: not %target-swift-frontend -typecheck %s -explicit-swift-module-map-file %t/inputs/map.json -disable-implicit-swift-modules
19+
// RUN: %target-swift-frontend -typecheck %s -explicit-swift-module-map-file %t/inputs/map.json -I %t/barinputs -Rmodule-loading 2>&1 | %FileCheck %s
20+
21+
#if canImport(Foo, _version: 9000)
22+
import Bar
23+
#endif
24+
25+
// 'Bar' can only be imported if the explicitly-loaded 'Foo' is known to be over 9000
26+
// CHECK: remark: loaded module 'Bar'

0 commit comments

Comments
 (0)