Skip to content

Commit 2c06936

Browse files
committed
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 ```
1 parent a3f6725 commit 2c06936

File tree

3 files changed

+18
-17
lines changed

3 files changed

+18
-17
lines changed

compiler/rustc_borrowck/src/diagnostics/move_errors.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -663,8 +663,15 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
663663
// | | `Option<Foo>`, which does not implement the
664664
// | | `Copy` trait
665665
// | captured outer variable
666-
(None, Some(init)) => init.span,
667-
(None, None) => use_span,
666+
//
667+
// We don't want the case where the initializer is something that spans
668+
// multiple lines, like a closure, as the ASCII art gets messy.
669+
(None, Some(init))
670+
if !self.infcx.tcx.sess.source_map().is_multiline(init.span) =>
671+
{
672+
init.span
673+
}
674+
_ => use_span,
668675
},
669676
_ => use_span,
670677
};

tests/ui/borrowck/unboxed-closures-move-upvar-from-non-once-ref-closure.stderr

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,13 @@ error[E0507]: cannot move out of `y`, a captured variable in an `Fn` closure
22
--> $DIR/unboxed-closures-move-upvar-from-non-once-ref-closure.rs:12:9
33
|
44
LL | let y = vec![format!("World")];
5-
| - ---------------------- move occurs because `y` has type `Vec<String>`, which does not implement the `Copy` trait
6-
| |
7-
| captured outer variable
5+
| - captured outer variable
86
LL | call(|| {
97
| -- captured by this `Fn` closure
108
LL | y.into_iter();
119
| ^ ----------- `y` moved due to this method call
1210
| |
13-
| `y` is moved here
11+
| move occurs because `y` has type `Vec<String>`, which does not implement the `Copy` trait
1412
|
1513
note: `into_iter` takes ownership of the receiver `self`, which moves `y`
1614
--> $SRC_DIR/core/src/iter/traits/collect.rs:LL:COL

tests/ui/span/borrowck-call-is-borrow-issue-12224.stderr

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,18 +35,14 @@ LL | fn test4(f: &mut Test) {
3535
error[E0507]: cannot move out of `f`, a captured variable in an `FnMut` closure
3636
--> $DIR/borrowck-call-is-borrow-issue-12224.rs:57:13
3737
|
38-
LL | let mut f = move |g: Box<dyn FnMut(isize)>, b: isize| {
39-
| _________-----___-
40-
| | |
41-
| | captured outer variable
42-
LL | | let _ = s.len();
43-
LL | | };
44-
| |_____- 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
45-
LL | f(Box::new(|a| {
46-
| --- captured by this `FnMut` closure
38+
LL | let mut f = move |g: Box<dyn FnMut(isize)>, b: isize| {
39+
| ----- captured outer variable
40+
...
41+
LL | f(Box::new(|a| {
42+
| --- captured by this `FnMut` closure
4743
LL |
48-
LL | foo(f);
49-
| ^ `f` is moved here
44+
LL | foo(f);
45+
| ^ 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
5046
|
5147
help: consider cloning the value if the performance cost is acceptable
5248
|

0 commit comments

Comments
 (0)