Skip to content

Commit 7d313ea

Browse files
committed
Auto merge of #51638 - spastorino:diagnostic-suggest-drop-in-reverse, r=nikomatsakis
Diagnostic suggest drop in reverse Closes #51195
2 parents cca43a7 + d3defca commit 7d313ea

22 files changed

+784
-454
lines changed

src/librustc/mir/mod.rs

+611-350
Large diffs are not rendered by default.

src/librustc_mir/borrow_check/error_reporting.rs

+50-48
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,21 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
use syntax_pos::Span;
11+
use borrow_check::WriteKind;
1212
use rustc::middle::region::ScopeTree;
1313
use rustc::mir::{BorrowKind, Field, Local, LocalKind, Location, Operand};
1414
use rustc::mir::{Place, ProjectionElem, Rvalue, Statement, StatementKind};
1515
use rustc::ty::{self, RegionKind};
1616
use rustc_data_structures::indexed_vec::Idx;
1717
use rustc_data_structures::sync::Lrc;
18+
use syntax_pos::Span;
1819

20+
use super::borrow_set::BorrowData;
1921
use super::{Context, MirBorrowckCtxt};
2022
use super::{InitializationRequiringAction, PrefixSet};
21-
use super::borrow_set::BorrowData;
2223

23-
use dataflow::{FlowAtLocation, MovingOutStatements};
2424
use dataflow::move_paths::MovePathIndex;
25+
use dataflow::{FlowAtLocation, MovingOutStatements};
2526
use util::borrowck_errors::{BorrowckErrors, Origin};
2627

2728
impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
@@ -39,22 +40,17 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
3940
.collect::<Vec<_>>();
4041

4142
if mois.is_empty() {
42-
let root_place = self.prefixes(&place, PrefixSet::All)
43-
.last()
44-
.unwrap();
43+
let root_place = self.prefixes(&place, PrefixSet::All).last().unwrap();
4544

46-
if self.moved_error_reported
47-
.contains(&root_place.clone())
48-
{
45+
if self.moved_error_reported.contains(&root_place.clone()) {
4946
debug!(
5047
"report_use_of_moved_or_uninitialized place: error about {:?} suppressed",
5148
root_place
5249
);
5350
return;
5451
}
5552

56-
self.moved_error_reported
57-
.insert(root_place.clone());
53+
self.moved_error_reported.insert(root_place.clone());
5854

5955
let item_msg = match self.describe_place(place) {
6056
Some(name) => format!("`{}`", name),
@@ -162,7 +158,7 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
162158
format!("borrow of {} occurs here", borrow_msg),
163159
);
164160
err.span_label(span, format!("move out of {} occurs here", value_msg));
165-
self.explain_why_borrow_contains_point(context, borrow, &mut err);
161+
self.explain_why_borrow_contains_point(context, borrow, None, &mut err);
166162
err.emit();
167163
}
168164

@@ -177,12 +173,13 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
177173
span,
178174
&self.describe_place(place).unwrap_or("_".to_owned()),
179175
self.retrieve_borrow_span(borrow),
180-
&self.describe_place(&borrow.borrowed_place)
176+
&self
177+
.describe_place(&borrow.borrowed_place)
181178
.unwrap_or("_".to_owned()),
182179
Origin::Mir,
183180
);
184181

185-
self.explain_why_borrow_contains_point(context, borrow, &mut err);
182+
self.explain_why_borrow_contains_point(context, borrow, None, &mut err);
186183

187184
err.emit();
188185
}
@@ -286,8 +283,8 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
286283
"mutable",
287284
) {
288285
(BorrowKind::Shared, lft, _, BorrowKind::Mut { .. }, _, rgt)
289-
| (BorrowKind::Mut { .. }, _, lft, BorrowKind::Shared, rgt, _) => {
290-
tcx.cannot_reborrow_already_borrowed(
286+
| (BorrowKind::Mut { .. }, _, lft, BorrowKind::Shared, rgt, _) => tcx
287+
.cannot_reborrow_already_borrowed(
291288
span,
292289
&desc_place,
293290
"",
@@ -298,30 +295,27 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
298295
"",
299296
None,
300297
Origin::Mir,
301-
)
302-
}
298+
),
303299

304-
(BorrowKind::Mut { .. }, _, _, BorrowKind::Mut { .. }, _, _) => {
305-
tcx.cannot_mutably_borrow_multiply(
300+
(BorrowKind::Mut { .. }, _, _, BorrowKind::Mut { .. }, _, _) => tcx
301+
.cannot_mutably_borrow_multiply(
306302
span,
307303
&desc_place,
308304
"",
309305
issued_span,
310306
"",
311307
None,
312308
Origin::Mir,
313-
)
314-
}
309+
),
315310

316-
(BorrowKind::Unique, _, _, BorrowKind::Unique, _, _) => {
317-
tcx.cannot_uniquely_borrow_by_two_closures(
311+
(BorrowKind::Unique, _, _, BorrowKind::Unique, _, _) => tcx
312+
.cannot_uniquely_borrow_by_two_closures(
318313
span,
319314
&desc_place,
320315
issued_span,
321316
None,
322317
Origin::Mir,
323-
)
324-
}
318+
),
325319

326320
(BorrowKind::Unique, _, _, _, _, _) => tcx.cannot_uniquely_borrow_by_one_closure(
327321
span,
@@ -334,8 +328,8 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
334328
Origin::Mir,
335329
),
336330

337-
(BorrowKind::Shared, lft, _, BorrowKind::Unique, _, _) => {
338-
tcx.cannot_reborrow_already_uniquely_borrowed(
331+
(BorrowKind::Shared, lft, _, BorrowKind::Unique, _, _) => tcx
332+
.cannot_reborrow_already_uniquely_borrowed(
339333
span,
340334
&desc_place,
341335
"",
@@ -344,11 +338,10 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
344338
"",
345339
None,
346340
Origin::Mir,
347-
)
348-
}
341+
),
349342

350-
(BorrowKind::Mut { .. }, _, lft, BorrowKind::Unique, _, _) => {
351-
tcx.cannot_reborrow_already_uniquely_borrowed(
343+
(BorrowKind::Mut { .. }, _, lft, BorrowKind::Unique, _, _) => tcx
344+
.cannot_reborrow_already_uniquely_borrowed(
352345
span,
353346
&desc_place,
354347
"",
@@ -357,8 +350,7 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
357350
"",
358351
None,
359352
Origin::Mir,
360-
)
361-
}
353+
),
362354

363355
(BorrowKind::Shared, _, _, BorrowKind::Shared, _, _) => unreachable!(),
364356
};
@@ -380,7 +372,7 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
380372
);
381373
}
382374

383-
self.explain_why_borrow_contains_point(context, issued_borrow, &mut err);
375+
self.explain_why_borrow_contains_point(context, issued_borrow, None, &mut err);
384376

385377
err.emit();
386378
}
@@ -389,10 +381,13 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
389381
&mut self,
390382
context: Context,
391383
borrow: &BorrowData<'tcx>,
392-
drop_span: Span,
384+
place_span: (&Place<'tcx>, Span),
385+
kind: Option<WriteKind>,
393386
) {
387+
let drop_span = place_span.1;
394388
let scope_tree = self.tcx.region_scope_tree(self.mir_def_id);
395-
let root_place = self.prefixes(&borrow.borrowed_place, PrefixSet::All)
389+
let root_place = self
390+
.prefixes(&borrow.borrowed_place, PrefixSet::All)
396391
.last()
397392
.unwrap();
398393

@@ -402,7 +397,8 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
402397
_ => drop_span,
403398
};
404399

405-
if self.access_place_error_reported
400+
if self
401+
.access_place_error_reported
406402
.contains(&(root_place.clone(), borrow_span))
407403
{
408404
debug!(
@@ -450,6 +446,7 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
450446
drop_span,
451447
borrow_span,
452448
proper_span,
449+
kind.map(|k| (k, place_span.0)),
453450
);
454451
}
455452
(RegionKind::ReEarlyBound(_), None)
@@ -471,8 +468,11 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
471468
| (RegionKind::ReClosureBound(_), _)
472469
| (RegionKind::ReCanonical(_), _)
473470
| (RegionKind::ReErased, _) => {
474-
span_bug!(drop_span, "region {:?} does not make sense in this context",
475-
borrow.region);
471+
span_bug!(
472+
drop_span,
473+
"region {:?} does not make sense in this context",
474+
borrow.region
475+
);
476476
}
477477
}
478478
}
@@ -495,7 +495,7 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
495495
drop_span,
496496
format!("`{}` dropped here while still borrowed", name),
497497
);
498-
self.explain_why_borrow_contains_point(context, borrow, &mut err);
498+
self.explain_why_borrow_contains_point(context, borrow, None, &mut err);
499499
err.emit();
500500
}
501501

@@ -517,7 +517,7 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
517517
"temporary value dropped here while still borrowed",
518518
);
519519
err.note("consider using a `let` binding to increase its lifetime");
520-
self.explain_why_borrow_contains_point(context, borrow, &mut err);
520+
self.explain_why_borrow_contains_point(context, borrow, None, &mut err);
521521
err.emit();
522522
}
523523

@@ -530,6 +530,7 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
530530
drop_span: Span,
531531
borrow_span: Span,
532532
_proper_span: Span,
533+
kind_place: Option<(WriteKind, &Place<'tcx>)>,
533534
) {
534535
debug!(
535536
"report_unscoped_local_value_does_not_live_long_enough(\
@@ -544,7 +545,7 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
544545
err.span_label(borrow_span, "borrowed value does not live long enough");
545546
err.span_label(drop_span, "borrowed value only lives until here");
546547

547-
self.explain_why_borrow_contains_point(context, borrow, &mut err);
548+
self.explain_why_borrow_contains_point(context, borrow, kind_place, &mut err);
548549
err.emit();
549550
}
550551

@@ -570,7 +571,7 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
570571
err.span_label(proper_span, "temporary value does not live long enough");
571572
err.span_label(drop_span, "temporary value only lives until here");
572573

573-
self.explain_why_borrow_contains_point(context, borrow, &mut err);
574+
self.explain_why_borrow_contains_point(context, borrow, None, &mut err);
574575
err.emit();
575576
}
576577

@@ -588,7 +589,7 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
588589
Origin::Mir,
589590
);
590591

591-
self.explain_why_borrow_contains_point(context, loan, &mut err);
592+
self.explain_why_borrow_contains_point(context, loan, None, &mut err);
592593

593594
err.emit();
594595
}
@@ -759,9 +760,10 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
759760
Place::Static(ref static_) => self.describe_field_from_ty(&static_.ty, field),
760761
Place::Projection(ref proj) => match proj.elem {
761762
ProjectionElem::Deref => self.describe_field(&proj.base, field),
762-
ProjectionElem::Downcast(def, variant_index) => {
763-
format!("{}", def.variants[variant_index].fields[field.index()].ident)
764-
}
763+
ProjectionElem::Downcast(def, variant_index) => format!(
764+
"{}",
765+
def.variants[variant_index].fields[field.index()].ident
766+
),
765767
ProjectionElem::Field(_, field_type) => {
766768
self.describe_field_from_ty(&field_type, field)
767769
}

src/librustc_mir/borrow_check/mod.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -1050,7 +1050,8 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
10501050
this.report_borrowed_value_does_not_live_long_enough(
10511051
context,
10521052
borrow,
1053-
place_span.1,
1053+
place_span,
1054+
Some(kind),
10541055
);
10551056
}
10561057
WriteKind::Mutate => {
@@ -1328,7 +1329,8 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
13281329
self.report_borrowed_value_does_not_live_long_enough(
13291330
context,
13301331
borrow,
1331-
span,
1332+
(place, span),
1333+
None,
13321334
)
13331335
}
13341336
}

0 commit comments

Comments
 (0)