1
Fork 0

rustdoc: Render Sized? on traits and generics

Both `trait Foo for Sized?` and `<Sized? T>` are handled correctly.

Fix #18515
This commit is contained in:
Tom Jakubowski 2014-11-24 10:14:46 -08:00
parent de94f0affb
commit 59d13820c4
6 changed files with 43 additions and 10 deletions

View file

@ -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
}
}

View file

@ -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 {
@ -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)
}
}
}
@ -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
}
}
}
@ -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() {
@ -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))
}
}
}
@ -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 {
@ -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)
}),
}
}
@ -2258,7 +2278,8 @@ impl Clean<Item> for ty::AssociatedType {
node: ast::DUMMY_NODE_ID
},
bounds: vec![],
default: None
default: None,
default_unbound: None
}),
visibility: None,
def_id: self.def_id,

View file

@ -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 {

View file

@ -94,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 {

View file

@ -1670,7 +1670,13 @@ fn item_function(w: &mut fmt::Formatter, it: &clean::Item,
fn item_trait(w: &mut fmt::Formatter, cx: &Context, it: &clean::Item,
t: &clean::Trait) -> fmt::Result {
let mut bounds = String::new();
if let Some(ref ty) = t.default_unbound {
bounds.push_str(format!(" for {}?", ty).as_slice());
}
if t.bounds.len() > 0 {
if bounds.len() > 0 {
bounds.push(' ');
}
bounds.push_str(": ");
for (i, p) in t.bounds.iter().enumerate() {
if i > 0 { bounds.push_str(" + "); }

View file

@ -322,7 +322,7 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
};
om.constants.push(s);
},
ast::ItemTrait(ref gen, _, ref b, ref items) => {
ast::ItemTrait(ref gen, ref def_ub, ref b, ref items) => {
let t = Trait {
name: name,
items: items.clone(),
@ -333,6 +333,7 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
whence: item.span,
vis: item.vis,
stab: self.stability(item.id),
default_unbound: def_ub.clone()
};
om.traits.push(t);
},