Skip to content

Commit

Permalink
Changes to RustDoc
Browse files Browse the repository at this point in the history
  • Loading branch information
nrc committed Dec 25, 2014
1 parent 4688aad commit c4640a2
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 56 deletions.
5 changes: 2 additions & 3 deletions src/librustdoc/clean/inline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,13 +163,12 @@ pub fn build_external_trait(cx: &DocContext, tcx: &ty::ctxt,
}
});
let trait_def = ty::lookup_trait_def(tcx, did);
let (bounds, default_unbound) = trait_def.bounds.clean(cx);
let bounds = trait_def.bounds.clean(cx);
clean::Trait {
unsafety: def.unsafety,
generics: (&def.generics, subst::TypeSpace).clean(cx),
items: items.collect(),
bounds: bounds,
default_unbound: default_unbound
}
}

Expand Down Expand Up @@ -328,7 +327,7 @@ fn build_impl(cx: &DocContext, tcx: &ty::ctxt,
derived: clean::detect_derived(attrs.as_slice()),
trait_: associated_trait.clean(cx).map(|bound| {
match bound {
clean::TraitBound(polyt) => polyt.trait_,
clean::TraitBound(polyt, _) => polyt.trait_,
clean::RegionBound(..) => unreachable!(),
}
}),
Expand Down
43 changes: 9 additions & 34 deletions src/librustdoc/clean/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -458,8 +458,6 @@ pub struct TyParam {
pub did: ast::DefId,
pub bounds: Vec<TyParamBound>,
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 @@ -469,7 +467,6 @@ impl Clean<TyParam> for ast::TyParam {
did: ast::DefId { krate: ast::LOCAL_CRATE, node: self.id },
bounds: self.bounds.clean(cx),
default: self.default.clean(cx),
default_unbound: self.unbound.clean(cx)
}
}
}
Expand All @@ -478,28 +475,27 @@ 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);
let bounds = self.bounds.clean(cx);
TyParam {
name: self.name.clean(cx),
did: self.def_id,
bounds: bounds,
default: self.default.clean(cx),
default_unbound: default_unbound
}
}
}

#[deriving(Clone, RustcEncodable, RustcDecodable, PartialEq)]
pub enum TyParamBound {
RegionBound(Lifetime),
TraitBound(PolyTrait)
TraitBound(PolyTrait, ast::TraitBoundModifier)
}

impl Clean<TyParamBound> for ast::TyParamBound {
fn clean(&self, cx: &DocContext) -> TyParamBound {
match *self {
ast::RegionTyParamBound(lt) => RegionBound(lt.clean(cx)),
ast::TraitTyParamBound(ref t) => TraitBound(t.clean(cx)),
ast::TraitTyParamBound(ref t, modifier) => TraitBound(t.clean(cx), modifier),
}
}
}
Expand Down Expand Up @@ -600,7 +596,7 @@ impl Clean<TyParamBound> for ty::BuiltinBound {
did: did,
},
lifetimes: vec![]
})
}, ast::TraitBoundModifier::None)
}
}

Expand Down Expand Up @@ -648,37 +644,20 @@ impl<'tcx> Clean<TyParamBound> for ty::TraitRef<'tcx> {
TraitBound(PolyTrait {
trait_: ResolvedPath { path: path, typarams: None, did: self.def_id, },
lifetimes: late_bounds
})
}, ast::TraitBoundModifier::None)
}
}

// Returns (bounds, default_unbound)
impl<'tcx> Clean<(Vec<TyParamBound>, Option<Type>)> for ty::ParamBounds<'tcx> {
fn clean(&self, cx: &DocContext) -> (Vec<TyParamBound>, Option<Type>) {
impl<'tcx> Clean<Vec<TyParamBound>> for ty::ParamBounds<'tcx> {
fn clean(&self, cx: &DocContext) -> Vec<TyParamBound> {
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() {
v.push(t.clean(cx));
}
for r in self.region_bounds.iter().filter_map(|r| r.clean(cx)) {
v.push(RegionBound(r));
}
if has_sized_bound {
(v, None)
} else {
let ty = match ty::BoundSized.clean(cx) {
TraitBound(polyt) => polyt.trait_,
_ => unreachable!()
};
(v, Some(ty))
}
v
}
}

Expand All @@ -689,7 +668,7 @@ impl<'tcx> Clean<Option<Vec<TyParamBound>>> for subst::Substs<'tcx> {
v.extend(self.types.iter().map(|t| TraitBound(PolyTrait {
trait_: t.clean(cx),
lifetimes: vec![]
})));
}, ast::TraitBoundModifier::None)));
if v.len() > 0 {Some(v)} else {None}
}
}
Expand Down Expand Up @@ -1047,8 +1026,6 @@ 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 @@ -1065,7 +1042,6 @@ 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 Down Expand Up @@ -2412,7 +2388,6 @@ impl Clean<Item> for ty::AssociatedType {
},
bounds: vec![],
default: None,
default_unbound: None
}),
visibility: None,
def_id: self.def_id,
Expand Down
1 change: 0 additions & 1 deletion src/librustdoc/doctree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,6 @@ 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
16 changes: 10 additions & 6 deletions src/librustdoc/html/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,6 @@ 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 @@ -182,8 +179,12 @@ impl fmt::Show for clean::TyParamBound {
clean::RegionBound(ref lt) => {
write!(f, "{}", *lt)
}
clean::TraitBound(ref ty) => {
write!(f, "{}", *ty)
clean::TraitBound(ref ty, modifier) => {
let modifier_str = match modifier {
ast::TraitBoundModifier::None => "",
ast::TraitBoundModifier::Maybe => "?",
};
write!(f, "{}{}", modifier_str, *ty)
}
}
}
Expand Down Expand Up @@ -458,12 +459,15 @@ impl fmt::Show for clean::Type {
for bound in decl.bounds.iter() {
match *bound {
clean::RegionBound(..) => {}
clean::TraitBound(ref t) => {
clean::TraitBound(ref t, modifier) => {
if ret.len() == 0 {
ret.push_str(": ");
} else {
ret.push_str(" + ");
}
if modifier == ast::TraitBoundModifier::Maybe {
ret.push_str("?");
}
ret.push_str(format!("{}",
*t).as_slice());
}
Expand Down
3 changes: 0 additions & 3 deletions src/librustdoc/html/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1679,9 +1679,6 @@ 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(' ');
Expand Down
3 changes: 1 addition & 2 deletions src/librustdoc/visit_ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
};
om.constants.push(s);
},
ast::ItemTrait(unsafety, ref gen, ref def_ub, ref b, ref items) => {
ast::ItemTrait(unsafety, ref gen, ref b, ref items) => {
let t = Trait {
unsafety: unsafety,
name: name,
Expand All @@ -334,7 +334,6 @@ 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);
},
Expand Down
19 changes: 12 additions & 7 deletions src/libsyntax/print/pprust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -965,13 +965,18 @@ impl<'a> State<'a> {
try!(self.word_nbsp("trait"));
try!(self.print_ident(item.ident));
try!(self.print_generics(generics));
// TODO find and print the unbound, remove it from bounds
/*if let &Some(ref tref) = unbound {
try!(space(&mut self.s));
try!(self.word_space("for ?"));
try!(self.print_trait_ref(tref));
}*/
try!(self.print_bounds(":", bounds[]));
let bounds: Vec<_> = bounds.iter().map(|b| b.clone()).collect();
let mut real_bounds = Vec::with_capacity(bounds.len());
for b in bounds.into_iter() {
if let TraitTyParamBound(ref ptr, ast::TraitBoundModifier::Maybe) = b {
try!(space(&mut self.s));
try!(self.word_space("for ?"));
try!(self.print_trait_ref(&ptr.trait_ref));
} else {
real_bounds.push(b);
}
}
try!(self.print_bounds(":", real_bounds[]));
try!(self.print_where_clause(generics));
try!(word(&mut self.s, " "));
try!(self.bopen());
Expand Down

0 comments on commit c4640a2

Please sign in to comment.