diff options
| author | Stuart Cook <Zalathar@users.noreply.github.com> | 2025-08-11 12:21:08 +1000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-08-11 12:21:08 +1000 |
| commit | 907076c3dd5dc983df954755ce953725eda159eb (patch) | |
| tree | b7cf4b0f1828e3af23ad9778148f449343dd4c1d /compiler/rustc_errors/src | |
| parent | 3724e13cc7de90804ff6be508cd47ee8d979feb8 (diff) | |
| parent | 74496bc4d9f64ea01ae3714b9680cc22c1535cb2 (diff) | |
| download | rust-907076c3dd5dc983df954755ce953725eda159eb.tar.gz rust-907076c3dd5dc983df954755ce953725eda159eb.zip | |
Rollup merge of #144558 - estebank:issue-68119, r=lcnr
Point at the `Fn()` or `FnMut()` bound that coerced a closure, which caused a move error
When encountering a move error involving a closure because the captured value isn't `Copy`, and the obligation comes from a bound on a type parameter that requires `Fn` or `FnMut`, we point at it and explain that an `FnOnce` wouldn't cause the move error.
```
error[E0507]: cannot move out of `foo`, a captured variable in an `Fn` closure
--> f111.rs:15:25
|
14 | fn do_stuff(foo: Option<Foo>) {
| --- ----------- move occurs because `foo` has type `Option<Foo>`, which does not implement the `Copy` trait
| |
| captured outer variable
15 | require_fn_trait(|| async {
| -- ^^^^^ `foo` is moved here
| |
| captured by this `Fn` closure
16 | if foo.map_or(false, |f| f.foo()) {
| --- variable moved due to use in coroutine
|
help: `Fn` and `FnMut` closures require captured values to be able to be consumed multiple times, but an `FnOnce` consume them only once
--> f111.rs:12:53
|
12 | fn require_fn_trait<F: Future<Output = ()>>(_: impl Fn() -> F) {}
| ^^^^^^^^^
help: consider cloning the value if the performance cost is acceptable
|
16 | if foo.clone().map_or(false, |f| f.foo()) {
| ++++++++
```
Fix rust-lang/rust#68119, by pointing at `Fn` and `FnMut` bounds involved in move errors.
Diffstat (limited to 'compiler/rustc_errors/src')
| -rw-r--r-- | compiler/rustc_errors/src/diagnostic.rs | 7 |
1 files changed, 4 insertions, 3 deletions
diff --git a/compiler/rustc_errors/src/diagnostic.rs b/compiler/rustc_errors/src/diagnostic.rs index 5a5563c7bb2..98be37fd84b 100644 --- a/compiler/rustc_errors/src/diagnostic.rs +++ b/compiler/rustc_errors/src/diagnostic.rs @@ -847,17 +847,18 @@ impl<'a, G: EmissionGuarantee> Diag<'a, G> { self } + with_fn! { with_span_help, /// Prints the span with some help above it. /// This is like [`Diag::help()`], but it gets its own span. #[rustc_lint_diagnostics] - pub fn span_help<S: Into<MultiSpan>>( + pub fn span_help( &mut self, - sp: S, + sp: impl Into<MultiSpan>, msg: impl Into<SubdiagMessage>, ) -> &mut Self { self.sub(Level::Help, msg, sp.into()); self - } + } } /// Disallow attaching suggestions to this diagnostic. /// Any suggestions attached e.g. with the `span_suggestion_*` methods |
