Open
Description
Rustc suggests borrowing of a function's return type when there is a trait not implemented error for the function's parameters.
Not only does the borrow not help, a dereferencing is required instead, but it is suggested for the return type, which is not where the problem is at all.
Consider the following program:
#[derive(Clone, Copy)]
struct Hello;
trait Tr: Clone + Copy {}
impl Tr for Hello {}
fn foo<T: Tr>(_v: T, _w: T) {}
fn main() {
let hellos = [Hello; 3];
for hi in hellos.iter() {
foo(hi, hi);
}
}
On latest nightly 1.85.0-nightly (2024-12-25 7c002ff9a70cb84fd1a9)
(same as on stable and beta):
error[E0277]: the trait bound `&Hello: Tr` is not satisfied
--> src/main.rs:14:9
|
14 | foo(hi, hi);
| ^^^ the trait `Tr` is not implemented for `&Hello`
|
note: required by a bound in `foo`
--> src/main.rs:7:11
|
7 | fn foo<T: Tr>(_v: T, _w: T) {}
| ^^ required by this bound in `foo`
help: consider borrowing here
|
14 | &foo(hi, hi);
| +
error[E0277]: the trait bound `&Hello: Tr` is not satisfied
--> src/main.rs:15:13
|
15 | bar(hi);
| --- ^^ the trait `Tr` is not implemented for `&Hello`
| |
| required by a bound introduced by this call
|
= help: the trait `Tr` is implemented for `Hello`
note: required by a bound in `bar`
--> src/main.rs:9:11
|
9 | fn bar<T: Tr>(_v: T) {}
| ^^ required by this bound in `bar`
The example shows that if there is one parameter, a borrow is not suggested. It happens only if there is two parameters.
Related (but not the same): #132041