Skip to content

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

Open
estebank wants to merge 2 commits into
rust-lang:mainfrom
estebank:issue-134805
Open

Account for ownership mismatch on argument that doesn't meet bound#153749
estebank wants to merge 2 commits into
rust-lang:mainfrom
estebank:issue-134805

Conversation

@estebank

@estebank estebank commented Mar 11, 2026

Copy link
Copy Markdown
Contributor
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

rustbot commented Mar 11, 2026

Copy link
Copy Markdown
Collaborator

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.

@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

lcnr commented Mar 30, 2026

Copy link
Copy Markdown
Contributor

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

@jackh726 jackh726 left a comment

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.

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

View changes since this review

Comment thread compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs Outdated
}
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?

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.

Sorry for the delay. I tried moving some of the suggestion logic to a closure, but there's some duplication in the pattern decomposition.

@jackh726 jackh726 added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Apr 12, 2026
@rustbot

This comment has been minimized.

@rust-log-analyzer

This comment was marked as resolved.

estebank added 2 commits July 29, 2026 09:08
```
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);
   |                 +    +
```
@rustbot

rustbot commented Jul 29, 2026

Copy link
Copy Markdown
Collaborator

This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

@jackh726

jackh726 commented Aug 3, 2026

Copy link
Copy Markdown
Member

@bors r+

@rust-bors

rust-bors Bot commented Aug 3, 2026

Copy link
Copy Markdown
Contributor

📌 Commit 1e1a699 has been approved by jackh726

It is now in the queue for this repository.

@rust-bors rust-bors Bot added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Aug 3, 2026
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Aug 3, 2026
Account for ownership mismatch on argument that doesn't meet bound

```
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 rust-lang#134805.
rust-bors Bot pushed a commit that referenced this pull request Aug 3, 2026
…uwer

Rollup of 23 pull requests

Successful merges:

 - #153749 (Account for ownership mismatch on argument that doesn't meet bound)
 - #159326 (Deny multiple EII impls on a single item)
 - #159535 (Optimize slice::contains for bytewise types)
 - #159595 (Promote loongarch32-unknown-none* to Tier 2)
 - #160007 (allow `-Ldependency` search paths for panic runtimes)
 - #160184 (Add -Zinstrument-mcount={fentry-nop-record,fentry-record})
 - #160320 (point at trait definition when it is used as a derive macro)
 - #160369 (When suggesting method names, prefer *exact* doc aliases over similar names)
 - #160406 (`DepKind` cleanups)
 - #160424 (Use `thread::available_parallelism` as the default limit for backend parallelism)
 - #159303 (Fix ICE for direct inline const generic defaults)
 - #159977 (Add regression test for bool indexing codegen)
 - #160011 (remove InterpError::map_err_info)
 - #160165 (reject `...` without pattern post-expansion)
 - #160295 (Fix rustdoc ICE when checking if a generic arg can be elided)
 - #160305 (Linkify C-SKY targets in `platform-support.md`)
 - #160314 (fix borrowck ICE for consts with fn pointer type)
 - #160322 (ElaborateBoxDeref: remove unnecessary projection)
 - #160338 (Add regression test for supertrait associated type normalization through dyn)
 - #160340 (Add regression test for unused_parens on contract clauses)
 - #160371 (Add doc aliases for transpositions `read_exact_buf` and `read_exact_buf_at`)
 - #160384 (Add PR body notes for Cargo lock file maintenance)
 - #160435 (bump tracing-tree)
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Aug 3, 2026
Account for ownership mismatch on argument that doesn't meet bound

```
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 rust-lang#134805.
rust-bors Bot pushed a commit that referenced this pull request Aug 4, 2026
…uwer

Rollup of 26 pull requests

Successful merges:

 - #153749 (Account for ownership mismatch on argument that doesn't meet bound)
 - #158727 (std: use `readdir` on nearly all UNIX platforms)
 - #159130 (a bit optimize four-digit chunks in integer formatting)
 - #159326 (Deny multiple EII impls on a single item)
 - #159535 (Optimize slice::contains for bytewise types)
 - #159595 (Promote loongarch32-unknown-none* to Tier 2)
 - #160184 (Add -Zinstrument-mcount={fentry-nop-record,fentry-record})
 - #160320 (point at trait definition when it is used as a derive macro)
 - #160369 (When suggesting method names, prefer *exact* doc aliases over similar names)
 - #160406 (`DepKind` cleanups)
 - #160424 (Use `thread::available_parallelism` as the default limit for backend parallelism)
 - #159303 (Fix ICE for direct inline const generic defaults)
 - #159977 (Add regression test for bool indexing codegen)
 - #160011 (remove InterpError::map_err_info)
 - #160165 (reject `...` without pattern post-expansion)
 - #160295 (Fix rustdoc ICE when checking if a generic arg can be elided)
 - #160305 (Linkify C-SKY targets in `platform-support.md`)
 - #160314 (fix borrowck ICE for consts with fn pointer type)
 - #160322 (ElaborateBoxDeref: remove unnecessary projection)
 - #160338 (Add regression test for supertrait associated type normalization through dyn)
 - #160340 (Add regression test for unused_parens on contract clauses)
 - #160371 (Add doc aliases for transpositions `read_exact_buf` and `read_exact_buf_at`)
 - #160384 (Add PR body notes for Cargo lock file maintenance)
 - #160412 (Move duplicate-names check for #[rustc_must_implement_one_of] to attribute parser)
 - #160435 (bump tracing-tree)
 - #160449 (Fix lookup of object files)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. 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