rust/crates/hir-def/src
bors 818c30c311 Auto merge of #16092 - kilpkonn:term_search_1, r=Veykril
feat: Introduce term search to rust-analyzer

# 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
```rust
fn wrap(arg: i32) -> Option<i32> {
    todo!();
}
```
From the types of values in scope and constructors of `Option`, we can produce the expected result of wrapping the argument in `Option`

Dependently typed languages such as `Idris2` and `Agda` have similar tools to help with proofs, but this can be also used in everyday development as a "auto-complete".

# Demo videos

https://github.com/rust-lang/rust-analyzer/assets/19900308/7b68a1b7-7dba-4e31-9221-6c7485e77d88

https://github.com/rust-lang/rust-analyzer/assets/19900308/0fae530a-aabb-4b28-af71-e19f8d3d64b2

# What does it currently do
- It works well with locals, free functions, type constructors and non-static impl methods that take items by value.
- Works with functions/methods that take shared references, but not with unique references (very conservative).
- Can handle projections to struct fields (eg. `foo.bar.baz`) but this might me more conservative than it has to be to avoid conflicting with borrow checker
- Should create only valid programs (no type / borrow checking errors). Tested with `rust-analyzer analysis-stats /path/to/ripgrep/Cargo.toml --run-term-search --validate-term-search` (basically running `cargo 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
```txt
ripgrep (latest)
Tail Expr syntactic hits: 130/1692 (7%)
Tail Exprs found: 523/1692 (30%)
Term search avg time: 9ms
Term search:         15.64s, 97ginstr, 8mb

rust-analyzer (on this branch)
Tail Expr syntactic hits: 804/13860 (5%)
Tail Exprs found: 6757/13860 (48%)
Term search avg time: 78ms
Term search:         1088.23s, 6765ginstr, 98mb
```
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 take `self` type (should be quite straight forward)~~ Done
- Be smarter (aka less restrictive) about borrow checking - this seems quite hard but since the current approach is rather naive I think some easy improvement is available.
- ``~~See if it works as a autocomplete while typing~~ Done

_Feel 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 😄_
2024-02-12 14:47:12 +00:00
..
attr Remove ffi_returns_twice references 2024-02-07 19:42:07 +00:00
body hir-def: Fix warnings about clippy str_to_string rule 2024-02-10 01:00:40 +09:00
data Move ChildbySource and HasSource impls to their corresponding modules 2024-02-10 12:09:12 +01:00
dyn_map Deduplicate 2024-01-15 10:58:05 +01:00
hir new_without_default 2024-01-31 19:06:36 +01:00
item_tree Cleanup visibility.rs 2024-02-10 13:50:45 +01:00
macro_expansion_tests Auto merge of #15923 - tamasfe:feat/better-ignored-macros2, r=Veykril 2024-02-12 12:42:45 +00:00
nameres Encode disabled proc-macros via boolean flag, not special Expander 2024-02-12 13:39:38 +01:00
path redundant_pattern_matching 2024-01-19 17:31:01 +01:00
attr.rs Cleanup term search related changes 2024-02-11 14:35:54 +02:00
body.rs internal: switch to tracing from log 2024-01-30 12:27:31 -05:00
builtin_type.rs
child_by_source.rs Move ChildbySource and HasSource impls to their corresponding modules 2024-02-10 12:09:12 +01:00
data.rs Encode disabled proc-macros via boolean flag, not special Expander 2024-02-12 13:39:38 +01:00
db.rs internal: switch to tracing from log 2024-01-30 12:27:31 -05:00
dyn_map.rs
expander.rs internal: Cleanup Expander a bit 2023-12-22 13:01:13 +01:00
find_path.rs Auto merge of #16474 - davidbarsky:david/import-on-the-fly-improvements, r=lnicola 2024-02-05 09:01:58 +00:00
generics.rs Re-organize hir-def/lib.rs 2024-02-10 12:40:23 +01:00
hir.rs Re-order mod declarations 2024-01-27 11:02:34 +01:00
import_map.rs hir-def: Fix warnings about clippy str_to_string rule 2024-02-10 01:00:40 +09:00
item_scope.rs hir-def: Fix warnings about clippy str_to_string rule 2024-02-10 01:00:40 +09:00
item_tree.rs Re-organize hir-def/lib.rs 2024-02-10 12:40:23 +01:00
lang_item.rs internal: switch to tracing from log 2024-01-30 12:27:31 -05:00
lib.rs internal: Remove SELF_REF hack for self referential SyntaxContexts 2024-02-10 16:20:02 +01:00
lower.rs
nameres.rs minor: correct some typos 2024-02-02 18:22:54 +08:00
path.rs needless_borrow 2024-01-19 17:31:01 +01:00
per_ns.rs derivable_impls 2024-01-31 19:06:18 +01:00
pretty.rs feat:Record FnAbi 2024-01-19 15:15:23 +01:00
resolver.rs Cleanup visibility.rs 2024-02-10 13:50:45 +01:00
src.rs Move ChildbySource and HasSource impls to their corresponding modules 2024-02-10 12:09:12 +01:00
test_db.rs borrow_deref_ref 2024-01-31 19:06:18 +01:00
trace.rs Move ChildbySource and HasSource impls to their corresponding modules 2024-02-10 12:09:12 +01:00
visibility.rs Cleanup visibility.rs 2024-02-10 13:50:45 +01:00