Skip to content

Commit 5edc55f

Browse files
committed
Fix borrowck closure span.
1 parent a950aa2 commit 5edc55f

10 files changed

+83
-38
lines changed

compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -903,9 +903,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
903903
if let Some(span) = arg {
904904
err.span_label(span, "change this to accept `FnMut` instead of `Fn`");
905905
err.span_label(func.span, "expects `Fn` instead of `FnMut`");
906-
if self.infcx.tcx.sess.source_map().is_multiline(self.body.span) {
907-
err.span_label(self.body.span, "in this closure");
908-
}
906+
err.span_label(self.body.span, "in this closure");
909907
look_at_return = false;
910908
}
911909
}

src/test/ui/borrowck/borrow-immutable-upvar-mutation.stderr

+18-12
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ LL | fn to_fn<A, F: Fn<A>>(f: F) -> F {
55
| - change this to accept `FnMut` instead of `Fn`
66
...
77
LL | let _f = to_fn(|| x = 42);
8-
| ----- ^^^^^^ cannot assign
9-
| |
8+
| ----- -- ^^^^^^ cannot assign
9+
| | |
10+
| | in this closure
1011
| expects `Fn` instead of `FnMut`
1112

1213
error[E0596]: cannot borrow `y` as mutable, as it is a captured variable in a `Fn` closure
@@ -16,8 +17,9 @@ LL | fn to_fn<A, F: Fn<A>>(f: F) -> F {
1617
| - change this to accept `FnMut` instead of `Fn`
1718
...
1819
LL | let _g = to_fn(|| set(&mut y));
19-
| ----- ^^^^^^ cannot borrow as mutable
20-
| |
20+
| ----- -- ^^^^^^ cannot borrow as mutable
21+
| | |
22+
| | in this closure
2123
| expects `Fn` instead of `FnMut`
2224

2325
error[E0594]: cannot assign to `z`, as it is a captured variable in a `Fn` closure
@@ -27,8 +29,9 @@ LL | fn to_fn<A, F: Fn<A>>(f: F) -> F {
2729
| - change this to accept `FnMut` instead of `Fn`
2830
...
2931
LL | to_fn(|| z = 42);
30-
| ----- ^^^^^^ cannot assign
31-
| |
32+
| ----- -- ^^^^^^ cannot assign
33+
| | |
34+
| | in this closure
3235
| expects `Fn` instead of `FnMut`
3336

3437
error[E0594]: cannot assign to `x`, as it is a captured variable in a `Fn` closure
@@ -38,8 +41,9 @@ LL | fn to_fn<A, F: Fn<A>>(f: F) -> F {
3841
| - change this to accept `FnMut` instead of `Fn`
3942
...
4043
LL | let _f = to_fn(move || x = 42);
41-
| ----- ^^^^^^ cannot assign
42-
| |
44+
| ----- ------- ^^^^^^ cannot assign
45+
| | |
46+
| | in this closure
4347
| expects `Fn` instead of `FnMut`
4448

4549
error[E0596]: cannot borrow `y` as mutable, as it is a captured variable in a `Fn` closure
@@ -49,8 +53,9 @@ LL | fn to_fn<A, F: Fn<A>>(f: F) -> F {
4953
| - change this to accept `FnMut` instead of `Fn`
5054
...
5155
LL | let _g = to_fn(move || set(&mut y));
52-
| ----- ^^^^^^ cannot borrow as mutable
53-
| |
56+
| ----- ------- ^^^^^^ cannot borrow as mutable
57+
| | |
58+
| | in this closure
5459
| expects `Fn` instead of `FnMut`
5560

5661
error[E0594]: cannot assign to `z`, as it is a captured variable in a `Fn` closure
@@ -60,8 +65,9 @@ LL | fn to_fn<A, F: Fn<A>>(f: F) -> F {
6065
| - change this to accept `FnMut` instead of `Fn`
6166
...
6267
LL | to_fn(move || z = 42);
63-
| ----- ^^^^^^ cannot assign
64-
| |
68+
| ----- ------- ^^^^^^ cannot assign
69+
| | |
70+
| | in this closure
6571
| expects `Fn` instead of `FnMut`
6672

6773
error[E0594]: cannot assign to `x`, as it is a captured variable in a `Fn` closure

src/test/ui/borrowck/borrow-raw-address-of-mutability.stderr

+6-2
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ LL | fn make_fn<F: Fn()>(f: F) -> F { f }
3333
| - change this to accept `FnMut` instead of `Fn`
3434
...
3535
LL | let f = make_fn(|| {
36-
| ------- expects `Fn` instead of `FnMut`
36+
| ------- -- in this closure
37+
| |
38+
| expects `Fn` instead of `FnMut`
3739
LL | let y = &raw mut x;
3840
| ^^^^^^^^^^ cannot borrow as mutable
3941

@@ -44,7 +46,9 @@ LL | fn make_fn<F: Fn()>(f: F) -> F { f }
4446
| - change this to accept `FnMut` instead of `Fn`
4547
...
4648
LL | let f = make_fn(move || {
47-
| ------- expects `Fn` instead of `FnMut`
49+
| ------- ------- in this closure
50+
| |
51+
| expects `Fn` instead of `FnMut`
4852
LL | let y = &raw mut x;
4953
| ^^^^^^^^^^ cannot borrow as mutable
5054

src/test/ui/borrowck/mutability-errors.stderr

+24-8
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,9 @@ LL | fn fn_ref<F: Fn()>(f: F) -> F { f }
123123
| - change this to accept `FnMut` instead of `Fn`
124124
...
125125
LL | fn_ref(|| {
126-
| ------ expects `Fn` instead of `FnMut`
126+
| ------ -- in this closure
127+
| |
128+
| expects `Fn` instead of `FnMut`
127129
LL | x = (1,);
128130
| ^^^^^^^^ cannot assign
129131

@@ -134,7 +136,9 @@ LL | fn fn_ref<F: Fn()>(f: F) -> F { f }
134136
| - change this to accept `FnMut` instead of `Fn`
135137
...
136138
LL | fn_ref(|| {
137-
| ------ expects `Fn` instead of `FnMut`
139+
| ------ -- in this closure
140+
| |
141+
| expects `Fn` instead of `FnMut`
138142
LL | x = (1,);
139143
LL | x.0 = 1;
140144
| ^^^^^^^ cannot assign
@@ -146,7 +150,9 @@ LL | fn fn_ref<F: Fn()>(f: F) -> F { f }
146150
| - change this to accept `FnMut` instead of `Fn`
147151
...
148152
LL | fn_ref(|| {
149-
| ------ expects `Fn` instead of `FnMut`
153+
| ------ -- in this closure
154+
| |
155+
| expects `Fn` instead of `FnMut`
150156
...
151157
LL | &mut x;
152158
| ^^^^^^ cannot borrow as mutable
@@ -158,7 +164,9 @@ LL | fn fn_ref<F: Fn()>(f: F) -> F { f }
158164
| - change this to accept `FnMut` instead of `Fn`
159165
...
160166
LL | fn_ref(|| {
161-
| ------ expects `Fn` instead of `FnMut`
167+
| ------ -- in this closure
168+
| |
169+
| expects `Fn` instead of `FnMut`
162170
...
163171
LL | &mut x.0;
164172
| ^^^^^^^^ cannot borrow as mutable
@@ -170,7 +178,9 @@ LL | fn fn_ref<F: Fn()>(f: F) -> F { f }
170178
| - change this to accept `FnMut` instead of `Fn`
171179
...
172180
LL | fn_ref(move || {
173-
| ------ expects `Fn` instead of `FnMut`
181+
| ------ ------- in this closure
182+
| |
183+
| expects `Fn` instead of `FnMut`
174184
LL | x = (1,);
175185
| ^^^^^^^^ cannot assign
176186

@@ -181,7 +191,9 @@ LL | fn fn_ref<F: Fn()>(f: F) -> F { f }
181191
| - change this to accept `FnMut` instead of `Fn`
182192
...
183193
LL | fn_ref(move || {
184-
| ------ expects `Fn` instead of `FnMut`
194+
| ------ ------- in this closure
195+
| |
196+
| expects `Fn` instead of `FnMut`
185197
LL | x = (1,);
186198
LL | x.0 = 1;
187199
| ^^^^^^^ cannot assign
@@ -193,7 +205,9 @@ LL | fn fn_ref<F: Fn()>(f: F) -> F { f }
193205
| - change this to accept `FnMut` instead of `Fn`
194206
...
195207
LL | fn_ref(move || {
196-
| ------ expects `Fn` instead of `FnMut`
208+
| ------ ------- in this closure
209+
| |
210+
| expects `Fn` instead of `FnMut`
197211
...
198212
LL | &mut x;
199213
| ^^^^^^ cannot borrow as mutable
@@ -205,7 +219,9 @@ LL | fn fn_ref<F: Fn()>(f: F) -> F { f }
205219
| - change this to accept `FnMut` instead of `Fn`
206220
...
207221
LL | fn_ref(move || {
208-
| ------ expects `Fn` instead of `FnMut`
222+
| ------ ------- in this closure
223+
| |
224+
| expects `Fn` instead of `FnMut`
209225
...
210226
LL | &mut x.0;
211227
| ^^^^^^^^ cannot borrow as mutable

src/test/ui/fn/fn-closure-mutable-capture.rs

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ pub fn foo() {
66
//~^ ERROR cannot assign to `x`, as it is a captured variable in a `Fn` closure
77
//~| NOTE cannot assign
88
//~| NOTE expects `Fn` instead of `FnMut`
9+
//~| NOTE in this closure
910
}
1011

1112
fn main() {}

src/test/ui/fn/fn-closure-mutable-capture.stderr

+3-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ LL | pub fn bar<F: Fn()>(_f: F) {}
55
| - change this to accept `FnMut` instead of `Fn`
66
...
77
LL | bar(move || x = 1);
8-
| --- ^^^^^ cannot assign
9-
| |
8+
| --- ------- ^^^^^ cannot assign
9+
| | |
10+
| | in this closure
1011
| expects `Fn` instead of `FnMut`
1112

1213
error: aborting due to previous error

src/test/ui/issues/issue-21600.stderr

+6-3
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ LL | fn call_it<F>(f: F) where F: Fn() { f(); }
55
| - change this to accept `FnMut` instead of `Fn`
66
...
77
LL | call_it(|| x.gen_mut());
8-
| ------- ^^^^^^^^^^^ cannot borrow as mutable
9-
| |
8+
| ------- -- ^^^^^^^^^^^ cannot borrow as mutable
9+
| | |
10+
| | in this closure
1011
| expects `Fn` instead of `FnMut`
1112

1213
error[E0596]: cannot borrow `x` as mutable, as it is a captured variable in a `Fn` closure
@@ -16,7 +17,9 @@ LL | fn call_it<F>(f: F) where F: Fn() { f(); }
1617
| - change this to accept `FnMut` instead of `Fn`
1718
...
1819
LL | call_it(|| {
19-
| ------- expects `Fn` instead of `FnMut`
20+
| ------- -- in this closure
21+
| |
22+
| expects `Fn` instead of `FnMut`
2023
LL | call_it(|| x.gen());
2124
LL | call_it(|| x.gen_mut());
2225
| ^^ - mutable borrow occurs due to use of `x` in closure

src/test/ui/nll/closure-captures.stderr

+18-6
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@ LL | fn fn_ref<F: Fn()>(f: F) -> F { f }
4141
| - change this to accept `FnMut` instead of `Fn`
4242
...
4343
LL | fn_ref(|| {
44-
| ------ expects `Fn` instead of `FnMut`
44+
| ------ -- in this closure
45+
| |
46+
| expects `Fn` instead of `FnMut`
4547
LL | ||
4648
| ^^ cannot borrow as mutable
4749
LL | x = 1;}
@@ -54,7 +56,9 @@ LL | fn fn_ref<F: Fn()>(f: F) -> F { f }
5456
| - change this to accept `FnMut` instead of `Fn`
5557
...
5658
LL | fn_ref(move || {
57-
| ------ expects `Fn` instead of `FnMut`
59+
| ------ ------- in this closure
60+
| |
61+
| expects `Fn` instead of `FnMut`
5862
LL | ||
5963
| ^^ cannot borrow as mutable
6064
LL | x = 1;});
@@ -76,7 +80,9 @@ LL | fn fn_ref<F: Fn()>(f: F) -> F { f }
7680
| - change this to accept `FnMut` instead of `Fn`
7781
...
7882
LL | fn_ref(|| {
79-
| ------ expects `Fn` instead of `FnMut`
83+
| ------ -- in this closure
84+
| |
85+
| expects `Fn` instead of `FnMut`
8086
LL | ||
8187
| ^^ cannot borrow as mutable
8288
LL | x = 1;}
@@ -98,7 +104,9 @@ LL | fn fn_ref<F: Fn()>(f: F) -> F { f }
98104
| - change this to accept `FnMut` instead of `Fn`
99105
...
100106
LL | fn_ref(move || {
101-
| ------ expects `Fn` instead of `FnMut`
107+
| ------ ------- in this closure
108+
| |
109+
| expects `Fn` instead of `FnMut`
102110
LL | ||
103111
| ^^ cannot borrow as mutable
104112
LL | x = 1;});
@@ -111,7 +119,9 @@ LL | fn fn_ref<F: Fn()>(f: F) -> F { f }
111119
| - change this to accept `FnMut` instead of `Fn`
112120
...
113121
LL | fn_ref(|| {
114-
| ------ expects `Fn` instead of `FnMut`
122+
| ------ -- in this closure
123+
| |
124+
| expects `Fn` instead of `FnMut`
115125
LL | ||
116126
| ^^ cannot borrow as mutable
117127
LL | *x = 1;});
@@ -124,7 +134,9 @@ LL | fn fn_ref<F: Fn()>(f: F) -> F { f }
124134
| - change this to accept `FnMut` instead of `Fn`
125135
...
126136
LL | fn_ref(move || {
127-
| ------ expects `Fn` instead of `FnMut`
137+
| ------ ------- in this closure
138+
| |
139+
| expects `Fn` instead of `FnMut`
128140
LL | ||
129141
| ^^ cannot borrow as mutable
130142
LL | *x = 1;});

src/test/ui/unboxed-closures/unboxed-closures-mutate-upvar.stderr

+3-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@ LL | fn to_fn<A,F:Fn<A>>(f: F) -> F { f }
3232
| - change this to accept `FnMut` instead of `Fn`
3333
...
3434
LL | let mut f = to_fn(move || {
35-
| ----- expects `Fn` instead of `FnMut`
35+
| ----- ------- in this closure
36+
| |
37+
| expects `Fn` instead of `FnMut`
3638
LL | n += 1;
3739
| ^^^^^^ cannot assign
3840

src/test/ui/unboxed-closures/unboxed-closures-mutated-upvar-from-fn-closure.stderr

+3-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ LL | fn call<F>(f: F) where F : Fn() {
55
| - change this to accept `FnMut` instead of `Fn`
66
...
77
LL | call(|| {
8-
| ---- expects `Fn` instead of `FnMut`
8+
| ---- -- in this closure
9+
| |
10+
| expects `Fn` instead of `FnMut`
911
LL | counter += 1;
1012
| ^^^^^^^^^^^^ cannot assign
1113

0 commit comments

Comments
 (0)