Commit Graph

383 Commits

Author SHA1 Message Date
Ryo Yoshida
29c957f973
Lower and handle trait aliases in HIR 2023-03-04 00:24:07 +09:00
Ryo Yoshida
bda2af71c6
feat: allow generate_function to generate in different local crate 2023-03-03 18:40:13 +09:00
Ryo Yoshida
943de55214
Fix typo 2023-03-03 01:12:39 +09:00
Ryo Yoshida
de4a8961dc
Support removing nested dbg!()s in remove_dbg 2023-03-01 19:06:05 +09:00
bors
a0be16b0b2 Auto merge of #14040 - HKalbasi:mir, r=HKalbasi
Beginning of MIR

This pull request introduces the initial implementation of MIR lowering and interpreting in Rust Analyzer.

The implementation of MIR has potential to bring several benefits:
- Executing a unit test without compiling it: This is my main goal. It can be useful for quickly testing code changes and print-debugging unit tests without the need for a full compilation (ideally in almost zero time, similar to languages like python and js). There is a probability that it goes nowhere, it might become slower than rustc, or it might need some unreasonable amount of memory, or we may fail to support a common pattern/function that make it unusable for most of the codes.
- Constant evaluation: MIR allows for easier and more correct constant evaluation, on par with rustc. If r-a wants to fully support the type system, it needs full const eval, which means arbitrary code execution, which needs MIR or something similar.
- Supporting more diagnostics: MIR can be used to detect errors, most famously borrow checker and lifetime errors,  but also mutability errors and uninitialized variables, which can be difficult/impossible to detect in HIR.
- Lowering closures: With MIR we can find out closure capture modes, which is useful in detecting if a closure implements the `FnMut` or `Fn` traits, and calculating its size and data layout.

But the current PR implements no diagnostics and doesn't support closures. About const eval, I removed the old const eval code and it now uses the mir interpreter. Everything that is supported in stable rustc is either implemented or is super easy to implement. About interpreting unit tests, I added an experimental config, disabled by default, that shows a `pass` or `fail` on hover of unit tests (ideally it should be a button similar to `Run test` button, but I didn't figured out how to add them). Currently, no real world test works, due to missing features including closures, heap allocation, `dyn Trait` and ... so at this point it is only useful for me selecting what to implement next.

The implementation of MIR is based on the design of rustc, the data structures are almost copy paste (so it should be easy to migrate it to a possible future stable-mir), but the lowering and interpreting code is from me.
2023-02-28 09:12:19 +00:00
hkalbasi
cd67589f63 beginning of MIR 2023-02-27 23:45:54 +03:30
Lukas Wirth
9e5fa74279 Simplify 2023-02-27 15:51:45 +01:00
Azriel Hoh
a6f54d6c5f Conditionally add snippet marker. 2023-02-16 10:50:27 +13:00
Lukas Wirth
3c0f20a7bd internal: Enable smallvec's union feature 2023-02-14 17:01:01 +01:00
lh123
626dc651ff fix: Insert spaces when inlining macros 2023-02-10 14:22:56 +08:00
bors
57ea9826b5 Auto merge of #13991 - vasilev-alex:add-braces-assist, r=Veykril
feat: add braces assist

This assist adds support for adding braces where it may be appropriate (e.g. lambda expressions)

![ex](https://user-images.githubusercontent.com/4973437/213783924-7c8a8ab5-6a52-4d80-837c-cf2a9b56f061.gif)
2023-02-07 17:12:01 +00:00
Ryo Yoshida
493cabbde7
Treat scope info retrieval failure as assist failure 2023-02-02 17:47:11 +09:00
Ryo Yoshida
3edde6fcc1
Support generic function in generate_function assist 2023-01-31 21:05:25 +09:00
Ryo Yoshida
0df70d37fc
Minor refactoring 2023-01-31 21:05:04 +09:00
Jonas Schievink
6321b25a21 Apply the assist to itself 2023-01-30 17:52:15 +01:00
Jonas Schievink
1f20804b6e Improve "match to let-else" assist 2023-01-30 17:50:02 +01:00
Jonas Schievink
e7a2d13ff6 Fix "add missing impl members" assist for impls inside blocks 2023-01-27 16:51:55 +01:00
Lukas Wirth
6829190611 Handle boolean scrutinees in match <-> if let replacement assists better 2023-01-27 12:33:40 +01:00
bors
6fd5769996 Auto merge of #14011 - bvanjoi:fix-unwrap-block, r=jonas-schievink
fix(ide-assists): unwrap block when it parent is let stmt

fix #13990
2023-01-25 15:04:03 +00:00
bvanjoi
90b1222b0b fix(ide-assists): unwrap block when it parent is let stmt 2023-01-25 10:25:59 +08:00
Ryo Yoshida
01d8b8947c
Don't escape non-snippets in assist 2023-01-23 00:22:46 +09:00
Alex Vasilev
2a4837089e fixes 2023-01-21 23:42:11 +05:30
Alex Vasilev
dd9dcca7a2 assist id fix 2023-01-21 00:47:02 +05:30
Alex Vasilev
1ab58b190e cargo test 2023-01-21 00:42:29 +05:30
Alex Vasilev
ddb2f8760b typo fix 2023-01-21 00:30:38 +05:30
Alex Vasilev
69ffbe25a1 feat: add braces assist 2023-01-21 00:17:23 +05:30
bvanjoi
8fa69f9f7d feat: array match 2023-01-20 10:59:30 +08:00
Laurențiu Nicola
210757769d Fix replace_arith label 2023-01-17 16:31:34 +02:00
bors
fa874627f0 Auto merge of #13969 - Veykril:workspace.dependencies, r=Veykril
Use workspace.dependencies to declare local dependencies
2023-01-17 10:29:27 +00:00
bors
62907858d5 Auto merge of #13964 - Veykril:workspace.package, r=Veykril
Specify authors, edition, license and rust-versian via workspace.package
2023-01-17 10:13:24 +00:00
Lukas Wirth
bed4db3c62 Use workspace.dependencies to declare local dependencies 2023-01-17 10:52:26 +01:00
bors
455ef0c806 Auto merge of #13935 - ModProg:assist_desugar_doc_comment, r=Veykril
Assist: desugar doc-comment

My need for this arose due to wanting to do feature dependent documentation and therefor convert parts of my doc-comments to attributes.

Not sure about the pub-making of the other handlers functions, but I didn't think it made much sense to reimplement them.
2023-01-16 19:11:19 +00:00
Roland Fredenhagen
ec06313a6d
fix test for required_hashes 2023-01-16 19:25:30 +01:00
Roland Fredenhagen
5e66e49c73
move required_hashes into utils 2023-01-16 18:39:16 +01:00
Lukas Wirth
e4858fe480 Specify authors, edition and license via workspace.package 2023-01-16 16:44:00 +01:00
Lukas Wirth
679df2adf1 Specify rust-version via workspace.package 2023-01-16 16:33:01 +01:00
Ryo Yoshida
fc56cacfc1
Test TraitRef equality before generating missing impl method body 2023-01-16 20:55:56 +09:00
Maybe Waffle
a7787533af Use the fact that Either: AstNode 2023-01-14 15:20:32 +00:00
bvanjoi
e9724e55df fix: check orpat in missing match 2023-01-14 18:54:45 +08:00
Neel Yadav
9721505bf1
Fix panicking Option unwraping in match arm analysis 2023-01-12 22:33:58 -06:00
Roland Fredenhagen
03bc46f96b
Convert pub to pub(crate) 2023-01-12 09:54:48 +01:00
Roland Fredenhagen
6f201cfc56
Assist: desugar doc-comment 2023-01-12 02:28:13 +01:00
Daniel Eades
d218b237fd collapse some nested blocks 2023-01-10 20:40:08 +00:00
Daniel Eades
ac3844a0bb a number of code simplifications 2023-01-10 18:48:51 +00:00
bors
f920b03c6c Auto merge of #13914 - WaffleLapkin:qualify_method_call_rewrite, r=lnicola
minor: Make `qualify_method_call` `RefactorRewrite`

See https://github.com/rust-lang/rust-analyzer/pull/13825#issuecomment-1363289767
2023-01-09 15:22:51 +00:00
bors
d33fa38cc9 Auto merge of #13825 - WaffleLapkin:ufcs_to_method_call_and_back, r=Veykril
feat: Add `unqualify_method_call` assist

...which is the inverse of `qualify_method_call` assist.

![Peek 2022-12-22 22-47](https://user-images.githubusercontent.com/38225716/209206554-8f067206-6fa6-48f8-849e-f6d36ee2e5a1.gif)

Optional future work:
- import the trait if needed
- remove excess references when auto-ref is possible
2023-01-09 14:37:40 +00:00
Maybe Waffle
bdaad9eb15 Make qualify_method_call RefactorRewrite 2023-01-09 14:26:48 +00:00
bors
336608aa92 Auto merge of #13810 - tfpk:tfpk/macro-inline, r=Veykril
Add action to expand a declarative macro once, inline. Fixes #13598

This commit adds a new r-a method, `expandMacroInline`, which expands the macro that's currently selected. See  #13598 for the most applicable issue; though I suspect it'll resolve part of #5949 and make #11888 significantly easier).

The macro works like this:

![rust-analyser-feature](https://user-images.githubusercontent.com/10906982/208813167-3123e379-8fd5-4206-a4f4-5af1129565f9.gif)

I have 2 questions before this PR can be merged:

1. **Should we rustfmt the output?** The advantage of doing this is neater code. The disadvantages are we'd have to format the whole expr/stmt/block (since there's no point just formatting one part, especially over multiple lines), and maybe it moves the code around more in weird ways. My suggestion here is to start off by not doing any formatting; and if it appears useful we can decide to do formatting in a later release.
2.   **Is it worth solving the `$crate` hygiene issue now?** -- I think this PR is usable as of right now for some use-cases; but it is annoying that many common macros (i.e. `println!()`, `format!()`) can't be expanded further unless the user guesses the correct `$crate` value. The trouble with solving that issue is that I think it's complicated and imperfect. If we do solve it; we'd also need to either change the existing `expandMacro`/`expandMacroInline` commands; provide some option to allow/disallow `$crate` expanding; or come to some other compromise.
2023-01-09 14:24:41 +00:00
Maybe Waffle
c782353a90 Rename assist: convert_ufcs_to_method => unqualify_method_call 2023-01-09 14:23:30 +00:00
bors
938a39ab89 Auto merge of #13891 - bvanjoi:reverse-whitespace-in-assists, r=Veykril
fix: keep whitespace in extract function handler

Fixed #13874
2023-01-09 14:11:40 +00:00
Tom Kunc
769273ca4c Simplify code with @Veykril's suggestion. 2023-01-09 07:01:41 -07:00
Maybe Waffle
44c84a8d28 Add convert_ufcs_to_method assist 2023-01-09 13:50:00 +00:00
bors
b0214d81e8 Auto merge of #13843 - Overpeek:master, r=Veykril
fix: generate async delegate methods

Fixes a bug where the generated async method doesn't await the result before returning it.

This is an example of what the output looked like:
```rust
struct Age<T>(T);
impl<T> Age<T> {
    pub(crate) async fn age<J, 'a>(&'a mut self, ty: T, arg: J) -> T {
        self.0
    }
}
struct Person<T> {
    age: Age<T>,
}
impl<T> Person<T> {
    pub(crate) async fn age<J, 'a>(&'a mut self, ty: T, arg: J) -> T {
        self.age.age(ty, arg) // .await is missing
    }
}
```
The `.await` is missing, so the return type is `impl Future<Output = T>` instead of `T`
2023-01-09 13:34:51 +00:00
bors
ae659125a5 Auto merge of #13763 - rami3l:fix/gen-partial-eq-generic, r=Veykril
fix: add generic `TypeBoundList` in generated derivable impl

Potentially fixes #13727.

Continuing with the work in #13732, this fix tries to add correct type bounds in the generated `impl` block:

```diff
  enum Either<T, U> {
      Left(T),
      Right(U),
  }

- impl<T, U> PartialEq for Either<T, U> {
+ impl<T: PartialEq, U: PartialEq> PartialEq for Either<T, U> {
      fn eq(&self, other: &Self) -> bool {
          match (self, other) {
              (Self::Left(l0), Self::Left(r0)) => l0 == r0,
              (Self::Right(l0), Self::Right(r0)) => l0 == r0,
              _ => false,
          }
      }
  }
```
2023-01-09 13:02:09 +00:00
bors
1e20bf38b2 Auto merge of #13684 - unvalley:extract-expressions-from-format-string, r=Veykril
feat: extract_expressions_from_format_string

closes #13640
- rename to `extract_expressions_from_format_string`
- leave identifier from format string
	- but this is from rustc version 1.65.0
	- Should I add flag or something?

Note: the assist behaves below cases for now. I'll create an issue for these.
```rs
let var = 1 + 1;
// ok
format!("{var} {1+1}");   // → format!("{var} {}", 1+1);
format!("{var:?} {1+1}"); // → format!("{var:?} {}", 1 + 1);
format!("{var} {var} {1+1}"); // → format!("{var} {var} {}", 1 + 1);

// breaks (need to handle minimum width by postfix`$`)
format!("{var:width$} {1+1}"); // → format!("{var:width\$} {}", 1+1);
format!("{var:.prec$} {1+1}"); // → format!("{var:.prec\$} {}", 1+1);
format!("Hello {:1$}! {1+1}", "x" 5); // → format("Hello {:1\$}! {}", "x", 1+1);
format!("Hello {:width$}! {1+1}", "x", width = 5); // → println!("Hello {:width\$}! {}", "x", 1+1);
```

https://user-images.githubusercontent.com/38400669/204344911-f1f8fbd2-706d-414e-b1ab-d309376efb9b.mov
2023-01-09 11:40:48 +00:00
bors
814ff01620 Auto merge of #13458 - cameron1024:suggest-checked-wrapping-saturating, r=Veykril
add wrapping/checked/saturating assist

This addresses #13452

I'm not sure about the structure of the code. I'm not sure if it needs to be 3 separate assists, and if that means it needs to be in 3 separate files as well.

Most of the logic is in `util.rs`, which feels funny to me, but there seems to be a pattern of 1 assist per file, and this seems better than duplicating the logic.

Let me know if anything needs changes 😁
2023-01-09 11:24:44 +00:00
unvalley
a310fc0cd5 docs: update assist comment 2023-01-09 12:23:06 +01:00
unvalley
872df2f413 chore: update assist label name 2023-01-09 12:22:29 +01:00
unvalley
285f09cfa8 feat: extract only expressions from format string 2023-01-09 12:22:29 +01:00
unvalley
796a106412 fix: ide assist handlers order 2023-01-09 12:22:25 +01:00
unvalley
9290399ec4 fix: rename to extract_expressions_from_format_string 2023-01-09 12:21:37 +01:00
Lukas Wirth
0dd2682178 Refactor replace_arith assists into one module 2023-01-09 11:59:17 +01:00
bors
f77b68a3cb Auto merge of #13860 - danieleades:clippy, r=lnicola
fix a bunch of clippy lints

fixes a bunch of clippy lints for fun and profit

i'm aware of this repo's position on clippy. The changes are split into separate commits so they can be reviewed separately
2023-01-08 17:29:57 +00:00
bvanjoi
ae73628f6b fix: keep whitespace in extract function handler 2023-01-04 22:10:17 +08:00
Lukas Wirth
b996a54cd8 Skip lifetime elision on fn pointers and fn trait types 2023-01-03 11:58:31 +01:00
Daniel Eades
0a0817905e return value directly from if/else block 2023-01-02 15:02:54 +00:00
Daniel Eades
efd2c20e96 remove useless conversions 2023-01-02 15:02:54 +00:00
Daniel Eades
cc80c5bd07 remove unnecessary lazy evaluations 2023-01-02 15:02:54 +00:00
Daniel Eades
7530d76f00 use pointer args 2023-01-02 14:52:32 +00:00
Daniel Eades
ed128872eb remove needless borrows 2023-01-02 14:52:32 +00:00
Daniel Eades
77051679d7 use inline format args 2023-01-02 14:52:32 +00:00
Ryo Yoshida
332dd6ad6e
fix: merge multiple intersecting ranges 2022-12-31 22:08:53 +09:00
Overpeek
b8f5115b84 fix: generate delegate async methods 2022-12-25 02:47:16 +02:00
Yuri Astrakhan
e16c76e3c3 Inline all format arguments where possible
This makes code more readale and concise,
moving all format arguments like `format!("{}", foo)`
into the more compact `format!("{foo}")` form.

The change was automatically created with, so there are far less change
of an accidental typo.

```
cargo clippy --fix -- -A clippy::all -W clippy::uninlined_format_args
```
2022-12-24 14:36:10 -05:00
Tom Kunc
16654ce154 Create new inline_macro assist. 2022-12-23 23:22:46 +11:00
Yuri Astrakhan
e341e996f7 Clippy-fix explicit auto-deref
Seems like these can be safely fixed. With one, I was particularly
surprised -- `Some(pats) => &**pats,` in body.rs?

```
cargo clippy --fix -- -A clippy::all -D clippy::explicit_auto_deref
```
2022-12-23 02:52:14 -05:00
Yuri Astrakhan
1d59c7b667 Remove non-needed clones
I am not certain if this will improve performance,
but it seems having a .clone() without any need should be removed.

This was done with clippy, and manually reviewed:

```
cargo clippy --fix -- -A clippy::all -D clippy::redundant_clone
```
2022-12-23 02:20:03 -05:00
rami3l
cfa914958c refactor: use generate_trait_impl_text_intransitive for From-like traits 2022-12-17 22:59:30 +08:00
rami3l
12b05d2416 fix: add generic TypeBoundList in generated derivable impl 2022-12-14 19:18:05 +08:00
Maybe Waffle
7ed0871ff6 Fix "needs parens" check in remove_parentheses assist 2022-12-13 00:06:00 +00:00
bors
4596847a88 Auto merge of #13746 - feniljain:fix_extract_function, r=jonas-schievink
fix: make make_body respect comments in extract_function

Possible fix for #13621

### Points to help in review:

- Earlier we were only considering statements in a block expr and hence comments were being ignored, now we handle tokens hence making it aware of comments and then preserving them using `hacky_block_expr_with_comments`

Seems like I am not able to attach output video, github is glitching for it :(
2022-12-12 14:51:03 +00:00
bors
e7dff7491a Auto merge of #13726 - feniljain:fix_assists, r=jonas-schievink
feat: allow unwrap block in let initializers

Possible fix for #13679

### Points to help in review:

- I just added a parent case for let statements and it seems everything else was in place already, so turned out to be a small fix
2022-12-12 14:06:45 +00:00
bors
3a7215b92e Auto merge of #13732 - rami3l:fix/gen-partial-eq, r=jonas-schievink
fix: add fallback case in generated `PartialEq` impl

Partially fixes #13727.

When generating `PartialEq` implementations for enums, the original code can already generate the following fallback case:

```rs
_ => std::mem::discriminant(self) == std::mem::discriminant(other),
```

However, it has been suppressed in the following example for no good reason:

```rs
enum Either<T, U> {
    Left(T),
    Right(U),
}

impl<T, U> PartialEq for Either<T, U> {
    fn eq(&self, other: &Self) -> bool {
        match (self, other) {
            (Self::Left(l0), Self::Left(r0)) => l0 == r0,
            (Self::Right(l0), Self::Right(r0)) => l0 == r0,
            // _ => std::mem::discriminant(self) == std::mem::discriminant(other),
            // ^ this completes the match arms!
        }
    }
}
```

This PR has removed that suppression logic.

~~Of course, the PR could have suppressed the fallback case generation for single-variant enums instead, but I believe that this case is quite rare and should be caught by `#[warn(unreachable_patterns)]` anyway.~~

After this fix, when the enum has >1 variants, the following fallback arm will be generated :

* `_ => false,` if we've already gone through every case where the variants of `self` and `other` match;
* The original one (as stated above) in other cases.

---

Note: The code example is still wrong after the fix due to incorrect trait bounds.
2022-12-12 13:52:49 +00:00
feniljain
d7183fb5d0 fix: make make_body respect comments in extract_function 2022-12-09 18:30:30 +05:30
Maybe Waffle
ba6f0befc8 Simplify remove_parentheses's implementation 2022-12-08 18:54:08 +00:00
Maybe Waffle
8d42439a7d Move precedence handling to crates/syntax 2022-12-08 18:46:30 +00:00
Maybe Waffle
2870b01ec0 Explicitly say that the assist removes *redundant* parentheses 2022-12-08 18:22:57 +00:00
rami3l
57fb18e3bd fix: refine fallback case in generated PartialEq impl 2022-12-07 10:25:17 +08:00
Maybe Waffle
ab061945a1 Consider expression precedense in remove_parentheses assist 2022-12-06 19:11:24 +00:00
Maybe Waffle
5f79279b48 Add remove_parentheses assist 2022-12-06 16:18:25 +00:00
rami3l
fed74c8b71 fix: add fallback case in generated PartialEq impl 2022-12-06 21:54:53 +08:00
feniljain
4794572e36 feat: allow unwrap block in let initializers 2022-12-05 09:00:16 +05:30
Ryo Yoshida
6d4538734e
Add move_const_to_impl assist 2022-12-03 01:22:00 +09:00
Ryo Yoshida
8e03f18e37
fix: check if range contains tail expression 2022-11-27 00:31:02 +09:00
Ryo Yoshida
822c61f559
refactor: remove unnecessary stuff 2022-11-27 00:01:16 +09:00
bors
e668eca632 Auto merge of #13647 - nyz93:fix/tuple-to-named-struct, r=Veykril
fix: tuple to named struct inside macros

seems to fix #13634
2022-11-25 09:41:21 +00:00
bors
5e3ad5ddf6 Auto merge of #13592 - MihailMihov:trait_impl_assist, r=Veykril
Add assist to generate trait impl's

resolves #13553

This pull request adds a `generate_trait_impl` assist, which generates trait impl's for a type. It is almost the same as the one to generate impl's and I also reduced the trigger range to only outside the `RecordFieldList`. Also moved all the tests into separate test functions. A few of the old tests seemed redundant, so I didn't port them.
2022-11-24 20:44:33 +00:00
bors
63a676eedf Auto merge of #13576 - Bben01:supress_missing_impl_inside_block, r=jonas-schievink
Suppress "Implement default members" inside contained items

Fixes #13561
2022-11-24 15:16:36 +00:00
Mihail Mihov
469f620b06 Combine generate_impl and generate_trait_impl into a single file 2022-11-21 22:58:43 +02:00
Mihail Mihov
0bd11f8171 Reduce trigger range of generate_impl assist and update tests 2022-11-21 22:27:26 +02:00