@@ -538,14 +538,15 @@ impl<'o, 'gcx: 'tcx, 'tcx> AstConv<'gcx, 'tcx>+'o {
538
538
/// Returns the appropriate lifetime to use for any output lifetimes
539
539
/// (if one exists) and a vector of the (pattern, number of lifetimes)
540
540
/// corresponding to each input type/pattern.
541
- fn find_implied_output_region < F > ( & self ,
541
+ fn find_implied_output_region < I > ( & self ,
542
542
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 >
545
545
{
546
546
let tcx = self . tcx ( ) ;
547
- let mut lifetimes_for_params = Vec :: new ( ) ;
547
+ let mut lifetimes_for_params = Vec :: with_capacity ( input_tys . len ( ) ) ;
548
548
let mut possible_implied_output_region = None ;
549
+ let mut lifetimes = 0 ;
549
550
550
551
for input_type in input_tys. iter ( ) {
551
552
let mut regions = FxHashSet ( ) ;
@@ -554,7 +555,9 @@ impl<'o, 'gcx: 'tcx, 'tcx> AstConv<'gcx, 'tcx>+'o {
554
555
debug ! ( "find_implied_output_regions: collected {:?} from {:?} \
555
556
have_bound_regions={:?}", & regions, input_type, have_bound_regions) ;
556
557
557
- if regions. len ( ) == 1 {
558
+ lifetimes += regions. len ( ) ;
559
+
560
+ if lifetimes == 1 && regions. len ( ) == 1 {
558
561
// there's a chance that the unique lifetime of this
559
562
// iteration will be the appropriate lifetime for output
560
563
// parameters, so lets store it.
@@ -571,12 +574,12 @@ impl<'o, 'gcx: 'tcx, 'tcx> AstConv<'gcx, 'tcx>+'o {
571
574
} ) ;
572
575
}
573
576
574
- if lifetimes_for_params . iter ( ) . map ( |e| e . lifetime_count ) . sum :: < usize > ( ) == 1 {
577
+ if lifetimes == 1 {
575
578
Ok ( * possible_implied_output_region. unwrap ( ) )
576
579
} else {
577
580
// Fill in the expensive `name` fields now that we know they're
578
581
// 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) {
580
583
info. name = input_pat;
581
584
}
582
585
Err ( Some ( lifetimes_for_params) )
@@ -615,8 +618,7 @@ impl<'o, 'gcx: 'tcx, 'tcx> AstConv<'gcx, 'tcx>+'o {
615
618
let inputs = self . tcx ( ) . mk_type_list ( data. inputs . iter ( ) . map ( |a_t| {
616
619
self . ast_ty_arg_to_ty ( & binding_rscope, None , region_substs, a_t)
617
620
} ) ) ;
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 ( ) ) ;
620
622
let implied_output_region = self . find_implied_output_region ( & inputs, input_params) ;
621
623
622
624
let ( output, output_span) = match data. output {
@@ -1777,14 +1779,10 @@ impl<'o, 'gcx: 'tcx, 'tcx> AstConv<'gcx, 'tcx>+'o {
1777
1779
let implied_output_region = match explicit_self {
1778
1780
Some ( ExplicitSelf :: ByReference ( region, _) ) => Ok ( * region) ,
1779
1781
_ => {
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
+
1788
1786
}
1789
1787
} ;
1790
1788
0 commit comments