[Serialization] Refactor logic deciding transitive module loading logic

Refactor and centralize the logic about how implementation-only and
package-only dependencies should be loaded.
This commit is contained in:
Alexis Laferrière
2023-03-15 22:19:21 -07:00
parent 2bc92a6a59
commit 61c0827427
5 changed files with 99 additions and 21 deletions

View File

@@ -1680,3 +1680,46 @@ ModuleFileSharedCore::ModuleFileSharedCore(
bool ModuleFileSharedCore::hasSourceInfo() const {
return !!DeclUSRsTable;
}
ModuleLoadingBehavior
ModuleFileSharedCore::getTransitiveLoadingBehavior(
const Dependency &dependency,
bool debuggerMode,
bool isPartialModule,
StringRef packageName) const {
if (isPartialModule) {
// Keep the merge-module behavior for legacy support. In that case
// we load all transitive dependencies from partial modules and
// error if it fails.
return ModuleLoadingBehavior::Required;
}
if (dependency.isImplementationOnly()) {
// Implementation-only dependencies are not usually loaded from
// transitive imports.
if (debuggerMode) {
// In the debugger, try to load the module if possible.
// Same in the case of a testable import, try to load the dependency
// but don't fail if it's missing as this could be source breaking.
return ModuleLoadingBehavior::Optional;
} else {
// When building normally, ignore transitive implementation-only
// imports.
return ModuleLoadingBehavior::Ignored;
}
}
if (dependency.isPackageOnly()) {
// Package dependencies are usually loaded only for import from the same
// package.
if (packageName == getModulePackageName()) {
return ModuleLoadingBehavior::Required;
} else {
return ModuleLoadingBehavior::Ignored;
}
}
// By default, imports are required dependencies.
return ModuleLoadingBehavior::Required;
}