Skip to content

Commit 3a38797

Browse files
committed
auto merge of #17798 : tomjakubowski/rust/rustdoc-fix-bounds, r=alexcrichton
This PR adds support in rustdoc for properly naming lifetimes in bounds, instead of just showing `'static` for everything. It also adds support for unboxed function sugar bounds, which were also previously rendered as `'static`.
2 parents 3edcdbb + 7a6eaea commit 3a38797

File tree

3 files changed

+42
-15
lines changed

3 files changed

+42
-15
lines changed

src/librustdoc/clean/inline.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,8 @@ fn build_impl(cx: &DocContext, tcx: &ty::ctxt,
324324
trait_: associated_trait.clean(cx).map(|bound| {
325325
match bound {
326326
clean::TraitBound(ty) => ty,
327-
clean::RegionBound => unreachable!(),
327+
clean::UnboxedFnBound(..) |
328+
clean::RegionBound(..) => unreachable!(),
328329
}
329330
}),
330331
for_: ty.ty.clean(cx),

src/librustdoc/clean/mod.rs

+33-11
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,7 @@ impl Clean<TyParam> for ty::TypeParameterDef {
462462
fn clean(&self, cx: &DocContext) -> TyParam {
463463
cx.external_typarams.borrow_mut().as_mut().unwrap()
464464
.insert(self.def_id, self.ident.clean(cx));
465+
465466
TyParam {
466467
name: self.ident.clean(cx),
467468
did: self.def_id,
@@ -473,26 +474,25 @@ impl Clean<TyParam> for ty::TypeParameterDef {
473474

474475
#[deriving(Clone, Encodable, Decodable, PartialEq)]
475476
pub enum TyParamBound {
476-
RegionBound, // FIXME(#16518) -- need to include name of actual region
477+
RegionBound(Lifetime),
478+
UnboxedFnBound(UnboxedFnType),
477479
TraitBound(Type)
478480
}
479481

480482
impl Clean<TyParamBound> for ast::TyParamBound {
481483
fn clean(&self, cx: &DocContext) -> TyParamBound {
482484
match *self {
483-
ast::RegionTyParamBound(_) => RegionBound,
484-
ast::UnboxedFnTyParamBound(_) => {
485-
// FIXME(pcwalton): Wrong.
486-
RegionBound
487-
}
485+
ast::RegionTyParamBound(lt) => RegionBound(lt.clean(cx)),
486+
ast::UnboxedFnTyParamBound(ref ty) => { UnboxedFnBound(ty.clean(cx)) },
488487
ast::TraitTyParamBound(ref t) => TraitBound(t.clean(cx)),
489488
}
490489
}
491490
}
492491

493492
impl Clean<Vec<TyParamBound>> for ty::ExistentialBounds {
494493
fn clean(&self, cx: &DocContext) -> Vec<TyParamBound> {
495-
let mut vec = vec!(RegionBound);
494+
let mut vec = vec![];
495+
self.region_bound.clean(cx).map(|b| vec.push(RegionBound(b)));
496496
for bb in self.builtin_bounds.iter() {
497497
vec.push(bb.clean(cx));
498498
}
@@ -521,7 +521,7 @@ impl Clean<TyParamBound> for ty::BuiltinBound {
521521
fn clean(&self, cx: &DocContext) -> TyParamBound {
522522
let tcx = match cx.tcx_opt() {
523523
Some(tcx) => tcx,
524-
None => return RegionBound,
524+
None => return RegionBound(Lifetime::statik())
525525
};
526526
let empty = subst::Substs::empty();
527527
let (did, path) = match *self {
@@ -554,7 +554,7 @@ impl Clean<TyParamBound> for ty::TraitRef {
554554
fn clean(&self, cx: &DocContext) -> TyParamBound {
555555
let tcx = match cx.tcx_opt() {
556556
Some(tcx) => tcx,
557-
None => return RegionBound,
557+
None => return RegionBound(Lifetime::statik())
558558
};
559559
let fqn = csearch::get_item_path(tcx, self.def_id);
560560
let fqn = fqn.into_iter().map(|i| i.to_string())
@@ -582,19 +582,37 @@ impl Clean<Vec<TyParamBound>> for ty::ParamBounds {
582582
for t in self.trait_bounds.iter() {
583583
v.push(t.clean(cx));
584584
}
585+
for r in self.region_bounds.iter().filter_map(|r| r.clean(cx)) {
586+
v.push(RegionBound(r));
587+
}
585588
return v;
586589
}
587590
}
588591

589592
impl Clean<Option<Vec<TyParamBound>>> for subst::Substs {
590593
fn clean(&self, cx: &DocContext) -> Option<Vec<TyParamBound>> {
591594
let mut v = Vec::new();
592-
v.extend(self.regions().iter().map(|_| RegionBound));
595+
v.extend(self.regions().iter().filter_map(|r| r.clean(cx)).map(RegionBound));
593596
v.extend(self.types.iter().map(|t| TraitBound(t.clean(cx))));
594597
if v.len() > 0 {Some(v)} else {None}
595598
}
596599
}
597600

601+
#[deriving(Clone, Encodable, Decodable, PartialEq)]
602+
pub struct UnboxedFnType {
603+
pub path: Path,
604+
pub decl: FnDecl
605+
}
606+
607+
impl Clean<UnboxedFnType> for ast::UnboxedFnBound {
608+
fn clean(&self, cx: &DocContext) -> UnboxedFnType {
609+
UnboxedFnType {
610+
path: self.path.clean(cx),
611+
decl: self.decl.clean(cx)
612+
}
613+
}
614+
}
615+
598616
#[deriving(Clone, Encodable, Decodable, PartialEq)]
599617
pub struct Lifetime(String);
600618

@@ -604,6 +622,10 @@ impl Lifetime {
604622
let s: &'a str = s.as_slice();
605623
return s;
606624
}
625+
626+
pub fn statik() -> Lifetime {
627+
Lifetime("'static".to_string())
628+
}
607629
}
608630

609631
impl Clean<Lifetime> for ast::Lifetime {
@@ -627,7 +649,7 @@ impl Clean<Lifetime> for ty::RegionParameterDef {
627649
impl Clean<Option<Lifetime>> for ty::Region {
628650
fn clean(&self, cx: &DocContext) -> Option<Lifetime> {
629651
match *self {
630-
ty::ReStatic => Some(Lifetime("'static".to_string())),
652+
ty::ReStatic => Some(Lifetime::statik()),
631653
ty::ReLateBound(_, ty::BrNamed(_, name)) =>
632654
Some(Lifetime(token::get_name(name).get().to_string())),
633655
ty::ReEarlyBound(_, _, _, name) => Some(Lifetime(name.clean(cx))),

src/librustdoc/html/format.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,11 @@ impl fmt::Show for clean::Lifetime {
140140
impl fmt::Show for clean::TyParamBound {
141141
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
142142
match *self {
143-
clean::RegionBound => {
144-
f.write("'static".as_bytes())
143+
clean::RegionBound(ref lt) => {
144+
write!(f, "{}", *lt)
145+
}
146+
clean::UnboxedFnBound(ref ty) => {
147+
write!(f, "{}{}", ty.path, ty.decl)
145148
}
146149
clean::TraitBound(ref ty) => {
147150
write!(f, "{}", *ty)
@@ -401,7 +404,8 @@ impl fmt::Show for clean::Type {
401404
let mut ret = String::new();
402405
for bound in decl.bounds.iter() {
403406
match *bound {
404-
clean::RegionBound => {}
407+
clean::RegionBound(..) |
408+
clean::UnboxedFnBound(..) => {}
405409
clean::TraitBound(ref t) => {
406410
if ret.len() == 0 {
407411
ret.push_str(": ");

0 commit comments

Comments
 (0)