Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit a12982c

Browse files
committedFeb 20, 2019
Run rustfmt
1 parent 801c3f0 commit a12982c

File tree

1 file changed

+155
-117
lines changed
  • src/librustc_mir/borrow_check/nll/explain_borrow

1 file changed

+155
-117
lines changed
 

‎src/librustc_mir/borrow_check/nll/explain_borrow/mod.rs

Lines changed: 155 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,14 @@ use std::collections::VecDeque;
22

33
use crate::borrow_check::borrow_set::BorrowData;
44
use crate::borrow_check::error_reporting::UseSpans;
5-
use crate::borrow_check::nll::ConstraintDescription;
65
use crate::borrow_check::nll::region_infer::{Cause, RegionName};
6+
use crate::borrow_check::nll::ConstraintDescription;
77
use crate::borrow_check::{Context, MirBorrowckCtxt, WriteKind};
8-
use rustc::ty::{self, TyCtxt};
98
use rustc::mir::{
10-
CastKind, ConstraintCategory, FakeReadCause, Local, Location, Mir, Operand,
11-
Place, Projection, ProjectionElem, Rvalue, Statement, StatementKind,
12-
TerminatorKind
9+
CastKind, ConstraintCategory, FakeReadCause, Local, Location, Mir, Operand, Place, Projection,
10+
ProjectionElem, Rvalue, Statement, StatementKind, TerminatorKind,
1311
};
12+
use rustc::ty::{self, TyCtxt};
1413
use rustc_data_structures::fx::FxHashSet;
1514
use rustc_errors::DiagnosticBuilder;
1615
use syntax_pos::Span;
@@ -63,50 +62,58 @@ impl BorrowExplanation {
6362
let message = match later_use_kind {
6463
LaterUseKind::TraitCapture => "borrow later captured here by trait object",
6564
LaterUseKind::ClosureCapture => "borrow later captured here by closure",
66-
LaterUseKind::Call => "borrow later used by call",
65+
LaterUseKind::Call => "borrow later used by call",
6766
LaterUseKind::FakeLetRead => "borrow later stored here",
6867
LaterUseKind::Other => "borrow later used here",
6968
};
7069
err.span_label(var_or_use_span, format!("{}{}", borrow_desc, message));
71-
},
70+
}
7271
BorrowExplanation::UsedLaterInLoop(later_use_kind, var_or_use_span) => {
7372
let message = match later_use_kind {
74-
LaterUseKind::TraitCapture =>
75-
"borrow captured here by trait object, in later iteration of loop",
76-
LaterUseKind::ClosureCapture =>
77-
"borrow captured here by closure, in later iteration of loop",
78-
LaterUseKind::Call => "borrow used by call, in later iteration of loop",
73+
LaterUseKind::TraitCapture => {
74+
"borrow captured here by trait object, in later iteration of loop"
75+
}
76+
LaterUseKind::ClosureCapture => {
77+
"borrow captured here by closure, in later iteration of loop"
78+
}
79+
LaterUseKind::Call => "borrow used by call, in later iteration of loop",
7980
LaterUseKind::FakeLetRead => "borrow later stored here",
8081
LaterUseKind::Other => "borrow used here, in later iteration of loop",
8182
};
8283
err.span_label(var_or_use_span, format!("{}{}", borrow_desc, message));
83-
},
84-
BorrowExplanation::UsedLaterWhenDropped { drop_loc, dropped_local,
85-
should_note_order } =>
86-
{
84+
}
85+
BorrowExplanation::UsedLaterWhenDropped {
86+
drop_loc,
87+
dropped_local,
88+
should_note_order,
89+
} => {
8790
let local_decl = &mir.local_decls[dropped_local];
8891
let (dtor_desc, type_desc) = match local_decl.ty.sty {
8992
// If type is an ADT that implements Drop, then
9093
// simplify output by reporting just the ADT name.
91-
ty::Adt(adt, _substs) if adt.has_dtor(tcx) && !adt.is_box() =>
92-
("`Drop` code", format!("type `{}`", tcx.item_path_str(adt.did))),
94+
ty::Adt(adt, _substs) if adt.has_dtor(tcx) && !adt.is_box() => (
95+
"`Drop` code",
96+
format!("type `{}`", tcx.item_path_str(adt.did)),
97+
),
9398

9499
// Otherwise, just report the whole type (and use
95100
// the intentionally fuzzy phrase "destructor")
96-
ty::Closure(..) =>
97-
("destructor", "closure".to_owned()),
98-
ty::Generator(..) =>
99-
("destructor", "generator".to_owned()),
101+
ty::Closure(..) => ("destructor", "closure".to_owned()),
102+
ty::Generator(..) => ("destructor", "generator".to_owned()),
100103

101104
_ => ("destructor", format!("type `{}`", local_decl.ty)),
102105
};
103106

104107
match local_decl.name {
105108
Some(local_name) => {
106-
let message =
107-
format!("{B}borrow might be used here, when `{LOC}` is dropped \
108-
and runs the {DTOR} for {TYPE}",
109-
B=borrow_desc, LOC=local_name, TYPE=type_desc, DTOR=dtor_desc);
109+
let message = format!(
110+
"{B}borrow might be used here, when `{LOC}` is dropped \
111+
and runs the {DTOR} for {TYPE}",
112+
B = borrow_desc,
113+
LOC = local_name,
114+
TYPE = type_desc,
115+
DTOR = dtor_desc
116+
);
110117
err.span_label(mir.source_info(drop_loc).span, message);
111118

112119
if should_note_order {
@@ -117,15 +124,22 @@ impl BorrowExplanation {
117124
}
118125
}
119126
None => {
120-
err.span_label(local_decl.source_info.span,
121-
format!("a temporary with access to the {B}borrow \
122-
is created here ...",
123-
B=borrow_desc));
124-
let message =
125-
format!("... and the {B}borrow might be used here, \
126-
when that temporary is dropped \
127-
and runs the {DTOR} for {TYPE}",
128-
B=borrow_desc, TYPE=type_desc, DTOR=dtor_desc);
127+
err.span_label(
128+
local_decl.source_info.span,
129+
format!(
130+
"a temporary with access to the {B}borrow \
131+
is created here ...",
132+
B = borrow_desc
133+
),
134+
);
135+
let message = format!(
136+
"... and the {B}borrow might be used here, \
137+
when that temporary is dropped \
138+
and runs the {DTOR} for {TYPE}",
139+
B = borrow_desc,
140+
TYPE = type_desc,
141+
DTOR = dtor_desc
142+
);
129143
err.span_label(mir.source_info(drop_loc).span, message);
130144

131145
if let Some(info) = &local_decl.is_block_tail {
@@ -149,7 +163,7 @@ impl BorrowExplanation {
149163
}
150164
}
151165
}
152-
},
166+
}
153167
BorrowExplanation::MustBeValidFor {
154168
category,
155169
span,
@@ -160,18 +174,28 @@ impl BorrowExplanation {
160174
region_name.highlight_region_name(err);
161175

162176
if let Some(desc) = opt_place_desc {
163-
err.span_label(span, format!(
164-
"{}requires that `{}` is borrowed for `{}`",
165-
category.description(), desc, region_name,
166-
));
177+
err.span_label(
178+
span,
179+
format!(
180+
"{}requires that `{}` is borrowed for `{}`",
181+
category.description(),
182+
desc,
183+
region_name,
184+
),
185+
);
167186
} else {
168-
err.span_label(span, format!(
169-
"{}requires that {}borrow lasts for `{}`",
170-
category.description(), borrow_desc, region_name,
171-
));
187+
err.span_label(
188+
span,
189+
format!(
190+
"{}requires that {}borrow lasts for `{}`",
191+
category.description(),
192+
borrow_desc,
193+
region_name,
194+
),
195+
);
172196
};
173-
},
174-
_ => {},
197+
}
198+
_ => {}
175199
}
176200
}
177201
}
@@ -217,10 +241,11 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
217241
region_sub
218242
);
219243

220-
match find_use::find(mir, regioncx, tcx, region_sub, context.loc) {
244+
match find_use::find(mir, regioncx, tcx, region_sub, context.loc) {
221245
Some(Cause::LiveVar(local, location)) => {
222246
let span = mir.source_info(location).span;
223-
let spans = self.move_spans(&Place::Local(local), location)
247+
let spans = self
248+
.move_spans(&Place::Local(local), location)
224249
.or_else(|| self.borrow_spans(span, location));
225250

226251
let borrow_location = context.loc;
@@ -236,55 +261,56 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
236261
}
237262
}
238263

239-
Some(Cause::DropVar(local, location)) => {
240-
let mut should_note_order = false;
241-
if mir.local_decls[local].name.is_some() {
242-
if let Some((WriteKind::StorageDeadOrDrop, place)) = kind_place {
243-
if let Place::Local(borrowed_local) = place {
244-
let dropped_local_scope = mir.local_decls[local].visibility_scope;
245-
let borrowed_local_scope =
246-
mir.local_decls[*borrowed_local].visibility_scope;
247-
248-
if mir.is_sub_scope(borrowed_local_scope, dropped_local_scope)
249-
&& local != *borrowed_local
250-
{
251-
should_note_order = true;
252-
}
253-
}
254-
}
255-
}
256-
257-
BorrowExplanation::UsedLaterWhenDropped {
258-
drop_loc: location,
259-
dropped_local: local,
260-
should_note_order,
261-
}
264+
Some(Cause::DropVar(local, location)) => {
265+
let mut should_note_order = false;
266+
if mir.local_decls[local].name.is_some() {
267+
if let Some((WriteKind::StorageDeadOrDrop, place)) = kind_place {
268+
if let Place::Local(borrowed_local) = place {
269+
let dropped_local_scope = mir.local_decls[local].visibility_scope;
270+
let borrowed_local_scope =
271+
mir.local_decls[*borrowed_local].visibility_scope;
272+
273+
if mir.is_sub_scope(borrowed_local_scope, dropped_local_scope)
274+
&& local != *borrowed_local
275+
{
276+
should_note_order = true;
277+
}
278+
}
279+
}
280+
}
281+
282+
BorrowExplanation::UsedLaterWhenDropped {
283+
drop_loc: location,
284+
dropped_local: local,
285+
should_note_order,
286+
}
262287
}
263288

264-
None => if let Some(region) = regioncx.to_error_region_vid(borrow_region_vid) {
265-
let (category, from_closure, span, region_name) = self
266-
.nonlexical_regioncx
267-
.free_region_constraint_info(
268-
self.mir,
269-
self.mir_def_id,
270-
self.infcx,
271-
borrow_region_vid,
272-
region,
273-
);
274-
if let Some(region_name) = region_name {
275-
let opt_place_desc = self.describe_place(&borrow.borrowed_place);
276-
BorrowExplanation::MustBeValidFor {
277-
category,
278-
from_closure,
279-
span,
280-
region_name,
281-
opt_place_desc,
289+
None => {
290+
if let Some(region) = regioncx.to_error_region_vid(borrow_region_vid) {
291+
let (category, from_closure, span, region_name) =
292+
self.nonlexical_regioncx.free_region_constraint_info(
293+
self.mir,
294+
self.mir_def_id,
295+
self.infcx,
296+
borrow_region_vid,
297+
region,
298+
);
299+
if let Some(region_name) = region_name {
300+
let opt_place_desc = self.describe_place(&borrow.borrowed_place);
301+
BorrowExplanation::MustBeValidFor {
302+
category,
303+
from_closure,
304+
span,
305+
region_name,
306+
opt_place_desc,
307+
}
308+
} else {
309+
BorrowExplanation::Unexplained
282310
}
283311
} else {
284312
BorrowExplanation::Unexplained
285313
}
286-
} else {
287-
BorrowExplanation::Unexplained
288314
}
289315
}
290316
}
@@ -427,27 +453,31 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
427453
&self,
428454
borrow: &BorrowData<'tcx>,
429455
use_spans: UseSpans,
430-
location: Location
456+
location: Location,
431457
) -> (LaterUseKind, Span) {
432458
match use_spans {
433459
UseSpans::ClosureUse { var_span, .. } => {
434460
// Used in a closure.
435461
(LaterUseKind::ClosureCapture, var_span)
436-
},
462+
}
437463
UseSpans::OtherUse(span) => {
438464
let block = &self.mir.basic_blocks()[location.block];
439465

440466
let kind = if let Some(&Statement {
441467
kind: StatementKind::FakeRead(FakeReadCause::ForLet, _),
442468
..
443-
}) = block.statements.get(location.statement_index) {
469+
}) = block.statements.get(location.statement_index)
470+
{
444471
LaterUseKind::FakeLetRead
445472
} else if self.was_captured_by_trait_object(borrow) {
446473
LaterUseKind::TraitCapture
447474
} else if location.statement_index == block.statements.len() {
448475
if let TerminatorKind::Call {
449-
ref func, from_hir_call: true, ..
450-
} = block.terminator().kind {
476+
ref func,
477+
from_hir_call: true,
478+
..
479+
} = block.terminator().kind
480+
{
451481
// Just point to the function, to reduce the chance of overlapping spans.
452482
let function_span = match func {
453483
Operand::Constant(c) => c.span,
@@ -458,7 +488,7 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
458488
} else {
459489
span
460490
}
461-
},
491+
}
462492
_ => span,
463493
};
464494
return (LaterUseKind::Call, function_span);
@@ -482,7 +512,10 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
482512
let location = borrow.reserve_location;
483513
let block = &self.mir[location.block];
484514
let stmt = block.statements.get(location.statement_index);
485-
debug!("was_captured_by_trait_object: location={:?} stmt={:?}", location, stmt);
515+
debug!(
516+
"was_captured_by_trait_object: location={:?} stmt={:?}",
517+
location, stmt
518+
);
486519

487520
// We make a `queue` vector that has the locations we want to visit. As of writing, this
488521
// will only ever have one item at any given time, but by using a vector, we can pop from
@@ -491,13 +524,17 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
491524
let mut target = if let Some(&Statement {
492525
kind: StatementKind::Assign(Place::Local(local), _),
493526
..
494-
}) = stmt {
527+
}) = stmt
528+
{
495529
local
496530
} else {
497531
return false;
498532
};
499533

500-
debug!("was_captured_by_trait: target={:?} queue={:?}", target, queue);
534+
debug!(
535+
"was_captured_by_trait: target={:?} queue={:?}",
536+
target, queue
537+
);
501538
while let Some(current_location) = queue.pop() {
502539
debug!("was_captured_by_trait: target={:?}", target);
503540
let block = &self.mir[current_location.block];
@@ -508,55 +545,55 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
508545
debug!("was_captured_by_trait_object: stmt={:?}", stmt);
509546

510547
// The only kind of statement that we care about is assignments...
511-
if let StatementKind::Assign(
512-
place,
513-
box rvalue,
514-
) = &stmt.kind {
548+
if let StatementKind::Assign(place, box rvalue) = &stmt.kind {
515549
let into = match place {
516550
Place::Local(into) => into,
517551
Place::Projection(box Projection {
518552
base: Place::Local(into),
519553
elem: ProjectionElem::Deref,
520554
}) => into,
521-
_ => {
555+
_ => {
522556
// Continue at the next location.
523557
queue.push(current_location.successor_within_block());
524558
continue;
525-
},
559+
}
526560
};
527561

528562
match rvalue {
529563
// If we see a use, we should check whether it is our data, and if so
530564
// update the place that we're looking for to that new place.
531565
Rvalue::Use(operand) => match operand {
532-
Operand::Copy(Place::Local(from)) |
533-
Operand::Move(Place::Local(from)) if *from == target => {
566+
Operand::Copy(Place::Local(from))
567+
| Operand::Move(Place::Local(from))
568+
if *from == target =>
569+
{
534570
target = *into;
535-
},
536-
_ => {},
571+
}
572+
_ => {}
537573
},
538574
// If we see a unsized cast, then if it is our data we should check
539575
// whether it is being cast to a trait object.
540576
Rvalue::Cast(CastKind::Unsize, operand, ty) => match operand {
541-
Operand::Copy(Place::Local(from)) |
542-
Operand::Move(Place::Local(from)) if *from == target => {
577+
Operand::Copy(Place::Local(from))
578+
| Operand::Move(Place::Local(from))
579+
if *from == target =>
580+
{
543581
debug!("was_captured_by_trait_object: ty={:?}", ty);
544582
// Check the type for a trait object.
545583
return match ty.sty {
546584
// `&dyn Trait`
547585
ty::TyKind::Ref(_, ty, _) if ty.is_trait() => true,
548586
// `Box<dyn Trait>`
549-
_ if ty.is_box() && ty.boxed_ty().is_trait() =>
550-
true,
587+
_ if ty.is_box() && ty.boxed_ty().is_trait() => true,
551588
// `dyn Trait`
552589
_ if ty.is_trait() => true,
553590
// Anything else.
554591
_ => false,
555592
};
556-
},
593+
}
557594
_ => return false,
558595
},
559-
_ => {},
596+
_ => {}
560597
}
561598
}
562599

@@ -571,7 +608,8 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
571608
destination: Some((Place::Local(dest), block)),
572609
args,
573610
..
574-
} = &terminator.kind {
611+
} = &terminator.kind
612+
{
575613
debug!(
576614
"was_captured_by_trait_object: target={:?} dest={:?} args={:?}",
577615
target, dest, args

0 commit comments

Comments
 (0)
Please sign in to comment.