Skip to content

Commit

Permalink
add auto-impl for primitive type
Browse files Browse the repository at this point in the history
  • Loading branch information
GuillaumeGomez committed May 15, 2018
1 parent cb1ce7d commit 564511e
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 16 deletions.
51 changes: 36 additions & 15 deletions src/librustdoc/clean/auto_trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,29 @@ impl<'a, 'tcx, 'rcx> AutoTraitFinder<'a, 'tcx, 'rcx> {
AdtKind::Enum => Def::Enum,
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> {
Expand All @@ -52,25 +71,26 @@ impl<'a, 'tcx, 'rcx> AutoTraitFinder<'a, 'tcx, 'rcx> {
_ => 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,
def_id: DefId,
def_ctor: fn(DefId) -> Def,
def_ctor: &F,
name: Option<String>,
) -> Vec<Item> {
) -> Vec<Item>
where F: Fn(DefId) -> Def {
if self.cx
.tcx
.get_attrs(def_id)
.lists("doc")
.has_word("hidden")
{
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",
def_id, def_ctor
def_id
);
return Vec::new();
}
Expand All @@ -79,8 +99,8 @@ impl<'a, 'tcx, 'rcx> AutoTraitFinder<'a, 'tcx, 'rcx> {
let generics = self.cx.tcx.generics_of(def_id);

debug!(
"get_auto_trait_impls(def_id={:?}, def_ctor={:?}, generics={:?}",
def_id, def_ctor, generics
"get_auto_trait_impls(def_id={:?}, def_ctor=..., generics={:?}",
def_id, generics
);
let auto_traits: Vec<_> = self.cx
.send_trait
Expand Down Expand Up @@ -110,23 +130,24 @@ impl<'a, 'tcx, 'rcx> AutoTraitFinder<'a, 'tcx, 'rcx> {
auto_traits
}

fn get_auto_trait_impl_for(
fn get_auto_trait_impl_for<F>(
&self,
def_id: DefId,
name: Option<String>,
generics: ty::Generics,
def_ctor: fn(DefId) -> Def,
def_ctor: &F,
trait_def_id: DefId,
) -> Option<Item> {
) -> Option<Item>
where F: Fn(DefId) -> Def {
if !self.cx
.generated_synthetics
.borrow_mut()
.insert((def_id, trait_def_id))
{
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",
def_id, generics, def_ctor, trait_def_id
def_id, generics, trait_def_id
);
return None;
}
Expand Down
8 changes: 8 additions & 0 deletions src/librustdoc/clean/inline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
if !def_id.is_local() {
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);
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/librustdoc/clean/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
names: Vec<String>,
}
Expand Down
17 changes: 17 additions & 0 deletions src/test/rustdoc/auto-impl-primitive.rs
Original file line number Diff line number Diff line change
@@ -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 {}

0 comments on commit 564511e

Please sign in to comment.