-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Solve a error .clone()
suggestion when moving a mutable reference
#127579
Conversation
This comment has been minimized.
This comment has been minimized.
.clone()
suggestion when moving a mutable reference.clone()
suggestion when moving a mutable reference
.clone()
suggestion when moving a mutable reference.clone()
suggestion when moving a mutable reference
.clone()
suggestion when moving a mutable reference.clone()
suggestion when moving a mutable reference
@@ -445,13 +442,36 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, '_, 'infcx, 'tcx> { | |||
} else { | |||
(None, &[][..], 0) | |||
}; | |||
let mut reborrow = false; |
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.
let mut reborrow = false; | |
let mut suggest_reborrow = false; |
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.
Done. Thank you.
&& let node = self.infcx.tcx.hir_node_by_def_id(def_id) | ||
&& let Some(fn_sig) = node.fn_sig() | ||
&& let Some(ident) = node.ident() | ||
&& let Some(pos) = args.iter().position(|arg| arg.hir_id == expr.hir_id) | ||
&& let Some(arg) = fn_sig.decl.inputs.get(pos + offset) |
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.
this only works for functions from the current crate, greatly lowering the usefulness of this new diagnostics.
You should instead be able to use tcx.fn_sig(def_id).skip_binder().skip_binder().inputs()[pos + offset].is_param()
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.
this only works for functions from the current crate, greatly lowering the usefulness of this new diagnostics.
You should instead be able to use
tcx.fn_sig(def_id).skip_binder().skip_binder().inputs()[pos + offset].is_param()
Done. Thank you.
Because is_param
in
rust/compiler/rustc_middle/src/ty/sty.rs
Line 1048 in c92a8e4
pub fn is_param(self, index: u32) -> bool { |
index
which I'm not sure what means, I don't use this function.
false | ||
}; | ||
reborrow = is_sugg_reborrow(); | ||
|
||
let mut span: MultiSpan = arg.span.into(); | ||
span.push_span_label( |
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.
also I feel like we should not emit the "consider changing this parameter type in function generic
to borrow instead if owning the value isn't necessary" note if a reborrow would work. so either suggest a reborrow or suggest changing the fn_sig, but not both
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.
also I feel like we should not emit the "consider changing this parameter type in function
generic
to borrow instead if owning the value isn't necessary" note if a reborrow would work. so either suggest a reborrow or suggest changing the fn_sig, but not both
Done. Thank you.
2b60569
to
1f78e60
Compare
This comment has been minimized.
This comment has been minimized.
.clone()
suggestion when moving a mutable reference.clone()
suggestion when moving a mutable reference
☔ The latest upstream changes (presumably #127614) made this pull request unmergeable. Please resolve the merge conflicts. |
.clone()
suggestion when moving a mutable reference.clone()
suggestion when moving a mutable reference
☔ The latest upstream changes (presumably #127796) made this pull request unmergeable. Please resolve the merge conflicts. |
Hello, I have made modifications, please review for me again. I received a conflict notification and I would like to resolve the conflict after approved. |
} | ||
false | ||
}; | ||
*has_suggest_reborrow = is_sugg_reborrow(); |
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.
why use a closure here instead of inlining the code below into return true
?
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.
why use a closure here instead of inlining the code below into
return true
?
Thank you. I just think that because it's in the function suggest_ref_or_clone function
, this judgment code checks a special situation and returns early, so maybe this check code could be organized into a closure. Should I abandon the closure here?
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.
yes, abandon the closure
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.
yes, abandon the closure
Done. Thank you.
…on and it's type is a generic param, it can be reborrowed to avoid moving. for example: ```rust struct Y(u32); // x's type is '& mut Y' and it is used in `fn generic<T>(x: T) {}`. fn generic<T>(x: T) {} ``` fixes rust-lang#127285
@bors r+ rollup |
…iaskrgr Rollup of 8 pull requests Successful merges: - rust-lang#125042 (Use ordinal number in argument error) - rust-lang#127229 (rustdoc: click target for sidebar items flush left) - rust-lang#127337 (Move a few intrinsics to Rust abi) - rust-lang#127472 (MIR building: Stop using `unpack!` for `BlockAnd<()>`) - rust-lang#127579 (Solve a error `.clone()` suggestion when moving a mutable reference) - rust-lang#127769 (Don't use implicit features in `Cargo.toml` in `compiler/`) - rust-lang#127844 (Remove invalid further restricting suggestion for type bound) - rust-lang#127855 (Add myself to review rotation) r? `@ghost` `@rustbot` modify labels: rollup
…iaskrgr Rollup of 8 pull requests Successful merges: - rust-lang#125042 (Use ordinal number in argument error) - rust-lang#127229 (rustdoc: click target for sidebar items flush left) - rust-lang#127337 (Move a few intrinsics to Rust abi) - rust-lang#127472 (MIR building: Stop using `unpack!` for `BlockAnd<()>`) - rust-lang#127579 (Solve a error `.clone()` suggestion when moving a mutable reference) - rust-lang#127769 (Don't use implicit features in `Cargo.toml` in `compiler/`) - rust-lang#127844 (Remove invalid further restricting suggestion for type bound) - rust-lang#127855 (Add myself to review rotation) r? `@ghost` `@rustbot` modify labels: rollup
…iaskrgr Rollup of 8 pull requests Successful merges: - rust-lang#125042 (Use ordinal number in argument error) - rust-lang#127229 (rustdoc: click target for sidebar items flush left) - rust-lang#127337 (Move a few intrinsics to Rust abi) - rust-lang#127472 (MIR building: Stop using `unpack!` for `BlockAnd<()>`) - rust-lang#127579 (Solve a error `.clone()` suggestion when moving a mutable reference) - rust-lang#127769 (Don't use implicit features in `Cargo.toml` in `compiler/`) - rust-lang#127844 (Remove invalid further restricting suggestion for type bound) - rust-lang#127855 (Add myself to review rotation) r? `@ghost` `@rustbot` modify labels: rollup
Rollup merge of rust-lang#127579 - surechen:fix_127285, r=lcnr Solve a error `.clone()` suggestion when moving a mutable reference If the moved value is a mut reference, it is used in a generic function and it's type is a generic param, suggest it can be reborrowed to avoid moving. for example: ```rust struct Y(u32); // x's type is '& mut Y' and it is used in `fn generic<T>(x: T) {}`. fn generic<T>(x: T) {} ``` fixes rust-lang#127285
If the moved value is a mut reference, it is used in a generic function and it's type is a generic param, suggest it can be reborrowed to avoid moving.
for example:
fixes #127285