-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
feat: Introduce term search to rust-analyzer #16092
Conversation
dc56fa6
to
0a912d0
Compare
interesting, it would be nicer if there give a bit more docs for |
I've added some more documentation there. If you find some caps / stuff that doesn't make sense etc let me know as it definitely can be improved further 😄 |
Just a headsup but it will take some time (I'm potentially speaking months) until this gets reviewed as there is a massive backlog + me having little time atm + holidays coming up. |
That's unfortunate :( But thanks for the heads-up, appreciate it! |
fa93910
to
4da835f
Compare
I added also autocomplete part to this PR. 2024-01-08_21-26-53.mp4Snippet is a little misleading `auto completion kind for it, but couldn't find any better :p |
8c0be25
to
e860e1a
Compare
e860e1a
to
42b2a1d
Compare
CI fails because |
Yep, rebasing onto master now. |
232bc6d
to
a2bdd37
Compare
☔ The latest upstream changes (presumably #16395) made this pull request unmergeable. Please resolve the merge conflicts. |
a2bdd37
to
62194da
Compare
☔ The latest upstream changes (presumably #16434) made this pull request unmergeable. Please resolve the merge conflicts. |
Sorry I haven't gotten around to this yet, been somewhat sick last week. |
bb7dab7
to
50d9a91
Compare
No worries, hope your well now 😄 |
21adceb
to
23f37ab
Compare
587ab93
to
1257913
Compare
Then let's merge this I guess :) Excited to try this out! |
☀️ Test successful - checks-actions |
feat: Add "make tuple" tactic to term search Follow up to #16092 Now term search also supports tuples. ```rust let a: i32 = 1; let b: f64 = 0.0; let c: (i32, (f64, i32)) = todo!(); // Finds (a, (b, a)) ``` In addition to new tactic that handles tuples I changed how the generics are handled. Previously it tried all possible options from types we had in scope but now it only tries useful ones that help us directly towards the goal or at least towards calling some other function. This changes O(2^n) to O(n^2) where n is amount of rounds which in practice allows using types that take generics for multiple rounds (previously limited to 1). Average case that also used to be exponential is now roughly linear. This means that deeply nested generics also work. ````rust // Finds all valid combos, including `Some(Some(Some(...)))` let a: Option<Option<Option<bool>>> = todo!(); ```` _Note that although the complexity is smaller allowing more types with generics the search overall slows down considerably. I hope it's fine tho as the autocomplete is disabled by default and for code actions it's not super slow. Might have to tweak the depth hyper parameter tho_ This resulted in a huge increase of results found (benchmarks on `ripgrep` crate): Before ```` Tail Expr syntactic hits: 149/1692 (8%) Tail Exprs found: 749/1692 (44%) Term search avg time: 18ms ``` After ``` Tail Expr syntactic hits: 291/1692 (17%) Tail Exprs found: 1253/1692 (74%) Term search avg time: 139ms ```` Most changes are local to term search except some tuple related stuff on `hir::Type`.
Introduce term search to
rust-analyzer
I've marked this as draft as there might be some shortcomings, please point them out so I can fix them. Otherwise I think it is kind of ready as I think I'll rather introduce extra functionality in follow up PRs.
Term search (or I guess expression search for rust) is a technique to generate code by basically making the types match.
Consider the following program
From the types of values in scope and constructors of
Option
, we can produce the expected result of wrapping the argument inOption
Dependently typed languages such as
Idris2
andAgda
have similar tools to help with proofs, but this can be also used in everyday development as a "auto-complete".Demo videos
2023-12-11_16-28-17.mp4
2023-12-11_15-41-30.mp4
What does it currently do
foo.bar.baz
) but this might me more conservative than it has to be to avoid conflicting with borrow checkerrust-analyzer analysis-stats /path/to/ripgrep/Cargo.toml --run-term-search --validate-term-search
(basically runningcargo check
on all of the generated programs and only error seems to be due to type inference which is more of issue of testing method.Performace / fitness
Highly generic code seems to blow up the search space so currently the amount of generics allowed is functions/methods is limited down to 0 (1 didn't give much improvement and 2 is already like 0.5+s search time)
Plans for the future (not in this PR)
Add impl methods that do not takeDoneself
type (should be quite straight forward)See if it works as a autocomplete while typingDoneFeel free to ask questions / point of shortcoming either here or on Zulip, I'll be happy to address them. I'm doing this as part of my MSc thesis so I'll be working on it till summer anyway 😄