Skip to content

Account for ownership mismatch on argument that doesn't meet bound#153749

Open
estebank wants to merge 1 commit intorust-lang:mainfrom
estebank:issue-134805
Open

Account for ownership mismatch on argument that doesn't meet bound#153749
estebank wants to merge 1 commit intorust-lang:mainfrom
estebank:issue-134805

Conversation

@estebank
Copy link
Copy Markdown
Contributor

@estebank estebank commented Mar 11, 2026

error[E0277]: the trait bound `needs_borrow::Hello: needs_borrow::Tr` is not satisfied
  --> $DIR/ownership-mismatch-on-arg.rs:42:13
   |
LL |             foo(hi, hi, hi);
   |             ^^^ --  -- `needs_borrow::Hello` doesn't satisfy the trait bound
   |             |   |
   |             |   `needs_borrow::Hello` doesn't satisfy the trait bound
   |             unsatisfied trait bound
   |
help: the trait `needs_borrow::Tr` is not implemented for `needs_borrow::Hello`
  --> $DIR/ownership-mismatch-on-arg.rs:27:5
   |
LL |     struct Hello;
   |     ^^^^^^^^^^^^
help: the trait `needs_borrow::Tr` is implemented for `&needs_borrow::Hello`
  --> $DIR/ownership-mismatch-on-arg.rs:30:5
   |
LL |     impl Tr for &Hello {}
   |     ^^^^^^^^^^^^^^^^^^
note: required by a bound in `needs_borrow::foo`
  --> $DIR/ownership-mismatch-on-arg.rs:32:15
   |
LL |     fn foo<T: Tr, K: std::fmt::Debug>(_v: T, _w: T, _k: K) {}
   |               ^^ required by this bound in `foo`
help: consider borrowing these argument
   |
LL |             foo(&hi, &hi, hi);
   |                 +    +

Fix #134805.

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Mar 11, 2026
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Mar 11, 2026

r? @JonathanBrouwer

rustbot has assigned @JonathanBrouwer.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: compiler, types
  • compiler, types expanded to 69 candidates
  • Random selection from 16 candidates

Comment on lines +2 to +23
mod needs_deref {
#[derive(Clone, Copy, Debug)]
struct Hello;

trait Tr: Clone + Copy {}
impl Tr for Hello {}

fn foo<T: Tr, K: std::fmt::Debug>(_v: T, _w: T, _k: K) {}

struct S;
impl S {
fn foo<K: std::fmt::Debug, T: Tr>(&self, _v: T, _w: T, _k: K) {}
}

fn bar() {
let hellos = [Hello; 3];
for hi in hellos.iter() {
foo(hi, hi, hi); //~ ERROR: the trait bound `&needs_deref::Hello: needs_deref::Tr` is not satisfied
S.foo(hi, hi, hi); //~ ERROR: the trait bound `&needs_deref::Hello: needs_deref::Tr` is not satisfied
}
}
}
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@arferreira This case I was mentioning that would be nice to handle after this PR (plus one other test for Hello not being Copy but still being Clone) is what still needs to be handled. We should suggest

            foo(*hi, *hi, hi);
            S.foo(*hi, *hi, hi);

for the case above and

            foo(hi.clone(), hi.clone(), hi);
            S.foo(hi.clone(), hi.clone(), hi);

if possible.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good, I'll pick up that. Thanks for the context!

@rust-log-analyzer

This comment has been minimized.

```
error[E0277]: the trait bound `needs_borrow::Hello: needs_borrow::Tr` is not satisfied
  --> $DIR/ownership-mismatch-on-arg.rs:42:13
   |
LL |             foo(hi, hi, hi);
   |             ^^^ --  -- `needs_borrow::Hello` doesn't satisfy the trait bound
   |             |   |
   |             |   `needs_borrow::Hello` doesn't satisfy the trait bound
   |             unsatisfied trait bound
   |
help: the trait `needs_borrow::Tr` is not implemented for `needs_borrow::Hello`
  --> $DIR/ownership-mismatch-on-arg.rs:27:5
   |
LL |     struct Hello;
   |     ^^^^^^^^^^^^
help: the trait `needs_borrow::Tr` is implemented for `&needs_borrow::Hello`
  --> $DIR/ownership-mismatch-on-arg.rs:30:5
   |
LL |     impl Tr for &Hello {}
   |     ^^^^^^^^^^^^^^^^^^
note: required by a bound in `needs_borrow::foo`
  --> $DIR/ownership-mismatch-on-arg.rs:32:15
   |
LL |     fn foo<T: Tr, K: std::fmt::Debug>(_v: T, _w: T, _k: K) {}
   |               ^^ required by this bound in `foo`
help: consider borrowing these argument
   |
LL |             foo(&hi, &hi, hi);
   |                 +    +
```
@JonathanBrouwer
Copy link
Copy Markdown
Contributor

r? types

@rustbot rustbot added the T-types Relevant to the types team, which will review and decide on the PR/issue. label Mar 12, 2026
@rustbot rustbot assigned lcnr and unassigned JonathanBrouwer Mar 12, 2026
@lcnr
Copy link
Copy Markdown
Contributor

lcnr commented Mar 30, 2026

sry for not getting to this for so long, currently cleaning up my notification backlog

r? types

@rustbot rustbot assigned jackh726 and unassigned lcnr Mar 30, 2026
Copy link
Copy Markdown
Member

@jackh726 jackh726 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd be okay to land as-is, but have some minor comments.

View changes since this review

Comment on lines +1363 to +1365
let ty = self.tcx.instantiate_bound_regions_with_erased(
poly_trait_pred.self_ty(),
);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor nit, but this can be pull out of the loop. Probably renamed to erased_self_ty

}
c
}
ObligationCauseCode::WhereClauseInExpr(def_id, _, hir_id, idx)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A lot of this code is duplicated...any way to deduplicate?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-types Relevant to the types team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

strange borrowing suggestion

7 participants