1
Fork 0

Improve code

This commit is contained in:
Guillaume Gomez 2018-07-21 13:35:29 +02:00
parent 6b830ec23e
commit d64c2ac01a
5 changed files with 47 additions and 67 deletions

View file

@ -10,7 +10,7 @@
use rustc::hir; use rustc::hir;
use rustc::traits::{self, auto_trait as auto}; 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::ty::subst::Subst;
use rustc::infer::InferOk; use rustc::infer::InferOk;
use std::fmt::Debug; use std::fmt::Debug;
@ -80,6 +80,33 @@ impl<'a, 'tcx, 'rcx> AutoTraitFinder<'a, 'tcx, 'rcx> {
self.get_auto_trait_impls(did, &def_ctor, Some(name)) 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>( pub fn get_auto_trait_impls<F>(
&self, &self,
def_id: DefId, def_id: DefId,
@ -140,7 +167,8 @@ impl<'a, 'tcx, 'rcx> AutoTraitFinder<'a, 'tcx, 'rcx> {
// Require the type the impl is implemented on to match // Require the type the impl is implemented on to match
// our type, and ignore the impl if there was a mismatch. // our type, and ignore the impl if there was a mismatch.
let cause = traits::ObligationCause::dummy(); 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 { if let Ok(InferOk { value: (), obligations }) = eq_result {
// FIXME(eddyb) ignoring `obligations` might cause false positives. // FIXME(eddyb) ignoring `obligations` might cause false positives.
drop(obligations); drop(obligations);
@ -156,36 +184,18 @@ impl<'a, 'tcx, 'rcx> AutoTraitFinder<'a, 'tcx, 'rcx> {
self.cx.generated_synthetics.borrow_mut() self.cx.generated_synthetics.borrow_mut()
.insert((def_id, trait_def_id)); .insert((def_id, trait_def_id));
let trait_ = hir::TraitRef { 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, ref_id: ast::DUMMY_NODE_ID,
}; };
let provided_trait_methods = infcx.tcx.provided_trait_methods(impl_def_id) let provided_trait_methods =
.into_iter() infcx.tcx.provided_trait_methods(impl_def_id)
.map(|meth| meth.ident.to_string()) .into_iter()
.collect(); .map(|meth| meth.ident.to_string())
.collect();
let path = get_path_for_type(self.cx.tcx, def_id, def_ctor); let ty = self.get_real_ty(def_id, def_ctor, &real_name, generics);
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),
};
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,
};
traits.push(Item { traits.push(Item {
source: Span::empty(), source: Span::empty(),
@ -202,7 +212,9 @@ impl<'a, 'tcx, 'rcx> AutoTraitFinder<'a, 'tcx, 'rcx> {
provided_trait_methods, provided_trait_methods,
trait_: Some(trait_.clean(self.cx)), trait_: Some(trait_.clean(self.cx)),
for_: ty.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, polarity: None,
synthetic: true, synthetic: true,
}), }),
@ -312,31 +324,8 @@ impl<'a, 'tcx, 'rcx> AutoTraitFinder<'a, 'tcx, 'rcx> {
} }
_ => unreachable!(), _ => 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)); let real_name = name.map(|name| Ident::from_str(&name));
let ty = self.get_real_ty(def_id, def_ctor, &real_name, &generics);
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,
};
return Some(Item { return Some(Item {
source: Span::empty(), source: Span::empty(),

View file

@ -3628,16 +3628,7 @@ fn render_assoc_items(w: &mut fmt::Formatter,
let (synthetic, concrete) = traits let (synthetic, concrete) = traits
.iter() .iter()
.partition::<Vec<&&Impl>, _>(|t| t.inner_impl().synthetic); .partition::<Vec<_>, _>(|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<_>>();
struct RendererStruct<'a, 'b, 'c>(&'a Context, Vec<&'b &'b Impl>, &'c clean::Item); struct RendererStruct<'a, 'b, 'c>(&'a Context, Vec<&'b &'b Impl>, &'c clean::Item);

View file

@ -12,7 +12,7 @@
use std::fmt; 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; pub struct Bar;
// @has foo/struct.Foo.html '//h3[@id="impl-ToString"]//code' 'impl<T> ToString for Foo' // @has foo/struct.Foo.html '//h3[@id="impl-ToString"]//code' 'impl<T> ToString for Foo'

View file

@ -12,7 +12,7 @@
// @has - '//code' 'impl<T> Send for Foo<T> where T: Send' // @has - '//code' 'impl<T> Send for Foo<T> where T: Send'
// @has - '//code' 'impl<T> Sync for Foo<T> where T: Sync' // @has - '//code' 'impl<T> Sync for Foo<T> where T: Sync'
// @count - '//*[@id="implementations-list"]/*[@class="impl"]' 0 // @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> { pub struct Foo<T> {
field: T, field: T,
} }

View file

@ -16,7 +16,7 @@
// 'impl<T> Send for Foo<T>' // 'impl<T> Send for Foo<T>'
// //
// @count - '//*[@id="implementations-list"]/*[@class="impl"]' 1 // @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> { pub struct Foo<T> {
field: T, field: T,
} }