From d64c2ac01a831d9936d155fd38eaa53ffe672893 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Sat, 21 Jul 2018 13:35:29 +0200 Subject: [PATCH] Improve code --- src/librustdoc/clean/auto_trait.rs | 97 ++++++++++------------- src/librustdoc/html/render.rs | 11 +-- src/test/rustdoc/generic-impl.rs | 2 +- src/test/rustdoc/synthetic_auto/basic.rs | 2 +- src/test/rustdoc/synthetic_auto/manual.rs | 2 +- 5 files changed, 47 insertions(+), 67 deletions(-) diff --git a/src/librustdoc/clean/auto_trait.rs b/src/librustdoc/clean/auto_trait.rs index f466183bec8..a4f9444e355 100644 --- a/src/librustdoc/clean/auto_trait.rs +++ b/src/librustdoc/clean/auto_trait.rs @@ -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 { self.get_auto_trait_impls(did, &def_ctor, Some(name)) } + fn get_real_ty(&self, def_id: DefId, def_ctor: &F, real_name: &Option, + 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( &self, def_id: DefId, @@ -140,7 +167,8 @@ pub fn get_auto_trait_impls( // 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( 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( provided_trait_methods, trait_: Some(trait_.clean(self.cx)), for_: ty.clean(self.cx), - items: infcx.tcx.associated_items(impl_def_id).collect::>().clean(self.cx), + items: infcx.tcx.associated_items(impl_def_id) + .collect::>() + .clean(self.cx), polarity: None, synthetic: true, }), @@ -312,31 +324,8 @@ fn get_auto_trait_impl_for( } _ => 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(), diff --git a/src/librustdoc/html/render.rs b/src/librustdoc/html/render.rs index 214f2da212e..fd39202b87c 100644 --- a/src/librustdoc/html/render.rs +++ b/src/librustdoc/html/render.rs @@ -3628,16 +3628,7 @@ fn render_assoc_items(w: &mut fmt::Formatter, let (synthetic, concrete) = traits .iter() - .partition::, _>(|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::>(); + .partition::, _>(|t| t.inner_impl().synthetic); struct RendererStruct<'a, 'b, 'c>(&'a Context, Vec<&'b &'b Impl>, &'c clean::Item); diff --git a/src/test/rustdoc/generic-impl.rs b/src/test/rustdoc/generic-impl.rs index 68277835d2b..e69a3277d7f 100644 --- a/src/test/rustdoc/generic-impl.rs +++ b/src/test/rustdoc/generic-impl.rs @@ -12,7 +12,7 @@ use std::fmt; -// @!has foo/struct.Bar.html 'impl ToString for Bar' +// @!has foo/struct.Bar.html '//h3[@id="impl-ToString"]//code' 'impl ToString for Bar' pub struct Bar; // @has foo/struct.Foo.html '//h3[@id="impl-ToString"]//code' 'impl ToString for Foo' diff --git a/src/test/rustdoc/synthetic_auto/basic.rs b/src/test/rustdoc/synthetic_auto/basic.rs index 200747bf6cd..f9a6c2607cd 100644 --- a/src/test/rustdoc/synthetic_auto/basic.rs +++ b/src/test/rustdoc/synthetic_auto/basic.rs @@ -12,7 +12,7 @@ // @has - '//code' 'impl Send for Foo where T: Send' // @has - '//code' 'impl Sync for Foo 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 { field: T, } diff --git a/src/test/rustdoc/synthetic_auto/manual.rs b/src/test/rustdoc/synthetic_auto/manual.rs index 461b922e28e..8c7f9d8cc65 100644 --- a/src/test/rustdoc/synthetic_auto/manual.rs +++ b/src/test/rustdoc/synthetic_auto/manual.rs @@ -16,7 +16,7 @@ // 'impl Send for Foo' // // @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 { field: T, } -- 2.44.0