Skip to content

rustdoc: Render Sized? on traits and generics #19272

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 27, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/librustdoc/clean/inline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,11 +159,12 @@ pub fn build_external_trait(cx: &DocContext, tcx: &ty::ctxt,
}
});
let trait_def = ty::lookup_trait_def(tcx, did);
let bounds = trait_def.bounds.clean(cx);
let (bounds, default_unbound) = trait_def.bounds.clean(cx);
clean::Trait {
generics: (&def.generics, subst::TypeSpace).clean(cx),
items: items.collect(),
bounds: bounds,
default_unbound: default_unbound
}
}

Expand Down
75 changes: 65 additions & 10 deletions src/librustdoc/clean/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ pub enum ItemEnum {
ForeignStaticItem(Static),
MacroItem(Macro),
PrimitiveItem(PrimitiveType),
AssociatedTypeItem,
AssociatedTypeItem(TyParam),
}

#[deriving(Clone, Encodable, Decodable)]
Expand Down Expand Up @@ -464,7 +464,9 @@ pub struct TyParam {
pub name: String,
pub did: ast::DefId,
pub bounds: Vec<TyParamBound>,
pub default: Option<Type>
pub default: Option<Type>,
/// An optional default bound on the parameter which is unbound, like `Sized?`
pub default_unbound: Option<Type>
}

impl Clean<TyParam> for ast::TyParam {
Expand All @@ -473,7 +475,8 @@ impl Clean<TyParam> for ast::TyParam {
name: self.ident.clean(cx),
did: ast::DefId { krate: ast::LOCAL_CRATE, node: self.id },
bounds: self.bounds.clean(cx),
default: self.default.clean(cx)
default: self.default.clean(cx),
default_unbound: self.unbound.clean(cx)
}
}
}
Expand All @@ -482,11 +485,13 @@ impl<'tcx> Clean<TyParam> for ty::TypeParameterDef<'tcx> {
fn clean(&self, cx: &DocContext) -> TyParam {
cx.external_typarams.borrow_mut().as_mut().unwrap()
.insert(self.def_id, self.name.clean(cx));
let (bounds, default_unbound) = self.bounds.clean(cx);
TyParam {
name: self.name.clean(cx),
did: self.def_id,
bounds: self.bounds.clean(cx),
default: self.default.clean(cx)
bounds: bounds,
default: self.default.clean(cx),
default_unbound: default_unbound
}
}
}
Expand Down Expand Up @@ -588,12 +593,16 @@ impl<'tcx> Clean<TyParamBound> for ty::TraitRef<'tcx> {
}
}

impl<'tcx> Clean<Vec<TyParamBound>> for ty::ParamBounds<'tcx> {
fn clean(&self, cx: &DocContext) -> Vec<TyParamBound> {
// Returns (bounds, default_unbound)
impl<'tcx> Clean<(Vec<TyParamBound>, Option<Type>)> for ty::ParamBounds<'tcx> {
fn clean(&self, cx: &DocContext) -> (Vec<TyParamBound>, Option<Type>) {
let mut v = Vec::new();
let mut has_sized_bound = false;
for b in self.builtin_bounds.iter() {
if b != ty::BoundSized {
v.push(b.clean(cx));
} else {
has_sized_bound = true;
}
}
for t in self.trait_bounds.iter() {
Expand All @@ -602,7 +611,15 @@ impl<'tcx> Clean<Vec<TyParamBound>> for ty::ParamBounds<'tcx> {
for r in self.region_bounds.iter().filter_map(|r| r.clean(cx)) {
v.push(RegionBound(r));
}
return v;
if has_sized_bound {
(v, None)
} else {
let ty = match ty::BoundSized.clean(cx) {
TraitBound(ty) => ty,
_ => unreachable!()
};
(v, Some(ty))
}
}
}

Expand Down Expand Up @@ -950,6 +967,8 @@ pub struct Trait {
pub items: Vec<TraitMethod>,
pub generics: Generics,
pub bounds: Vec<TyParamBound>,
/// An optional default bound not required for `Self`, like `Sized?`
pub default_unbound: Option<Type>
}

impl Clean<Item> for doctree::Trait {
Expand All @@ -965,6 +984,7 @@ impl Clean<Item> for doctree::Trait {
items: self.items.clean(cx),
generics: self.generics.clean(cx),
bounds: self.bounds.clean(cx),
default_unbound: self.default_unbound.clean(cx)
}),
}
}
Expand All @@ -982,6 +1002,8 @@ impl Clean<Type> for ast::PolyTraitRef {
}
}

/// An item belonging to a trait, whether a method or associated. Could be named
/// TraitItem except that's already taken by an exported enum variant.
#[deriving(Clone, Encodable, Decodable)]
pub enum TraitMethod {
RequiredMethod(Item),
Expand All @@ -1002,6 +1024,12 @@ impl TraitMethod {
_ => false,
}
}
pub fn is_type(&self) -> bool {
match self {
&TypeTraitItem(..) => true,
_ => false,
}
}
pub fn item<'a>(&'a self) -> &'a Item {
match *self {
RequiredMethod(ref item) => item,
Expand Down Expand Up @@ -1127,6 +1155,11 @@ pub enum Type {
mutability: Mutability,
type_: Box<Type>,
},
QPath {
name: String,
self_type: Box<Type>,
trait_: Box<Type>
},
// region, raw, other boxes, mutable
}

Expand Down Expand Up @@ -1252,6 +1285,7 @@ impl Clean<Type> for ast::Ty {
TyProc(ref c) => Proc(box c.clean(cx)),
TyBareFn(ref barefn) => BareFunction(box barefn.clean(cx)),
TyParen(ref ty) => ty.clean(cx),
TyQPath(ref qp) => qp.clean(cx),
ref x => panic!("Unimplemented type {}", x),
}
}
Expand Down Expand Up @@ -1354,6 +1388,16 @@ impl<'tcx> Clean<Type> for ty::Ty<'tcx> {
}
}

impl Clean<Type> for ast::QPath {
fn clean(&self, cx: &DocContext) -> Type {
Type::QPath {
name: self.item_name.clean(cx),
self_type: box self.self_type.clean(cx),
trait_: box self.trait_ref.clean(cx)
}
}
}

#[deriving(Clone, Encodable, Decodable)]
pub enum StructField {
HiddenStructField, // inserted later by strip passes
Expand Down Expand Up @@ -2211,7 +2255,7 @@ impl Clean<Item> for ast::AssociatedType {
source: self.ty_param.span.clean(cx),
name: Some(self.ty_param.ident.clean(cx)),
attrs: self.attrs.clean(cx),
inner: AssociatedTypeItem,
inner: AssociatedTypeItem(self.ty_param.clean(cx)),
visibility: None,
def_id: ast_util::local_def(self.ty_param.id),
stability: None,
Expand All @@ -2225,7 +2269,18 @@ impl Clean<Item> for ty::AssociatedType {
source: DUMMY_SP.clean(cx),
name: Some(self.name.clean(cx)),
attrs: Vec::new(),
inner: AssociatedTypeItem,
// FIXME(#18048): this is wrong, but cross-crate associated types are broken
// anyway, for the time being.
inner: AssociatedTypeItem(TyParam {
name: self.name.clean(cx),
did: ast::DefId {
krate: 0,
node: ast::DUMMY_NODE_ID
},
bounds: vec![],
default: None,
default_unbound: None
}),
visibility: None,
def_id: self.def_id,
stability: None,
Expand Down
1 change: 1 addition & 0 deletions src/librustdoc/doctree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ pub struct Trait {
pub whence: Span,
pub vis: ast::Visibility,
pub stab: Option<attr::Stability>,
pub default_unbound: Option<ast::TraitRef> // FIXME(tomjakubowski)
}

pub struct Impl {
Expand Down
9 changes: 7 additions & 2 deletions src/librustdoc/html/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,8 @@ pub struct Stability<'a>(pub &'a Option<clean::Stability>);
pub struct ConciseStability<'a>(pub &'a Option<clean::Stability>);
/// Wrapper struct for emitting a where clause from Generics.
pub struct WhereClause<'a>(pub &'a clean::Generics);

/// Wrapper struct for emitting type parameter bounds.
struct TyParamBounds<'a>(pub &'a [clean::TyParamBound]);
pub struct TyParamBounds<'a>(pub &'a [clean::TyParamBound]);

impl VisSpace {
pub fn get(&self) -> Option<ast::Visibility> {
Expand Down Expand Up @@ -95,6 +94,9 @@ impl fmt::Show for clean::Generics {
if i > 0 {
try!(f.write(", ".as_bytes()))
}
if let Some(ref unbound) = tp.default_unbound {
try!(write!(f, "{}? ", unbound));
};
try!(f.write(tp.name.as_bytes()));

if tp.bounds.len() > 0 {
Expand Down Expand Up @@ -486,6 +488,9 @@ impl fmt::Show for clean::Type {
}
}
}
clean::QPath { ref name, ref self_type, ref trait_ } => {
write!(f, "&lt;{} as {}&gt;::{}", self_type, trait_, name)
}
clean::Unique(..) => {
panic!("should have been cleaned")
}
Expand Down
2 changes: 1 addition & 1 deletion src/librustdoc/html/item_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ pub fn shortty(item: &clean::Item) -> ItemType {
clean::ForeignStaticItem(..) => ForeignStatic,
clean::MacroItem(..) => Macro,
clean::PrimitiveItem(..) => Primitive,
clean::AssociatedTypeItem => AssociatedType,
clean::AssociatedTypeItem(..) => AssociatedType,
}
}

Loading