Skip to content

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

Merged
merged 1 commit into from
Jul 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1290,6 +1290,58 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
span,
format!("if `{ty}` implemented `Clone`, you could clone the value"),
);
} else if let ty::Adt(_, _) = ty.kind()
&& let Some(clone_trait) = self.infcx.tcx.lang_items().clone_trait()
{
// For cases like `Option<NonClone>`, where `Option<T>: Clone` if `T: Clone`, we point
// at the types that should be `Clone`.
let ocx = ObligationCtxt::new_with_diagnostics(self.infcx);
let cause = ObligationCause::misc(expr.span, self.mir_def_id());
ocx.register_bound(cause, self.infcx.param_env, ty, clone_trait);
let errors = ocx.select_all_or_error();
if errors.iter().all(|error| {
match error.obligation.predicate.as_clause().and_then(|c| c.as_trait_clause()) {
Some(clause) => match clause.self_ty().skip_binder().kind() {
ty::Adt(def, _) => def.did().is_local() && clause.def_id() == clone_trait,
_ => false,
},
None => false,
}
}) {
let mut type_spans = vec![];
let mut types = FxIndexSet::default();
for clause in errors
.iter()
.filter_map(|e| e.obligation.predicate.as_clause())
.filter_map(|c| c.as_trait_clause())
{
let ty::Adt(def, _) = clause.self_ty().skip_binder().kind() else { continue };
type_spans.push(self.infcx.tcx.def_span(def.did()));
types.insert(
self.infcx
.tcx
.short_string(clause.self_ty().skip_binder(), &mut err.long_ty_path()),
);
}
let mut span: MultiSpan = type_spans.clone().into();
for sp in type_spans {
span.push_span_label(sp, "consider implementing `Clone` for this type");
}
span.push_span_label(expr.span, "you could clone this value");
let types: Vec<_> = types.into_iter().collect();
let msg = match &types[..] {
[only] => format!("`{only}`"),
[head @ .., last] => format!(
"{} and `{last}`",
head.iter().map(|t| format!("`{t}`")).collect::<Vec<_>>().join(", ")
),
[] => unreachable!(),
};
err.span_note(
span,
format!("if {msg} implemented `Clone`, you could clone the value"),
);
}
}
}

Expand Down
18 changes: 18 additions & 0 deletions tests/ui/borrowck/borrowck-partial-reinit-1.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,15 @@ LL | drop(t);
| - value moved here
LL | t.b = Some(u);
| ^^^ value assigned here after move
|
note: if `Test2` implemented `Clone`, you could clone the value
--> $DIR/borrowck-partial-reinit-1.rs:3:1
|
LL | struct Test2 {
| ^^^^^^^^^^^^ consider implementing `Clone` for this type
...
LL | drop(t);
| - you could clone this value

error[E0382]: assign of moved value: `t`
--> $DIR/borrowck-partial-reinit-1.rs:33:5
Expand All @@ -19,6 +28,15 @@ LL | drop(t);
| - value moved here
LL | t.0 = Some(u);
| ^^^ value assigned here after move
|
note: if `Test3` implemented `Clone`, you could clone the value
--> $DIR/borrowck-partial-reinit-1.rs:7:1
|
LL | struct Test3(Option<Test>);
| ^^^^^^^^^^^^ consider implementing `Clone` for this type
...
LL | drop(t);
| - you could clone this value

error: aborting due to 2 previous errors

Expand Down
9 changes: 9 additions & 0 deletions tests/ui/borrowck/borrowck-partial-reinit-2.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@ LL | let mut u = Test { a: 2, b: Some(Box::new(t))};
| - value moved here
LL | t.b = Some(Box::new(u));
| ^^^ value assigned here after move
|
note: if `Test` implemented `Clone`, you could clone the value
--> $DIR/borrowck-partial-reinit-2.rs:1:1
|
LL | struct Test {
| ^^^^^^^^^^^ consider implementing `Clone` for this type
...
LL | let mut u = Test { a: 2, b: Some(Box::new(t))};
| - you could clone this value

error: aborting due to 1 previous error

Expand Down
9 changes: 9 additions & 0 deletions tests/ui/borrowck/borrowck-union-move-assign.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@ LL | let a = u.a;
| --- value moved here
LL | let a = u.a;
| ^^^ value used here after move
|
note: if `U` implemented `Clone`, you could clone the value
--> $DIR/borrowck-union-move-assign.rs:7:1
|
LL | union U {
| ^^^^^^^ consider implementing `Clone` for this type
...
LL | let a = u.a;
| --- you could clone this value

error: aborting due to 1 previous error

Expand Down
54 changes: 54 additions & 0 deletions tests/ui/borrowck/borrowck-union-move.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@ LL | let a = u.n1;
| ---- value moved here
LL | let a = u.n1;
| ^^^^ value used here after move
|
note: if `Unn` implemented `Clone`, you could clone the value
--> $DIR/borrowck-union-move.rs:7:1
|
LL | union Unn {
| ^^^^^^^^^ consider implementing `Clone` for this type
...
LL | let a = u.n1;
| ---- you could clone this value

error[E0382]: use of moved value: `u`
--> $DIR/borrowck-union-move.rs:31:21
Expand All @@ -17,6 +26,15 @@ LL | let a = u.n1;
| ---- value moved here
LL | let a = u;
| ^ value used here after move
|
note: if `Unn` implemented `Clone`, you could clone the value
--> $DIR/borrowck-union-move.rs:7:1
|
LL | union Unn {
| ^^^^^^^^^ consider implementing `Clone` for this type
...
LL | let a = u.n1;
| ---- you could clone this value

error[E0382]: use of moved value: `u`
--> $DIR/borrowck-union-move.rs:36:21
Expand All @@ -27,6 +45,15 @@ LL | let a = u.n1;
| ---- value moved here
LL | let a = u.n2;
| ^^^^ value used here after move
|
note: if `Unn` implemented `Clone`, you could clone the value
--> $DIR/borrowck-union-move.rs:7:1
|
LL | union Unn {
| ^^^^^^^^^ consider implementing `Clone` for this type
...
LL | let a = u.n1;
| ---- you could clone this value

error[E0382]: use of moved value: `u`
--> $DIR/borrowck-union-move.rs:63:21
Expand All @@ -37,6 +64,15 @@ LL | let a = u.n;
| --- value moved here
LL | let a = u.n;
| ^^^ value used here after move
|
note: if `Ucn` implemented `Clone`, you could clone the value
--> $DIR/borrowck-union-move.rs:15:1
|
LL | union Ucn {
| ^^^^^^^^^ consider implementing `Clone` for this type
...
LL | let a = u.n;
| --- you could clone this value

error[E0382]: use of moved value: `u`
--> $DIR/borrowck-union-move.rs:68:21
Expand All @@ -47,6 +83,15 @@ LL | let a = u.n;
| --- value moved here
LL | let a = u.c;
| ^^^ value used here after move
|
note: if `Ucn` implemented `Clone`, you could clone the value
--> $DIR/borrowck-union-move.rs:15:1
|
LL | union Ucn {
| ^^^^^^^^^ consider implementing `Clone` for this type
...
LL | let a = u.n;
| --- you could clone this value

error[E0382]: use of moved value: `u`
--> $DIR/borrowck-union-move.rs:83:21
Expand All @@ -57,6 +102,15 @@ LL | let a = u.n;
| --- value moved here
LL | let a = u;
| ^ value used here after move
|
note: if `Ucn` implemented `Clone`, you could clone the value
--> $DIR/borrowck-union-move.rs:15:1
|
LL | union Ucn {
| ^^^^^^^^^ consider implementing `Clone` for this type
...
LL | let a = u.n;
| --- you could clone this value

error: aborting due to 6 previous errors

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@ LL | drop(u);
| - value moved here
LL | u.0 = S(1);
| ^^^^^^^^^^ value partially assigned here after move
|
note: if `Tpair` implemented `Clone`, you could clone the value
--> $DIR/issue-54499-field-mutation-of-moved-out-with-mut.rs:6:1
|
LL | struct Tpair(S, i32);
| ^^^^^^^^^^^^ consider implementing `Clone` for this type
...
LL | drop(u);
| - you could clone this value

error[E0382]: assign to part of moved value: `v`
--> $DIR/issue-54499-field-mutation-of-moved-out-with-mut.rs:31:9
Expand All @@ -27,6 +36,15 @@ LL | drop(v);
| - value moved here
LL | v.x = S(1);
| ^^^^^^^^^^ value partially assigned here after move
|
note: if `Spair` implemented `Clone`, you could clone the value
--> $DIR/issue-54499-field-mutation-of-moved-out-with-mut.rs:7:1
|
LL | struct Spair { x: S, y: i32 }
| ^^^^^^^^^^^^ consider implementing `Clone` for this type
...
LL | drop(v);
| - you could clone this value

error: aborting due to 3 previous errors

Expand Down
18 changes: 18 additions & 0 deletions tests/ui/borrowck/issue-54499-field-mutation-of-moved-out.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,15 @@ LL | drop(u);
| - value moved here
LL | u.0 = S(1);
| ^^^^^^^^^^ value partially assigned here after move
|
note: if `Tpair` implemented `Clone`, you could clone the value
--> $DIR/issue-54499-field-mutation-of-moved-out.rs:6:1
|
LL | struct Tpair(S, i32);
| ^^^^^^^^^^^^ consider implementing `Clone` for this type
...
LL | drop(u);
| - you could clone this value

error[E0594]: cannot assign to `u.1`, as `u` is not declared as mutable
--> $DIR/issue-54499-field-mutation-of-moved-out.rs:27:9
Expand Down Expand Up @@ -82,6 +91,15 @@ LL | drop(v);
| - value moved here
LL | v.x = S(1);
| ^^^^^^^^^^ value partially assigned here after move
|
note: if `Spair` implemented `Clone`, you could clone the value
--> $DIR/issue-54499-field-mutation-of-moved-out.rs:7:1
|
LL | struct Spair { x: S, y: i32 }
| ^^^^^^^^^^^^ consider implementing `Clone` for this type
...
LL | drop(v);
| - you could clone this value

error[E0594]: cannot assign to `v.y`, as `v` is not declared as mutable
--> $DIR/issue-54499-field-mutation-of-moved-out.rs:38:9
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@ error[E0507]: cannot move out of `*array` which is behind a shared reference
LL | *array
| ^^^^^^ move occurs because `*array` has type `Vec<Value>`, which does not implement the `Copy` trait
|
note: if `Value` implemented `Clone`, you could clone the value
--> $DIR/issue-54597-reject-move-out-of-borrow-via-pat.rs:4:1
|
LL | struct Value;
| ^^^^^^^^^^^^ consider implementing `Clone` for this type
...
LL | *array
| ------ you could clone this value
help: consider removing the dereference here
|
LL - *array
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,15 @@ LL | E::Number(_) if let E::String(s) = *value => { }
...
LL | let x = value;
| ^^^^^ value used here after move
|
note: if `E` implemented `Clone`, you could clone the value
--> $DIR/if-let-guards-errors.rs:32:1
|
LL | E::Number(_) if let E::String(s) = *value => { }
| ------ you could clone this value
...
LL | enum E {
| ^^^^^^ consider implementing `Clone` for this type

error: aborting due to 2 previous errors

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,15 @@ LL | E::Number(_) if let E::String(s) = *value => { }
...
LL | let x = value;
| ^^^^^ value used here after move
|
note: if `E` implemented `Clone`, you could clone the value
--> $DIR/if-let-guards-errors.rs:32:1
|
LL | E::Number(_) if let E::String(s) = *value => { }
| ------ you could clone this value
...
LL | enum E {
| ^^^^^^ consider implementing `Clone` for this type

error: aborting due to 2 previous errors

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ LL | let mut copy: Vec<U> = map.clone().into_values().collect();
|
note: `HashMap::<K, V, S>::into_values` takes ownership of the receiver `self`, which moves value
--> $SRC_DIR/std/src/collections/hash/map.rs:LL:COL
note: if `Hash128_1` implemented `Clone`, you could clone the value
--> $DIR/suggest-clone-when-some-obligation-is-unmet.rs:8:1
|
LL | pub struct Hash128_1;
| ^^^^^^^^^^^^^^^^^^^^ consider implementing `Clone` for this type
...
LL | let mut copy: Vec<U> = map.clone().into_values().collect();
| ----------- you could clone this value
help: you could `clone` the value and consume it, if the `Hash128_1: Clone` trait bound could be satisfied
|
LL - let mut copy: Vec<U> = map.clone().into_values().collect();
Expand Down
18 changes: 18 additions & 0 deletions tests/ui/nll/issue-21232-partial-init-and-erroneous-use.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@ LL | drop(d);
| - value moved here
LL | d.x = 10;
| ^^^^^^^^ value assigned here after move
|
note: if `D` implemented `Clone`, you could clone the value
--> $DIR/issue-21232-partial-init-and-erroneous-use.rs:11:1
|
LL | struct D {
| ^^^^^^^^ consider implementing `Clone` for this type
...
LL | drop(d);
| - you could clone this value

error[E0381]: partially assigned binding `d` isn't fully initialized
--> $DIR/issue-21232-partial-init-and-erroneous-use.rs:45:5
Expand Down Expand Up @@ -57,6 +66,15 @@ LL | drop(d);
| - value moved here
LL | d.s.y = 20;
| ^^^^^^^^^^ value partially assigned here after move
|
note: if `D` implemented `Clone`, you could clone the value
--> $DIR/issue-21232-partial-init-and-erroneous-use.rs:11:1
|
LL | struct D {
| ^^^^^^^^ consider implementing `Clone` for this type
...
LL | drop(d);
| - you could clone this value

error: aborting due to 6 previous errors

Expand Down
Loading
Loading