Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
2bb0074
pattern_analysis: add option to get a full set of witnesses
Nadrieril Jul 19, 2025
9b01de2
List all the variants of non-exhaustive enums in exhaustive mode
Nadrieril Jul 19, 2025
af07c08
Silence a warning
Nadrieril Jul 19, 2025
a9fd8d0
bootstrap: Move musl-root fallback out of sanity check
Gelbpunkt Jul 22, 2025
3d186ea
Fix tests/assembly-llvm/dwarf-mixed-versions-lto.rs test failure on r…
Jul 23, 2025
2e49c52
Fix tests/codegen-llvm/const-vector.rs test failure on riscv64
Jul 23, 2025
de93fb1
Add `ignore-backends` annotations in failing GCC backend ui tests
GuillaumeGomez Jul 23, 2025
910ee2d
Fix `compiletest` bad handling of `ignore-backends`
GuillaumeGomez Jul 23, 2025
23fda60
RustWrapper: Suppress getNextNonDebugInfoInstruction
heiher Jul 23, 2025
93d2003
Update `dlmalloc` dependency of libstd
alexcrichton Jul 23, 2025
e7441fb
Clippy fixup
Gelbpunkt Jul 23, 2025
a924d44
Rehome tests/ui/issues/ tests [1/?]
Oneirical Jul 13, 2025
1106183
Mention type that could be `Clone` but isn't in more cases
estebank Jul 20, 2025
9c13f4f
Rollup merge of #144089 - Oneirical:uncountable-integer-1, r=jieyouxu
tgross35 Jul 26, 2025
3f7d497
Rollup merge of #144171 - Nadrieril:exhaustive-witnesses, r=davidtwco
tgross35 Jul 26, 2025
72e3767
Rollup merge of #144201 - estebank:suggest-clone, r=SparrowLii
tgross35 Jul 26, 2025
b950b67
Rollup merge of #144316 - Gelbpunkt:musl-libdir-bootstrap, r=Kobzol
tgross35 Jul 26, 2025
e30017b
Rollup merge of #144339 - CaiWeiran:dwarf-mixed-versions-lto_test, r=…
tgross35 Jul 26, 2025
a262dad
Rollup merge of #144341 - CaiWeiran:const-vector_test, r=wesleywiser
tgross35 Jul 26, 2025
2671afe
Rollup merge of #144352 - heiher:llvm-22, r=dianqk
tgross35 Jul 26, 2025
6b1b68f
Rollup merge of #144356 - GuillaumeGomez:gcc-ignore-tests, r=jieyouxu
tgross35 Jul 26, 2025
a230b4f
Rollup merge of #144364 - alexcrichton:update-dlmalloc, r=Mark-Simula…
tgross35 Jul 26, 2025
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
Prev Previous commit
Next Next commit
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
```
  • Loading branch information
estebank committed Jul 25, 2025
commit 11061831f703fdb5d05816860368e4b9ab4edee7
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