Skip to content

Detect when trait bound requires closure to return itself#159459

Merged
rust-bors[bot] merged 1 commit into
rust-lang:mainfrom
estebank:closure-ret-self
Jul 18, 2026
Merged

Detect when trait bound requires closure to return itself#159459
rust-bors[bot] merged 1 commit into
rust-lang:mainfrom
estebank:closure-ret-self

Conversation

@estebank

@estebank estebank commented Jul 17, 2026

Copy link
Copy Markdown
Contributor

A trait bound T: Fn() -> T is non-sensical code, point that out:

error[E0271]: expected `{[email protected]:8:25}` to return `{[email protected]:8:25}`, but it returns `{integer}`
  --> $DIR/trait-bound-expects-closure-returns-itself.rs:8:28
   |
LL |     closure_ret_closure(|| 4);
   |                         -- ^ expected closure, found integer
   |                         |
   |                         the expected closure
   |
note: a bound in `closure_ret_closure` requires that a closure return itself, which is not possible
  --> $DIR/trait-bound-expects-closure-returns-itself.rs:1:39
   |
LL | fn closure_ret_closure<T: FnOnce() -> T>(f: T) -> T {
   |                                       ^ this requires the closure to return itself
...
LL |     closure_ret_closure(|| 4);
   |                         ---- this closure would have to return itself
   = note: expected closure `{closure@$DIR/trait-bound-expects-closure-returns-itself.rs:8:25: 8:27}`
                 found type `{integer}`

This was highlighted in #80638. We'd since improved the output enough to close the ticket, but I wanted to ensure we had something even clearer.

A trait bound `T: Fn() -> T` is non-sensical code, point that out:

```
error[E0271]: expected `{[email protected]:8:25}` to return `{[email protected]:8:25}`, but it returns `{integer}`
  --> $DIR/trait-bound-expects-closure-returns-itself.rs:8:28
   |
LL |     closure_ret_closure(|| 4);
   |                         -- ^ expected closure, found integer
   |                         |
   |                         the expected closure
   |
note: a bound in `closure_ret_closure` requires that a closure return itself, which is not possible
  --> $DIR/trait-bound-expects-closure-returns-itself.rs:1:39
   |
LL | fn closure_ret_closure<T: FnOnce() -> T>(f: T) -> T {
   |                                       ^ this requires the closure to return itself
...
LL |     closure_ret_closure(|| 4);
   |                         ---- this closure would have to return itself
   = note: expected closure `{closure@$DIR/trait-bound-expects-closure-returns-itself.rs:8:25: 8:27}`
                 found type `{integer}`
```
@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 Jul 17, 2026
@rustbot

rustbot commented Jul 17, 2026

Copy link
Copy Markdown
Collaborator

r? @mu001999

rustbot has assigned @mu001999.
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 74 candidates
  • Random selection from 18 candidates

@estebank

Copy link
Copy Markdown
Contributor Author

We should probably have a lint too for when crate authors write an API this way but haven't yet written a test that uses it.

@mu001999 mu001999 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.

@rust-bors

rust-bors Bot commented Jul 18, 2026

Copy link
Copy Markdown
Contributor

📌 Commit b43da85 has been approved by mu001999

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-review Status: Awaiting review from the assignee but also interested parties. labels Jul 18, 2026
rust-bors Bot pushed a commit that referenced this pull request Jul 18, 2026
…uwer

Rollup of 8 pull requests

Successful merges:

 - #159425 (Windows: add context when opening NUL for child stdio fails)
 - #159467 (Add explicit `Iterator::count` impl for `str::EncodeUtf16`)
 - #157860 (Move rustdoc run-make tests into new `tests/run-make/rustdoc/`)
 - #158545 (Move `std::io::read_to_string` to `alloc::io`)
 - #159328 (tests/assembly-llvm: pin frame pointer in issue-141649 aarch64 test)
 - #159459 (Detect when trait bound requires closure to return itself)
 - #159470 (Fix string indexing in diagnostic format strings)
 - #159500 (Move compiletest CLI parsing to `cli.rs`)
@rust-bors
rust-bors Bot merged commit 7cdce76 into rust-lang:main Jul 18, 2026
13 checks passed
@rustbot rustbot added this to the 1.99.0 milestone Jul 18, 2026
rust-timer added a commit that referenced this pull request Jul 18, 2026
Rollup merge of #159459 - estebank:closure-ret-self, r=mu001999

Detect when trait bound requires closure to return itself

A trait bound `T: Fn() -> T` is non-sensical code, point that out:

```
error[E0271]: expected `{[email protected]:8:25}` to return `{[email protected]:8:25}`, but it returns `{integer}`
  --> $DIR/trait-bound-expects-closure-returns-itself.rs:8:28
   |
LL |     closure_ret_closure(|| 4);
   |                         -- ^ expected closure, found integer
   |                         |
   |                         the expected closure
   |
note: a bound in `closure_ret_closure` requires that a closure return itself, which is not possible
  --> $DIR/trait-bound-expects-closure-returns-itself.rs:1:39
   |
LL | fn closure_ret_closure<T: FnOnce() -> T>(f: T) -> T {
   |                                       ^ this requires the closure to return itself
...
LL |     closure_ret_closure(|| 4);
   |                         ---- this closure would have to return itself
   = note: expected closure `{closure@$DIR/trait-bound-expects-closure-returns-itself.rs:8:25: 8:27}`
                 found type `{integer}`
```

This was highlighted in #80638. We'd since improved the output enough to close the ticket, but I wanted to ensure we had something even clearer.
github-actions Bot pushed a commit to rust-lang/rustc-dev-guide that referenced this pull request Jul 20, 2026
…uwer

Rollup of 8 pull requests

Successful merges:

 - rust-lang/rust#159425 (Windows: add context when opening NUL for child stdio fails)
 - rust-lang/rust#159467 (Add explicit `Iterator::count` impl for `str::EncodeUtf16`)
 - rust-lang/rust#157860 (Move rustdoc run-make tests into new `tests/run-make/rustdoc/`)
 - rust-lang/rust#158545 (Move `std::io::read_to_string` to `alloc::io`)
 - rust-lang/rust#159328 (tests/assembly-llvm: pin frame pointer in issue-141649 aarch64 test)
 - rust-lang/rust#159459 (Detect when trait bound requires closure to return itself)
 - rust-lang/rust#159470 (Fix string indexing in diagnostic format strings)
 - rust-lang/rust#159500 (Move compiletest CLI parsing to `cli.rs`)
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.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants