-
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?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| // Issue #128381: Check that we don't suggest changing closure parameter types | ||
| // when they are constrained by method signatures like `Option::inspect`. | ||
|
|
||
| #[derive(Debug)] | ||
| struct A { | ||
| a: u32, | ||
| } | ||
|
|
||
| fn main() { | ||
| let mut opt = Some(A { a: 123 }); | ||
| let ref_mut_opt = opt.as_mut().inspect(|a| { | ||
| a.a += 123; | ||
| //~^ ERROR cannot assign to `a.a`, which is behind a `&` reference | ||
| }); | ||
| dbg!(ref_mut_opt); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| error[E0594]: cannot assign to `a.a`, which is behind a `&` reference | ||
| --> $DIR/issue-128381-mutate-through-option-inspect.rs:12:9 | ||
| | | ||
| LL | a.a += 123; | ||
| | ^^^^^^^^^^ `a` is a `&` reference, so the data it refers to cannot be written | ||
|
|
||
| error: aborting due to 1 previous error | ||
|
|
||
| For more information about this error, try `rustc --explain E0594`. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,12 +15,10 @@ LL | <Option<&mut dyn FnMut()> as Clone>::clone(&cb).map(|cb| cb()); | |
| | ++++++++++++++++++++++++++++++++++++++++++++ + | ||
|
|
||
| error[E0596]: cannot borrow `*cb` as mutable, as it is behind a `&` reference | ||
| --> $DIR/suggest-as-ref-on-mut-closure.rs:12:26 | ||
| --> $DIR/suggest-as-ref-on-mut-closure.rs:12:30 | ||
| | | ||
| LL | cb.as_ref().map(|cb| cb()); | ||
| | -- ^^ `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 | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The suggestion "consider changing this binding's type to be: 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? |
||
|
|
||
| error: aborting due to 2 previous errors | ||
|
|
||
|
|
||
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|inopt.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_paramto match all patterns, theBindingFinderwon't find simple closure parameters like|a|inopt.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 forNode::Param+ update same test expectations again. Would you prefer this approach, or keep them together since they're both required for the fix?Uh oh!
There was an error while loading. Please reload this page.
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.