Skip to content

Commit 919f434

Browse files
committed
Rollup merge of rust-lang#55151 - ljedrz:cleanup_nll, r=estebank
Cleanup nll - improve allocations - improve `format!` calls - improve common patterns
2 parents 89b7ccf + ffecbc5 commit 919f434

File tree

6 files changed

+51
-54
lines changed

6 files changed

+51
-54
lines changed

src/librustc_mir/borrow_check/nll/constraint_generation.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ impl<'cg, 'cx, 'gcx, 'tcx> Visitor<'tcx> for ConstraintGeneration<'cg, 'cx, 'gcx
141141
if let Some(all_facts) = self.all_facts {
142142
if let Place::Local(temp) = place {
143143
if let Some(borrow_indices) = self.borrow_set.local_map.get(temp) {
144+
all_facts.killed.reserve(borrow_indices.len());
144145
for &borrow_index in borrow_indices {
145146
let location_index = self.location_table.mid_index(location);
146147
all_facts.killed.push((borrow_index, location_index));
@@ -164,7 +165,9 @@ impl<'cg, 'cx, 'gcx, 'tcx> Visitor<'tcx> for ConstraintGeneration<'cg, 'cx, 'gcx
164165
self.location_table.mid_index(location),
165166
));
166167

167-
for successor_block in terminator.successors() {
168+
let successor_blocks = terminator.successors();
169+
all_facts.cfg_edge.reserve(successor_blocks.size_hint().0);
170+
for successor_block in successor_blocks {
168171
all_facts.cfg_edge.push((
169172
self.location_table.mid_index(location),
170173
self.location_table

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

Lines changed: 34 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,9 @@ impl<'tcx> BorrowExplanation<'tcx> {
8787
// Otherwise, just report the whole type (and use
8888
// the intentionally fuzzy phrase "destructor")
8989
ty::Closure(..) =>
90-
("destructor", format!("closure")),
90+
("destructor", "closure".to_owned()),
9191
ty::Generator(..) =>
92-
("destructor", format!("generator")),
92+
("destructor", "generator".to_owned()),
9393

9494
_ => ("destructor", format!("type `{}`", local_decl.ty)),
9595
};
@@ -279,9 +279,8 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
279279
pending_locations.push(target.start_location());
280280
},
281281
TerminatorKind::SwitchInt { ref targets, .. } => {
282-
for target in targets {
283-
pending_locations.push(target.start_location());
284-
}
282+
pending_locations.extend(
283+
targets.into_iter().map(|target| target.start_location()));
285284
},
286285
TerminatorKind::Drop { target, unwind, .. } |
287286
TerminatorKind::DropAndReplace { target, unwind, .. } |
@@ -303,9 +302,8 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
303302
},
304303
TerminatorKind::FalseEdges { real_target, ref imaginary_targets, .. } => {
305304
pending_locations.push(real_target.start_location());
306-
for target in imaginary_targets {
307-
pending_locations.push(target.start_location());
308-
}
305+
pending_locations.extend(
306+
imaginary_targets.into_iter().map(|target| target.start_location()));
309307
},
310308
_ => {},
311309
}
@@ -441,17 +439,17 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
441439
Operand::Move(Place::Local(from)) if *from == target => {
442440
debug!("was_captured_by_trait_object: ty={:?}", ty);
443441
// Check the type for a trait object.
444-
match ty.sty {
442+
return match ty.sty {
445443
// `&dyn Trait`
446-
ty::TyKind::Ref(_, ty, _) if ty.is_trait() => return true,
444+
ty::TyKind::Ref(_, ty, _) if ty.is_trait() => true,
447445
// `Box<dyn Trait>`
448446
_ if ty.is_box() && ty.boxed_ty().is_trait() =>
449-
return true,
447+
true,
450448
// `dyn Trait`
451-
_ if ty.is_trait() => return true,
449+
_ if ty.is_trait() => true,
452450
// Anything else.
453-
_ => return false,
454-
}
451+
_ => false,
452+
};
455453
},
456454
_ => return false,
457455
},
@@ -466,32 +464,29 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
466464
let terminator = block.terminator();
467465
debug!("was_captured_by_trait_object: terminator={:?}", terminator);
468466

469-
match &terminator.kind {
470-
TerminatorKind::Call {
471-
destination: Some((Place::Local(dest), block)),
472-
args,
473-
..
474-
} => {
475-
debug!(
476-
"was_captured_by_trait_object: target={:?} dest={:?} args={:?}",
477-
target, dest, args
478-
);
479-
// Check if one of the arguments to this function is the target place.
480-
let found_target = args.iter().any(|arg| {
481-
if let Operand::Move(Place::Local(potential)) = arg {
482-
*potential == target
483-
} else {
484-
false
485-
}
486-
});
487-
488-
// If it is, follow this to the next block and update the target.
489-
if found_target {
490-
target = *dest;
491-
queue.push(block.start_location());
467+
if let TerminatorKind::Call {
468+
destination: Some((Place::Local(dest), block)),
469+
args,
470+
..
471+
} = &terminator.kind {
472+
debug!(
473+
"was_captured_by_trait_object: target={:?} dest={:?} args={:?}",
474+
target, dest, args
475+
);
476+
// Check if one of the arguments to this function is the target place.
477+
let found_target = args.iter().any(|arg| {
478+
if let Operand::Move(Place::Local(potential)) = arg {
479+
*potential == target
480+
} else {
481+
false
492482
}
493-
},
494-
_ => {},
483+
});
484+
485+
// If it is, follow this to the next block and update the target.
486+
if found_target {
487+
target = *dest;
488+
queue.push(block.start_location());
489+
}
495490
}
496491
}
497492

src/librustc_mir/borrow_check/nll/invalidation.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ pub(super) fn generate_invalidates<'cx, 'gcx, 'tcx>(
3535
mir: &Mir<'tcx>,
3636
borrow_set: &BorrowSet<'tcx>,
3737
) {
38-
if !all_facts.is_some() {
38+
if all_facts.is_none() {
3939
// Nothing to do if we don't have any facts
4040
return;
4141
}

src/librustc_mir/borrow_check/nll/region_infer/dump_mir.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,12 @@ impl<'tcx> RegionInferenceContext<'tcx> {
3636
let outlived_by = self.universal_region_relations.regions_outlived_by(region);
3737
writeln!(
3838
out,
39-
"| {r:rw$} | {c:cw$} | {ob}",
40-
r = format!("{:?}", region),
39+
"| {r:rw$?} | {c:cw$?} | {ob:?}",
40+
r = region,
4141
rw = REGION_WIDTH,
42-
c = format!("{:?}", classification),
42+
c = classification,
4343
cw = 8, // "External" at most
44-
ob = format!("{:?}", outlived_by)
44+
ob = outlived_by
4545
)?;
4646
}
4747
}
@@ -51,8 +51,8 @@ impl<'tcx> RegionInferenceContext<'tcx> {
5151
for region in self.regions() {
5252
writeln!(
5353
out,
54-
"| {r:rw$} | {ui:4?} | {v}",
55-
r = format!("{:?}", region),
54+
"| {r:rw$?} | {ui:4?} | {v}",
55+
r = region,
5656
rw = REGION_WIDTH,
5757
ui = self.region_universe(region),
5858
v = self.region_value_str(region),

src/librustc_mir/borrow_check/nll/region_infer/error_reporting/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -550,7 +550,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
550550
let span = infcx.tcx.def_span(*did);
551551
if let Ok(snippet) = infcx.tcx.sess.source_map().span_to_snippet(span) {
552552
let suggestable_fr_name = if fr_name.was_named() {
553-
format!("{}", fr_name)
553+
fr_name.to_string()
554554
} else {
555555
"'_".to_string()
556556
};

src/librustc_mir/borrow_check/nll/region_infer/error_reporting/region_name.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -462,9 +462,8 @@ impl<'tcx> RegionInferenceContext<'tcx> {
462462
argument_hir_ty: &hir::Ty,
463463
counter: &mut usize,
464464
) -> Option<RegionName> {
465-
let search_stack: &mut Vec<(Ty<'tcx>, &hir::Ty)> = &mut Vec::new();
466-
467-
search_stack.push((argument_ty, argument_hir_ty));
465+
let search_stack: &mut Vec<(Ty<'tcx>, &hir::Ty)> =
466+
&mut vec![(argument_ty, argument_hir_ty)];
468467

469468
while let Some((ty, hir_ty)) = search_stack.pop() {
470469
match (&ty.sty, &hir_ty.node) {
@@ -567,10 +566,10 @@ impl<'tcx> RegionInferenceContext<'tcx> {
567566
| hir::LifetimeName::Underscore => {
568567
let region_name = self.synthesize_region_name(counter);
569568
let ampersand_span = lifetime.span;
570-
return Some(RegionName {
569+
Some(RegionName {
571570
name: region_name,
572571
source: RegionNameSource::MatchedAdtAndSegment(ampersand_span),
573-
});
572+
})
574573
}
575574

576575
hir::LifetimeName::Implicit => {
@@ -585,7 +584,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
585584
// T>`. We don't consider this a match; instead we let
586585
// the "fully elaborated" type fallback above handle
587586
// it.
588-
return None;
587+
None
589588
}
590589
}
591590
}

0 commit comments

Comments
 (0)