]> git.lizzy.rs Git - rust.git/commitdiff
Refactor a few push loops to iterators in librustc
authorljedrz <ljedrz@gmail.com>
Mon, 16 Jul 2018 17:35:45 +0000 (19:35 +0200)
committerljedrz <ljedrz@gmail.com>
Fri, 20 Jul 2018 19:57:12 +0000 (21:57 +0200)
src/librustc/infer/mod.rs
src/librustc/middle/dead.rs
src/librustc_codegen_llvm/abi.rs
src/librustc_typeck/astconv.rs

index 5b5ae6473f84011cdf3215cc4d3a7e8e532d19f1..9283705b7b78e89dd5f23ff88939774b1dd43c0c 100644 (file)
@@ -562,36 +562,25 @@ pub fn type_is_unconstrained_numeric(&'a self, ty: Ty) -> UnconstrainedNumeric {
     }
 
     pub fn unsolved_variables(&self) -> Vec<Ty<'tcx>> {
-        let mut variables = Vec::new();
-
-        {
-            let mut type_variables = self.type_variables.borrow_mut();
-            variables.extend(
-                type_variables
-                    .unsolved_variables()
-                    .into_iter()
-                    .map(|t| self.tcx.mk_var(t)));
-        }
-
-        {
-            let mut int_unification_table = self.int_unification_table.borrow_mut();
-            variables.extend(
+        let mut type_variables = self.type_variables.borrow_mut();
+        let mut int_unification_table = self.int_unification_table.borrow_mut();
+        let mut float_unification_table = self.float_unification_table.borrow_mut();
+
+        type_variables
+            .unsolved_variables()
+            .into_iter()
+            .map(|t| self.tcx.mk_var(t))
+            .chain(
                 (0..int_unification_table.len())
                     .map(|i| ty::IntVid { index: i as u32 })
                     .filter(|&vid| int_unification_table.probe_value(vid).is_none())
-                    .map(|v| self.tcx.mk_int_var(v)));
-        }
-
-        {
-            let mut float_unification_table = self.float_unification_table.borrow_mut();
-            variables.extend(
+                    .map(|v| self.tcx.mk_int_var(v))
+            ).chain(
                 (0..float_unification_table.len())
                     .map(|i| ty::FloatVid { index: i as u32 })
                     .filter(|&vid| float_unification_table.probe_value(vid).is_none())
-                    .map(|v| self.tcx.mk_float_var(v)));
-        }
-
-        return variables;
+                    .map(|v| self.tcx.mk_float_var(v))
+            ).collect()
     }
 
     fn combine_fields(&'a self, trace: TypeTrace<'tcx>, param_env: ty::ParamEnv<'tcx>)
index 180469a5d848ad4d5f099d77366da6a2717f037b..af57f9032f5241b8a82c9db767875098c2d260df 100644 (file)
@@ -396,16 +396,12 @@ fn visit_impl_item(&mut self, _item: &hir::ImplItem) {
 fn create_and_seed_worklist<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
                                       access_levels: &privacy::AccessLevels,
                                       krate: &hir::Crate)
-                                      -> Vec<ast::NodeId> {
-    let mut worklist = Vec::new();
-    for (id, _) in &access_levels.map {
-        worklist.push(*id);
-    }
-
-    // Seed entry point
-    if let Some((id, _, _)) = *tcx.sess.entry_fn.borrow() {
-        worklist.push(id);
-    }
+                                      -> Vec<ast::NodeId>
+{
+    let worklist = access_levels.map.iter().map(|(&id, _)| id).chain(
+        // Seed entry point
+        tcx.sess.entry_fn.borrow().map(|(id, _, _)| id)
+    ).collect::<Vec<_>>();
 
     // Seed implemented trait items
     let mut life_seeder = LifeSeeder {
index b1502902079b9f2c81d4f6280384ba375540f7db..f1a6e9913d6bdfc0cc81161af8a3912c32d2b2c1 100644 (file)
@@ -565,7 +565,13 @@ fn adjust_for_abi(&mut self,
     }
 
     fn llvm_type(&self, cx: &CodegenCx<'a, 'tcx>) -> Type {
-        let mut llargument_tys = Vec::new();
+        let args_capacity: usize = self.args.iter().map(|arg|
+            if arg.pad.is_some() { 1 } else { 0 } +
+            if let PassMode::Pair(_, _) = arg.mode { 2 } else { 1 }
+        ).sum();
+        let mut llargument_tys = Vec::with_capacity(
+            if let PassMode::Indirect(_) = self.ret.mode { 1 } else { 0 } + args_capacity
+        );
 
         let llreturn_ty = match self.ret.mode {
             PassMode::Ignore => Type::void(cx),
index 5e38c0bbcb4a3c5e6670409ea3ccd864e34109f4..88bcd7f0edf38351f62e7acd5a74851324c871a1 100644 (file)
@@ -1464,35 +1464,34 @@ impl<'a, 'gcx, 'tcx> Bounds<'tcx> {
     pub fn predicates(&self, tcx: TyCtxt<'a, 'gcx, 'tcx>, param_ty: Ty<'tcx>)
                       -> Vec<ty::Predicate<'tcx>>
     {
-        let mut vec = Vec::new();
-
         // If it could be sized, and is, add the sized predicate
-        if self.implicitly_sized {
-            if let Some(sized) = tcx.lang_items().sized_trait() {
+        let sized_predicate = if self.implicitly_sized {
+            tcx.lang_items().sized_trait().map(|sized| {
                 let trait_ref = ty::TraitRef {
                     def_id: sized,
                     substs: tcx.mk_substs_trait(param_ty, &[])
                 };
-                vec.push(trait_ref.to_predicate());
-            }
-        }
-
-        for &region_bound in &self.region_bounds {
-            // account for the binder being introduced below; no need to shift `param_ty`
-            // because, at present at least, it can only refer to early-bound regions
-            let region_bound = tcx.mk_region(ty::fold::shift_region(*region_bound, 1));
-            vec.push(
-                ty::Binder::dummy(ty::OutlivesPredicate(param_ty, region_bound)).to_predicate());
-        }
-
-        for bound_trait_ref in &self.trait_bounds {
-            vec.push(bound_trait_ref.to_predicate());
-        }
-
-        for projection in &self.projection_bounds {
-            vec.push(projection.to_predicate());
-        }
+                trait_ref.to_predicate()
+            })
+        } else {
+            None
+        };
 
-        vec
+        sized_predicate.into_iter().chain(
+            self.region_bounds.iter().map(|&region_bound| {
+                // account for the binder being introduced below; no need to shift `param_ty`
+                // because, at present at least, it can only refer to early-bound regions
+                let region_bound = tcx.mk_region(ty::fold::shift_region(*region_bound, 1));
+                ty::Binder::dummy(ty::OutlivesPredicate(param_ty, region_bound)).to_predicate()
+            }).chain(
+                self.trait_bounds.iter().map(|bound_trait_ref| {
+                    bound_trait_ref.to_predicate()
+                })
+            ).chain(
+                self.projection_bounds.iter().map(|projection| {
+                    projection.to_predicate()
+                })
+            )
+        ).collect()
     }
 }