Skip to content

Conversation

JohnTitor
Copy link
Member

Closes #147312

@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 Oct 11, 2025
@rustbot
Copy link
Collaborator

rustbot commented Oct 11, 2025

r? @davidtwco

rustbot has assigned @davidtwco.
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

err.span_suggestion_verbose(
pat.span.shrink_to_hi(),
"specify the type in the closure argument list",
format!(": &{concrete_type}"),
Copy link
Member

Choose a reason for hiding this comment

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

This also suggests & for PatKind::Ref(_, Mut) when it should probably suggest &mut

Copy link
Member

@fmease fmease Oct 11, 2025

Choose a reason for hiding this comment

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

This doesn't account for nested refs like &&x (should suggest : &&T not : &T)

Copy link
Member Author

Choose a reason for hiding this comment

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

Good point! Added a check in dbc7327 (#147577)
(I cannot come up with a mutable test case which is caught by E0689 🤔)

Copy link
Member

Choose a reason for hiding this comment

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

I cannot come up with a mutable test case which is caught by E0689

[&mut 0].into_iter().map(|&mut x| x.pow(0))

Copy link
Member Author

@JohnTitor JohnTitor Oct 11, 2025

Choose a reason for hiding this comment

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

I think it isn't caught by E0689 but other codes like E0282 (I guess other inference errors are stronger here), so the suggestion here wouldn't be showed.

}
// For closure parameters with reference patterns (e.g., |&v|), suggest the type annotation
// on the pattern itself, e.g., |&v: &i32|
(FileName::Real(_), Node::Pat(pat))
Copy link
Member

@fmease fmease Oct 11, 2025

Choose a reason for hiding this comment

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

(preexisting) Matching on the file name presumably to suppress this suggestion if this comes from a macro expansion (FileName::MacroExpansion) is wild and simply incorrect, lol. E.g., it means it never suggests anything if the source is STDIN (FileName::Anon) like for printf '/* ... */' | rustc -.

Copy link
Member Author

Choose a reason for hiding this comment

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

Makes sense, but addressing it in this PR feels like out-of-scope, so added FIXME: 632e759 (#147577)

Or do you want me to resolve it here?

Copy link
Member

Choose a reason for hiding this comment

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

Or do you want me to resolve it here?

No, a FIXME is fine 👍

Comment on lines +2574 to +2579
if let Node::Pat(binding_pat) = self.tcx.hir_node(hir_id)
&& let hir::PatKind::Binding(..) = binding_pat.kind
&& let Node::Pat(parent_pat) = parent_node
&& matches!(parent_pat.kind, hir::PatKind::Ref(..)) =>
Copy link
Member

@fmease fmease Oct 11, 2025

Choose a reason for hiding this comment

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

Food for thought (I don't have time to investigate this): If the unsolved inference variable is not an integer/float one ({integer} / {float}) but a normal one, then need_type_info / InferSourceKind is able to figure out the type annotation for various kinds of patterns.

E.g.,

[].into_iter().filter(|&x| x.pow(0)); // suggests `: &T` on master
[].into_iter().filter(|(x,)| x.pow(0)); // suggests `: &(T,)` on master

So I guess ideally we would be modifying needs_type_info to properly deal with integer/float ty vars since that seems to have all the infrastructure in place already (unless they really follow a different code path in which case my suggestion would be nonsensical).

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

Labels

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.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

suggestion for "can't call method ... on ambiguous numeric type" does not account for references

4 participants