Skip to content

Commit 543f7f5

Browse files
committed
rollup merge of rust-lang#19174: tomjakubowski/rustdoc-assoc-types
2 parents c067e17 + 8775c21 commit 543f7f5

File tree

6 files changed

+115
-39
lines changed

6 files changed

+115
-39
lines changed

src/librustdoc/clean/mod.rs

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ pub enum ItemEnum {
336336
ForeignStaticItem(Static),
337337
MacroItem(Macro),
338338
PrimitiveItem(PrimitiveType),
339-
AssociatedTypeItem,
339+
AssociatedTypeItem(TyParam),
340340
}
341341

342342
#[deriving(Clone, Encodable, Decodable)]
@@ -982,6 +982,8 @@ impl Clean<Type> for ast::PolyTraitRef {
982982
}
983983
}
984984

985+
/// An item belonging to a trait, whether a method or associated. Could be named
986+
/// TraitItem except that's already taken by an exported enum variant.
985987
#[deriving(Clone, Encodable, Decodable)]
986988
pub enum TraitMethod {
987989
RequiredMethod(Item),
@@ -1002,6 +1004,12 @@ impl TraitMethod {
10021004
_ => false,
10031005
}
10041006
}
1007+
pub fn is_type(&self) -> bool {
1008+
match self {
1009+
&TypeTraitItem(..) => true,
1010+
_ => false,
1011+
}
1012+
}
10051013
pub fn item<'a>(&'a self) -> &'a Item {
10061014
match *self {
10071015
RequiredMethod(ref item) => item,
@@ -1127,6 +1135,11 @@ pub enum Type {
11271135
mutability: Mutability,
11281136
type_: Box<Type>,
11291137
},
1138+
QPath {
1139+
name: String,
1140+
self_type: Box<Type>,
1141+
trait_: Box<Type>
1142+
},
11301143
// region, raw, other boxes, mutable
11311144
}
11321145

@@ -1252,6 +1265,7 @@ impl Clean<Type> for ast::Ty {
12521265
TyProc(ref c) => Proc(box c.clean(cx)),
12531266
TyBareFn(ref barefn) => BareFunction(box barefn.clean(cx)),
12541267
TyParen(ref ty) => ty.clean(cx),
1268+
TyQPath(ref qp) => qp.clean(cx),
12551269
ref x => panic!("Unimplemented type {}", x),
12561270
}
12571271
}
@@ -1354,6 +1368,16 @@ impl<'tcx> Clean<Type> for ty::Ty<'tcx> {
13541368
}
13551369
}
13561370

1371+
impl Clean<Type> for ast::QPath {
1372+
fn clean(&self, cx: &DocContext) -> Type {
1373+
Type::QPath {
1374+
name: self.item_name.clean(cx),
1375+
self_type: box self.self_type.clean(cx),
1376+
trait_: box self.trait_ref.clean(cx)
1377+
}
1378+
}
1379+
}
1380+
13571381
#[deriving(Clone, Encodable, Decodable)]
13581382
pub enum StructField {
13591383
HiddenStructField, // inserted later by strip passes
@@ -2211,7 +2235,7 @@ impl Clean<Item> for ast::AssociatedType {
22112235
source: self.ty_param.span.clean(cx),
22122236
name: Some(self.ty_param.ident.clean(cx)),
22132237
attrs: self.attrs.clean(cx),
2214-
inner: AssociatedTypeItem,
2238+
inner: AssociatedTypeItem(self.ty_param.clean(cx)),
22152239
visibility: None,
22162240
def_id: ast_util::local_def(self.ty_param.id),
22172241
stability: None,
@@ -2225,7 +2249,17 @@ impl Clean<Item> for ty::AssociatedType {
22252249
source: DUMMY_SP.clean(cx),
22262250
name: Some(self.name.clean(cx)),
22272251
attrs: Vec::new(),
2228-
inner: AssociatedTypeItem,
2252+
// FIXME(#18048): this is wrong, but cross-crate associated types are broken
2253+
// anyway, for the time being.
2254+
inner: AssociatedTypeItem(TyParam {
2255+
name: self.name.clean(cx),
2256+
did: ast::DefId {
2257+
krate: 0,
2258+
node: ast::DUMMY_NODE_ID
2259+
},
2260+
bounds: vec![],
2261+
default: None
2262+
}),
22292263
visibility: None,
22302264
def_id: self.def_id,
22312265
stability: None,

src/librustdoc/html/format.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,8 @@ pub struct Stability<'a>(pub &'a Option<clean::Stability>);
4646
pub struct ConciseStability<'a>(pub &'a Option<clean::Stability>);
4747
/// Wrapper struct for emitting a where clause from Generics.
4848
pub struct WhereClause<'a>(pub &'a clean::Generics);
49-
5049
/// Wrapper struct for emitting type parameter bounds.
51-
struct TyParamBounds<'a>(pub &'a [clean::TyParamBound]);
50+
pub struct TyParamBounds<'a>(pub &'a [clean::TyParamBound]);
5251

5352
impl VisSpace {
5453
pub fn get(&self) -> Option<ast::Visibility> {
@@ -486,6 +485,9 @@ impl fmt::Show for clean::Type {
486485
}
487486
}
488487
}
488+
clean::QPath { ref name, ref self_type, ref trait_ } => {
489+
write!(f, "&lt;{} as {}&gt;::{}", self_type, trait_, name)
490+
}
489491
clean::Unique(..) => {
490492
panic!("should have been cleaned")
491493
}

src/librustdoc/html/item_type.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ pub fn shortty(item: &clean::Item) -> ItemType {
9595
clean::ForeignStaticItem(..) => ForeignStatic,
9696
clean::MacroItem(..) => Macro,
9797
clean::PrimitiveItem(..) => Primitive,
98-
clean::AssociatedTypeItem => AssociatedType,
98+
clean::AssociatedTypeItem(..) => AssociatedType,
9999
}
100100
}
101101

src/librustdoc/html/render.rs

Lines changed: 65 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ use clean;
5959
use doctree;
6060
use fold::DocFolder;
6161
use html::format::{VisSpace, Method, FnStyleSpace, MutableSpace, Stability};
62-
use html::format::{ConciseStability, WhereClause};
62+
use html::format::{ConciseStability, TyParamBounds, WhereClause};
6363
use html::highlight;
6464
use html::item_type::{ItemType, shortty};
6565
use html::item_type;
@@ -1705,27 +1705,23 @@ fn item_trait(w: &mut fmt::Formatter, cx: &Context, it: &clean::Item,
17051705
t.generics,
17061706
bounds,
17071707
WhereClause(&t.generics)));
1708-
let required = t.items.iter()
1709-
.filter(|m| {
1710-
match **m {
1711-
clean::RequiredMethod(_) => true,
1712-
_ => false,
1713-
}
1714-
})
1715-
.collect::<Vec<&clean::TraitMethod>>();
1716-
let provided = t.items.iter()
1717-
.filter(|m| {
1718-
match **m {
1719-
clean::ProvidedMethod(_) => true,
1720-
_ => false,
1721-
}
1722-
})
1723-
.collect::<Vec<&clean::TraitMethod>>();
1708+
1709+
let types = t.items.iter().filter(|m| m.is_type()).collect::<Vec<_>>();
1710+
let required = t.items.iter().filter(|m| m.is_req()).collect::<Vec<_>>();
1711+
let provided = t.items.iter().filter(|m| m.is_def()).collect::<Vec<_>>();
17241712

17251713
if t.items.len() == 0 {
17261714
try!(write!(w, "{{ }}"));
17271715
} else {
17281716
try!(write!(w, "{{\n"));
1717+
for t in types.iter() {
1718+
try!(write!(w, " "));
1719+
try!(render_method(w, t.item()));
1720+
try!(write!(w, ";\n"));
1721+
}
1722+
if types.len() > 0 && required.len() > 0 {
1723+
try!(w.write("\n".as_bytes()));
1724+
}
17291725
for m in required.iter() {
17301726
try!(write!(w, " "));
17311727
try!(render_method(w, m.item()));
@@ -1758,6 +1754,17 @@ fn item_trait(w: &mut fmt::Formatter, cx: &Context, it: &clean::Item,
17581754
Ok(())
17591755
}
17601756

1757+
if types.len() > 0 {
1758+
try!(write!(w, "
1759+
<h2 id='associated-types'>Associated Types</h2>
1760+
<div class='methods'>
1761+
"));
1762+
for t in types.iter() {
1763+
try!(trait_item(w, *t));
1764+
}
1765+
try!(write!(w, "</div>"));
1766+
}
1767+
17611768
// Output the documentation for each function individually
17621769
if required.len() > 0 {
17631770
try!(write!(w, "
@@ -1812,7 +1819,7 @@ fn item_trait(w: &mut fmt::Formatter, cx: &Context, it: &clean::Item,
18121819
}
18131820

18141821
fn render_method(w: &mut fmt::Formatter, meth: &clean::Item) -> fmt::Result {
1815-
fn fun(w: &mut fmt::Formatter, it: &clean::Item, fn_style: ast::FnStyle,
1822+
fn method(w: &mut fmt::Formatter, it: &clean::Item, fn_style: ast::FnStyle,
18161823
g: &clean::Generics, selfty: &clean::SelfTy,
18171824
d: &clean::FnDecl) -> fmt::Result {
18181825
write!(w, "{}fn <a href='#{ty}.{name}' class='fnname'>{name}</a>\
@@ -1827,14 +1834,28 @@ fn render_method(w: &mut fmt::Formatter, meth: &clean::Item) -> fmt::Result {
18271834
decl = Method(selfty, d),
18281835
where_clause = WhereClause(g))
18291836
}
1837+
fn assoc_type(w: &mut fmt::Formatter, it: &clean::Item,
1838+
typ: &clean::TyParam) -> fmt::Result {
1839+
try!(write!(w, "type {}", it.name.as_ref().unwrap()));
1840+
if typ.bounds.len() > 0 {
1841+
try!(write!(w, ": {}", TyParamBounds(&*typ.bounds)))
1842+
}
1843+
if let Some(ref default) = typ.default {
1844+
try!(write!(w, " = {}", default));
1845+
}
1846+
Ok(())
1847+
}
18301848
match meth.inner {
18311849
clean::TyMethodItem(ref m) => {
1832-
fun(w, meth, m.fn_style, &m.generics, &m.self_, &m.decl)
1850+
method(w, meth, m.fn_style, &m.generics, &m.self_, &m.decl)
18331851
}
18341852
clean::MethodItem(ref m) => {
1835-
fun(w, meth, m.fn_style, &m.generics, &m.self_, &m.decl)
1853+
method(w, meth, m.fn_style, &m.generics, &m.self_, &m.decl)
18361854
}
1837-
_ => unreachable!()
1855+
clean::AssociatedTypeItem(ref typ) => {
1856+
assoc_type(w, meth, typ)
1857+
}
1858+
_ => panic!("render_method called on non-method")
18381859
}
18391860
}
18401861

@@ -2091,11 +2112,26 @@ fn render_impl(w: &mut fmt::Formatter, i: &Impl) -> fmt::Result {
20912112

20922113
fn doctraititem(w: &mut fmt::Formatter, item: &clean::Item, dox: bool)
20932114
-> fmt::Result {
2094-
try!(write!(w, "<h4 id='method.{}' class='method'>{}<code>",
2095-
*item.name.as_ref().unwrap(),
2096-
ConciseStability(&item.stability)));
2097-
try!(render_method(w, item));
2098-
try!(write!(w, "</code></h4>\n"));
2115+
match item.inner {
2116+
clean::MethodItem(..) => {
2117+
try!(write!(w, "<h4 id='method.{}' class='{}'>{}<code>",
2118+
*item.name.as_ref().unwrap(),
2119+
shortty(item),
2120+
ConciseStability(&item.stability)));
2121+
try!(render_method(w, item));
2122+
try!(write!(w, "</code></h4>\n"));
2123+
}
2124+
clean::TypedefItem(ref tydef) => {
2125+
let name = item.name.as_ref().unwrap();
2126+
try!(write!(w, "<h4 id='assoc_type.{}' class='{}'>{}<code>",
2127+
*name,
2128+
shortty(item),
2129+
ConciseStability(&item.stability)));
2130+
try!(write!(w, "type {} = {}", name, tydef.type_));
2131+
try!(write!(w, "</code></h4>\n"));
2132+
}
2133+
_ => panic!("unknown trait item with name {}", item.name)
2134+
}
20992135
match item.doc_value() {
21002136
Some(s) if dox => {
21012137
try!(write!(w, "<div class='docblock'>{}</div>", Markdown(s)));
@@ -2105,7 +2141,7 @@ fn render_impl(w: &mut fmt::Formatter, i: &Impl) -> fmt::Result {
21052141
}
21062142
}
21072143

2108-
try!(write!(w, "<div class='impl-methods'>"));
2144+
try!(write!(w, "<div class='impl-items'>"));
21092145
for trait_item in i.impl_.items.iter() {
21102146
try!(doctraititem(w, trait_item, true));
21112147
}
@@ -2127,6 +2163,8 @@ fn render_impl(w: &mut fmt::Formatter, i: &Impl) -> fmt::Result {
21272163

21282164
// If we've implemented a trait, then also emit documentation for all
21292165
// default methods which weren't overridden in the implementation block.
2166+
// FIXME: this also needs to be done for associated types, whenever defaults
2167+
// for them work.
21302168
match i.impl_.trait_ {
21312169
Some(clean::ResolvedPath { did, .. }) => {
21322170
try!({

src/librustdoc/html/static/main.css

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ h2 {
8484
h3 {
8585
font-size: 1.3em;
8686
}
87-
h1, h2, h3:not(.impl):not(.method), h4:not(.method) {
87+
h1, h2, h3:not(.impl):not(.method):not(.type), h4:not(.method):not(.type) {
8888
color: black;
8989
font-weight: 500;
9090
margin: 20px 0 15px 0;
@@ -94,15 +94,15 @@ h1.fqn {
9494
border-bottom: 1px dashed #D5D5D5;
9595
margin-top: 0;
9696
}
97-
h2, h3:not(.impl):not(.method), h4:not(.method) {
97+
h2, h3:not(.impl):not(.method):not(.type), h4:not(.method):not(.type) {
9898
border-bottom: 1px solid #DDDDDD;
9999
}
100-
h3.impl, h3.method, h4.method {
100+
h3.impl, h3.method, h4.method, h3.type, h4.type {
101101
font-weight: 600;
102102
margin-top: 10px;
103103
margin-bottom: 10px;
104104
}
105-
h3.impl, h3.method {
105+
h3.impl, h3.method, h3.type {
106106
margin-top: 15px;
107107
}
108108
h1, h2, h3, h4, section.sidebar, a.source, .search-input, .content table :not(code)>a, .collapse-toggle {
@@ -234,6 +234,7 @@ nav.sub {
234234
.content .highlighted.struct { background-color: #e7b1a0; }
235235
.content .highlighted.fn { background-color: #c6afb3; }
236236
.content .highlighted.method { background-color: #c6afb3; }
237+
.content .highlighted.type { background-color: #c6afb3; }
237238
.content .highlighted.ffi { background-color: #c6afb3; }
238239

239240
.docblock.short.nowrap {
@@ -306,7 +307,7 @@ nav.sub {
306307
}
307308
.content .methods .docblock { margin-left: 40px; }
308309

309-
.content .impl-methods .docblock { margin-left: 40px; }
310+
.content .impl-items .docblock { margin-left: 40px; }
310311

311312
nav {
312313
border-bottom: 1px solid #e0e0e0;
@@ -440,7 +441,7 @@ h1 .stability {
440441
padding: 4px 10px;
441442
}
442443

443-
.impl-methods .stability, .methods .stability {
444+
.impl-items .stability, .methods .stability {
444445
margin-right: 20px;
445446
}
446447

src/librustdoc/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
#![allow(unknown_features)]
1919
#![feature(globs, macro_rules, phase, slicing_syntax, tuple_indexing)]
20+
#![feature(if_let)]
2021

2122
extern crate arena;
2223
extern crate getopts;

0 commit comments

Comments
 (0)