Skip to content

[SourceKit] Merge local rename and related identifiers #68554

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

Conversation

ahoppen
Copy link
Member

@ahoppen ahoppen commented Sep 15, 2023

The related identifiers request was sufficiently similar to local rename that we can implement it in terms of local rename.

@ahoppen
Copy link
Member Author

ahoppen commented Sep 15, 2023

@swift-ci Please smoke test

Local rename and related identifiers were sufficiently similar that we can implement related identifiers in terms of local rename.
…nges instead of putting them in an array first

After implementing related identifiers in terms of rename, this step seems to no longer be necessary.
@ahoppen ahoppen force-pushed the ahoppen/merge-local-rename-and-related-idents branch from 13cb467 to 53a6bca Compare September 15, 2023 20:49
@ahoppen
Copy link
Member Author

ahoppen commented Sep 15, 2023

@swift-ci Please smoke test

@ahoppen
Copy link
Member Author

ahoppen commented Sep 15, 2023

@swift-ci Please SourceKit stress test

/// The new name that should be given to this symbol.
///
/// This may not be known if the rename locations are specified by the client
/// using the a rename locations dicationary in syntactic rename.
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
/// using the a rename locations dicationary in syntactic rename.
/// using the a rename locations dictionary in syntactic rename.

Is NewName ever not empty then?


//--- dummy.swift

// RUN: %sourcekitd-test -req=related-idents -pos=3:10 %t/a.swift -- %t/a.swift %t/b.swift -o implicit_vis.o | %FileCheck -check-prefix=CHECK1 %s
Copy link
Contributor

Choose a reason for hiding this comment

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

Personally I'd put turn on --leading-lines and then put the RUN + CHECKs above using the various line variables. Then also use CHECK-NEXT and remove the -check-prefix=CHECK1 since there's only a single check here.

(I realize you are just changing the test to use split-file here, but may as well clean it up more too!)

func foo(x: Int) {
#if true
print(x)
#else {
Copy link
Contributor

Choose a reason for hiding this comment

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

Spurious {

// Ignore any errors produced by `resolveRenameLocations` since, if some
// symbol failed to resolve, we still want to return all the other
// symbols. This makes related idents more fault-tolerant.
DiagnosticEngine Diags(SrcMgr);
Copy link
Contributor

Choose a reason for hiding this comment

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

Could just make the diagnostic engine optional (eg. nullptr) in this case

Comment on lines +2581 to +2582
std::vector<ResolvedLoc> ResolvedLocs =
resolveRenameLocations(Locs.getLocations(), *SrcFile, Diags);
Copy link
Contributor

Choose a reason for hiding this comment

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

It's unfortunate we go from source locations -> line/col (in the index to produce a symbol) -> back to range here. But re-using the index/rename seems better than implementing related idents separately so 🤷.

Comment on lines +304 to +305
std::unique_ptr<StringScratchSpace> stringStorage;
std::vector<RenameLoc> locations;
Copy link
Contributor

Choose a reason for hiding this comment

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

Is the collector actually used again with newName, or do we always end up doing the syntactic rename after we have locations?

@ahoppen
Copy link
Member Author

ahoppen commented Sep 16, 2023

While looking at open issues regarding related idents and local rename, we found that there are still a few issues here, one of them is that NameMatcher currently assumes that it’s iterating in source order while in-fact it isn’t because it’s an ASTVisitor. This causes us to skip references that have a postfix operator after them.

The proper solution here is to write NameMatcher in terms of SwiftSyntax but since ResolvedLoc contains a vector of CharSourceRange, writing a C bridging layer is a pain. So, we decided to punt this until we enable C++ interop inside the compiler.

The next steps to do then are:

  • Rewrite NameMatcher in SwiftSyntax
  • Ensure that the following issues are fixed
    • rdar://108267794
    • rdar://101556709
    • rdar://84061868
  • Delete the local rename refactoring action. Renaming should always go through the sourcekitd requests
  • When we remove the rename refactoring, I think we also don’t need OldName and NewName in RenameLoc anymore, which means that we can also remove the StringScratchSpace in RenameLocs and replace RenameLocs by std::vector<RenameLoc>.

@ahoppen ahoppen marked this pull request as draft September 16, 2023 00:47
@ahoppen
Copy link
Member Author

ahoppen commented Nov 29, 2023

Superseded by #70008

@ahoppen ahoppen closed this Nov 29, 2023
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.

2 participants