Skip to content

Commit 632c0af

Browse files
committed
borrowck diagnostics: address review comments.
1 parent c70aa34 commit 632c0af

File tree

5 files changed

+31
-26
lines changed

5 files changed

+31
-26
lines changed

src/librustc_mir/borrow_check/diagnostics/conflict_errors.rs

+14-14
Original file line numberDiff line numberDiff line change
@@ -256,8 +256,8 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
256256
"report_move_out_while_borrowed: location={:?} place={:?} span={:?} borrow={:?}",
257257
location, place, span, borrow
258258
);
259-
let value_msg = self.describe_place_str(place.as_ref());
260-
let borrow_msg = self.describe_place_str(borrow.borrowed_place.as_ref());
259+
let value_msg = self.describe_any_place(place.as_ref());
260+
let borrow_msg = self.describe_any_place(borrow.borrowed_place.as_ref());
261261

262262
let borrow_spans = self.retrieve_borrow_spans(borrow);
263263
let borrow_span = borrow_spans.args_or_use();
@@ -266,7 +266,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
266266
let span = move_spans.args_or_use();
267267

268268
let mut err =
269-
self.cannot_move_when_borrowed(span, &self.describe_place_str(place.as_ref()));
269+
self.cannot_move_when_borrowed(span, &self.describe_any_place(place.as_ref()));
270270
err.span_label(borrow_span, format!("borrow of {} occurs here", borrow_msg));
271271
err.span_label(span, format!("move out of {} occurs here", value_msg));
272272

@@ -306,14 +306,14 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
306306

307307
let mut err = self.cannot_use_when_mutably_borrowed(
308308
span,
309-
&self.describe_place_str(place.as_ref()),
309+
&self.describe_any_place(place.as_ref()),
310310
borrow_span,
311-
&self.describe_place_str(borrow.borrowed_place.as_ref()),
311+
&self.describe_any_place(borrow.borrowed_place.as_ref()),
312312
);
313313

314314
borrow_spans.var_span_label(&mut err, {
315315
let place = &borrow.borrowed_place;
316-
let desc_place = self.describe_place_str(place.as_ref());
316+
let desc_place = self.describe_any_place(place.as_ref());
317317
format!("borrow occurs due to use of {}{}", desc_place, borrow_spans.describe())
318318
});
319319

@@ -506,7 +506,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
506506
);
507507
} else {
508508
let borrow_place = &issued_borrow.borrowed_place;
509-
let borrow_place_desc = self.describe_place_str(borrow_place.as_ref());
509+
let borrow_place_desc = self.describe_any_place(borrow_place.as_ref());
510510
issued_spans.var_span_label(
511511
&mut err,
512512
format!(
@@ -647,12 +647,12 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
647647
&& proj_base == target_base.projection
648648
{
649649
return Some((
650-
self.describe_place_str(PlaceRef {
650+
self.describe_any_place(PlaceRef {
651651
local,
652652
projection: proj_base,
653653
}),
654-
self.describe_place_str(first_borrowed_place.as_ref()),
655-
self.describe_place_str(second_borrowed_place.as_ref()),
654+
self.describe_any_place(first_borrowed_place.as_ref()),
655+
self.describe_any_place(second_borrowed_place.as_ref()),
656656
union_ty.to_string(),
657657
));
658658
}
@@ -665,7 +665,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
665665
// If we didn't find a field access into a union, or both places match, then
666666
// only return the description of the first place.
667667
(
668-
self.describe_place_str(first_borrowed_place.as_ref()),
668+
self.describe_any_place(first_borrowed_place.as_ref()),
669669
"".to_string(),
670670
"".to_string(),
671671
"".to_string(),
@@ -1388,7 +1388,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
13881388
let loan_spans = self.retrieve_borrow_spans(loan);
13891389
let loan_span = loan_spans.args_or_use();
13901390

1391-
let descr_place = self.describe_place_str(place.as_ref());
1391+
let descr_place = self.describe_any_place(place.as_ref());
13921392
if loan.kind == BorrowKind::Shallow {
13931393
if let Some(section) = self.classify_immutable_section(&loan.assigned_place) {
13941394
let mut err = self.cannot_mutate_in_immutable_section(
@@ -1463,8 +1463,8 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
14631463
})
14641464
| Some(LocalDecl { local_info: LocalInfo::StaticRef { .. }, .. })
14651465
| Some(LocalDecl { local_info: LocalInfo::Other, .. })
1466-
| None => (self.describe_place_str(place.as_ref()), assigned_span),
1467-
Some(decl) => (self.describe_place_str(err_place.as_ref()), decl.source_info.span),
1466+
| None => (self.describe_any_place(place.as_ref()), assigned_span),
1467+
Some(decl) => (self.describe_any_place(err_place.as_ref()), decl.source_info.span),
14681468
};
14691469

14701470
let mut err = self.cannot_reassign_immutable(span, &place_description, from_arg);

src/librustc_mir/borrow_check/diagnostics/mod.rs

+9-3
Original file line numberDiff line numberDiff line change
@@ -138,10 +138,16 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
138138
}
139139

140140
/// End-user visible description of `place` if one can be found.
141-
/// If the place is a temporary for instance, `value` will be returned.
142-
pub(super) fn describe_place_str(&self, place_ref: PlaceRef<'tcx>) -> String {
141+
/// If the place is a temporary for instance, `"value"` will be returned.
142+
pub(super) fn describe_any_place(&self, place_ref: PlaceRef<'tcx>) -> String {
143143
match self.describe_place(place_ref) {
144-
Some(descr) => format!("`{}`", descr),
144+
Some(mut descr) => {
145+
// Surround descr with `backticks`.
146+
descr.reserve(2);
147+
descr.insert_str(0, "`");
148+
descr.push_str("`");
149+
descr
150+
}
145151
None => "value".to_string(),
146152
}
147153
}

src/librustc_mir/borrow_check/diagnostics/move_errors.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -272,14 +272,14 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
272272
span: Span,
273273
) -> DiagnosticBuilder<'a> {
274274
let description = if place.projection.len() == 1 {
275-
format!("static item {}", self.describe_place_str(place.as_ref()))
275+
format!("static item {}", self.describe_any_place(place.as_ref()))
276276
} else {
277277
let base_static = PlaceRef { local: place.local, projection: &[ProjectionElem::Deref] };
278278

279279
format!(
280280
"{} as {} is a static item",
281-
self.describe_place_str(place.as_ref()),
282-
self.describe_place_str(base_static),
281+
self.describe_any_place(place.as_ref()),
282+
self.describe_any_place(base_static),
283283
)
284284
};
285285

@@ -349,7 +349,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
349349
let upvar_name = upvar.name;
350350
let upvar_span = self.infcx.tcx.hir().span(upvar_hir_id);
351351

352-
let place_name = self.describe_place_str(move_place.as_ref());
352+
let place_name = self.describe_any_place(move_place.as_ref());
353353

354354
let place_description =
355355
if self.is_upvar_field_projection(move_place.as_ref()).is_some() {

src/librustc_mir/borrow_check/diagnostics/mutability_errors.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
170170
&mut err,
171171
format!(
172172
"mutable borrow occurs due to use of {} in closure",
173-
self.describe_place_str(access_place.as_ref()),
173+
self.describe_any_place(access_place.as_ref()),
174174
),
175175
);
176176
borrow_span

src/librustc_mir/util/borrowck_errors.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ impl<'cx, 'tcx> crate::borrow_check::MirBorrowckCtxt<'cx, 'tcx> {
5353
old_load_end_span: Option<Span>,
5454
) -> DiagnosticBuilder<'cx> {
5555
let via =
56-
|msg: &str| if msg.is_empty() { msg.to_string() } else { format!(" (via {})", msg) };
56+
|msg: &str| if msg.is_empty() { "".to_string() } else { format!(" (via {})", msg) };
5757
let mut err = struct_span_err!(
5858
self,
5959
new_loan_span,
@@ -201,13 +201,12 @@ impl<'cx, 'tcx> crate::borrow_check::MirBorrowckCtxt<'cx, 'tcx> {
201201
old_load_end_span: Option<Span>,
202202
) -> DiagnosticBuilder<'cx> {
203203
let via =
204-
|msg: &str| if msg.is_empty() { msg.to_string() } else { format!(" (via {})", msg) };
204+
|msg: &str| if msg.is_empty() { "".to_string() } else { format!(" (via {})", msg) };
205205
let mut err = struct_span_err!(
206206
self,
207207
span,
208208
E0502,
209-
"cannot borrow {}{} as {} because {} is also borrowed \
210-
as {}{}",
209+
"cannot borrow {}{} as {} because {} is also borrowed as {}{}",
211210
desc_new,
212211
via(msg_new),
213212
kind_new,

0 commit comments

Comments
 (0)