Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

strange borrowing suggestion #134805

Open
est31 opened this issue Dec 26, 2024 · 0 comments
Open

strange borrowing suggestion #134805

est31 opened this issue Dec 26, 2024 · 0 comments
Labels
A-borrow-checker Area: The borrow checker A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@est31
Copy link
Member

est31 commented Dec 26, 2024

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

@est31 est31 added the A-diagnostics Area: Messages for errors, warnings, and lints label Dec 26, 2024
@jieyouxu jieyouxu added T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. A-borrow-checker Area: The borrow checker labels Dec 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-borrow-checker Area: The borrow checker A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

2 participants