diff options
| author | Esteban Küber <esteban@kuber.com.ar> | 2025-07-20 02:22:55 +0000 |
|---|---|---|
| committer | Esteban Küber <esteban@kuber.com.ar> | 2025-07-21 16:21:23 +0000 |
| commit | 8df93e6966e71da8a249a0022680b83eff105f42 (patch) | |
| tree | 9208683bd104beb777378e9451f2dbdb7c67848e | |
| parent | 5082e6a300974459aec6dc73e76cc039c3a517c1 (diff) | |
| download | rust-8df93e6966e71da8a249a0022680b83eff105f42.tar.gz rust-8df93e6966e71da8a249a0022680b83eff105f42.zip | |
Tweak spans when encountering multiline initializer in move error
```
error[E0507]: cannot move out of `f`, a captured variable in an `FnMut` closure
--> $DIR/borrowck-call-is-borrow-issue-12224.rs:57:13
|
LL | let mut f = move |g: Box<dyn FnMut(isize)>, b: isize| {
| ----- captured outer variable
...
LL | f(Box::new(|a| {
| --- captured by this `FnMut` closure
LL |
LL | foo(f);
| ^ move occurs because `f` has type `{closure@$DIR/borrowck-call-is-borrow-issue-12224.rs:52:17: 52:58}`, which does not implement the `Copy` trait
```
instead of
```
error[E0507]: cannot move out of `f`, a captured variable in an `FnMut` closure
--> $DIR/borrowck-call-is-borrow-issue-12224.rs:57:13
|
LL | let mut f = move |g: Box<dyn FnMut(isize)>, b: isize| {
| _________-----___-
| | |
| | captured outer variable
LL | | let _ = s.len();
LL | | };
| |_____- move occurs because `f` has type `{closure@$DIR/borrowck-call-is-borrow-issue-12224.rs:52:17: 52:58}`, which does not implement the `Copy` trait
LL | f(Box::new(|a| {
| --- captured by this `FnMut` closure
LL |
LL | foo(f);
| ^ `f` is moved here
```
3 files changed, 18 insertions, 17 deletions
diff --git a/compiler/rustc_borrowck/src/diagnostics/move_errors.rs b/compiler/rustc_borrowck/src/diagnostics/move_errors.rs index 56f7fe68854..40361906e04 100644 --- a/compiler/rustc_borrowck/src/diagnostics/move_errors.rs +++ b/compiler/rustc_borrowck/src/diagnostics/move_errors.rs @@ -663,8 +663,15 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> { // | | `Option<Foo>`, which does not implement the // | | `Copy` trait // | captured outer variable - (None, Some(init)) => init.span, - (None, None) => use_span, + // + // We don't want the case where the initializer is something that spans + // multiple lines, like a closure, as the ASCII art gets messy. + (None, Some(init)) + if !self.infcx.tcx.sess.source_map().is_multiline(init.span) => + { + init.span + } + _ => use_span, }, _ => use_span, }; diff --git a/tests/ui/borrowck/unboxed-closures-move-upvar-from-non-once-ref-closure.stderr b/tests/ui/borrowck/unboxed-closures-move-upvar-from-non-once-ref-closure.stderr index da0e133ba02..177e9c8d248 100644 --- a/tests/ui/borrowck/unboxed-closures-move-upvar-from-non-once-ref-closure.stderr +++ b/tests/ui/borrowck/unboxed-closures-move-upvar-from-non-once-ref-closure.stderr @@ -2,15 +2,13 @@ error[E0507]: cannot move out of `y`, a captured variable in an `Fn` closure --> $DIR/unboxed-closures-move-upvar-from-non-once-ref-closure.rs:12:9 | LL | let y = vec![format!("World")]; - | - ---------------------- move occurs because `y` has type `Vec<String>`, which does not implement the `Copy` trait - | | - | captured outer variable + | - captured outer variable LL | call(|| { | -- captured by this `Fn` closure LL | y.into_iter(); | ^ ----------- `y` moved due to this method call | | - | `y` is moved here + | move occurs because `y` has type `Vec<String>`, which does not implement the `Copy` trait | note: `into_iter` takes ownership of the receiver `self`, which moves `y` --> $SRC_DIR/core/src/iter/traits/collect.rs:LL:COL diff --git a/tests/ui/span/borrowck-call-is-borrow-issue-12224.stderr b/tests/ui/span/borrowck-call-is-borrow-issue-12224.stderr index 28761692413..f37dc320fa3 100644 --- a/tests/ui/span/borrowck-call-is-borrow-issue-12224.stderr +++ b/tests/ui/span/borrowck-call-is-borrow-issue-12224.stderr @@ -35,18 +35,14 @@ LL | fn test4(f: &mut Test) { error[E0507]: cannot move out of `f`, a captured variable in an `FnMut` closure --> $DIR/borrowck-call-is-borrow-issue-12224.rs:57:13 | -LL | let mut f = move |g: Box<dyn FnMut(isize)>, b: isize| { - | _________-----___- - | | | - | | captured outer variable -LL | | let _ = s.len(); -LL | | }; - | |_____- move occurs because `f` has type `{closure@$DIR/borrowck-call-is-borrow-issue-12224.rs:52:17: 52:58}`, which does not implement the `Copy` trait -LL | f(Box::new(|a| { - | --- captured by this `FnMut` closure +LL | let mut f = move |g: Box<dyn FnMut(isize)>, b: isize| { + | ----- captured outer variable +... +LL | f(Box::new(|a| { + | --- captured by this `FnMut` closure LL | -LL | foo(f); - | ^ `f` is moved here +LL | foo(f); + | ^ move occurs because `f` has type `{closure@$DIR/borrowck-call-is-borrow-issue-12224.rs:52:17: 52:58}`, which does not implement the `Copy` trait | help: consider cloning the value if the performance cost is acceptable | |
