]> git.lizzy.rs Git - rust.git/commitdiff
Improve code
authorGuillaume Gomez <guillaume1.gomez@gmail.com>
Sat, 21 Jul 2018 11:35:29 +0000 (13:35 +0200)
committerGuillaume Gomez <guillaume1.gomez@gmail.com>
Sun, 22 Jul 2018 19:03:21 +0000 (21:03 +0200)
src/librustdoc/clean/auto_trait.rs
src/librustdoc/html/render.rs
src/test/rustdoc/generic-impl.rs
src/test/rustdoc/synthetic_auto/basic.rs
src/test/rustdoc/synthetic_auto/manual.rs

index f466183bec80a24835ec0f87413760c1ca861aa8..a4f9444e355acacce27a140c71e16acd47baccc5 100644 (file)
@@ -10,7 +10,7 @@
 
 use rustc::hir;
 use rustc::traits::{self, auto_trait as auto};
-use rustc::ty::{ToPredicate, TypeFoldable};
+use rustc::ty::{self, ToPredicate, TypeFoldable};
 use rustc::ty::subst::Subst;
 use rustc::infer::InferOk;
 use std::fmt::Debug;
@@ -80,6 +80,33 @@ pub fn get_with_node_id(&self, id: ast::NodeId, name: String) -> Vec<Item> {
         self.get_auto_trait_impls(did, &def_ctor, Some(name))
     }
 
+    fn get_real_ty<F>(&self, def_id: DefId, def_ctor: &F, real_name: &Option<Ident>,
+                      generics: &ty::Generics) -> hir::Ty
+    where F: Fn(DefId) -> Def {
+        let path = get_path_for_type(self.cx.tcx, def_id, def_ctor);
+        let mut segments = path.segments.into_vec();
+        let last = segments.pop().unwrap();
+
+        segments.push(hir::PathSegment::new(
+            real_name.unwrap_or(last.ident),
+            self.generics_to_path_params(generics.clone()),
+            false,
+        ));
+
+        let new_path = hir::Path {
+            span: path.span,
+            def: path.def,
+            segments: HirVec::from_vec(segments),
+        };
+
+        hir::Ty {
+            id: ast::DUMMY_NODE_ID,
+            node: hir::TyKind::Path(hir::QPath::Resolved(None, P(new_path))),
+            span: DUMMY_SP,
+            hir_id: hir::DUMMY_HIR_ID,
+        }
+    }
+
     pub fn get_auto_trait_impls<F>(
         &self,
         def_id: DefId,
@@ -140,7 +167,8 @@ pub fn get_auto_trait_impls<F>(
                             // Require the type the impl is implemented on to match
                             // our type, and ignore the impl if there was a mismatch.
                             let cause = traits::ObligationCause::dummy();
-                            let eq_result = infcx.at(&cause, param_env).eq(trait_ref.self_ty(), ty2);
+                            let eq_result = infcx.at(&cause, param_env)
+                                                 .eq(trait_ref.self_ty(), ty2);
                             if let Ok(InferOk { value: (), obligations }) = eq_result {
                                 // FIXME(eddyb) ignoring `obligations` might cause false positives.
                                 drop(obligations);
@@ -156,36 +184,18 @@ pub fn get_auto_trait_impls<F>(
                                 self.cx.generated_synthetics.borrow_mut()
                                                             .insert((def_id, trait_def_id));
                                 let trait_ = hir::TraitRef {
-                                    path: get_path_for_type(infcx.tcx, trait_def_id, hir::def::Def::Trait),
+                                    path: get_path_for_type(infcx.tcx,
+                                                            trait_def_id,
+                                                            hir::def::Def::Trait),
                                     ref_id: ast::DUMMY_NODE_ID,
                                 };
-                                let provided_trait_methods = infcx.tcx.provided_trait_methods(impl_def_id)
-                                                                      .into_iter()
-                                                                      .map(|meth| meth.ident.to_string())
-                                                                      .collect();
-
-                                let path = get_path_for_type(self.cx.tcx, def_id, def_ctor);
-                                let mut segments = path.segments.into_vec();
-                                let last = segments.pop().unwrap();
-
-                                segments.push(hir::PathSegment::new(
-                                    real_name.unwrap_or(last.ident),
-                                    self.generics_to_path_params(generics.clone()),
-                                    false,
-                                ));
+                                let provided_trait_methods =
+                                    infcx.tcx.provided_trait_methods(impl_def_id)
+                                             .into_iter()
+                                             .map(|meth| meth.ident.to_string())
+                                             .collect();
 
-                                let new_path = hir::Path {
-                                    span: path.span,
-                                    def: path.def,
-                                    segments: HirVec::from_vec(segments),
-                                };
-
-                                let ty = hir::Ty {
-                                    id: ast::DUMMY_NODE_ID,
-                                    node: hir::Ty_::TyPath(hir::QPath::Resolved(None, P(new_path))),
-                                    span: DUMMY_SP,
-                                    hir_id: hir::DUMMY_HIR_ID,
-                                };
+                                let ty = self.get_real_ty(def_id, def_ctor, &real_name, generics);
 
                                 traits.push(Item {
                                     source: Span::empty(),
@@ -202,7 +212,9 @@ pub fn get_auto_trait_impls<F>(
                                         provided_trait_methods,
                                         trait_: Some(trait_.clean(self.cx)),
                                         for_: ty.clean(self.cx),
-                                        items: infcx.tcx.associated_items(impl_def_id).collect::<Vec<_>>().clean(self.cx),
+                                        items: infcx.tcx.associated_items(impl_def_id)
+                                                        .collect::<Vec<_>>()
+                                                        .clean(self.cx),
                                         polarity: None,
                                         synthetic: true,
                                     }),
@@ -312,31 +324,8 @@ fn get_auto_trait_impl_for<F>(
                 }
                 _ => unreachable!(),
             };
-
-            let path = get_path_for_type(self.cx.tcx, def_id, def_ctor);
-            let mut segments = path.segments.into_vec();
-            let last = segments.pop().unwrap();
-
             let real_name = name.map(|name| Ident::from_str(&name));
-
-            segments.push(hir::PathSegment::new(
-                real_name.unwrap_or(last.ident),
-                self.generics_to_path_params(generics.clone()),
-                false,
-            ));
-
-            let new_path = hir::Path {
-                span: path.span,
-                def: path.def,
-                segments: HirVec::from_vec(segments),
-            };
-
-            let ty = hir::Ty {
-                id: ast::DUMMY_NODE_ID,
-                node: hir::TyKind::Path(hir::QPath::Resolved(None, P(new_path))),
-                span: DUMMY_SP,
-                hir_id: hir::DUMMY_HIR_ID,
-            };
+            let ty = self.get_real_ty(def_id, def_ctor, &real_name, &generics);
 
             return Some(Item {
                 source: Span::empty(),
index 214f2da212eefb1a0660852667382d6b077964f5..fd39202b87c1a3ac22ce8634365500c07c810b30 100644 (file)
@@ -3628,16 +3628,7 @@ fn render_assoc_items(w: &mut fmt::Formatter,
 
         let (synthetic, concrete) = traits
             .iter()
-            .partition::<Vec<&&Impl>, _>(|t| t.inner_impl().synthetic);
-
-        // ugly hacks to remove duplicates.
-        let synthetic = synthetic.into_iter()
-                                 .filter(|t| {
-            !concrete.iter()
-                     .any(|tt| {
-                         tt.inner_impl().trait_.def_id() == t.inner_impl().trait_.def_id()
-                     })
-        }).collect::<Vec<_>>();
+            .partition::<Vec<_>, _>(|t| t.inner_impl().synthetic);
 
         struct RendererStruct<'a, 'b, 'c>(&'a Context, Vec<&'b &'b Impl>, &'c clean::Item);
 
index 68277835d2bbe2d5e7f6b65a3336a467912124b8..e69a3277d7f2bd88e7f8ff117d795636eb7e650f 100644 (file)
@@ -12,7 +12,7 @@
 
 use std::fmt;
 
-// @!has foo/struct.Bar.html 'impl<T> ToString for Bar'
+// @!has foo/struct.Bar.html '//h3[@id="impl-ToString"]//code' 'impl<T> ToString for Bar'
 pub struct Bar;
 
 // @has foo/struct.Foo.html '//h3[@id="impl-ToString"]//code' 'impl<T> ToString for Foo'
index 200747bf6cd2125fcf8d08e531cf4c6c33fad9a0..f9a6c2607cd771e5d8d93222a3b1eb9f898770c9 100644 (file)
@@ -12,7 +12,7 @@
 // @has - '//code' 'impl<T> Send for Foo<T> where T: Send'
 // @has - '//code' 'impl<T> Sync for Foo<T> where T: Sync'
 // @count - '//*[@id="implementations-list"]/*[@class="impl"]' 0
-// @count - '//*[@id="synthetic-implementations-list"]/*[@class="impl"]' 11
+// @count - '//*[@id="synthetic-implementations-list"]/*[@class="impl"]' 9
 pub struct Foo<T> {
     field: T,
 }
index 461b922e28edb9eeb2eb41cdce5556c3e79e63e0..8c7f9d8cc659e9f4a3fe4c95be26b7a68c73482c 100644 (file)
@@ -16,7 +16,7 @@
 // 'impl<T> Send for Foo<T>'
 //
 // @count - '//*[@id="implementations-list"]/*[@class="impl"]' 1
-// @count - '//*[@id="synthetic-implementations-list"]/*[@class="impl"]' 10
+// @count - '//*[@id="synthetic-implementations-list"]/*[@class="impl"]' 8
 pub struct Foo<T> {
     field: T,
 }