Commit graph

20743 commits

Author SHA1 Message Date
bors
6dad8c5528 Auto merge of #18146 - ChayimFriedman2:allow-comment, r=Veykril
fix: Remove check that text of `parse_expr_from_str()` matches the produced parsed tree

This check is incorrect when we have comments and whitespace in the text.

We can strip comments, but then we still have whitespace, which we cannot strip without changing meaning for the parser. So instead I opt to remove the check, and wrap the expression in parentheses (asserting what produced is a parenthesized expression) to strengthen verification.

Fixes #18144.
2024-09-19 13:27:21 +00:00
Chayim Refael Friedman
9ef460ba2d Remove check that text of parse_expr_from_str() matches the produced parsed tree
This check is incorrect when we have comments and whitespace in the text.

We can strip comments, but then we still have whitespace, which we cannot strip without changing meaning for the parser. So instead I opt to remove the check, and wrap the expression in parentheses (asserting what produced is a parenthesized expression) to strengthen verification.
2024-09-19 14:18:07 +03:00
bors
990c48cb0d Auto merge of #18131 - ChayimFriedman2:macro-expand-dollar-crate, r=Veykril
fix: Get rid of `$crate` in expansions shown to the user

Be it "Expand Macro Recursively", "Inline macro" or few other things.

We replace it with the crate name, as should've always been.

Probably fixes some issues, but I don't know what they are.
2024-09-18 20:17:21 +00:00
bors
bd59dc8e4c Auto merge of #18139 - ShoyuVanilla:issue-18109, r=Veykril
fix: Extend `type_variable_table` when modifying index is larger than the table size

Fixes #18109

Whenever we create an inference variable in r-a, we extend `type_variable_table` to matching size here;

f4aca78c92/crates/hir-ty/src/infer/unify.rs (L378-L381)

But sometimes, an inference variable is [created from chalk](ab710e0c9b/chalk-solve/src/infer/unify.rs (L743)) and passed to r-a as a type of an expression or a pattern.
If r-a set diverging flag to this before the table is extended to a sufficient size, it panics here;

f4aca78c92/crates/hir-ty/src/infer/unify.rs (L275-L277)

I think that extending table when setting diverging flag is reasonable becase we are already doing such extending to a size that covers the inference vars created from chalk and this change only covers the order-dependent random cases that this might fail
2024-09-18 20:01:38 +00:00
Chayim Refael Friedman
cfb701ac78 Get rid of $crate in expansions shown to the user
Be it "Expand Macro Recursively", "Inline macro" or few other things.

We replace it with the crate name, as should've always been.
2024-09-18 18:30:59 +03:00
Shoyu Vanilla
b368f9125b fix: Extend type_variable_table when modifying index is larger than table size 2024-09-19 00:02:08 +09:00
bors
f4aca78c92 Auto merge of #18117 - ChayimFriedman2:issue-18089, r=Veykril
fix: Always cache macro expansions' root node in Semantics

Previously some expansions were not cached, but were cached in the expansion cache, which caused panics when later queries tried to lookup the node from the expansion cache.

Fixes #18089.
2024-09-18 09:19:30 +00:00
bors
4a08e77842 Auto merge of #18128 - ChayimFriedman2:external-macros-lint, r=Veykril
fix: Handle errors and lints from external macros

Some lints should not be reported if they originate from an external macro, and quickfixes should be disabled (or they'll change library code).

Fixes #18122.
Closes #18124.
2024-09-18 09:04:53 +00:00
bors
5b7c812634 Auto merge of #18136 - valadaptive:no-mangle-lints, r=Veykril
Don't lint names of #[no_mangle] extern fns

[Rust doesn't run the `non_snake_case_name` lint on `extern fn`s with the `#[no_mangle]` attribute](https://github.com/rust-lang/rust/pull/44966).

The conditions are:
- The function must be `extern` and have a `#[no_mangle]` attribute.
- The function's ABI must not be explicitly set to "Rust".

This PR replicates that logic here.
2024-09-18 08:50:40 +00:00
bors
30e0953187 Auto merge of #18135 - ChayimFriedman2:unsafe-op-in-unsafe-fn, r=Veykril
feat: Add diagnostics for `unsafe_op_in_unsafe_fn`

Turns out it's pretty easy, but I did have to add support for allowed-by-default lints.
2024-09-18 08:35:42 +00:00
bors
2617bd4b92 Auto merge of #18137 - ShoyuVanilla:expr-2021, r=Veykril
feat: Implement `expr_2021`

Resolves #18062
2024-09-18 08:21:24 +00:00
Shoyu Vanilla
d34a663c70 feat: Implement expr_2021 2024-09-18 15:58:03 +09:00
valadaptive
893f79270f Don't lint names of #[no_mangle] extern fns 2024-09-18 01:54:03 -04:00
Chayim Refael Friedman
f6eb5be591 Add diagnostics for unsafe_op_in_unsafe_fn
Turns out it's pretty easy, but I did have to add support for allowed-by-default lints.
2024-09-18 03:02:12 +03:00
DropDemBits
5537e2739c
Use SyntaxEditor in extract_type_alias 2024-09-17 18:25:32 -04:00
DropDemBits
23c4ab7e4b
internal: Extend SourceChangeBuilder to make make working with SyntaxEditors easier 2024-09-17 18:24:27 -04:00
Chayim Refael Friedman
30f53583c8 Handle errors and lints from external macros
Some lints should not be reported if they originate from an external macro, and quickfixes should be disabled (or they'll change library code).
2024-09-17 20:03:56 +03:00
Chayim Refael Friedman
35e171aa01 Always cache macro expansions' root node in Semantics
Previously some expansions were not cached, but were cached in the expansion cache, which caused panics when later queries tried to lookup the node from the expansion cache.
2024-09-17 00:19:39 +03: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