Skip to content

[5.7] Patch Out a Source of Iterator Invalidation #59145

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
May 31, 2022
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
6 changes: 6 additions & 0 deletions include/swift/AST/FileUnit.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ class FileUnit : public DeclContext, public ASTAllocated<FileUnit> {
/// Returns the synthesized file for this source file, if it exists.
SynthesizedFileUnit *getSynthesizedFile() const;

/// Returns the synthesized file for this source file, creating one and
/// inserting it into the module if it does not exist.
///
/// \warning Because this function mutates the parent module's list of files,
/// it will invalidate the iterators of any upstream callers of
/// \c ModuleDecl::getFiles().
SynthesizedFileUnit &getOrCreateSynthesizedFile();

/// Look up a (possibly overloaded) value set at top-level scope
Expand Down
9 changes: 9 additions & 0 deletions lib/AST/Module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3055,6 +3055,15 @@ SynthesizedFileUnit &FileUnit::getOrCreateSynthesizedFile() {
return *thisSynth;
SynthesizedFile = new (getASTContext()) SynthesizedFileUnit(*this);
SynthesizedFileAndKind.setPointer(SynthesizedFile);
// FIXME: Mutating the module in-flight is not a good idea. Any
// callers above us in the stack that are iterating over
// the module's files will have their iterators invalidated. There's
// a strong chance that whatever analysis led to this function being
// called is doing just that!
//
// Instead we ought to just call ModuleDecl::clearLookupCache() here
// and patch out the places looking for synthesized files hanging off of
// source files.
getParentModule()->addFile(*SynthesizedFile);
}
return *SynthesizedFile;
Expand Down
9 changes: 7 additions & 2 deletions lib/Frontend/Frontend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1166,8 +1166,13 @@ bool CompilerInstance::loadPartialModulesAndImplicitImports(
bool CompilerInstance::forEachFileToTypeCheck(
llvm::function_ref<bool(SourceFile &)> fn) {
if (isWholeModuleCompilation()) {
for (auto fileName : getMainModule()->getFiles()) {
auto *SF = dyn_cast<SourceFile>(fileName);
// FIXME: Do not refactor this to use an iterator as long as
// ModuleDecl::addFile is called during Sema. Synthesized files pushed
// during semantic analysis will cause iterator invalidation here.
// See notes in SourceFile::getOrCreateSynthesizedFile() for more.
unsigned i = 0;
while (i < getMainModule()->getFiles().size()) {
auto *SF = dyn_cast<SourceFile>(getMainModule()->getFiles()[i++]);
if (!SF) {
continue;
}
Expand Down