Skip to content

[Dependency Scanning] Remove hard failure mode (crash) during cross-import resolution #79702

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 11 additions & 9 deletions lib/DependencyScan/ModuleDependencyScanner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -602,7 +602,7 @@ static void discoverCrossImportOverlayFiles(
auto getModuleIDForImportIdentifier =
[](const std::string &importIdentifierStr,
const ModuleDependencyIDSet &directSwiftDepsSet,
const ModuleDependencyIDSet &directClangDepsSet) -> ModuleDependencyID {
const ModuleDependencyIDSet &directClangDepsSet) -> std::optional<ModuleDependencyID> {
if (auto textualDepIt = directSwiftDepsSet.find(
{importIdentifierStr, ModuleDependencyKind::SwiftInterface});
textualDepIt != directSwiftDepsSet.end())
Expand All @@ -615,8 +615,8 @@ static void discoverCrossImportOverlayFiles(
{importIdentifierStr, ModuleDependencyKind::Clang});
clangDepIt != directClangDepsSet.end())
return *clangDepIt;
llvm_unreachable(
"Unresolved import during cross-import overlay resolution");
else
return std::nullopt;
};

// Collect the set of directly-imported module dependencies
Expand All @@ -625,9 +625,10 @@ static void discoverCrossImportOverlayFiles(
auto importResolvedModuleID = getModuleIDForImportIdentifier(
import.importIdentifier, mainModuleDirectSwiftDepsSet,
mainModuleDirectClangDepsSet);
for (const auto &importLocation : import.importLocations)
perSourceFileDependencies[importLocation.bufferIdentifier].insert(
importResolvedModuleID);
if (importResolvedModuleID)
for (const auto &importLocation : import.importLocations)
perSourceFileDependencies[importLocation.bufferIdentifier].insert(
*importResolvedModuleID);
}

// For each source-file, build a set of module dependencies of the
Expand Down Expand Up @@ -658,9 +659,10 @@ static void discoverCrossImportOverlayFiles(
auto importResolvedDepID = getModuleIDForImportIdentifier(
import.importIdentifier, directSwiftDepsSet,
directClangDepsSet);
if (!perSourceFileDependencies[bufferIdentifier].count(
importResolvedDepID))
worklist.push_back(importResolvedDepID);
if (importResolvedDepID &&
!perSourceFileDependencies[bufferIdentifier].count(
*importResolvedDepID))
worklist.push_back(*importResolvedDepID);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion test/ScanDependencies/test_clang_gmodules.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// RUN: %empty-directory(%t)
// RUN: %empty-directory(%t/module-cache)
// RUN: %target-swift-frontend -scan-dependencies -module-cache-path %t/module-cache %s -o %t/deps.json -I %S/Inputs/CHeaders -I %S/Inputs/Swift -target %target-cpu-apple-macosx10.14
// RUN: %target-swift-frontend -scan-dependencies -module-cache-path %t/module-cache %s -o %t/deps.json -I %S/Inputs/CHeaders -I %S/Inputs/Swift -target %target-cpu-apple-macosx10.14 -disable-implicit-concurrency-module-import -disable-implicit-string-processing-module-import
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this test mean it usually triggered from implicit imported modules? And implicit module cannot trigger cross import?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, these implicit imports do not actually result in any cross-import overlays. The way we noticed an issue here is that implicitly-imported Concurrency module was not able to be resolved for a given target platform. And that caused there to be a hanging unresolved import, which caused the code this PR is changing to crash.

So I wanted to both address this issue and also fix the test to not have a possibility of this missing module error which is fully unrelated to what it is meant to be testing.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible to write a test for the failing case previously?

// RUN: %validate-json %t/deps.json | %FileCheck %s

import X
Expand Down