Skip to content

Conversation

@heathdutton
Copy link
Contributor

@heathdutton heathdutton commented Jan 12, 2026

When a closure has an inferred parameter type like |ch| and the expected type differs in borrowing (e.g., char vs &char), the suggestion would incorrectly propose |char| instead of something valid like |ch: char|.

This happened because the code couldn't walk explicit & references in the HIR when the type is inferred, and fell back to replacing the entire parameter span with the expected type name.

Fix by only emitting the suggestion when we can properly identify the & syntax to remove.

Fixes #150693

@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 Jan 12, 2026
@rustbot
Copy link
Collaborator

rustbot commented Jan 12, 2026

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

@rustbot

This comment has been minimized.

When a closure has an inferred parameter type like `|ch|` and the
expected type differs in borrowing (e.g., `char` vs `&char`), the
suggestion code would incorrectly suggest `|char|` instead of the
valid `|ch: char|`.

This happened because the code couldn't walk explicit `&` references
in the HIR when the type is inferred, and fell back to replacing the
entire parameter span with the expected type name.

Fix by only emitting the suggestion when we can properly identify the
`&` syntax to remove.
@heathdutton heathdutton force-pushed the issue-150693-closure-arg-suggestion branch from 74abd3d to afe76df Compare January 12, 2026 23:07
Copy link
Member

@jieyouxu jieyouxu left a comment

Choose a reason for hiding this comment

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

@jieyouxu
Copy link
Member

@bors r+ rollup

@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 Jan 13, 2026
@rust-bors
Copy link
Contributor

rust-bors bot commented Jan 13, 2026

📌 Commit afe76df has been approved by jieyouxu

It is now in the queue for this repository.

rust-bors bot pushed a commit that referenced this pull request Jan 13, 2026
…uwer

Rollup of 14 pull requests

Successful merges:

 - #150151 (Destabilise `target-spec-json`)
 - #150826 (Add `f16` inline ASM support for s390x)
 - #150883 (Improve span for "unresolved intra doc link" on `deprecated` attribute)
 - #150934 (Move some checks from `check_doc_attrs` directly into `rustc_attr_parsing`)
 - #150943 (Port `#[must_not_suspend]` to attribute parser)
 - #150990 (std: sys: net: uefi: Make TcpStream Send)
 - #150995 (core: ptr: split_at_mut: fix typo in safety doc)
 - #150998 (Relax test expectation for @__llvm_profile_runtime_user)
 - #151002 (Remove a workaround for a bug (take 2))
 - #151005 (Fix typo in `MaybeUninit` docs)
 - #151011 (Update books)
 - #151029 (rustc-dev-guide subtree update)
 - #151032 (fix: added missing backtick in triagebot.toml)
 - #151035 (Don't suggest replacing closure parameter with type name)

r? @ghost
@rust-bors rust-bors bot merged commit 7ee6257 into rust-lang:main Jan 13, 2026
11 checks passed
rust-timer added a commit that referenced this pull request Jan 13, 2026
Rollup merge of #151035 - issue-150693-closure-arg-suggestion, r=jieyouxu

Don't suggest replacing closure parameter with type name

When a closure has an inferred parameter type like `|ch|` and the expected type differs in borrowing (e.g., `char` vs `&char`), the suggestion would incorrectly propose `|char|` instead of something valid like `|ch: char|`.

This happened because the code couldn't walk explicit `&` references in the HIR when the type is inferred, and fell back to replacing the entire parameter span with the expected type name.

Fix by only emitting the suggestion when we can properly identify the `&` syntax to remove.

Fixes #150693
@rustbot rustbot added this to the 1.94.0 milestone Jan 13, 2026
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.

E0631 in closure arguments suggest invalid type parameter

3 participants