Commit Graph

832 Commits

Author SHA1 Message Date
Laurențiu Nicola
6bbd106c70 Merge commit '9d8889cdfcc3aa0302353fc988ed21ff9bc9925c' into sync-from-ra 2024-01-15 11:40:09 +02:00
Nicholas Nethercote
6001c50cac Detect NulInCStr error earlier.
By making it an `EscapeError` instead of a `LitError`. This makes it
like the other errors produced when checking string literals contents,
e.g. for invalid escape sequences or bare CR chars.

NOTE: this means these errors are issued earlier, before expansion,
which changes behaviour. It will be possible to move the check back to
the later point if desired. If that happens, it's likely that all the
string literal contents checks will be delayed together.

One nice thing about this: the old approach had some code in
`report_lit_error` to calculate the span of the nul char from a range.
This code used a hardwired `+2` to account for the `c"` at the start of
a C string literal, but this should have changed to a `+3` for raw C
string literals to account for the `cr"`, which meant that the caret in
`cr"` nul error messages was one short of where it should have been. The
new approach doesn't need any of this and avoids the off-by-one error.
2024-01-12 16:19:37 +11:00
bors
da6f7e2c7b Auto merge of #16275 - davidsemakula:ast-path-segments, r=Veykril
fix: Fix `ast::Path::segments` implementation

calling `ast::Path::segments` on a qualifier currently returns all the segments of the top path instead of just the segments of the qualifier.

The issue can be summarized by the simple failing test below:
```rust
#[test]
fn path_segments() {
    //use ra_ap_syntax::ast;
    let path: ast::Path = ...; // e.g. `ast::Path` for "foo::bar::item".

    let path_segments: Vec<_> = path.segments().collect();
    let qualifier_segments: Vec<_> = path.qualifier().unwrap().segments().collect();
    assert_eq!(path_segments.len(), qualifier_segments.len() + 1); // Fails because `LHS = RHS`.
}
```

This PR:
- Fixes the implementation of `ast::Path::segments`
- Fixes `ast::Path::segments` callers that either implicitly relied on behavior of previous implementation or exhibited other "wrong" behavior directly related to the result of `ast::Path::segments` (all callers have been reviewed, only one required modification)
- Removes unnecessary (and now unused) `ast::Path::segments` alternatives
2024-01-09 15:41:48 +00:00
Lukas Wirth
b97ab00bd4 Split out rustc_lexer from rustc_dependencies 2024-01-08 15:24:46 +01:00
Laurențiu Nicola
3afeb24198 Merge commit 'af40101841c45aa75b56f4e9ca745369da8fb4ba' into sync-from-ra 2024-01-08 11:29:25 +02:00
riverbl
3c378b9c70 Add inlay hint for exclusive ranges
Adds an inlay hint containing a '<' character to exclusive range expressions and patterns that specify an upper bound.
2024-01-07 09:33:56 +00:00
davidsemakula
89d6b011c4 remove unnecessary ast::Path::segments alternatives 2024-01-06 12:53:56 +03:00
davidsemakula
08c44a6c24 fix ast::Path::segments implementation 2024-01-06 08:41:11 +03:00
roife
bf0c4acef4 internal: refactor generate_delegate_trait and add comments 2024-01-05 13:59:34 +08:00
Laurențiu Nicola
932d85b529 Merge commit '426d2842c1f0e5cc5e34bb37c7ac3ee0945f9746' into sync-from-ra2 2024-01-03 11:35:07 +02:00
bors
86e559bf3f Auto merge of #16211 - tetsuharuohzeki:update-lint, r=Veykril
Use Cargo's [workspace.lints.*] to config clippy

This change begin to use [`[workspace.lints.*]`](https://doc.rust-lang.org/cargo/reference/workspaces.html#the-lints-table) that is stabilized since [Rust 1.74](https://blog.rust-lang.org/2023/11/16/Rust-1.74.0.html).

By this change, we make the configure more readable and simplify `xargo lint` more.
2024-01-02 14:53:22 +00:00
bors
34df29620a Auto merge of #16112 - roife:rewrite-generate-delete-trait, r=Veykril
fix: rewrite code_action `generate_delegate_trait`

I've made substantial enhancements to the "generate delegate trait" code action in rust-analyzer. Here's a summary of the changes:

#### Resolved the "Can’t find CONST_ARG@158..159 in AstIdMap" error

Fix #15804, fix #15968, fix #15108

The issue stemmed from an incorrect application of PathTransform in the original code. Previously, a new 'impl' was generated first and then transformed, causing PathTransform to fail in locating the correct AST node, resulting in an error. I rectified this by performing the transformation before generating the new 'impl' (using make::impl_trait), ensuring a step-by-step transformation of associated items.

#### Rectified generation of `Self` type

`generate_delegate_trait` is unable to properly handle trait with `Self` type.

Let's take the following code as an example:

```rust
trait Trait {
    fn f() -> Self;
}

struct B {}
impl Trait for B {
    fn f() -> B { B{} }
}

struct S {
    b: B,
}
```

Here, if we implement `Trait` for `S`, the type of `f` should be `() -> Self`, i.e. `() -> S`. However we cannot automatically generate a function that constructs `S`.

To ensure that the code action doesn't generate delegate traits for traits with Self types, I add a function named `has_self_type` to handle it.

#### Extended support for generics in structs and fields within this code action

The former version of `generate_delegate_trait` cannot handle structs with generics properly. Here's an example:

```rust
struct B<T> {
    a: T
}

trait Trait<T> {
    fn f(a: T);
}

impl<T1, T2> Trait<T1> for B<T2> {
    fn f(a: T1) -> T2 { self.a }
}

struct A {}
struct S {
    b$0 : B<A>,
}
```

The former version  will generates improper code:

```rust
impl<T1, T2> Trait<T1, T2> for S {
    fn f(&self, a: T1) -> T1 {
        <B as Trait<T1, T2>>::f( &self.b , a)
    }
}
```

The rewritten version can handle generics properly:

```rust
impl<T1> Trait<T1> for S {
    fn f(&self, a: T1) -> T1 {
        <B<A> as Trait<T1>>::f(&self.b, a)
    }
}
```

See more examples in added unit tests.

I enabled support for generic structs in `generate_delegate_trait` through the following steps (using the code example provided):

1. Initially, to prevent conflicts between the generic parameters in struct `S` and the ones in the impl of `B`, I renamed the generic parameters of `S`.
2. Then, since `B`'s parameters are instantiated within `S`, the original generic parameters of `B` needed removal within `S` (to avoid errors from redundant parameters). An important consideration here arises when Trait and B share parameters in `B`'s impl. In such cases, these shared generic parameters cannot be removed.
3. Next, I addressed the matching of types between `B`'s type in `S` and its type in the impl. Given that some generic parameters in the impl are instantiated in `B`, I replaced these parameters with their instantiated results using PathTransform. For instance, in the example provided, matching `B<A>` and `B<T2>`, where `T2` is instantiated as `A`, I replaced all occurrences of `T2` in the impl with `A` (i.e. apply the instantiated generic arguments to the params).
4. Finally, I performed transformations on each assoc item (also to prevent the initial issue) and handled redundant where clauses.

For a more detailed explanation, please refer to the code and comments. I welcome suggestions and any further questions!
2024-01-02 12:30:19 +00:00
Tetsuharu Ohzeki
efc87092b3 Use Cargo's [workspace.lints.*] to config clippy 2023-12-29 23:51:32 +09:00
Young-Flash
2426d421b4 fix: remove wrong comma after remove unnecessary braces 2023-12-22 21:02:13 +08:00
bors
65ed198819 Auto merge of #16066 - Young-Flash:auto_remove_brace, r=lnicola
fix: auto remove unnecessary braces after remove unused imports

before
![before](https://github.com/rust-lang/rust-analyzer/assets/71162630/8d44f955-f84f-4a92-b13f-5a2dee2ded36)

after
![after](https://github.com/rust-lang/rust-analyzer/assets/71162630/1eab23c8-39bd-4711-97c1-d483ce400a18)
2023-12-20 13:22:00 +00:00
Young-Flash
bc2dee7429 feat: auto remove unnecessary braces after remove unused imports 2023-12-20 19:44:53 +08:00
Lukas Wirth
f49a2fed3f internal: Move out WithFixture into dev-dep only crate 2023-12-18 15:24:08 +01:00
Laurențiu Nicola
e37cf75791 Merge commit '21b06c1beb9bb59369ffd652f5d617bcf6952e05' into sync-from-ra 2023-12-18 09:21:55 +02:00
roife
59aa791fe6 fix: rewrite code_action generate_delegate_trait 2023-12-13 11:22:42 +08:00
bors
d4accf8e96 Auto merge of #16099 - Veykril:flyimport, r=Veykril
internal: Improve import asset perf a bit

And bump the query limit from 40 to 100
2023-12-12 14:37:38 +00:00
Lukas Wirth
34ec665ba1 Simplify and improve perf of import_assets::import_for_item 2023-12-12 11:35:34 +01:00
hkalbasi
801c0ea478 Replace doc_comments_and_attrs with collect_attrs, 2nd round 2023-12-11 22:56:50 +03:30
Laurențiu Nicola
f532576ac5 Merge commit '457b966b171b09a7e57acb710fbca29a4b3526f0' into sync-from-ra 2023-12-11 11:16:01 +02:00
Lukas Wirth
b1a8f83a0c fix: Smaller spans for unresolved field and method diagnostics 2023-12-08 18:46:36 +01:00
Lukas Wirth
9cb13b6efb Allow navigation targets to be duplicated when the focus range lies in the macro definition site 2023-12-06 12:38:19 +01:00
Lukas Wirth
d2cd30007c Implicit format args support 2023-12-05 17:07:00 +01:00
Igor Matuszewski
a7224c998d Don't explicitly warn against semicolon_in_expressions_from_macros
This has been warn-by-default for two years now and has already been
added to the future-incompat lints in 1.68.
2023-12-05 11:35:09 +01:00
Lukas Wirth
890eb17b4e Replace ID based TokenMap with proper relative text-ranges / spans 2023-11-28 10:55:39 +01:00
Laurențiu Nicola
59f5d51852 Merge commit '141fc695dca1df7cfc3c9803972ec19bb178dcbc' into sync-from-ra 2023-11-16 22:27:35 +02:00
Laurențiu Nicola
2814d646cb Bump rayon 2023-11-15 21:14:56 +02:00
Laurențiu Nicola
1fc056ec93 Bump indexmap 2023-11-15 13:21:34 +02:00
Laurențiu Nicola
cb8434e594 Bump either 2023-11-15 13:07:36 +02:00
Laurențiu Nicola
f66df10f87 Bump itertools 2023-11-15 12:53:56 +02:00
bors
535eb0da9d Auto merge of #15874 - DropDemBits:structured-snippet-migrate-4, r=Veykril
internal: Migrate assists to the structured snippet API, part 4

Continuing from #15260

Migrates the following assists:
- `add_turbo_fish`
- `add_type_ascription`
- `destructure_tuple_binding`
- `destructure_tuple_binding_in_subpattern`

I did this a while ago, but forgot to make a PR for the changes until now. 😅
2023-11-15 09:54:45 +00:00
bors
a47330308b Auto merge of #15875 - Young-Flash:fix_grammar, r=Veykril
fix `PathSegment` grammar

close https://github.com/rust-lang/rust-analyzer/issues/15778
2023-11-15 09:43:11 +00:00
DropDemBits
df629627c5
Add tests for LetStmt::set_ty 2023-11-14 17:35:24 -05:00
DropDemBits
787ca888e3
Add IdentPat::set_pat
Needed so that the `tuple_pat` node gets added to the syntax tree,
which is required as we're using structured snippets.
2023-11-13 20:41:06 -05:00
Young-Flash
3e5bc9a9c8 impl qualifying_trait for PathSegment 2023-11-13 18:14:00 +08:00
Young-Flash
23fde40fed fix PathSegment grammar 2023-11-12 11:20:14 +08:00
DropDemBits
4aaa592a9a
Migrate destructure_tuple_binding to mutable ast
Due to the way the current tree mutation api works, we need to collect
changes before we can apply them to the real syntax tree, and also can only
switch to a file once.

`destructure_tuple_binding_in_sub_pattern` also gets migrated even
though can't be used.
2023-11-11 21:07:19 -05:00
DropDemBits
5fc8cc52e2
Add LetStmt::set_ty
Way for setting and removing the type ascription of a let stmt
2023-11-11 21:05:27 -05:00
DropDemBits
92422f7488
Use syntax's version of SyntaxElement 2023-11-11 21:05:27 -05:00
DropDemBits
02c7b8b9ba
Add MethodCallExpr::get_or_create_generic_arg_list
Mirrors `PathSegment's` version, except that it always generates a
turbofish
2023-11-11 21:05:26 -05:00
Laurențiu Nicola
d1d111d09e Merge commit '3b7c7f97e4a7bb253a8d398ee4f8346f6cf2817b' into sync-from-ra 2023-11-08 08:15:03 +02:00
Young-Flash
8d8d12120d feat: add generate_mut_trait_impl assist 2023-11-05 12:27:10 +08:00
cui fliter
9c99afe3aa Fix some typos
Signed-off-by: cui fliter <imcusg@gmail.com>
2023-10-30 10:31:50 +08:00
bohan
d20471334f use visibility to check unused imports and delete some stmts 2023-10-22 21:27:46 +08:00
Lukas Wirth
88a00bf49d Shrink PatPtr by swapping its AstPtr and Either wrap order 2023-10-06 12:32:37 +02:00
Lukas Wirth
0dbde71159 Simplify 2023-09-26 12:25:59 +02:00
bors
df75809a85 Auto merge of #15484 - rmehri01:14779_bool_to_enum_assist, r=Veykril
feat: Bool to enum assist

This adds the `bool_to_enum` assist, which converts the type of boolean local variables, fields, constants and statics to a new `enum` type, making it easier to distinguish the meaning of `true` and `false` by renaming the variants.

Closes #14779
2023-09-22 07:19:12 +00:00
bors
11ffcc08a3 Auto merge of #15615 - shogo-nakano-desu:refactor/fix-clippy-lints, r=Veykril
Refactor/fix clippy lints

As title says.
2023-09-22 06:46:29 +00:00
Laurențiu Nicola
bcfc997eac Merge commit '258b15c506a2d3ad862fd17ae24eaf272443f477' into sync-from-ra 2023-09-18 12:33:49 +03:00
hkalbasi
f4704bc8ae Switch to in-tree rustc dependencies with a cfg flag 2023-09-15 18:10:11 +03:30
shogo-nakano-desu
0bb2298ac6 refactor: remove TODO with no explanation 2023-09-15 16:43:31 +09:00
shogo-nakano-desu
ebbbaaa90f refactor: fix clippy lints 2023-09-15 16:43:21 +09:00
Ryan Mehri
d7a8e800c9 feat: initial version of bool_to_enum assist 2023-09-09 11:58:19 -07:00
Lukas Wirth
abe8f1ece4 Implement builtin#format_args, using rustc's format_args parser 2023-09-05 19:19:46 +02:00
Lukas Wirth
3431d586e5 Insert builtin#asm into asm! expansion 2023-09-05 14:00:49 +02:00
Lukas Wirth
15048304e3 Implement offset_of in hir-def and hir-ty 2023-09-05 12:27:52 +02:00
Lukas Wirth
9b8eb807a3 Parse builtin# syntax 2023-09-05 10:36:35 +02:00
Laurențiu Nicola
30d8aa1bec Merge commit '9b3d03408c66749d56466bb09baf2a7177deb6ce' into sync-from-ra 2023-08-21 12:44:09 +03:00
bors
b771de3fdc Auto merge of #15179 - ponyii:fix/default-values-of-const-params-are-ignored, r=HKalbasi
the "add missing members" assists: implemented substitution of default values of const params

To achieve this, I've made `hir::ConstParamData` store the default values
2023-08-15 10:17:43 +00:00
bors
7ca45dcf04 Auto merge of #15410 - alibektas:15240/invalid-demorgan, r=Veykril
internal : rewrite DeMorgan assist

fixes #15239 , #15240 . This PR is a rewrite of the DeMorgan assist that essentially rids of all the string manipulation and modifies syntax trees to apply demorgan on a binary expr. The main reason for the rewrite is that I wanted to use `Expr::needs_parens_in` method to see if the expr on which the assist is applied would still need the parens it had once the parent expression's operator had equal precedence with that of the expression. I used `.clone_(subtree|for_update)` left and right and probably more than I should have, so I would also be happy to hear how I could have prevented redundant cloning.
2023-08-15 07:29:28 +00:00
Lukas Wirth
e5b23e3bc1 Derive block attributes from block item tree 2023-08-10 18:52:27 +02:00
ponyii
4ebdc6f052 syntax update: the default value of ConstParam turned from Expr into ConstArg 2023-08-08 21:57:54 +04:00
ponyii
52b4392724 the "add missing members" assists: implemented substitution of default values of const params 2023-08-08 21:57:54 +04:00
Ali Bektas
ef5c6daf6e Rewrite DeMorgan without str manipulation. 2023-08-08 15:49:27 +02:00
Lukas Wirth
cba39f8553 fix: Fix float parser hack creating empty NameRef tokens 2023-08-08 14:44:33 +02:00
Laurențiu Nicola
aa55ce9567 Merge commit 'baee6b338b0ea076cd7a9f18d47f175dd2ba0e5d' into sync-from-ra 2023-08-07 12:03:15 +03:00
bors
62dcf39ef0 Auto merge of #14723 - obsgolem:master, r=Veykril
Added remove unused imports assist

This resolves the most important part of #5131. I needed to make a couple of cosmetic changes to the search infrastructure to do this.

A few open questions:
* Should imports that don't resolve to anything be considered unused? I figured probably not, but it would be a trivial change to make if we want it.
* Is there a cleaner way to make the edits to the use list?
* Is there a cleaner way to get the list of uses that intersect the current selection?
* Is the performance acceptable? When testing this on itself, it takes a good couple seconds to perform the assist.
* Is there a way to hide the rustc diagnostics that overlap with this functionality?
2023-08-01 09:50:16 +00:00
Lukas Wirth
7c765d9f9e fix: Expand eager macros to delimited comma separated expression list 2023-07-30 17:31:26 +02:00
Laurențiu Nicola
0155385b57 Merge commit '99718d0c8bc5aadd993acdcabc1778fc7b5cc572' into sync-from-ra 2023-07-24 12:21:34 +03:00
oxalica
59a3e42ac9
Fix unescaping of C string literals 2023-07-18 18:52:34 +08:00
oxalica
de1f766820
Fix highlighting of byte escape sequences
Currently non-UTF8 escape sequences in byte strings and any escape
sequences in byte literals are ignored.
2023-07-17 22:52:58 +08:00
Laurențiu Nicola
4704881b64 Merge commit '37f84c101bca43b11027f30ab0c2852f9325bc3d' into sync-from-ra 2023-07-17 16:49:15 +03:00
bors
132ba2cf77 Auto merge of #15248 - Veykril:eager, r=Veykril
Fix eager token mapping panics
2023-07-13 07:39:30 +00:00
Lukas Wirth
6a7b905c86 Fix the eager token maps by re-mapping the textranges between the input and input expansion 2023-07-13 09:22:38 +02:00
DropDemBits
8c40fa33df
Add HasVisibilityEdit::set_visibility 2023-07-09 20:50:20 -04:00
Josiah Bills
7a87a35f1a Added remove_unused_imports assist. 2023-07-09 17:44:27 -04:00
DropDemBits
419f641d49
add AttrsOwnerEdit::add_attr 2023-07-07 20:06:09 -04:00
DropDemBits
c0172333c2
add attr-related make functions 2023-07-07 20:06:09 -04:00
bors
aa91eda902 Auto merge of #15152 - alibektas:14987, r=Veykril,lowr
assist : generate trait from impl

fixes #14987 . As the name suggests this assist is used to generate traits from inherent impls while adapting the original impl to fit to the newly generated trait. I made some decisions regarding when the assist should be applicable. These are surely open to discussion. I looking forward to any feedback.

![generate_trait_from_impl_v1](https://github.com/rust-lang/rust-analyzer/assets/20956650/05d4dda5-604a-4108-8b82-9b60bd45894a)
2023-07-05 16:56:37 +00:00
Ali Bektas
03423116ad Generate trait from impl v2 2023-07-04 17:33:45 +02:00
Ali Bektas
677151e4e1 Version 1 2023-06-27 18:03:36 +02:00
Josh Stone
380773b3e8 Upgrade to indexmap v2 2023-06-24 17:35:20 -07:00
oxalica
2cd7b0cfce Remove unused StmtList::push_front 2023-06-19 22:56:31 +08:00
oxalica
52f1ce17aa Correctly handle inlining of async fn 2023-06-19 17:14:17 +08:00
Laurențiu Nicola
9326cf7f0c Merge commit 'cd3bf9fe51676b520c546460e6d8919b8c8ff99f' into sync-from-ra 2023-06-19 09:14:04 +03:00
hkalbasi
f8594f78bb Use ConstArg instead of Expr for AstId of InTypeConstId 2023-06-11 00:39:28 +03:30
Laurențiu Nicola
c48062fe2a Merge commit 'aa9bc8612514d216f84eec218dfd19ab83f3598a' into sync-from-ra 2023-06-05 12:04:23 +03:00
DropDemBits
dd5f05590e
Migrate extract_type_alias to mutable ast 2023-06-05 00:24:12 -04:00
bors
e33c0b4b5e Auto merge of #14842 - alibektas:internal/ast-make-improve, r=Veykril
Improve ast::make

Add `ty_alias` and make `impl_trait` , `fn` and `impl_` have more coverage.
2023-05-30 16:09:46 +00:00
Sebastian Ziebell
95f59668e6 Fix removal of generic param from list
This removes an existing generic param from the `GenericParamList`. It
also considers to remove the extra colon & whitespace to the previous
sibling.

* change order to get all param types first and mark them as mutable
  before the first edit happens
* add helper function to remove a generic parameter
* fix test output
2023-05-26 13:13:15 +02:00
Sebastian Ziebell
59f8827a6f Implement assist to replace named generic with impl
This adds a new assist named "replace named generic with impl" to move
the generic param type from the generic param list into the function
signature.

```rust
fn new<T: ToString>(input: T) -> Self {}
```

becomes

```rust
fn new(input: impl ToString) -> Self {}
```

The first step is to determine if the assist can be applied, there has
to be a match between generic trait param & function paramter types.

* replace function parameter type(s) with impl
* add new `impl_trait_type` function to generate the new trait bounds with `impl` keyword  for use in the
  function signature
2023-05-26 13:13:11 +02:00
bors
a512774fd9 Auto merge of #14752 - ponyii:fix/generate_derive_breaks_indentation, r=Veykril
fix: assists no longer break indentation

Fixes https://github.com/rust-lang/rust-analyzer/issues/14674

These are _ad hoc_ patches for a number of assists that can produce incorrectly indented code, namely:
- generate_derive
- add_missing_impl_members
- add_missing_default_members

Some general solution is required in future, as the same problem arises in many other assists, e.g.
- replace_derive_with...
- generate_default_from_enum...
- generate_default_from_new
- generate_delegate_methods
(the list is incomplete)
2023-05-24 18:58:07 +00:00
bors
2df56cadcb Auto merge of #14755 - poliorcetics:clippy-fixes, r=Veykril
Fix: a TODO and some clippy fixes

- fix(todo): implement IntoIterator for ArenaMap<IDX, V>
- chore: remove unused method
- fix: remove useless `return`s
- fix: various clippy lints
- fix: simplify boolean test to a single negation
2023-05-24 11:13:52 +00:00
alibektas
51ec2ced69 Improve ast::make
Add `ty_alias` and make `impl_trait` , `fn` and `impl_` have more coverage.
2023-05-23 03:12:57 +02:00
Lukas Wirth
4b577e2bc8 Support c string literals 2023-05-18 11:06:05 +02:00
Lukas Wirth
099b5b3b15 internal: Bump rustc_lexer 2023-05-18 10:17:40 +02:00
ponyii
e07d6382de add_missing_impl_members no longer breaks indentation 2023-05-10 16:35:48 +04:00