Skip to content
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

[SR-13765] Index store does not contain reference occurrence for labeled parameters #56162

Closed
swift-ci opened this issue Oct 22, 2020 · 7 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior.

Comments

@swift-ci
Copy link
Collaborator

Previous ID SR-13765
Radar None
Original Reporter Leitch (JIRA User)
Type Bug
Status Resolved
Resolution Invalid
Environment

Apple Swift version 5.3.1 (swiftlang-1200.0.41 clang-1200.0.32.8)
Target: x86_64-apple-darwin19.6.0

Xcode 12.2
Build version 12B5035g

Additional Detail from JIRA
Votes 0
Component/s Source Tooling
Labels Bug
Assignee None
Priority Medium

md5: 5a0394619470c3593109b43635315023

Issue Description:

class MyClass {
    func someMethod(_ foo: String) {
        print(foo)
    }
}

Given the above code, the index store should contain an occurrence with the 'reference' role for the use of `foo` in the print statement. This bug only exists for labeled parameters, and the name of the label appears to make no difference.

@swift-ci
Copy link
Collaborator Author

Comment by Ian Leitch (JIRA)

I'm willing to have a go at fixing this, though I've no idea where to start looking /cc @benlangmuir @rintaro

@benlangmuir
Copy link
Member

This is behaving as designed. The general rule is that we do not index local variables, which includes function parameters. We made a compromise to index parameters that match their argument label, because we need that information for global rename to work correctly.

If we wanted to change the rule here, we could either

  1. Index all locals, at the cost of making the index larger and more expensive to create/process.
  2. Specifically handle parameters, as suggested in this bug. This probably has less impact than (1), but would still require analysis to show it didn't regress significantly.
  3. Do (1) or (2) but only when some special compiler flag is passed so that it's off by default

My suspicion is that option (1) would be too expensive to enable by default, and it's unclear to me what the value of (2) is on its own. Do you have a specific use case for having parameters in the index without having all local declarations?

@akyrtzi
Copy link
Member

akyrtzi commented Oct 22, 2020

I think a frontend flag to opt-in and enable indexing locals would be reasonable.

@swift-ci
Copy link
Collaborator Author

Comment by Ian Leitch (JIRA)

Context: I'm the author of Periphery, and I'm working on migrating from SourceKit to using the IndexStore exclusively, if possible. I noticed recently that the IndexStore now indexed parameters, which I thought might provide a way forward to migrating the unused parameter detection feature. The current implementation uses SwiftSyntax + a SourceKit cursor info query to then associate the parameters with the function declaration by USR.

If we're to resolve this in the index store, (2) sounds reasonable to me. However perhaps it's better that I investigate another method to associate parameters parsed with SwiftSyntax to indexed functions that doesn't depend upon SourceKit. I'll report back if I find a reliable solution.

@swift-ci
Copy link
Collaborator Author

Comment by Ian Leitch (JIRA)

@akyrtzi In general that would be nice, since then Periphery could also report unused local variables. One caveat is that Periphery would need to rebuild the project being analyzed from scratch with the new flag applied. Periphery actually does already always build projects from scratch in order to obtain the correct compiler flags for SourceKit, though I'd been hoping to avoid doing that with a potential switch to using the index store exclusively.

@swift-ci
Copy link
Collaborator Author

Comment by Ian Leitch (JIRA)

I've implemented a workaround that allows me to use SwiftSyntax again for identifying unused parameters. We can close this now unless you'd like to keep it open to track work towards (1) or (2).

@swift-ci
Copy link
Collaborator Author

swift-ci commented Jun 2, 2021

Comment by Ian Leitch (JIRA)

This was implemented in Periphery by parsing the AST.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior.
Projects
None yet
Development

No branches or pull requests

3 participants