-
Notifications
You must be signed in to change notification settings - Fork 0
Fix bogus "consider changing this binding's type" suggestion for closure parameters #4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
Co-authored-by: Enselic <115040+Enselic@users.noreply.github.com>
Co-authored-by: Enselic <115040+Enselic@users.noreply.github.com>
Co-authored-by: Enselic <115040+Enselic@users.noreply.github.com>
| | -- ^^ `cb` is a `&` reference, so the data it refers to cannot be borrowed as mutable | ||
| | | | ||
| | consider changing this binding's type to be: `&mut &mut dyn FnMut()` | ||
| | ^^ `cb` is a `&` reference, so the data it refers to cannot be borrowed as mutable |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wasn't this a valid suggestion? Also check the other tests you updated.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The suggestion "consider changing this binding's type to be: &mut &mut dyn FnMut()" is not actionable because cb is a closure parameter whose type is dictated by the map method signature. The user cannot change |cb| to |cb: &mut &mut dyn FnMut()| because as_ref().map() provides &&mut dyn FnMut(), not &mut &mut dyn FnMut().
The test itself notes this with the comment "This is not exactly right, yet" - acknowledging the suggestions aren't ideal.
Should I revert these test changes and take a more targeted approach that only affects the specific case in issue rust-lang#128381?
|
@copilot You changed the suggestion output of several tests. Aren't those suggestions valid? If yes, the output for those tests should not be altered. |
The suggestions are not actionable - they tell users to change closure parameter types that are constrained by method signatures. For example, in Issue rust-lang#128381 specifically reported this as a "bogus suggestion" because "there is nothing for me to change there." The fix removes these misleading suggestions while preserving helpful ones like "you may want to use However, if you'd like me to take a more conservative approach, I can revert these changes and refine the fix to be more targeted. Please let me know your preference. |
|
@copilot If the suggestions were wrong it's fine to update test outputs. But why does diagnostic line numbers change? |
The line numbers changed because removing the suggestion label on the closure parameter (e.g., the |
| if let hir::Pat { kind: hir::PatKind::Ref(_, _), span, .. } = param.pat | ||
| && *span == self.span | ||
| { | ||
| if param.pat.span == self.span { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot Why did you change this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The original code only matched parameters with explicit reference patterns (PatKind::Ref, like |&x|). This meant closure parameters with inferred types like |a| in opt.inspect(|a| ...) weren't found, so they still got the bogus suggestion.
I changed it to match all parameter patterns by just checking param.pat.span == self.span, which allows finding both |a| and |&a| style parameters. This ensures the fix applies to all closure parameters, not just those with explicit reference patterns.
Commit: a80a61d
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot It sounds like it would make sense to split that out in a separate PR since it probably affects test output? If yes, please do that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The two changes are interdependent - without extending visit_param to match all patterns, the BindingFinder won't find simple closure parameters like |a| in opt.inspect(|a| ...), so the fix for issue rust-lang#128381 wouldn't work.
Splitting into separate PRs would mean: (1) PR to extend visit_param + update test expectations, then (2) PR to skip suggestions for Node::Param + update same test expectations again. Would you prefer this approach, or keep them together since they're both required for the fix?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot I prefer splitting to separate PRs.
Fixes rust-lang#128381
The compiler suggested changing closure parameter types when attempting to mutate through immutable references, even when the parameter type is constrained by the method signature (e.g.,
Option::inspect,Iterator::for_each).Changes
BindingFinder::visit_paramto match all parameter patterns, not just explicit reference patternsNode::Param), since:iter_muthere"Warning
Firewall rules blocked me from connecting to one or more addresses (expand for details)
I tried to connect to the following addresses, but was blocked by firewall rules:
ci-artifacts.rust-lang.orgcurl --location --speed-time 30 --speed-limit 10 --connect-timeout 30 --output /home/REDACTED/work/rust/rust/build/tmp/rust-dev-nightly-x86_64-REDACTED-linux-gnu.tar.xz --continue-at - --retry 3 --show-error --remote-time --fail --silent --retry-all-errors REDACTED(dns block)If you need me to access, download, or install something from one of these locations, you can either:
Original prompt
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.