Commit 813d288
authored
Rollup merge of #158934 - Rohan-Singla:fix/158492, r=jackh726
diagnostics: fix `let x: vec![]` suggestion pointing into stdlib
# Fixes #158492
When a macro call like `vec![]` appears in type position (`let x: vec![];`), the compiler correctly detects the likely typo (`:` instead of `=`), but emits a broken suggestion pointing into the standard library (`library/alloc/src/macros.rs:44`) instead of the user's own code.
## Root cause
After macro expansion, `vec![]` becomes `Vec::new()`. The HIR type node's span therefore points to the expanded code inside the macro definition rather than the original `vec![]` call site.
The suggestion span was computed as:
`stmt.pat.span.between(hir_ty.span)`
This creates a span crossing from the user's code into the standard library, causing the suggestion renderer to display the standard library location.
## Fix
Compute the span using `find_ancestor_in_same_ctxt` so that the pattern and type are resolved into a common syntax context before calling `between`.
For `let x: vec![];`, this walks the type's span up the expansion chain until it reaches the user's file, keeping the suggestion within a single file.
This also handles the reverse case, which `source_callsite` alone does not: when the `let` itself comes from a macro body while the pattern is a call-site metavariable, there is no common context. In that case, no suggestion is emitted rather than producing a nonsensical one.
## Before
```text
help: use `=` if you meant to assign
--> library/alloc/src/macros.rs:44:9
|
44 - $crate::vec::Vec::new()
44 + =
```
## After
```text
help: use `=` if you meant to assign
|
LL - let x: vec![];
LL + let x = vec![];
```
## Tests
Two test cases were added to `tests/ui/suggestions/let-binding-init-expr-as-ty.rs`, which already covers the related `let x: Vec::new()` and `let x: S::new(())` cases:
1. The `vec![]` case described above.
2. A `let` inside a `macro_rules!` body where no suggestion should be emitted.
## Note on file layout
The new `eq_ctxt_suggestion_span` helper lives in `hir_ty_lowering/errors.rs` rather than next to its callers in `mod.rs`.
`mod.rs` was already within a few lines of tidy's 3000-line limit, so keeping the helper there caused the style check to fail.
This is pure code motion with no behavior change.4 files changed
Lines changed: 79 additions & 25 deletions
File tree
- compiler/rustc_hir_analysis/src/hir_ty_lowering
- tests/ui/suggestions
Lines changed: 18 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2055 | 2055 | | |
2056 | 2056 | | |
2057 | 2057 | | |
| 2058 | + | |
| 2059 | + | |
| 2060 | + | |
| 2061 | + | |
| 2062 | + | |
| 2063 | + | |
| 2064 | + | |
| 2065 | + | |
| 2066 | + | |
| 2067 | + | |
| 2068 | + | |
| 2069 | + | |
| 2070 | + | |
| 2071 | + | |
| 2072 | + | |
| 2073 | + | |
| 2074 | + | |
| 2075 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
56 | 56 | | |
57 | 57 | | |
58 | 58 | | |
59 | | - | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
60 | 62 | | |
61 | 63 | | |
62 | 64 | | |
| |||
3302 | 3304 | | |
3303 | 3305 | | |
3304 | 3306 | | |
3305 | | - | |
3306 | | - | |
3307 | | - | |
3308 | | - | |
3309 | | - | |
3310 | | - | |
3311 | | - | |
3312 | | - | |
| 3307 | + | |
| 3308 | + | |
| 3309 | + | |
| 3310 | + | |
| 3311 | + | |
| 3312 | + | |
| 3313 | + | |
3313 | 3314 | | |
3314 | | - | |
| 3315 | + | |
3315 | 3316 | | |
3316 | 3317 | | |
| 3318 | + | |
3317 | 3319 | | |
3318 | 3320 | | |
3319 | 3321 | | |
| |||
3328 | 3330 | | |
3329 | 3331 | | |
3330 | 3332 | | |
3331 | | - | |
3332 | | - | |
3333 | | - | |
3334 | | - | |
3335 | | - | |
3336 | | - | |
3337 | | - | |
3338 | | - | |
| 3333 | + | |
| 3334 | + | |
| 3335 | + | |
| 3336 | + | |
| 3337 | + | |
| 3338 | + | |
| 3339 | + | |
3339 | 3340 | | |
3340 | | - | |
| 3341 | + | |
3341 | 3342 | | |
3342 | 3343 | | |
| 3344 | + | |
3343 | 3345 | | |
3344 | 3346 | | |
3345 | 3347 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
28 | 28 | | |
29 | 29 | | |
30 | 30 | | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
31 | 42 | | |
32 | 43 | | |
33 | 44 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | | - | |
| 2 | + | |
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
| |||
13 | 13 | | |
14 | 14 | | |
15 | 15 | | |
16 | | - | |
| 16 | + | |
17 | 17 | | |
18 | 18 | | |
19 | 19 | | |
| |||
40 | 40 | | |
41 | 41 | | |
42 | 42 | | |
43 | | - | |
| 43 | + | |
44 | 44 | | |
45 | 45 | | |
46 | 46 | | |
| |||
53 | 53 | | |
54 | 54 | | |
55 | 55 | | |
56 | | - | |
| 56 | + | |
57 | 57 | | |
58 | 58 | | |
59 | 59 | | |
60 | 60 | | |
61 | 61 | | |
62 | 62 | | |
63 | 63 | | |
64 | | - | |
| 64 | + | |
65 | 65 | | |
66 | 66 | | |
67 | 67 | | |
| |||
158 | 158 | | |
159 | 159 | | |
160 | 160 | | |
161 | | - | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
162 | 185 | | |
163 | 186 | | |
164 | 187 | | |
0 commit comments