Skip to content

Commit ca942c2

Browse files
committed
[CodeCompletion] Fix an issue that causes an iterator to be invalidated while iterating
In `SourceLookupCache::lookupVisibleDecls`, copy the top level values before iterating them. If we have 'addinitstotoplevel' enabled, calling 'addConstructorCallsForType' can cause macros to get expanded, which can then cause new members ot get added to 'TopLevelValues', invalidating the current iterator. I have not been able to reduce this to a test case that doesn’t rely on the `Observation` module in the SDK but here is the test case with which I was able to reproduce the issue very reliably. ```swift import Foundation import Observation @observable class MyObject {} extension MyObject {} // RUN: ~/sbin/sourcekitd-test \ // RUN: -req=complete.open \ // RUN: -req-opts=addinitstotoplevel=1 \ // RUN: -pos=8:1 \ // RUN: %s \ // RUN: -- \ // RUN: %s \ // RUN: -Xfrontend \ // RUN: -load-plugin-library \ // RUN: -Xfrontend \ // RUN: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/host/plugins/libObservationMacros.dylib \ // RUN: -sdk \ // RUN: /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.0.sdk ``` rdar://109202157
1 parent 4bacb7f commit ca942c2

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

lib/AST/Module.cpp

+7-1
Original file line numberDiff line numberDiff line change
@@ -542,7 +542,13 @@ void SourceLookupCache::lookupVisibleDecls(ImportPath::Access AccessPath,
542542
return;
543543
}
544544

545-
for (auto &tlv : TopLevelValues) {
545+
// Copy the top level values before iterating them.
546+
// If we have 'addinitstotoplevel' enabled, calling
547+
// 'addConstructorCallsForType' can cause macros to get expanded, which can
548+
// then cause new members ot get added to 'TopLevelValues', invalidating the
549+
// current iterator.
550+
ValueDeclMap TLV = TopLevelValues;
551+
for (auto &tlv : TLV) {
546552
for (ValueDecl *vd : tlv.second) {
547553
// Declarations are added under their full and simple names. Skip the
548554
// entry for the simple name so that we report each declaration once.

0 commit comments

Comments
 (0)