Commit graph

20726 commits

Author SHA1 Message Date
DropDemBits
23c4ab7e4b
internal: Extend SourceChangeBuilder to make make working with SyntaxEditors easier 2024-09-17 18:24:27 -04:00
Chayim Refael Friedman
798c963875 Fix printing of constants greater than i128::MAX 2024-09-16 01:30:18 +03:00
bors
4221354a8f Auto merge of #18073 - alibektas:immutable_tree_panics, r=lnicola
fix: Immutable tree panic in `generate_delegate_trait`

fixes #17835
2024-09-13 09:44:28 +00:00
Ali Bektas
8f5a5e02aa fix: Immutable tree panic in generate_delegate_trait 2024-09-12 23:52:09 +02:00
bors
fd243cd0fb Auto merge of #18099 - ChayimFriedman2:diag-only-necessary, r=Veykril
Use more correct handling of lint attributes

The previous analysis was top-down, and worked on a single file (expanding macros). The new analysis is bottom-up, starting from the diagnostics and climbing up the syntax and module tree.

While this is more efficient (and in fact, efficiency was the motivating reason to work on this), unfortunately the code was already fast enough. But luckily, it also fixes a correctness problem: outline parent modules' attributes were not respected for the previous analysis. Case lints specifically did their own analysis to accommodate that, but it was limited to only them. The new analysis works on all kinds of lints, present and future.

It was basically impossible to fix the old analysis without rewriting it because navigating the module hierarchy must come bottom-up, and if we already have a bottom-up analysis (including syntax analysis because modules can be nested in other syntax elements, including macros), it makes sense to use only this kind of analysis.

Few other bugs (not fundamental to the previous analysis) are also fixed, e.g. overwriting of lint levels (i.e. `#[allow(lint)] mod foo { #[warn(lint)] mod bar; }`.

After this PR is merged I intend to work on an editor command that does workspace-wide diagnostics analysis (that is, `rust-analyzer diagnostics` but from your editor and without having to spawn a new process, which will have to analyze the workspace from scratch). This can be useful to users who do not want to enable check on save because of its overhead, but want to see workspace wide diagnostics from r-a (or to maintainers of rust-analyzer).

Closes #18086.
Closes #18081.
Fixes #18056.
2024-09-12 12:39:27 +00:00
Chayim Refael Friedman
4eb19df5e9 Use more correct handling of lint attributes
The previous analysis was top-down, and worked on a single file (expanding macros). The new analysis is bottom-up, starting from the diagnostics and climbing up the syntax and module tree.

While this is more efficient (and in fact, efficiency was the motivating reason to work on this), unfortunately the code was already fast enough. But luckily, it also fixes a correctness problem: outline parent modules' attributes were not respected for the previous analysis. Case lints specifically did their own analysis to accommodate that, but it was limited to only them. The new analysis works on all kinds of lints, present and future.

It was basically impossible to fix the old analysis without rewriting it because navigating the module hierarchy must come bottom-up, and if we already have a bottom-up analysis (including syntax analysis because modules can be nested in other syntax elements, including macros), it makes sense to use only this kind of analysis.

Few other bugs (not fundamental ti the previous analysis) are also fixed, e.g. overwriting of lint levels (i.e. `#[allow(lint)] mod foo { #[warn(lint)] mod bar; }`.
2024-09-12 15:24:38 +03:00
bors
a91ca0e2fc Auto merge of #18038 - roife:fix-issue-18034, r=Veykril
feat: generate names for tuple-struct in add-missing-match-arms

fix #18034.

This PR includes the following enhancement:

- Introduced a `NameGenerator` in `suggest_name`, which implements an automatic renaming algorithm to avoid name conflicts. Here are a few examples:

```rust
let mut generator = NameGenerator::new();
assert_eq!(generator.suggest_name("a"), "a");
assert_eq!(generator.suggest_name("a"), "a1");
assert_eq!(generator.suggest_name("a"), "a2");

assert_eq!(generator.suggest_name("b"), "b");
assert_eq!(generator.suggest_name("b"), "b1");
assert_eq!(generator.suggest_name("b2"), "b2");
assert_eq!(generator.suggest_name("b"), "b3");
assert_eq!(generator.suggest_name("b"), "b4");
assert_eq!(generator.suggest_name("b3"), "b5");
```

- Updated existing testcases in ide-assists for the new `NameGenerator` (only modified generated names).
- Generate names for tuple structs instead of using wildcard patterns in `add-missing-match-arms`.
2024-09-12 08:21:37 +00:00
bors
27fb6d69ea Auto merge of #18107 - Veykril:push-oopkquknxqxs, r=Veykril
fix: Don't emit empty inlay hint parts
2024-09-12 06:49:21 +00:00
Lukas Wirth
0bb7a8e28b fix: Don't emit empty inlay hint parts 2024-09-12 08:48:06 +02:00
bors
58418ab0ce Auto merge of #18106 - Veykril:push-yzsqoykyowts, r=Veykril
fix: Don't report typed hole error in asm! out ops

Fixes https://github.com/rust-lang/rust-analyzer/issues/18103
2024-09-12 06:21:01 +00:00
Lukas Wirth
6daa6d59f3 fix: Don't report typed hole error in asm! out ops 2024-09-12 08:16:49 +02:00
bors
772acefce9 Auto merge of #18105 - Veykril:push-rquxwznuuwpu, r=Veykril
fix: Faulty notifications should not bring down the server

Fixes https://github.com/rust-lang/rust-analyzer/issues/18055, if a client sends us an unregistered document path in a did save notification it would force us to exit the thread. That is obviously not great behavior, we should be more fallible here
2024-09-12 06:06:14 +00:00
Lukas Wirth
3b8fe6d500 fix: Faulty notifications should not bring down the server 2024-09-12 08:03:45 +02:00
Chayim Refael Friedman
5c42c08518 Fix inference of literals when the expectation is Castable
I followed the compiler: 5bce6d48ff/compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs (L1560-L1579).
2024-09-12 00:57:34 +03:00
bors
c54a827f50 Auto merge of #18075 - roife:fix-issue-17858, r=Veykril
feat: render patterns in params for hovering

Fix #17858

This PR introduces an option to [hir-def/src/body/pretty.rs](08c7bbc2db/crates/hir-def/src/body/pretty.rs) to render the result as a single line, which is then reused for rendering patterns in parameters for hovering.
2024-09-11 12:05:57 +00:00
bors
cfe8e376c9 Auto merge of #18080 - Veykril:dedup, r=Veykril
Remove crate graph deduplication logic

Fixes https://github.com/rust-lang/rust-analyzer/issues/17748
2024-09-11 11:47:17 +00:00
bors
14a18b4849 Auto merge of #18078 - cuishuang:master, r=Veykril
Remove unnecessary symbols and add missing symbols
2024-09-11 11:32:46 +00:00
bors
0267296278 Auto merge of #18057 - alibektas:better_ratoml_testing, r=Veykril
internal: Better testing infra for ratoml

This PR makes some improvements on how we test configs that come from `rust-analyzer.toml` files.
It was primarily used to solve #18021 but along the way I could not really determine the cause of the said issue which makes me think that it may not be related to the changes that I made earlier to the ratoml infra. In either way `custom_snippets` are now made `global` because we still don't have a tree that maps a `SourceRootId` to a set of `Snippet`s.
2024-09-11 11:02:50 +00:00
bors
77e1969c15 Auto merge of #18052 - Coekjan:fix-inline-const, r=Veykril
fix: Fix `inline_const_as_literal` error when the number >= 10

## Description

### The Bug

This PR fixes a small bug in the IDE assistence (`inline_const_as_literal`). When the being-inlined constant is a number and it is greater than or equal to 10, the assistence inserts unexpected string `(0x...)` after the number itself. A simple example is followed:

Current `inline_const_as_literal` changes

```rs
const A: usize = 16;

fn f() -> usize {
    A  // inline the constant
}
```

into

```rs
const A: usize = 16;

fn f() -> usize {
    16 (0x10)
}
```

The bug originates from #14925 & #15306 . #14925 added some unittests, but it just tested the number-inlining behavior when the number is `0`.

50882fbfa2/crates/ide-assists/src/handlers/inline_const_as_literal.rs (L124-L138)

And #15306 modified the behavior of `Const::render_eval` and added the `(0x...)` part after the number (if the number >= `10`). Because of insufficient unittests in #14925, changes about `Const::render_eval` in #15306 introduced this bug with no CI failure.

### The Fix

I think `Const::render_eval` is intended for user-facing value displaying (e.g. hover) and not designed for `inline_const_as_literal`. To fix the bug, I defined a new function named `Const::eval`, which evaluates the value itself faithfully and simply and does nothing else.

## Thanks

Thanks `@roife` for your kind help. Your guidance helped me better understand the code.
2024-09-11 10:48:32 +00:00
bors
b98a2f892c Auto merge of #18018 - ChayimFriedman2:unit-ret-complete-semi, r=Veykril
feat: Automatically add semicolon when completing unit-returning functions

But provide a config to suppress that.

I didn't check whether we are in statement expression position, because this is hard in completion (due to the natural incompleteness of source code when completion is invoked), and anyway using function returning unit as an argument to something seems... dubious.

Fixes #17263.
2024-09-11 10:33:43 +00:00
Lukas Wirth
c2258d8880 Properly set the working directory for proc-macro execution 2024-09-11 12:23:12 +02:00
bors
6c56df8cb4 Auto merge of #17904 - darichey:unresolved-references, r=Veykril
Add command to report unresolved references

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
2024-09-11 10:19:30 +00:00
Lukas Wirth
db04f514f2 Lift out workspace related data into a separate query to preserve crategraph deduplication 2024-09-11 12:16:41 +02:00
bors
a90d78f327 Auto merge of #18050 - rust-lang:davidbarsky/push-uyvtlsvoqrxw, r=Veykril
assist: ensure `replace_qualified_name_with_use` applies to the first path segment

This change helps a bit with the discoverability of `replace_qualified_name_with_use`. Specifically, it ensures that a cursor on the first path segment (e.g., `$0std::fmt::Debug`, where `$0` is the cursor) would result in an import along the lines of `use std::fmt;` and `fmt::Debug;` at the usage sites.
2024-09-11 10:04:56 +00:00
Lukas Wirth
8905f86d8a Remove crate graph deduplication logic 2024-09-11 11:38:42 +02:00
bors
dd026ce6a4 Auto merge of #18093 - ShoyuVanilla:skip-dyn-trait-cast-check, r=Veykril
Skip checks for cast to dyn traits

It seems that chalk fails to solve some obvious goals when there are some recursiveness in trait environments.
And it doesn't support trait upcasting yet. rust-lang/chalk#796

This PR just skips for casting into types containing `dyn Trait` to prevent false positive diagnostics like #18047 and #18083
2024-09-11 07:14:58 +00:00
Shoyu Vanilla
569ac44daf Skip checks for cast to dyn traits 2024-09-11 01:40:13 +09:00
Chayim Refael Friedman
7c44d453c8 Correctly escape strings in our quote macro
This is a small change, but it was the cause of 90% of the errors in `rust-analyzer diagnostics .` 🫢

With this change and #18085 together, all remaining errors are type errors.

This may mean we can enable more errors, but this is out of scope for this PR.
2024-09-10 19:00:32 +03:00
bors
f13c776361 Auto merge of #18032 - DropDemBits:sed-tree-edits, r=davidbarsky
internal: Add preliminary `SyntaxEditor` functionality

Related to #15710

Implements a `SyntaxEditor` interface to abstract over the details of modifying syntax trees, to both simplify creating new code fixes and code actions, as well as start on the path of getting rid of mutable syntax nodes.

`SyntaxEditor` relies on `SyntaxMappingBuilder`s to feed in the correct information to map AST nodes created by `make` constructors, as `make` constructors do not guarantee that node identity is preserved. This is to paper over the fact that `make` constructors simply re-parse text input instead of building AST nodes from the ground up and re-using the provided syntax nodes.

`SyntaxAnnotation`s are used to find where syntax elements have ended up after edits are applied. This is primarily useful for the `add_{placeholder,tabstop}` set of methods on `SourceChangeBuilder`, as that currently relies on the nodes provided being in the final syntax tree.

Eventually, the goal should be to move this into the `rowan` crate when we move away from mutable syntax nodes, but for now it'll stay in the `syntax` crate.

---

Closes #14921 as `SyntaxEditor` ensures that all replace changes are disjoint
Closes #9649 by implementing `SyntaxAnnotation`s
2024-09-10 14:13:08 +00:00
roife
8b0fea8317 feat: generate names for tuple-struct in add-missing-match-arms 2024-09-10 00:30:48 +08:00
roife
825dec8108 refactor: introduce NameGenerator in suggest_name 2024-09-10 00:30:46 +08:00
roife
5db510b258 feat: use shorthand when pretty-print record pat 2024-09-09 23:20:31 +08:00
bors
e35227d186 Auto merge of #18041 - roife:fix-issue-17631, r=Veykril
feat: better name suggestions for fn

fix #17631.

Better name suggestions for fn-calls / method-calls in the form of `from()`, `from_xxx()`, `into()`, etc.
2024-09-09 13:55:20 +00:00
roife
c3c575ebd0 feat: better name suggestions for fn 2024-09-09 21:53:11 +08:00
roife
5c97361622 fix: add parenthesis for or-pattern 2024-09-09 20:59:23 +08:00
roife
5caa56e18a fix: use pretty_print_pat for params in fn 2024-09-09 20:59:23 +08:00
roife
60c42c25c7 feat: add prettifier for Pat 2024-09-09 20:59:23 +08:00
roife
5004371a4a feat: Allow hir-def prettifier formatting into one-line 2024-09-09 20:59:23 +08:00
cuishuang
2f29feab2f Remove unnecessary symbols and add missing symbols
Signed-off-by: cuishuang <imcusg@gmail.com>
2024-09-09 16:36:37 +08:00
Ali Bektas
31ed8c9361 Better testing infra for ratoml 2024-09-08 23:40:38 +02:00
Chayim Refael Friedman
779a7cb0e3 Automatically add semicolon when completing unit-returning functions
But provide a config to suppress that.

I didn't check whether we are in statement expression position, because this is hard in completion (due to the natural incompleteness of source code when completion is invoked), and anyway using function returning unit as an argument to something seems... dubious.
2024-09-08 23:41:16 +03:00
Lukas Wirth
7c5275939a fix: Properly prevent mir building with unknown types present 2024-09-06 14:44:05 +02:00
Lukas Wirth
20f7ab5ab4 fix: Always explicitly set trait ref self types when lowering 2024-09-06 14:06:41 +02:00
Lukas Wirth
70ee4e5545 Bump lsp-server 2024-09-06 14:06:38 +02:00
bors
e09dabf1f3 Auto merge of #18065 - Veykril:catchy-diagnostics, r=Veykril
fix: Catch panics from diagnostics computation
2024-09-06 06:22:41 +00:00
Lukas Wirth
f19a9fe7ba fix: Catch panics from diagnostics computation 2024-09-06 08:21:08 +02:00
Wilfred Hughes
3cf28f1fc6 fix: Updating settings should not clobber discovered projects
`linkedProjects` is owned by the user's configuration, so when users
update this setting, `linkedProjects` is reset. This is problematic when
`linkedProjects` also contains projects discovered with `discoverCommand`.

The buggy behaviour occurred when:

(1) The user configures `discoverCommand` and loads a Rust project.

(2) The user changes any setting in VS Code, so rust-analyzer receives
`workspace/didChangeConfiguration`.

(3) `handle_did_change_configuration` ultimately calls
`Client::apply_change_with_sink()`, which updates `config.user_config`
and discards any items we added in `linkedProjects`.

Instead, separate out `discovered_projects_from_filesystem` and
`discovered_projects_from_command` from user configuration, so user
settings cannot affect any type of discovered project.

This fixes the subtle issue mentioned here:
https://github.com/rust-lang/rust-analyzer/pull/17246#issuecomment-2185259122
2024-09-05 15:46:03 -07:00
David Richey
e602e015e5 Add command to report unresolved references 2024-09-05 12:11:28 -05:00
Lukas Wirth
5b79d922b2 fix: Fix parser panicking on invalid asm options 2024-09-05 17:07:10 +02:00
Lukas Wirth
f74a0c8801 asm! parsing and lowering fixes 2024-09-05 15:08:16 +02:00