Skip to content

Commit 7494015

Browse files
committed
When possible, suggest cloning the result of a call instead of an argument
``` error[E0505]: cannot move out of `a` because it is borrowed --> $DIR/variance-issue-20533.rs:28:14 | LL | let a = AffineU32(1); | - binding `a` declared here LL | let x = foo(&a); | -- borrow of `a` occurs here LL | drop(a); | ^ move out of `a` occurs here LL | drop(x); | - borrow later used here | help: consider cloning the value if the performance cost is acceptable | LL | let x = foo(&a).clone(); | ++++++++ ```
1 parent f031316 commit 7494015

File tree

6 files changed

+157
-19
lines changed

6 files changed

+157
-19
lines changed

compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs

Lines changed: 83 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
464464
{
465465
// We already suggest cloning for these cases in `explain_captures`.
466466
} else {
467-
self.suggest_cloning(err, ty, expr);
467+
self.suggest_cloning(err, ty, expr, None);
468468
}
469469
}
470470
if let Some(pat) = finder.pat {
@@ -747,7 +747,78 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
747747
true
748748
}
749749

750-
pub(crate) fn suggest_cloning(&self, err: &mut Diag<'_>, ty: Ty<'tcx>, expr: &hir::Expr<'_>) {
750+
pub(crate) fn suggest_cloning(
751+
&self,
752+
err: &mut Diag<'_>,
753+
ty: Ty<'tcx>,
754+
expr: &hir::Expr<'_>,
755+
other_expr: Option<&hir::Expr<'_>>,
756+
) {
757+
'outer: {
758+
if let ty::Ref(..) = ty.kind() {
759+
// We check for either `let binding = foo(expr, other_expr);` or
760+
// `foo(expr, other_expr);` and if so we don't suggest an incorrect
761+
// `foo(expr, other_expr).clone()`
762+
if let Some(other_expr) = other_expr
763+
&& let Some(parent_let) =
764+
self.infcx.tcx.hir().parent_iter(expr.hir_id).find_map(|n| {
765+
if let (hir_id, hir::Node::Local(_) | hir::Node::Stmt(_)) = n {
766+
Some(hir_id)
767+
} else {
768+
None
769+
}
770+
})
771+
&& let Some(other_parent_let) =
772+
self.infcx.tcx.hir().parent_iter(other_expr.hir_id).find_map(|n| {
773+
if let (hir_id, hir::Node::Local(_) | hir::Node::Stmt(_)) = n {
774+
Some(hir_id)
775+
} else {
776+
None
777+
}
778+
})
779+
&& parent_let == other_parent_let
780+
{
781+
// Explicitly check that we don't have `foo(&*expr, other_expr)`, as cloning the
782+
// result of `foo(...)` won't help.
783+
break 'outer;
784+
}
785+
786+
// We're suggesting `.clone()` on an borrowed value. See if the expression we have
787+
// is an argument to a function or method call, and try to suggest cloning the
788+
// *result* of the call, instead of the argument. This is closest to what people
789+
// would actually be looking for in most cases, with maybe the exception of things
790+
// like `fn(T) -> T`, but even then it is reasonable.
791+
let typeck_results = self.infcx.tcx.typeck(self.mir_def_id());
792+
let mut prev = expr;
793+
while let hir::Node::Expr(parent) = self.infcx.tcx.parent_hir_node(prev.hir_id) {
794+
if let hir::ExprKind::Call(..) | hir::ExprKind::MethodCall(..) = parent.kind
795+
&& let Some(call_ty) = typeck_results.node_type_opt(parent.hir_id)
796+
&& let call_ty = call_ty.peel_refs()
797+
&& (!call_ty
798+
.walk()
799+
.any(|t| matches!(t.unpack(), ty::GenericArgKind::Lifetime(_)))
800+
|| if let ty::Alias(ty::Projection, _) = call_ty.kind() {
801+
// FIXME: this isn't quite right with lifetimes on assoc types,
802+
// but ignore for now. We will only suggest cloning if
803+
// `<Ty as Trait>::Assoc: Clone`, which should keep false positives
804+
// down to a managable ammount.
805+
true
806+
} else {
807+
false
808+
})
809+
&& let Some(clone_trait_def) = self.infcx.tcx.lang_items().clone_trait()
810+
&& self
811+
.infcx
812+
.type_implements_trait(clone_trait_def, [call_ty], self.param_env)
813+
.must_apply_modulo_regions()
814+
&& self.suggest_cloning_inner(err, call_ty, parent)
815+
{
816+
return;
817+
}
818+
prev = parent;
819+
}
820+
}
821+
}
751822
let ty = ty.peel_refs();
752823
if let Some(clone_trait_def) = self.infcx.tcx.lang_items().clone_trait()
753824
&& self
@@ -774,12 +845,17 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
774845
}
775846
}
776847

777-
fn suggest_cloning_inner(&self, err: &mut Diag<'_>, ty: Ty<'tcx>, expr: &hir::Expr<'_>) {
848+
fn suggest_cloning_inner(
849+
&self,
850+
err: &mut Diag<'_>,
851+
ty: Ty<'tcx>,
852+
expr: &hir::Expr<'_>,
853+
) -> bool {
778854
let tcx = self.infcx.tcx;
779855
if let Some(_) = self.clone_on_reference(expr) {
780856
// Avoid redundant clone suggestion already suggested in `explain_captures`.
781857
// See `tests/ui/moves/needs-clone-through-deref.rs`
782-
return;
858+
return false;
783859
}
784860
// Try to find predicates on *generic params* that would allow copying `ty`
785861
let suggestion =
@@ -796,7 +872,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
796872
if let hir::ExprKind::AddrOf(_, hir::Mutability::Mut, _) = inner_expr.kind {
797873
// We assume that `&mut` refs are desired for their side-effects, so cloning the
798874
// value wouldn't do what the user wanted.
799-
return;
875+
return false;
800876
}
801877
inner_expr = inner;
802878
}
@@ -819,6 +895,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
819895
"consider cloning the value if the performance cost is acceptable"
820896
};
821897
err.multipart_suggestion_verbose(msg, sugg, Applicability::MachineApplicable);
898+
true
822899
}
823900

824901
fn suggest_adding_bounds(&self, err: &mut Diag<'_>, ty: Ty<'tcx>, def_id: DefId, span: Span) {
@@ -927,7 +1004,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
9271004
if let Some(expr) = self.find_expr(borrow_span)
9281005
&& let Some(ty) = typeck_results.node_type_opt(expr.hir_id)
9291006
{
930-
self.suggest_cloning(&mut err, ty, expr);
1007+
self.suggest_cloning(&mut err, ty, expr, self.find_expr(span));
9311008
}
9321009
self.buffer_error(err);
9331010
}

compiler/rustc_borrowck/src/diagnostics/move_errors.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,9 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
435435

436436
fn add_move_hints(&self, error: GroupedMoveError<'tcx>, err: &mut Diag<'_>, span: Span) {
437437
match error {
438-
GroupedMoveError::MovesFromPlace { mut binds_to, move_from, .. } => {
438+
GroupedMoveError::MovesFromPlace {
439+
mut binds_to, move_from, span: other_span, ..
440+
} => {
439441
self.add_borrow_suggestions(err, span);
440442
if binds_to.is_empty() {
441443
let place_ty = move_from.ty(self.body, self.infcx.tcx).ty;
@@ -445,7 +447,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
445447
};
446448

447449
if let Some(expr) = self.find_expr(span) {
448-
self.suggest_cloning(err, place_ty, expr);
450+
self.suggest_cloning(err, place_ty, expr, self.find_expr(other_span));
449451
}
450452

451453
err.subdiagnostic(
@@ -472,15 +474,15 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
472474
}
473475
// No binding. Nothing to suggest.
474476
GroupedMoveError::OtherIllegalMove { ref original_path, use_spans, .. } => {
475-
let span = use_spans.var_or_use();
477+
let use_span = use_spans.var_or_use();
476478
let place_ty = original_path.ty(self.body, self.infcx.tcx).ty;
477479
let place_desc = match self.describe_place(original_path.as_ref()) {
478480
Some(desc) => format!("`{desc}`"),
479481
None => "value".to_string(),
480482
};
481483

482-
if let Some(expr) = self.find_expr(span) {
483-
self.suggest_cloning(err, place_ty, expr);
484+
if let Some(expr) = self.find_expr(use_span) {
485+
self.suggest_cloning(err, place_ty, expr, self.find_expr(span));
484486
}
485487

486488
err.subdiagnostic(
@@ -489,7 +491,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
489491
is_partial_move: false,
490492
ty: place_ty,
491493
place: &place_desc,
492-
span,
494+
span: use_span,
493495
},
494496
);
495497

@@ -593,7 +595,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
593595
let place_desc = &format!("`{}`", self.local_names[*local].unwrap());
594596

595597
if let Some(expr) = self.find_expr(binding_span) {
596-
self.suggest_cloning(err, bind_to.ty, expr);
598+
self.suggest_cloning(err, bind_to.ty, expr, None);
597599
}
598600

599601
err.subdiagnostic(

tests/ui/associated-types/associated-types-outlives.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
// fn body, causing this (invalid) code to be accepted.
44

55
pub trait Foo<'a> {
6-
type Bar;
6+
type Bar: Clone;
77
}
88

9-
impl<'a, T:'a> Foo<'a> for T {
9+
impl<'a, T: 'a> Foo<'a> for T {
1010
type Bar = &'a T;
1111
}
1212

tests/ui/associated-types/associated-types-outlives.stderr

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ LL | drop(x);
1010
| ^ move out of `x` occurs here
1111
LL | return f(y);
1212
| - borrow later used here
13+
|
14+
help: consider cloning the value if the performance cost is acceptable
15+
|
16+
LL | 's: loop { y = denormalise(&x).clone(); break }
17+
| ++++++++
1318

1419
error: aborting due to 1 previous error
1520

tests/ui/variance/variance-issue-20533.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,15 @@ fn baz<'a, T>(_x: &'a T) -> Baked<'a> {
1919
Baked(PhantomData)
2020
}
2121

22+
fn bat(x: &AffineU32) -> &u32 {
23+
&x.0
24+
}
25+
2226
struct AffineU32(u32);
2327

28+
#[derive(Clone)]
29+
struct ClonableAffineU32(u32);
30+
2431
fn main() {
2532
{
2633
let a = AffineU32(1);
@@ -40,4 +47,16 @@ fn main() {
4047
drop(a); //~ ERROR cannot move out of `a`
4148
drop(x);
4249
}
50+
{
51+
let a = AffineU32(1);
52+
let x = bat(&a);
53+
drop(a); //~ ERROR cannot move out of `a`
54+
drop(x);
55+
}
56+
{
57+
let a = ClonableAffineU32(1);
58+
let x = foo(&a);
59+
drop(a); //~ ERROR cannot move out of `a`
60+
drop(x);
61+
}
4362
}

tests/ui/variance/variance-issue-20533.stderr

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error[E0505]: cannot move out of `a` because it is borrowed
2-
--> $DIR/variance-issue-20533.rs:28:14
2+
--> $DIR/variance-issue-20533.rs:35:14
33
|
44
LL | let a = AffineU32(1);
55
| - binding `a` declared here
@@ -11,7 +11,7 @@ LL | drop(x);
1111
| - borrow later used here
1212

1313
error[E0505]: cannot move out of `a` because it is borrowed
14-
--> $DIR/variance-issue-20533.rs:34:14
14+
--> $DIR/variance-issue-20533.rs:41:14
1515
|
1616
LL | let a = AffineU32(1);
1717
| - binding `a` declared here
@@ -23,7 +23,7 @@ LL | drop(x);
2323
| - borrow later used here
2424

2525
error[E0505]: cannot move out of `a` because it is borrowed
26-
--> $DIR/variance-issue-20533.rs:40:14
26+
--> $DIR/variance-issue-20533.rs:47:14
2727
|
2828
LL | let a = AffineU32(1);
2929
| - binding `a` declared here
@@ -34,6 +34,41 @@ LL | drop(a);
3434
LL | drop(x);
3535
| - borrow later used here
3636

37-
error: aborting due to 3 previous errors
37+
error[E0505]: cannot move out of `a` because it is borrowed
38+
--> $DIR/variance-issue-20533.rs:53:14
39+
|
40+
LL | let a = AffineU32(1);
41+
| - binding `a` declared here
42+
LL | let x = bat(&a);
43+
| -- borrow of `a` occurs here
44+
LL | drop(a);
45+
| ^ move out of `a` occurs here
46+
LL | drop(x);
47+
| - borrow later used here
48+
|
49+
help: consider cloning the value if the performance cost is acceptable
50+
|
51+
LL | let x = bat(&a).clone();
52+
| ++++++++
53+
54+
error[E0505]: cannot move out of `a` because it is borrowed
55+
--> $DIR/variance-issue-20533.rs:59:14
56+
|
57+
LL | let a = ClonableAffineU32(1);
58+
| - binding `a` declared here
59+
LL | let x = foo(&a);
60+
| -- borrow of `a` occurs here
61+
LL | drop(a);
62+
| ^ move out of `a` occurs here
63+
LL | drop(x);
64+
| - borrow later used here
65+
|
66+
help: consider cloning the value if the performance cost is acceptable
67+
|
68+
LL - let x = foo(&a);
69+
LL + let x = foo(a.clone());
70+
|
71+
72+
error: aborting due to 5 previous errors
3873

3974
For more information about this error, try `rustc --explain E0505`.

0 commit comments

Comments
 (0)