]> git.lizzy.rs Git - rust.git/commitdiff
Deduplicate some code
authorMatthew Jasper <mjjasper1@gmail.com>
Mon, 5 Oct 2020 16:19:24 +0000 (17:19 +0100)
committerMatthew Jasper <mjjasper1@gmail.com>
Tue, 6 Oct 2020 10:19:33 +0000 (11:19 +0100)
compiler/rustc_traits/src/chalk/db.rs

index 5553d4585244d681c1a39ffdabb63bf4b7ede0d3..223635e9aecd91aa70f80c259fd002011b3acd4c 100644 (file)
@@ -50,6 +50,19 @@ fn where_clauses_for(
             .map(|wc| wc.fold_with(&mut regions_substitutor))
             .filter_map(|wc| LowerInto::<Option<chalk_ir::QuantifiedWhereClause<RustInterner<'tcx>>>>::lower_into(wc, &self.interner)).collect()
     }
+
+    fn bounds_for<T>(&self, def_id: DefId, bound_vars: SubstsRef<'tcx>) -> Vec<T>
+    where
+        ty::Predicate<'tcx>: LowerInto<'tcx, std::option::Option<T>>,
+    {
+        self.interner
+            .tcx
+            .explicit_item_bounds(def_id)
+            .iter()
+            .map(|(bound, _)| bound.subst(self.interner.tcx, &bound_vars))
+            .filter_map(|bound| LowerInto::<Option<_>>::lower_into(bound, &self.interner))
+            .collect()
+    }
 }
 
 impl<'tcx> chalk_solve::RustIrDatabase<RustInterner<'tcx>> for RustIrDatabase<'tcx> {
@@ -75,19 +88,7 @@ fn associated_ty_data(
         let binders = binders_for(&self.interner, bound_vars);
 
         let where_clauses = self.where_clauses_for(def_id, bound_vars);
-
-        let bounds = self
-            .interner
-            .tcx
-            .explicit_item_bounds(def_id)
-            .iter()
-            .map(|(bound, _)| bound.subst(self.interner.tcx, &bound_vars))
-            .filter_map(|bound| {
-                LowerInto::<
-                        Option<chalk_solve::rust_ir::QuantifiedInlineBound<RustInterner<'tcx>>>,
-                    >::lower_into(bound, &self.interner)
-            })
-            .collect();
+        let bounds = self.bounds_for(def_id, bound_vars);
 
         Arc::new(chalk_solve::rust_ir::AssociatedTyDatum {
             trait_id: chalk_ir::TraitId(trait_def_id),
@@ -453,17 +454,7 @@ fn opaque_ty_data(
         let bound_vars = bound_vars_for_item(self.interner.tcx, opaque_ty_id.0);
         let binders = binders_for(&self.interner, bound_vars);
         let where_clauses = self.where_clauses_for(opaque_ty_id.0, bound_vars);
-
-        let bounds: Vec<_> = self
-            .interner
-            .tcx
-            .explicit_item_bounds(opaque_ty_id.0)
-            .iter()
-            .map(|(bound, _)| bound.subst(self.interner.tcx, &bound_vars))
-            .filter_map(|bound| {
-                LowerInto::<Option<chalk_ir::QuantifiedWhereClause<RustInterner<'tcx>>>>::lower_into(bound, &self.interner)
-            })
-            .collect();
+        let bounds = self.bounds_for(opaque_ty_id.0, bound_vars);
 
         let value = chalk_solve::rust_ir::OpaqueTyDatumBound {
             bounds: chalk_ir::Binders::new(binders.clone(), bounds),