Skip to content

diagnostics: fix let x: vec![] suggestion pointing into stdlib - #158934

Open
Rohan-Singla wants to merge 1 commit into
rust-lang:mainfrom
Rohan-Singla:fix/158492
Open

diagnostics: fix let x: vec![] suggestion pointing into stdlib#158934
Rohan-Singla wants to merge 1 commit into
rust-lang:mainfrom
Rohan-Singla:fix/158492

Conversation

@Rohan-Singla

@Rohan-Singla Rohan-Singla commented Jul 7, 2026

Copy link
Copy Markdown
Contributor

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

help: use `=` if you meant to assign
  --> library/alloc/src/macros.rs:44:9
   |
44 -         $crate::vec::Vec::new()
44 +          =

After

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.

@rustbot

rustbot commented Jul 7, 2026

Copy link
Copy Markdown
Collaborator

HIR ty lowering was modified

cc @fmease

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jul 7, 2026
@rustbot

rustbot commented Jul 7, 2026

Copy link
Copy Markdown
Collaborator

r? @jackh726

rustbot has assigned @jackh726.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: compiler
  • compiler expanded to 75 candidates
  • Random selection from 19 candidates

@rustbot

This comment has been minimized.

@rustbot

This comment has been minimized.

@Rohan-Singla Rohan-Singla changed the title Fix/158492 diagnostics: fix let x: vec![] suggestion pointing into stdlib Jul 7, 2026
@rustbot

This comment has been minimized.

@rustbot

This comment has been minimized.

@Kivooeo

Kivooeo commented Jul 8, 2026

Copy link
Copy Markdown
Member

Hi, can you please take a look at this review #158509 (review).

@Rohan-Singla

Rohan-Singla commented Jul 8, 2026

Copy link
Copy Markdown
Contributor Author

Hi, can you please take a look at this review #158509 (review).

Thanks for the feedback! Switched from source_callsite() to find_ancestor_in_same_ctxt the old approach jumped too far back in the expansion chain.

Added a helper eq_ctxt_suggestion_span that tries to find ancestors of pat and ty in each other's syntax context, only emitting the suggestion when both spans can be reconciled in the same context.

Also added a test for the macro case which was pointed out it now correctly emits no suggestion instead of make!( = ).

@Rohan-Singla

Copy link
Copy Markdown
Contributor Author

Hey , I am still waiting up for review on this one just a follow up . thanks !

cc : @Kivooeo @jackh726

@rust-bors

This comment has been minimized.

@jackh726

Copy link
Copy Markdown
Member

Sorry for the delay. r=me after rebase

@rustbot

rustbot commented Aug 19, 2026

Copy link
Copy Markdown
Collaborator

This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

When a macro call like `vec![]` appears in type position, the compiler's
"use `=` if you meant to assign" suggestion was pointing into the macro
definition in stdlib instead of the user's own code.

The suggestion span was computed as `stmt.pat.span.between(hir_ty.span)`.
After expansion `hir_ty.span` lies inside the macro body, so that span
crossed syntax contexts and the renderer displayed the stdlib location.

Compute the span via `find_ancestor_in_same_ctxt` instead, so the pattern
and the type are compared in a common syntax context. When no common
context exists -- e.g. the `let` comes from a macro body while the pattern
is a call-site metavariable -- no suggestion is emitted at all.
@Rohan-Singla

Rohan-Singla commented Aug 19, 2026

Copy link
Copy Markdown
Contributor Author

Sorry for the delay. r=me after rebase

Hey Jack , no worried i have rebased the code and fixed the conflicts i have also updated the PR description would encourage you to have a look at it .

r? @jackh726

Thanks!

@rustbot

rustbot commented Aug 19, 2026

Copy link
Copy Markdown
Collaborator

Requested reviewer is already assigned to this pull request.

Please choose another assignee.

@jackh726

Copy link
Copy Markdown
Member

@bors r+

@rust-bors

rust-bors Bot commented Aug 19, 2026

Copy link
Copy Markdown
Contributor

📌 Commit f151cb9 has been approved by jackh726

It is now in the queue for this repository.

🌲 The tree is currently closed for pull requests below priority 10. This pull request will be tested once the tree is reopened.

Reason for tree closure: Main branch is broken

@rust-bors rust-bors Bot added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Aug 19, 2026
@Kivooeo

Kivooeo commented Aug 20, 2026

Copy link
Copy Markdown
Member

please, disclose llm usage, we have new policy that requires it

JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Aug 20, 2026
diagnostics: fix `let x: vec![]` suggestion pointing into stdlib

# Fixes rust-lang#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.
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Aug 20, 2026
diagnostics: fix `let x: vec![]` suggestion pointing into stdlib

# Fixes rust-lang#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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

let x: vec![] makes rust suggest modifying stdlib

4 participants