Skip to content

Commit 637fd2e

Browse files
committed
Auto merge of #51651 - spastorino:fix_var_name_in_e0502, r=nikomatsakis
Fix variable name in E0502 double borrow error Closes #51268 r? @nikomatsakis
2 parents 7d313ea + 3d31e5f commit 637fd2e

File tree

3 files changed

+58
-0
lines changed

3 files changed

+58
-0
lines changed

src/librustc_mir/borrow_check/error_reporting.rs

+3
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,9 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
356356
};
357357

358358
if let Some((_, var_span)) = old_closure_span {
359+
let place = &issued_borrow.borrowed_place;
360+
let desc_place = self.describe_place(place).unwrap_or("_".to_owned());
361+
359362
err.span_label(
360363
var_span,
361364
format!(

src/test/ui/nll/issue-51268.rs

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// ignore-tidy-linelength
12+
13+
#![feature(nll)]
14+
15+
struct Bar;
16+
17+
impl Bar {
18+
fn bar(&mut self, _: impl Fn()) {}
19+
}
20+
21+
struct Foo {
22+
thing: Bar,
23+
number: usize,
24+
}
25+
26+
impl Foo {
27+
fn foo(&mut self) {
28+
self.thing.bar(|| {
29+
//~^ ERROR cannot borrow `self.thing` as mutable because it is also borrowed as immutable [E0502]
30+
&self.number;
31+
});
32+
}
33+
}
34+
35+
fn main() {}

src/test/ui/nll/issue-51268.stderr

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
error[E0502]: cannot borrow `self.thing` as mutable because it is also borrowed as immutable
2+
--> $DIR/issue-51268.rs:28:9
3+
|
4+
LL | self.thing.bar(|| {
5+
| ^ -- immutable borrow occurs here
6+
| _________|
7+
| |_________|
8+
| ||
9+
LL | || //~^ ERROR cannot borrow `self.thing` as mutable because it is also borrowed as immutable [E0502]
10+
LL | || &self.number;
11+
| || ---- previous borrow occurs due to use of `self` in closure
12+
LL | || });
13+
| || ^
14+
| ||__________|
15+
| |___________mutable borrow occurs here
16+
| borrow later used here
17+
18+
error: aborting due to previous error
19+
20+
For more information about this error, try `rustc --explain E0502`.

0 commit comments

Comments
 (0)