Skip to content

E0373 examples are out of date #137962

Closed
@Eclips4

Description

@Eclips4

Code

fn foo(x: u32) -> Box<Fn(u32) -> u32> {
    Box::new(|y| x + y)
}

Current output

error[E0782]: expected a type, found a trait
 --> src/main.rs:1:23
  |
1 | fn foo(x: u32) -> Box<Fn(u32) -> u32> {
  |                       ^^^^^^^^^^^^^^
  |
help: you can add the `dyn` keyword if you want a trait object
  |
1 | fn foo(x: u32) -> Box<dyn Fn(u32) -> u32> {
  |                       +++

For more information about this error, try `rustc --explain E0782`.

Desired output

error[E0373]: closure may outlive the current function, but it borrows `x`, which is owned by the current function
 --> src/main.rs:2:14
  |
2 |     Box::new(|y| x + y)
  |              ^^^ - `x` is borrowed here
  |              |
  |              may outlive borrowed value `x`
  |
note: closure is returned here
 --> src/main.rs:2:5
  |
2 |     Box::new(|y| x + y)
  |     ^^^^^^^^^^^^^^^^^^^
help: to force the closure to take ownership of `x` (and any other referenced variables), use the `move` keyword
  |
2 |     Box::new(move |y| x + y)
  |              ++++

For more information about this error, try `rustc --explain E0373`.

Rationale and extra context

AFAIK, this was allowed before edition 2021, but now it is prohibited.

Rust Version

rustc 1.85.0 (4d91de4e4 2025-02-17)
binary: rustc
commit-hash: 4d91de4e48198da2e33413efdcd9cd2cc0c46688
commit-date: 2025-02-17
host: x86_64-unknown-linux-gnu
release: 1.85.0
LLVM version: 19.1.7

Anything else?

No response

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions