Skip to content

[CodeCompletion] Fix an issue that causes an iterator to be invalidated while iterating #66517

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
Jun 12, 2023

Conversation

ahoppen
Copy link
Member

@ahoppen ahoppen commented Jun 9, 2023

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 to 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.

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

@ahoppen ahoppen requested review from rintaro and bnbarham June 9, 2023 22:51
@ahoppen
Copy link
Member Author

ahoppen commented Jun 9, 2023

@swift-ci Please smoke test

…ed 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
@ahoppen ahoppen force-pushed the ahoppen/iterator-invalidation branch from ca942c2 to 0996342 Compare June 10, 2023 00:11
@ahoppen
Copy link
Member Author

ahoppen commented Jun 10, 2023

@swift-ci Please test


void foundDecl(ValueDecl *VD, DeclVisibilityKind Reason,
DynamicLookupInfo DynamicLookupInfo = {}) override {
FoundDecls.push_back({VD, Reason, DynamicLookupInfo});
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
FoundDecls.push_back({VD, Reason, DynamicLookupInfo});
FoundDecls.emplace_back(VD, Reason, DynamicLookupInfo);

//
// 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
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
// to the code completion results. However, it's preferrable to miss results
// to the code completion results. However, it's preferable to miss results

@ahoppen ahoppen merged commit 9f51c06 into swiftlang:main Jun 12, 2023
@ahoppen ahoppen deleted the ahoppen/iterator-invalidation branch June 12, 2023 18:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants