You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
[CodeCompletion] Fix an issue that causes an iterator to be invalidated while iterating
If we have 'addinitstotoplevel' enabled, calling `foundDecl` on `this` can cause macros to get expanded, which can then cause new members ot get added to 'TopLevelValues', invalidating iterator over `TopLevelDecls` in `SourceLookupCache::lookupVisibleDecls`.
Technically `foundDecl` should not expand macros or discover new top level members in any way because those newly discovered decls will not be added to the code completion results. However, it's preferrable to miss results than to silently invalidate a collection, resulting in hard-to-diagnose crashes.
Thus, store all the decls found by `CurrModule->lookupVisibleDecls` in a vector first and only call `this->foundDecl` once we have left the iteration loop over `TopLevelDecls`.
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
0 commit comments