Skip to content

Commit ee25d99

Browse files
authored
Rollup merge of #128241 - compiler-errors:clone-sugg, r=jieyouxu
Remove logic to suggest clone of function output I can't exactly tell, but I believe that this suggestion is operating off of a heuristic that the lifetime of a function's input is correlated with the lifetime of a function's output in such a way that cloning would fix an error. I don't think that actually manages to hit the bar of "actually provides useful suggestions" most of the time. Specifically, I've hit false-positives due to this suggestion *twice* when fixing ICEs in the compiler, so I don't think it's worthwhile having this logic around. Neither of the two affected UI tests are actually fixed by the suggestion.
2 parents 9164dbd + e7eae53 commit ee25d99

File tree

3 files changed

+14
-37
lines changed

3 files changed

+14
-37
lines changed

compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs

-31
Original file line numberDiff line numberDiff line change
@@ -1306,37 +1306,6 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, '_, 'infcx, 'tcx> {
13061306
// result of `foo(...)` won't help.
13071307
break 'outer;
13081308
}
1309-
1310-
// We're suggesting `.clone()` on an borrowed value. See if the expression we have
1311-
// is an argument to a function or method call, and try to suggest cloning the
1312-
// *result* of the call, instead of the argument. This is closest to what people
1313-
// would actually be looking for in most cases, with maybe the exception of things
1314-
// like `fn(T) -> T`, but even then it is reasonable.
1315-
let typeck_results = self.infcx.tcx.typeck(self.mir_def_id());
1316-
let mut prev = expr;
1317-
while let hir::Node::Expr(parent) = self.infcx.tcx.parent_hir_node(prev.hir_id) {
1318-
if let hir::ExprKind::Call(..) | hir::ExprKind::MethodCall(..) = parent.kind
1319-
&& let Some(call_ty) = typeck_results.node_type_opt(parent.hir_id)
1320-
&& let call_ty = call_ty.peel_refs()
1321-
&& (!call_ty
1322-
.walk()
1323-
.any(|t| matches!(t.unpack(), ty::GenericArgKind::Lifetime(_)))
1324-
|| if let ty::Alias(ty::Projection, _) = call_ty.kind() {
1325-
// FIXME: this isn't quite right with lifetimes on assoc types,
1326-
// but ignore for now. We will only suggest cloning if
1327-
// `<Ty as Trait>::Assoc: Clone`, which should keep false positives
1328-
// down to a managable ammount.
1329-
true
1330-
} else {
1331-
false
1332-
})
1333-
&& self.implements_clone(call_ty)
1334-
&& self.suggest_cloning_inner(err, call_ty, parent)
1335-
{
1336-
return;
1337-
}
1338-
prev = parent;
1339-
}
13401309
}
13411310
}
13421311
let ty = ty.peel_refs();

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

+7-3
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,14 @@ LL | drop(x);
1111
LL | return f(y);
1212
| - borrow later used here
1313
|
14-
help: consider cloning the value if the performance cost is acceptable
14+
help: if `T` implemented `Clone`, you could clone the value
15+
--> $DIR/associated-types-outlives.rs:17:21
1516
|
16-
LL | 's: loop { y = denormalise(&x).clone(); break }
17-
| ++++++++
17+
LL | pub fn free_and_use<T: for<'a> Foo<'a>,
18+
| ^ consider constraining this type parameter with `Clone`
19+
...
20+
LL | 's: loop { y = denormalise(&x); break }
21+
| -- you could clone this value
1822

1923
error: aborting due to 1 previous error
2024

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

+7-3
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,14 @@ LL | drop(a);
7373
LL | drop(x);
7474
| - borrow later used here
7575
|
76-
help: consider cloning the value if the performance cost is acceptable
76+
note: if `AffineU32` implemented `Clone`, you could clone the value
77+
--> $DIR/variance-issue-20533.rs:26:1
7778
|
78-
LL | let x = bat(&a).clone();
79-
| ++++++++
79+
LL | struct AffineU32(u32);
80+
| ^^^^^^^^^^^^^^^^ consider implementing `Clone` for this type
81+
...
82+
LL | let x = bat(&a);
83+
| -- you could clone this value
8084

8185
error[E0505]: cannot move out of `a` because it is borrowed
8286
--> $DIR/variance-issue-20533.rs:59:14

0 commit comments

Comments
 (0)