1
Fork 0

add auto-impl for primitive type

This commit is contained in:
Guillaume Gomez 2018-05-07 14:01:53 +02:00
parent cb1ce7ddf8
commit 564511e58b
4 changed files with 63 additions and 16 deletions

View file

@ -35,10 +35,29 @@ impl<'a, 'tcx, 'rcx> AutoTraitFinder<'a, 'tcx, 'rcx> {
AdtKind::Enum => Def::Enum, AdtKind::Enum => Def::Enum,
AdtKind::Union => Def::Union, AdtKind::Union => Def::Union,
} }
_ => panic!("Unexpected type {:?}", def_id), ty::TyInt(_) |
ty::TyUint(_) |
ty::TyFloat(_) |
ty::TyStr |
ty::TyBool |
ty::TyChar => return self.get_auto_trait_impls(def_id, &move |_: DefId| {
match ty.sty {
ty::TyInt(x) => Def::PrimTy(hir::TyInt(x)),
ty::TyUint(x) => Def::PrimTy(hir::TyUint(x)),
ty::TyFloat(x) => Def::PrimTy(hir::TyFloat(x)),
ty::TyStr => Def::PrimTy(hir::TyStr),
ty::TyBool => Def::PrimTy(hir::TyBool),
ty::TyChar => Def::PrimTy(hir::TyChar),
_ => unreachable!(),
}
}, None),
_ => {
debug!("Unexpected type {:?}", def_id);
return Vec::new()
}
}; };
self.get_auto_trait_impls(def_id, def_ctor, None) self.get_auto_trait_impls(def_id, &def_ctor, None)
} }
pub fn get_with_node_id(&self, id: ast::NodeId, name: String) -> Vec<Item> { pub fn get_with_node_id(&self, id: ast::NodeId, name: String) -> Vec<Item> {
@ -52,15 +71,16 @@ impl<'a, 'tcx, 'rcx> AutoTraitFinder<'a, 'tcx, 'rcx> {
_ => panic!("Unexpected type {:?} {:?}", item, id), _ => panic!("Unexpected type {:?} {:?}", item, id),
}; };
self.get_auto_trait_impls(did, def_ctor, Some(name)) self.get_auto_trait_impls(did, &def_ctor, Some(name))
} }
pub fn get_auto_trait_impls( pub fn get_auto_trait_impls<F>(
&self, &self,
def_id: DefId, def_id: DefId,
def_ctor: fn(DefId) -> Def, def_ctor: &F,
name: Option<String>, name: Option<String>,
) -> Vec<Item> { ) -> Vec<Item>
where F: Fn(DefId) -> Def {
if self.cx if self.cx
.tcx .tcx
.get_attrs(def_id) .get_attrs(def_id)
@ -68,9 +88,9 @@ impl<'a, 'tcx, 'rcx> AutoTraitFinder<'a, 'tcx, 'rcx> {
.has_word("hidden") .has_word("hidden")
{ {
debug!( debug!(
"get_auto_trait_impls(def_id={:?}, def_ctor={:?}): item has doc('hidden'), \ "get_auto_trait_impls(def_id={:?}, def_ctor=...): item has doc('hidden'), \
aborting", aborting",
def_id, def_ctor def_id
); );
return Vec::new(); return Vec::new();
} }
@ -79,8 +99,8 @@ impl<'a, 'tcx, 'rcx> AutoTraitFinder<'a, 'tcx, 'rcx> {
let generics = self.cx.tcx.generics_of(def_id); let generics = self.cx.tcx.generics_of(def_id);
debug!( debug!(
"get_auto_trait_impls(def_id={:?}, def_ctor={:?}, generics={:?}", "get_auto_trait_impls(def_id={:?}, def_ctor=..., generics={:?}",
def_id, def_ctor, generics def_id, generics
); );
let auto_traits: Vec<_> = self.cx let auto_traits: Vec<_> = self.cx
.send_trait .send_trait
@ -110,23 +130,24 @@ impl<'a, 'tcx, 'rcx> AutoTraitFinder<'a, 'tcx, 'rcx> {
auto_traits auto_traits
} }
fn get_auto_trait_impl_for( fn get_auto_trait_impl_for<F>(
&self, &self,
def_id: DefId, def_id: DefId,
name: Option<String>, name: Option<String>,
generics: ty::Generics, generics: ty::Generics,
def_ctor: fn(DefId) -> Def, def_ctor: &F,
trait_def_id: DefId, trait_def_id: DefId,
) -> Option<Item> { ) -> Option<Item>
where F: Fn(DefId) -> Def {
if !self.cx if !self.cx
.generated_synthetics .generated_synthetics
.borrow_mut() .borrow_mut()
.insert((def_id, trait_def_id)) .insert((def_id, trait_def_id))
{ {
debug!( debug!(
"get_auto_trait_impl_for(def_id={:?}, generics={:?}, def_ctor={:?}, \ "get_auto_trait_impl_for(def_id={:?}, generics={:?}, def_ctor=..., \
trait_def_id={:?}): already generated, aborting", trait_def_id={:?}): already generated, aborting",
def_id, generics, def_ctor, trait_def_id def_id, generics, trait_def_id
); );
return None; return None;
} }

View file

@ -302,6 +302,14 @@ pub fn build_impls(cx: &DocContext, did: DefId, auto_traits: bool) -> Vec<clean:
for def_id in primitive_impls.iter().filter_map(|&def_id| def_id) { for def_id in primitive_impls.iter().filter_map(|&def_id| def_id) {
if !def_id.is_local() { if !def_id.is_local() {
build_impl(cx, def_id, &mut impls); build_impl(cx, def_id, &mut impls);
let auto_impls = get_auto_traits_with_def_id(cx, def_id);
let mut renderinfo = cx.renderinfo.borrow_mut();
let new_impls: Vec<clean::Item> = auto_impls.into_iter()
.filter(|i| renderinfo.inlined.insert(i.def_id)).collect();
impls.extend(new_impls);
} }
} }

View file

@ -4124,7 +4124,8 @@ pub fn path_to_def(tcx: &TyCtxt, path: &[&str]) -> Option<DefId> {
} }
} }
fn get_path_for_type(tcx: TyCtxt, def_id: DefId, def_ctor: fn(DefId) -> Def) -> hir::Path { fn get_path_for_type<F>(tcx: TyCtxt, def_id: DefId, def_ctor: F) -> hir::Path
where F: Fn(DefId) -> Def {
struct AbsolutePathBuffer { struct AbsolutePathBuffer {
names: Vec<String>, names: Vec<String>,
} }

View file

@ -0,0 +1,17 @@
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
#![crate_name = "foo"]
pub use std::fs::File;
// @has 'foo/primitive.i16.html' '//h2[@id="synthetic-implementations"]' 'Auto Trait Implementation'
#[doc(primitive = "i16")]
/// I love poneys!
mod prim {}