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 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 { clean::Trait {
generics: (&def.generics, subst::TypeSpace).clean(cx), generics: (&def.generics, subst::TypeSpace).clean(cx),
items: items.collect(), items: items.collect(),
bounds: bounds, bounds: bounds,
default_unbound: default_unbound
} }
} }

View file

@ -464,7 +464,9 @@ pub struct TyParam {
pub name: String, pub name: String,
pub did: ast::DefId, pub did: ast::DefId,
pub bounds: Vec<TyParamBound>, 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 { impl Clean<TyParam> for ast::TyParam {
@ -473,7 +475,8 @@ impl Clean<TyParam> for ast::TyParam {
name: self.ident.clean(cx), name: self.ident.clean(cx),
did: ast::DefId { krate: ast::LOCAL_CRATE, node: self.id }, did: ast::DefId { krate: ast::LOCAL_CRATE, node: self.id },
bounds: self.bounds.clean(cx), 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 { fn clean(&self, cx: &DocContext) -> TyParam {
cx.external_typarams.borrow_mut().as_mut().unwrap() cx.external_typarams.borrow_mut().as_mut().unwrap()
.insert(self.def_id, self.name.clean(cx)); .insert(self.def_id, self.name.clean(cx));
let (bounds, default_unbound) = self.bounds.clean(cx);
TyParam { TyParam {
name: self.name.clean(cx), name: self.name.clean(cx),
did: self.def_id, did: self.def_id,
bounds: self.bounds.clean(cx), bounds: bounds,
default: self.default.clean(cx) 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> { // Returns (bounds, default_unbound)
fn clean(&self, cx: &DocContext) -> Vec<TyParamBound> { 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 v = Vec::new();
let mut has_sized_bound = false;
for b in self.builtin_bounds.iter() { for b in self.builtin_bounds.iter() {
if b != ty::BoundSized { if b != ty::BoundSized {
v.push(b.clean(cx)); v.push(b.clean(cx));
} else {
has_sized_bound = true;
} }
} }
for t in self.trait_bounds.iter() { 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)) { for r in self.region_bounds.iter().filter_map(|r| r.clean(cx)) {
v.push(RegionBound(r)); 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 items: Vec<TraitMethod>,
pub generics: Generics, pub generics: Generics,
pub bounds: Vec<TyParamBound>, 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 { impl Clean<Item> for doctree::Trait {
@ -965,6 +984,7 @@ impl Clean<Item> for doctree::Trait {
items: self.items.clean(cx), items: self.items.clean(cx),
generics: self.generics.clean(cx), generics: self.generics.clean(cx),
bounds: self.bounds.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 node: ast::DUMMY_NODE_ID
}, },
bounds: vec![], bounds: vec![],
default: None default: None,
default_unbound: None
}), }),
visibility: None, visibility: None,
def_id: self.def_id, def_id: self.def_id,

View file

@ -177,6 +177,7 @@ pub struct Trait {
pub whence: Span, pub whence: Span,
pub vis: ast::Visibility, pub vis: ast::Visibility,
pub stab: Option<attr::Stability>, pub stab: Option<attr::Stability>,
pub default_unbound: Option<ast::TraitRef> // FIXME(tomjakubowski)
} }
pub struct Impl { pub struct Impl {

View file

@ -94,6 +94,9 @@ impl fmt::Show for clean::Generics {
if i > 0 { if i > 0 {
try!(f.write(", ".as_bytes())) try!(f.write(", ".as_bytes()))
} }
if let Some(ref unbound) = tp.default_unbound {
try!(write!(f, "{}? ", unbound));
};
try!(f.write(tp.name.as_bytes())); try!(f.write(tp.name.as_bytes()));
if tp.bounds.len() > 0 { 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, fn item_trait(w: &mut fmt::Formatter, cx: &Context, it: &clean::Item,
t: &clean::Trait) -> fmt::Result { t: &clean::Trait) -> fmt::Result {
let mut bounds = String::new(); 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 t.bounds.len() > 0 {
if bounds.len() > 0 {
bounds.push(' ');
}
bounds.push_str(": "); bounds.push_str(": ");
for (i, p) in t.bounds.iter().enumerate() { for (i, p) in t.bounds.iter().enumerate() {
if i > 0 { bounds.push_str(" + "); } if i > 0 { bounds.push_str(" + "); }

View file

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