Skip to content

Commit b5d0f90

Browse files
committed
Auto merge of #38036 - Mark-Simulacrum:polish-2, r=nagisa,eddyb
Simplify calling find_implied_output_region. @nnethercote added the optimization that find_implied_output_region takes a closure as an optimization in #37014, but passing an iterator is simpler, and more ergonomic for callers.
2 parents 1842efb + cc6edb2 commit b5d0f90

File tree

1 file changed

+15
-17
lines changed

1 file changed

+15
-17
lines changed

src/librustc_typeck/astconv.rs

+15-17
Original file line numberDiff line numberDiff line change
@@ -538,14 +538,15 @@ impl<'o, 'gcx: 'tcx, 'tcx> AstConv<'gcx, 'tcx>+'o {
538538
/// Returns the appropriate lifetime to use for any output lifetimes
539539
/// (if one exists) and a vector of the (pattern, number of lifetimes)
540540
/// corresponding to each input type/pattern.
541-
fn find_implied_output_region<F>(&self,
541+
fn find_implied_output_region<I>(&self,
542542
input_tys: &[Ty<'tcx>],
543-
input_pats: F) -> ElidedLifetime
544-
where F: FnOnce() -> Vec<String>
543+
input_pats: I) -> ElidedLifetime
544+
where I: Iterator<Item=String>
545545
{
546546
let tcx = self.tcx();
547-
let mut lifetimes_for_params = Vec::new();
547+
let mut lifetimes_for_params = Vec::with_capacity(input_tys.len());
548548
let mut possible_implied_output_region = None;
549+
let mut lifetimes = 0;
549550

550551
for input_type in input_tys.iter() {
551552
let mut regions = FxHashSet();
@@ -554,7 +555,9 @@ impl<'o, 'gcx: 'tcx, 'tcx> AstConv<'gcx, 'tcx>+'o {
554555
debug!("find_implied_output_regions: collected {:?} from {:?} \
555556
have_bound_regions={:?}", &regions, input_type, have_bound_regions);
556557

557-
if regions.len() == 1 {
558+
lifetimes += regions.len();
559+
560+
if lifetimes == 1 && regions.len() == 1 {
558561
// there's a chance that the unique lifetime of this
559562
// iteration will be the appropriate lifetime for output
560563
// parameters, so lets store it.
@@ -571,12 +574,12 @@ impl<'o, 'gcx: 'tcx, 'tcx> AstConv<'gcx, 'tcx>+'o {
571574
});
572575
}
573576

574-
if lifetimes_for_params.iter().map(|e| e.lifetime_count).sum::<usize>() == 1 {
577+
if lifetimes == 1 {
575578
Ok(*possible_implied_output_region.unwrap())
576579
} else {
577580
// Fill in the expensive `name` fields now that we know they're
578581
// needed.
579-
for (info, input_pat) in lifetimes_for_params.iter_mut().zip(input_pats()) {
582+
for (info, input_pat) in lifetimes_for_params.iter_mut().zip(input_pats) {
580583
info.name = input_pat;
581584
}
582585
Err(Some(lifetimes_for_params))
@@ -615,8 +618,7 @@ impl<'o, 'gcx: 'tcx, 'tcx> AstConv<'gcx, 'tcx>+'o {
615618
let inputs = self.tcx().mk_type_list(data.inputs.iter().map(|a_t| {
616619
self.ast_ty_arg_to_ty(&binding_rscope, None, region_substs, a_t)
617620
}));
618-
let inputs_len = inputs.len();
619-
let input_params = || vec![String::new(); inputs_len];
621+
let input_params = iter::repeat(String::new()).take(inputs.len());
620622
let implied_output_region = self.find_implied_output_region(&inputs, input_params);
621623

622624
let (output, output_span) = match data.output {
@@ -1777,14 +1779,10 @@ impl<'o, 'gcx: 'tcx, 'tcx> AstConv<'gcx, 'tcx>+'o {
17771779
let implied_output_region = match explicit_self {
17781780
Some(ExplicitSelf::ByReference(region, _)) => Ok(*region),
17791781
_ => {
1780-
// `pat_to_string` is expensive and
1781-
// `find_implied_output_region` only needs its result when
1782-
// there's an error. So we wrap it in a closure to avoid
1783-
// calling it until necessary.
1784-
let arg_pats = || {
1785-
arg_params.iter().map(|a| pprust::pat_to_string(&a.pat)).collect()
1786-
};
1787-
self.find_implied_output_region(&arg_tys, arg_pats)
1782+
self.find_implied_output_region(&arg_tys,
1783+
arg_params.iter()
1784+
.map(|a| pprust::pat_to_string(&a.pat)))
1785+
17881786
}
17891787
};
17901788

0 commit comments

Comments
 (0)