Skip to content

Use the call site for the assign suggestion on macro-expanded types#158509

Closed
ChronoCoders wants to merge 1 commit into
rust-lang:mainfrom
ChronoCoders:diag-assign-sugg-macro-span
Closed

Use the call site for the assign suggestion on macro-expanded types#158509
ChronoCoders wants to merge 1 commit into
rust-lang:mainfrom
ChronoCoders:diag-assign-sugg-macro-span

Conversation

@ChronoCoders

@ChronoCoders ChronoCoders commented Jun 28, 2026

Copy link
Copy Markdown
Contributor

Fixes #158492.

When a let binding's type is a macro invocation, for example:

fn main() {
    let x: vec![];
}

the compiler emits "expected type, found associated function call" and
suggests using = to assign. The suggestion span was derived from the
expanded type span, which points inside the macro definition, so the
help pointed at the standard library and proposed editing the
expansion of vec!:

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

This maps the suggestion span back to the call site with
source_callsite(), so it now points at the user's code:

help: use `=` if you meant to assign
   |
LL -     let x: vec![];
LL +     let x = vec![];

For non-macro types source_callsite() returns the same span, so the
existing cases (Vec::new(), S::new(()), i32::from_be(num), and so
on) are unchanged. The same fix is applied to the sibling
primitive-type branch.

A regression case is added to
tests/ui/suggestions/let-binding-init-expr-as-ty.rs.

When a `let` binding's type is actually a macro call such as
`let x: vec![];`, lowering reports "expected type, found associated
function call" and suggests writing `=` instead. The suggestion span
was built from the expanded type span, which resolves inside the macro
definition, so the suggestion pointed at the standard library source
and proposed editing the expansion of `vec!`.

Compute the span from `hir_ty.span.source_callsite()` so it maps back
to the user's code. For non-macro types `source_callsite` returns the
same span, so existing suggestions are unchanged.
@rustbot

rustbot commented Jun 28, 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 Jun 28, 2026
@rustbot

rustbot commented Jun 28, 2026

Copy link
Copy Markdown
Collaborator

r? @jieyouxu

rustbot has assigned @jieyouxu.
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 73 candidates
  • Random selection from 18 candidates

@rust-log-analyzer

Copy link
Copy Markdown
Collaborator

A job failed! Check out the build log: (web) (plain enhanced) (plain)

Click to see the possible cause of the failure (guessed by this bot)
  PR_CI_JOB: 1
##[endgroup]
    Updating crates.io index
error: failed to get `adler2` as a dependency of package `miniz_oxide v0.8.8`
    ... which satisfies dependency `miniz_oxide = "^0.8.5"` of package `flate2 v1.1.9`
    ... which satisfies dependency `flate2 = "^1.1.9"` of package `citool v0.1.0 (/home/runner/work/rust/rust/src/ci/citool)`

Caused by:
  failed to load source for dependency `adler2`

Caused by:
  unable to update registry `crates-io`

Caused by:
  download of ad/le/adler2 failed

Caused by:
  curl failed

Caused by:

@rustbot rustbot removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jun 28, 2026
@ChronoCoders ChronoCoders reopened this Jun 28, 2026
@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jun 28, 2026
@Kivooeo

Kivooeo commented Jun 28, 2026

Copy link
Copy Markdown
Member

r? me @bors r+ rollup

@rust-bors

rust-bors Bot commented Jun 28, 2026

Copy link
Copy Markdown
Contributor

📌 Commit 503344e has been approved by Kivooeo

It is now in the queue for this repository.

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

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

Kivooeo commented Jun 28, 2026

Copy link
Copy Markdown
Member

hey!

first of all - thank you for fixing this! secondly, i'd like to encourage you to not use llms to write descriptio for PRs, this change is self documented and simple enough to not provide any description at all, but for some reason it's a whole essay about this fix for no reason 🤷‍♂️, please use your own language (even if not perfect english) to write descriptions

@rust-bors

rust-bors Bot commented Jun 28, 2026

Copy link
Copy Markdown
Contributor

⌛ Testing commit 503344e with merge 78e36b7...

Workflow: https://github.com/rust-lang/rust/actions/runs/28313019164

rust-bors Bot pushed a commit that referenced this pull request Jun 28, 2026
…ivooeo

Use the call site for the assign suggestion on macro-expanded types

Fixes #158492.

When a `let` binding's type is a macro invocation, for example:

    fn main() {
        let x: vec![];
    }

the compiler emits "expected type, found associated function call" and
suggests using `=` to assign. The suggestion span was derived from the
expanded type span, which points inside the macro definition, so the
help pointed at the standard library and proposed editing the
expansion of `vec!`:

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

This maps the suggestion span back to the call site with
`source_callsite()`, so it now points at the user's code:

    help: use `=` if you meant to assign
       |
    LL -     let x: vec![];
    LL +     let x = vec![];

For non-macro types `source_callsite()` returns the same span, so the
existing cases (`Vec::new()`, `S::new(())`, `i32::from_be(num)`, and so
on) are unchanged. The same fix is applied to the sibling
primitive-type branch.

A regression case is added to
tests/ui/suggestions/let-binding-init-expr-as-ty.rs.

@fmease fmease left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You probably wanna use Span::find_ancestor_in_same_ctxt instead, Span::source_callsite is too drastic and insensitive to the span of the pattern.

Maybe just (pseudo) "pat.between(ty.in(pat) or bail out)" or more generally(?) (pseudo) "pat.between(ty.in(pat) if some) or (pat.in(ty) if some).between(ty) or bail out", I haven't tried out anything. If none of these work well (i.e., don't address the snippets I gave), there might be other methods.

@bors r-

View changes since this review

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The suggestion is still incorrect for

macro_rules! make {
    () => { let x: vec![]; };
}

fn main() {
    make!();
}

namely

error: expected type, found associated function call
  --> u.rs:5:16
   |
 5 |         let x: vec![];
   |                ^^^^^^
...
12 |     make!();
   |     ------- in this macro invocation
   |
   = note: this error originates in the macro `vec` which comes from the expansion of the macro `make` (in Nightly builds, run with -Z macro-backtrace for more info)
help: use `=` if you meant to assign
   |
 5 -         let x: vec![];
 5 +         let  = : vec![];
   |

@fmease fmease Jun 28, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moreover for

macro_rules! make {
    ($pat:pat, $ty:ty) => {
        let $pat: $ty;
    };
}

fn main() {
    make!(_, vec![]);
}

your branch now suggests

error: expected type, found associated function call
 --> u.rs:8:14
  |
8 |     make!(_, vec![]);
  |              ^^^^^^
  |
help: use `=` if you meant to assign
  |
8 -     make!(_, vec![]);
8 +     make!(_ = vec![]);
  |

main also emits an incorrect suggestion here but it's a different one.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's more, for the following input

macro_rules! make {
    ($pat:pat) => {
        let $pat: Vec::new();
    };
}

fn main() {
    make!(_);
}

main correctly emits

error: expected type, found associated function call
 --> u.rs:3:19
  |
3 |         let $pat: Vec::new();
  |                   ^^^^^^^^^^
...
8 |     make!(_);
  |     -------- in this macro invocation
  |
  = note: this error originates in the macro `make` (in Nightly builds, run with -Z macro-backtrace for more info)
help: use `=` if you meant to assign
  |
3 -         let $pat: Vec::new();
3 +         let $pat = Vec::new();

but your branch incorrectly emits

error: expected type, found associated function call
 --> u.rs:3:19
  |
3 |         let $pat: Vec::new();
  |                   ^^^^^^^^^^
...
8 |     make!(_);
  |     -------- in this macro invocation
  |
  = note: this error originates in the macro `make` (in Nightly builds, run with -Z macro-backtrace for more info)
help: use `=` if you meant to assign
  |
8 -     make!(_);
8 +     make!( = );
  |

@rust-bors rust-bors Bot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Jun 28, 2026
@rust-bors

rust-bors Bot commented Jun 28, 2026

Copy link
Copy Markdown
Contributor

This pull request was unapproved.

Auto build was cancelled due to unapproval. Cancelled workflows:

View changes since this unapproval

@rustbot rustbot removed the S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. label Jun 28, 2026
@ChronoCoders

Copy link
Copy Markdown
Contributor Author

Closing this. For the record: I'm a native English speaker and wrote that description myself. The comment "use your own language, even if not perfect English" is patronizing and based on an assumption with no evidence. A PR description being thorough is not proof of LLM use. I hope future contributors are treated with more good faith.

@Kivooeo

Kivooeo commented Jun 29, 2026

Copy link
Copy Markdown
Member

Thanks, for explaining this.

is patronizing and based on an assumption with no evidence

Below is the reasons why I think so:

Broken formatting (this is how your message looks when press "edit" so it's original look)

image

Usually it should like this, where you specify code block using triple backtick

image

Your description does not using any code blocks and using spacing that makes no sense to me

image

Look once again how inconsistent and so much space loosing it is

This is how copied text looks like after copying from somewhere (here's an real example (one of many and many such cases) with same broken formatting rust-lang/portable-simd#491)

I hope future contributors are treated with more good faith

Not sure where you saw there a bad faith. I was thankful and not judgeful at all. I just told you my feelings towards description because such bloated (in an era of LLM for a simple 2 line change having such big description from new contributor most of the times means it was LLM generated) description usually put an additional work to maintainers

@ChronoCoders

Copy link
Copy Markdown
Contributor Author

Your description does not use any code blocks and uses spacing that makes no sense to me

The description was prose, not code. Prose doesn't require triple backticks.

such bloated description from new contributor most of the times means it was LLM generated

Most of the time ≠ this time. You assumed, you were wrong. That's the bad faith I mentioned — not your tone, your reasoning. Assuming incompetence or AI authorship from a new contributor because they wrote more than two lines is exactly the problem.

@Kivooeo

Kivooeo commented Jun 29, 2026

Copy link
Copy Markdown
Member

Let's take another random PRs from first time contributor and what we will see there - #158473, #157690, #156993

The same problems with spacing, moreover I'm interested in why new contributors (and only) using such interesting wrapping style

Interesting but your previous PR - #158376 have no problems with spacing, what's changed since then?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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

6 participants