Commit ec96819
committed
Auto merge of rust-lang#13816 - WaffleLapkin:postfix_adjustment_hints, r=Veykril
Postfix adjustment hints
# Basic Description
This PR implements "postfix" adjustment hints:

They are identical to normal adjustment hints, but are rendered _after_ the expression. E.g. `expr.*` instead of `*expr`. ~~This mirrors "postfix deref" feature that I'm planning to eventually propose to the compiler.~~
# Motivation
The advantage of being postfix is that you need to add parentheses less often:


This is because a lot of "reborrow" hints are caused by field access or method calls, both of which are postfix and have higher "precedence" than prefix `&` and `*`.
Also IMHO it just looks nicer and it's more clear what is happening (order of operations).
# Modes
However, there are some cases where postfix hints need parentheses but prefix don't (for example `&x` being turned into `(&x).*.*.&` or `&**&x`).
This PR allows users to choose which look they like more. There are 4 options (`rust-analyzer.inlayHints.expressionAdjustmentHints.mode` setting):
- `prefix` — always use prefix hints (default, what was used before that PR)
- `postfix` — always use postfix hints
- `prefer_prefix` — try to minimize number of parentheses, breaking ties in favor of prefix
- `prefer_postfix` — try to minimize number of parentheses, breaking ties in favor of postfix
Comparison of all modes:




# Edge cases
Where are some rare cases where chain hints weirdly interact with adjustment hints, for example (note `SourceAnalyzer.&`):

This is pre-existing, you can get the same effect with prefix hints (`SourceAnalyzer)`).
----
Another weird thing is this:

Here `.&` is a hint and `?` is written in the source code. It looks like `?` is part of the hint because `?.` is ligature in my font. IMO this is a bug in vscode, but still worth mentioning (I'm also too lazy to report it there...).
# Fixed bugs
I've used the "needs parens" API and this accidentally fixed a bug with parens around `as`, see the test diff:
```diff,rust
let _: *const u32 = &mut 0u32 as *mut u32;
//^^^^^^^^^^^^^^^^^^^^^<mut-ptr-to-const-ptr>
+ //^^^^^^^^^^^^^^^^^^^^^(
+ //^^^^^^^^^^^^^^^^^^^^^)
...
let _: *const u32 = &mut 0u32 as *mut u32;
//^^^^^^^^^^^^^^^^^^^^^<mut-ptr-to-const-ptr>
+ //^^^^^^^^^^^^^^^^^^^^^(
+ //^^^^^^^^^^^^^^^^^^^^^)
```
# Changelog
changelog feature Add an option to make adjustment hints (aka reborrow hints) postfix
changelog fix Fix placement of parentheses around `as` casts for adjustment hintsFile tree
8 files changed
+402
-32
lines changed- crates
- ide/src
- inlay_hints
- rust-analyzer/src
- docs/user
- editors/code
8 files changed
+402
-32
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
35 | 35 | | |
36 | 36 | | |
37 | 37 | | |
| 38 | + | |
38 | 39 | | |
39 | 40 | | |
40 | 41 | | |
| |||
74 | 75 | | |
75 | 76 | | |
76 | 77 | | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
77 | 86 | | |
78 | 87 | | |
79 | 88 | | |
| |||
82 | 91 | | |
83 | 92 | | |
84 | 93 | | |
| 94 | + | |
85 | 95 | | |
86 | 96 | | |
87 | 97 | | |
| |||
430 | 440 | | |
431 | 441 | | |
432 | 442 | | |
433 | | - | |
| 443 | + | |
434 | 444 | | |
435 | 445 | | |
436 | 446 | | |
| |||
446 | 456 | | |
447 | 457 | | |
448 | 458 | | |
| 459 | + | |
449 | 460 | | |
450 | 461 | | |
451 | 462 | | |
| |||
0 commit comments