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

Add command to report unresolved references #17904

Merged
merged 1 commit into from
Sep 11, 2024

Conversation

darichey
Copy link
Contributor

Adds rust-analyzer unresolved-references which reports unresolved references. This is useful for debugging and regression testing for both rust-analyzer and project generators like Buck's rust-project.

As discussed: https://rust-lang.zulipchat.com/#narrow/stream/185405-t-compiler.2Frust-analyzer/topic/Command.20to.20report.20unresolved.20references

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Aug 16, 2024
@darichey darichey marked this pull request as draft August 16, 2024 02:07
@darichey
Copy link
Contributor Author

This isn't quite working totally yet, but opening the PR to get some feedback and ask a couple questions. The approach is based on NameRefClass, but there are some cases this doesn't handle properly on its own. I think highlight.rs has all of the answers, but I'd appreciate some pointers :)

This is the output when running on rust-analyzer itself: https://gist.github.com/darichey/55e411f55b722d9cc1602e8f273c1a67

It does find some actual issues! e.g.,

rust-analyzer/crates/hir-expand/src/files.rs:199:71: text_range
rust-analyzer/crates/salsa/src/runtime.rs:468:45: LEN
rust-analyzer/crates/syntax/src/ted.rs:71:31: last_child_or_token
rust-analyzer/crates/hir-def/src/resolver.rs:1058:13: module

However, there's a lot of false positives. In particular, code within #[tracing::instrument] (and other proc macros, like Self inside a #[salsa::database]) gets reported as unresolved. I'm not sure why that is.

Also, as reported in #10935, proc macro attributes are unresolved. Should we copy the hack for highlighting in #10937 or keep them since they are technically unresolved?

@darichey darichey force-pushed the unresolved-references branch 2 times, most recently from faf8af0 to 80357b8 Compare August 22, 2024 19:04
@darichey
Copy link
Contributor Author

darichey commented Aug 22, 2024

I've fixed the handling of macros and decided to ignore attributes. This is the new output when running on rust-analyzer: https://gist.github.com/darichey/2f74b0e45f5c1ff18d4ea78039e2c831

All of those are correctly-identified unresolved references except the last one:

let cfg = CfgExpr::arbitrary(&mut u).unwrap();

I assume it has something to do with the conditional deriving of Arbitrary, but I wasn't able to repro in isolation.
Other than that issue, I think this is ready for review.

@darichey darichey marked this pull request as ready for review August 22, 2024 19:05
@Veykril
Copy link
Member

Veykril commented Sep 3, 2024

Had a quick look but I am also not sure why that is. Either way commited some small cleansups and fixes (maybe that fixes things though I doubt it)

@Veykril Veykril added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Sep 3, 2024
@darichey darichey force-pushed the unresolved-references branch 2 times, most recently from 18707cc to 57c29eb Compare September 5, 2024 15:57
@darichey darichey force-pushed the unresolved-references branch from 57c29eb to e602e01 Compare September 5, 2024 17:11
@darichey
Copy link
Contributor Author

darichey commented Sep 5, 2024

Figured it out! We need to set all_targets: true in the CargoConfig. Without it, we don't get artifact info for dev-dependencies when building build dependencies, so we can't expand proc macros from them.

For now, I've switched to creating the CargoConfig via Config, the same way scip does. This ensures we get the same defaults rust-analyzer does in normal usage. As a follow up, it would be good to extract the common workspace loading logic from scip, lsif, diagnostics, and unresolved_references to make sure they all do the right thing (it looks like lsif and diagnostics would have the same problem).

With that, there are no more false positives in rust-analyzer: https://gist.github.com/darichey/10a7b13961216e680f0d6b1085f79ab5

I will run it on our monorepo to check for any more false positives, but I think this is a good start we can iterate on!

@Veykril
Copy link
Member

Veykril commented Sep 11, 2024

@bors r+

@bors
Copy link
Contributor

bors commented Sep 11, 2024

📌 Commit e602e01 has been approved by Veykril

It is now in the queue for this repository.

@bors
Copy link
Contributor

bors commented Sep 11, 2024

⌛ Testing commit e602e01 with merge 6c56df8...

@bors
Copy link
Contributor

bors commented Sep 11, 2024

☀️ Test successful - checks-actions
Approved by: Veykril
Pushing 6c56df8 to master...

@bors bors merged commit 6c56df8 into rust-lang:master Sep 11, 2024
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants