-
Notifications
You must be signed in to change notification settings - Fork 13.6k
Mention type that could be Clone
but isn't in more cases
#144201
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
Conversation
r? @SparrowLii rustbot has assigned @SparrowLii. Use |
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 impl looks fine, but I'm not sure suggesting using clone for all errors about value used after move is a best way. After all, it often reduces the performance and increases the redundancy. We should guide programmers to adopt better designs rather than simply workarounds IMO.
@compiler-errors How do you think? |
Do notice that for the case of a bare
I do think that we should provide better suggestions for architectural changes people should make, and we need to be very careful not to send people down the wrong path of writing brittle code. Specially because some people might assume that all suggestions are always correct (regardless of how much weasel wording like "consider" or "you might have meant" we use). But I disagree that cloning is inherently a problematic step for people to take, specially in code being worked on. The There's a tension between the information that experienced rustaceans need and what newcomers need. But even as an experienced rustacean, faced with a situation like the one in the description: #![allow(dead_code)]
use std::future::Future;
struct Foo;
impl Foo {
fn foo(&self) -> bool {
true
}
}
fn require_fn_trait<F: Future<Output = ()>>(_: impl Fn() -> F) {}
fn do_stuff(foo: Option<Foo>) {
require_fn_trait(|| async {
if foo.map_or(false, |f| f.foo()) {
panic!("foo");
}
})
}
fn main() {} I would absolutely I think that's an argument to improve the logic before |
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.
Thanks for the explanation, it makes sense
@bors r+ |
☔ The latest upstream changes (presumably #144440) made this pull request unmergeable. Please resolve the merge conflicts. |
When encountering a moved value of a type that isn't `Clone` because of unmet obligations, but where all the unmet predicates reference crate-local types, mention them and suggest cloning, as we do in other cases already: ``` error[E0507]: cannot move out of `foo`, a captured variable in an `Fn` closure --> f111.rs:14:25 | 13 | fn do_stuff(foo: Option<Foo>) { | --- captured outer variable 14 | require_fn_trait(|| async { | -- ^^^^^ `foo` is moved here | | | captured by this `Fn` closure 15 | if foo.map_or(false, |f| f.foo()) { | --- | | | variable moved due to use in coroutine | move occurs because `foo` has type `Option<Foo>`, which does not implement the `Copy` trait | note: if `Foo` implemented `Clone`, you could clone the value --> f111.rs:4:1 | 4 | struct Foo; | ^^^^^^^^^^ consider implementing `Clone` for this type ... 15 | if foo.map_or(false, |f| f.foo()) { | --- you could clone this value ```
@bors r=SparrowLii |
Rollup of 9 pull requests Successful merges: - #144089 (Rehome 35 `tests/ui/issues/` tests to other subdirectories under `tests/ui/`) - #144171 (pattern_analysis: add option to get a full set of witnesses) - #144201 (Mention type that could be `Clone` but isn't in more cases) - #144316 (bootstrap: Move musl-root fallback out of sanity check) - #144339 (Enable dwarf-mixed-versions-lto.rs test on RISC-V (riscv64)) - #144341 (Enable const-vector.rs test on RISC-V (riscv64)) - #144352 (RustWrapper: Suppress getNextNonDebugInfoInstruction) - #144356 (Add `ignore-backends` annotations in failing GCC backend ui tests) - #144364 (Update `dlmalloc` dependency of libstd) r? `@ghost` `@rustbot` modify labels: rollup
Rollup merge of #144201 - estebank:suggest-clone, r=SparrowLii Mention type that could be `Clone` but isn't in more cases When encountering a moved value of a type that isn't `Clone` because of unmet obligations, but where all the unmet predicates reference crate-local types, mention them and suggest cloning, as we do in other cases already: ``` error[E0507]: cannot move out of `foo`, a captured variable in an `Fn` closure --> f111.rs:14:25 | 13 | fn do_stuff(foo: Option<Foo>) { | --- captured outer variable 14 | require_fn_trait(|| async { | -- ^^^^^ `foo` is moved here | | | captured by this `Fn` closure 15 | if foo.map_or(false, |f| f.foo()) { | --- | | | variable moved due to use in coroutine | move occurs because `foo` has type `Option<Foo>`, which does not implement the `Copy` trait | note: if `Foo` implemented `Clone`, you could clone the value --> f111.rs:4:1 | 4 | struct Foo; | ^^^^^^^^^^ consider implementing `Clone` for this type ... 15 | if foo.map_or(false, |f| f.foo()) { | --- you could clone this value ``` CC #68119.
When encountering a moved value of a type that isn't
Clone
because of unmet obligations, but where all the unmet predicates reference crate-local types, mention them and suggest cloning, as we do in other cases already:CC #68119.