diff options
| author | Trevor Gross <t.gross35@gmail.com> | 2024-07-27 13:32:57 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-07-27 13:32:57 -0400 |
| commit | ee25d992998cff9b067ce6a63ead16e7b3112bf7 (patch) | |
| tree | f5242fa0ae8df49871b67cb36fa9ec9321f88095 | |
| parent | 9164dbd48c7d5ca051d665bd30470ceda79d8849 (diff) | |
| parent | e7eae5370e304fb1534c1964852a6c127f1f09f8 (diff) | |
| download | rust-ee25d992998cff9b067ce6a63ead16e7b3112bf7.tar.gz rust-ee25d992998cff9b067ce6a63ead16e7b3112bf7.zip | |
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.
| -rw-r--r-- | compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs | 31 | ||||
| -rw-r--r-- | tests/ui/associated-types/associated-types-outlives.stderr | 10 | ||||
| -rw-r--r-- | tests/ui/variance/variance-issue-20533.stderr | 10 |
3 files changed, 14 insertions, 37 deletions
diff --git a/compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs b/compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs index e5bc4b38748..6fe50ad0d96 100644 --- a/compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs +++ b/compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs @@ -1306,37 +1306,6 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, '_, 'infcx, 'tcx> { // result of `foo(...)` won't help. break 'outer; } - - // We're suggesting `.clone()` on an borrowed value. See if the expression we have - // is an argument to a function or method call, and try to suggest cloning the - // *result* of the call, instead of the argument. This is closest to what people - // would actually be looking for in most cases, with maybe the exception of things - // like `fn(T) -> T`, but even then it is reasonable. - let typeck_results = self.infcx.tcx.typeck(self.mir_def_id()); - let mut prev = expr; - while let hir::Node::Expr(parent) = self.infcx.tcx.parent_hir_node(prev.hir_id) { - if let hir::ExprKind::Call(..) | hir::ExprKind::MethodCall(..) = parent.kind - && let Some(call_ty) = typeck_results.node_type_opt(parent.hir_id) - && let call_ty = call_ty.peel_refs() - && (!call_ty - .walk() - .any(|t| matches!(t.unpack(), ty::GenericArgKind::Lifetime(_))) - || if let ty::Alias(ty::Projection, _) = call_ty.kind() { - // FIXME: this isn't quite right with lifetimes on assoc types, - // but ignore for now. We will only suggest cloning if - // `<Ty as Trait>::Assoc: Clone`, which should keep false positives - // down to a managable ammount. - true - } else { - false - }) - && self.implements_clone(call_ty) - && self.suggest_cloning_inner(err, call_ty, parent) - { - return; - } - prev = parent; - } } } let ty = ty.peel_refs(); diff --git a/tests/ui/associated-types/associated-types-outlives.stderr b/tests/ui/associated-types/associated-types-outlives.stderr index c97af672c33..1164869bf19 100644 --- a/tests/ui/associated-types/associated-types-outlives.stderr +++ b/tests/ui/associated-types/associated-types-outlives.stderr @@ -11,10 +11,14 @@ LL | drop(x); LL | return f(y); | - borrow later used here | -help: consider cloning the value if the performance cost is acceptable +help: if `T` implemented `Clone`, you could clone the value + --> $DIR/associated-types-outlives.rs:17:21 | -LL | 's: loop { y = denormalise(&x).clone(); break } - | ++++++++ +LL | pub fn free_and_use<T: for<'a> Foo<'a>, + | ^ consider constraining this type parameter with `Clone` +... +LL | 's: loop { y = denormalise(&x); break } + | -- you could clone this value error: aborting due to 1 previous error diff --git a/tests/ui/variance/variance-issue-20533.stderr b/tests/ui/variance/variance-issue-20533.stderr index 03cada07a9e..0a810b7222e 100644 --- a/tests/ui/variance/variance-issue-20533.stderr +++ b/tests/ui/variance/variance-issue-20533.stderr @@ -73,10 +73,14 @@ LL | drop(a); LL | drop(x); | - borrow later used here | -help: consider cloning the value if the performance cost is acceptable +note: if `AffineU32` implemented `Clone`, you could clone the value + --> $DIR/variance-issue-20533.rs:26:1 | -LL | let x = bat(&a).clone(); - | ++++++++ +LL | struct AffineU32(u32); + | ^^^^^^^^^^^^^^^^ consider implementing `Clone` for this type +... +LL | let x = bat(&a); + | -- you could clone this value error[E0505]: cannot move out of `a` because it is borrowed --> $DIR/variance-issue-20533.rs:59:14 |
