Patch Out a Source of Iterator Invalidation

Synthesized file units were designed for autodiff to emit synthesized declarations, and also to sidestep the design implications of doing so late in the compiler pipeline.

A call to materialize synthesized file units was added to the GetImplicitSendable request. This introduced a source of iterator invalidation into forEachFileToTypeCheck in whole-module builds. Any call to insert a new file into the module has the potential to cause the underlying SmallVector to reallocate.

This patch provides a narrow workaround that stops using iterators altogether in forEachFileToTypeCheck. However, this bug reveals a severe architectural flaw in the concept of a synthesized file unit. Iterating over the files in a module is an extremely common operation, and there now are myriad ways we could wind up calling a function that mutates the module's list of files in the process. This also means the number and kind of files being visited by compiler analyses is dependent upon whether a request that inserts these files has or has not been called.

This suggests the call to ModuleDecl::addFile in FileUnit::getOrCreateSynthesizedFile is deleterious and should be removed. Doing so will come as part of a larger refactoring.

rdar://94043340
This commit is contained in:
Robert Widmann
2022-05-28 12:06:42 -07:00
parent e9fb9091b8
commit bbe4608fa4
3 changed files with 22 additions and 2 deletions

View File

@@ -1168,8 +1168,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;
}