Skip to content

Commit adf516b

Browse files
Rollup merge of rust-lang#58243 - GuillaumeGomez:trait-alias-docs, r=Manishearth
Add trait alias support in rustdoc Fixes rust-lang#57595. r? @QuietMisdreavus
2 parents cdbd07c + b1d82ac commit adf516b

File tree

10 files changed

+153
-47
lines changed

10 files changed

+153
-47
lines changed

src/librustdoc/clean/mod.rs

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -517,6 +517,7 @@ pub enum ItemEnum {
517517
StaticItem(Static),
518518
ConstantItem(Constant),
519519
TraitItem(Trait),
520+
TraitAliasItem(TraitAlias),
520521
ImplItem(Impl),
521522
/// A method signature only. Used for required methods in traits (ie,
522523
/// non-default-methods).
@@ -554,6 +555,7 @@ impl ItemEnum {
554555
ItemEnum::TyMethodItem(ref i) => &i.generics,
555556
ItemEnum::MethodItem(ref i) => &i.generics,
556557
ItemEnum::ForeignFunctionItem(ref f) => &f.generics,
558+
ItemEnum::TraitAliasItem(ref ta) => &ta.generics,
557559
_ => return None,
558560
})
559561
}
@@ -603,6 +605,7 @@ impl Clean<Item> for doctree::Module {
603605
items.extend(self.impls.iter().flat_map(|x| x.clean(cx)));
604606
items.extend(self.macros.iter().map(|x| x.clean(cx)));
605607
items.extend(self.proc_macros.iter().map(|x| x.clean(cx)));
608+
items.extend(self.trait_aliases.iter().map(|x| x.clean(cx)));
606609

607610
// determine if we should display the inner contents or
608611
// the outer `mod` item for the source code.
@@ -1909,13 +1912,38 @@ impl Clean<Item> for doctree::Trait {
19091912
items: self.items.clean(cx),
19101913
generics: self.generics.clean(cx),
19111914
bounds: self.bounds.clean(cx),
1912-
is_spotlight: is_spotlight,
1915+
is_spotlight,
19131916
is_auto: self.is_auto.clean(cx),
19141917
}),
19151918
}
19161919
}
19171920
}
19181921

1922+
#[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
1923+
pub struct TraitAlias {
1924+
pub generics: Generics,
1925+
pub bounds: Vec<GenericBound>,
1926+
}
1927+
1928+
impl Clean<Item> for doctree::TraitAlias {
1929+
fn clean(&self, cx: &DocContext) -> Item {
1930+
let attrs = self.attrs.clean(cx);
1931+
Item {
1932+
name: Some(self.name.clean(cx)),
1933+
attrs,
1934+
source: self.whence.clean(cx),
1935+
def_id: cx.tcx.hir().local_def_id(self.id),
1936+
visibility: self.vis.clean(cx),
1937+
stability: self.stab.clean(cx),
1938+
deprecation: self.depr.clean(cx),
1939+
inner: TraitAliasItem(TraitAlias {
1940+
generics: self.generics.clean(cx),
1941+
bounds: self.bounds.clean(cx),
1942+
}),
1943+
}
1944+
}
1945+
}
1946+
19191947
impl Clean<bool> for hir::IsAuto {
19201948
fn clean(&self, _: &DocContext) -> bool {
19211949
match *self {
@@ -2247,6 +2275,7 @@ pub enum TypeKind {
22472275
Macro,
22482276
Attr,
22492277
Derive,
2278+
TraitAlias,
22502279
}
22512280

22522281
pub trait GetDefId {
@@ -3858,10 +3887,9 @@ pub fn register_def(cx: &DocContext, def: Def) -> DefId {
38583887
MacroKind::Derive => (i, TypeKind::Derive),
38593888
MacroKind::ProcMacroStub => unreachable!(),
38603889
},
3890+
Def::TraitAlias(i) => (i, TypeKind::TraitAlias),
38613891
Def::SelfTy(Some(def_id), _) => (def_id, TypeKind::Trait),
3862-
Def::SelfTy(_, Some(impl_def_id)) => {
3863-
return impl_def_id
3864-
}
3892+
Def::SelfTy(_, Some(impl_def_id)) => return impl_def_id,
38653893
_ => return def.def_id()
38663894
};
38673895
if did.is_local() { return did }

src/librustdoc/doctree.rs

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ pub struct Module {
3838
pub foreigns: Vec<hir::ForeignMod>,
3939
pub macros: Vec<Macro>,
4040
pub proc_macros: Vec<ProcMacro>,
41+
pub trait_aliases: Vec<TraitAlias>,
4142
pub is_crate: bool,
4243
}
4344

@@ -53,21 +54,22 @@ impl Module {
5354
where_inner: syntax_pos::DUMMY_SP,
5455
attrs : hir::HirVec::new(),
5556
extern_crates: Vec::new(),
56-
imports : Vec::new(),
57-
structs : Vec::new(),
58-
unions : Vec::new(),
59-
enums : Vec::new(),
60-
fns : Vec::new(),
61-
mods : Vec::new(),
62-
typedefs : Vec::new(),
63-
existentials: Vec::new(),
64-
statics : Vec::new(),
65-
constants : Vec::new(),
66-
traits : Vec::new(),
67-
impls : Vec::new(),
68-
foreigns : Vec::new(),
69-
macros : Vec::new(),
70-
proc_macros: Vec::new(),
57+
imports : Vec::new(),
58+
structs : Vec::new(),
59+
unions : Vec::new(),
60+
enums : Vec::new(),
61+
fns : Vec::new(),
62+
mods : Vec::new(),
63+
typedefs : Vec::new(),
64+
existentials: Vec::new(),
65+
statics : Vec::new(),
66+
constants : Vec::new(),
67+
traits : Vec::new(),
68+
impls : Vec::new(),
69+
foreigns : Vec::new(),
70+
macros : Vec::new(),
71+
proc_macros: Vec::new(),
72+
trait_aliases: Vec::new(),
7173
is_crate : false,
7274
}
7375
}
@@ -208,6 +210,18 @@ pub struct Trait {
208210
pub depr: Option<attr::Deprecation>,
209211
}
210212

213+
pub struct TraitAlias {
214+
pub name: Name,
215+
pub generics: hir::Generics,
216+
pub bounds: hir::HirVec<hir::GenericBound>,
217+
pub attrs: hir::HirVec<ast::Attribute>,
218+
pub id: ast::NodeId,
219+
pub whence: Span,
220+
pub vis: hir::Visibility,
221+
pub stab: Option<attr::Stability>,
222+
pub depr: Option<attr::Deprecation>,
223+
}
224+
211225
#[derive(Debug)]
212226
pub struct Impl {
213227
pub unsafety: hir::Unsafety,

src/librustdoc/html/item_type.rs

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ pub enum ItemType {
4242
Existential = 22,
4343
ProcAttribute = 23,
4444
ProcDerive = 24,
45+
TraitAlias = 25,
4546
}
4647

4748

@@ -86,6 +87,7 @@ impl<'a> From<&'a clean::Item> for ItemType {
8687
clean::AssociatedTypeItem(..) => ItemType::AssociatedType,
8788
clean::ForeignTypeItem => ItemType::ForeignType,
8889
clean::KeywordItem(..) => ItemType::Keyword,
90+
clean::TraitAliasItem(..) => ItemType::TraitAlias,
8991
clean::ProcMacroItem(ref mac) => match mac.kind {
9092
MacroKind::Bang => ItemType::Macro,
9193
MacroKind::Attr => ItemType::ProcAttribute,
@@ -100,20 +102,21 @@ impl<'a> From<&'a clean::Item> for ItemType {
100102
impl From<clean::TypeKind> for ItemType {
101103
fn from(kind: clean::TypeKind) -> ItemType {
102104
match kind {
103-
clean::TypeKind::Struct => ItemType::Struct,
104-
clean::TypeKind::Union => ItemType::Union,
105-
clean::TypeKind::Enum => ItemType::Enum,
106-
clean::TypeKind::Function => ItemType::Function,
107-
clean::TypeKind::Trait => ItemType::Trait,
108-
clean::TypeKind::Module => ItemType::Module,
109-
clean::TypeKind::Static => ItemType::Static,
110-
clean::TypeKind::Const => ItemType::Constant,
111-
clean::TypeKind::Variant => ItemType::Variant,
112-
clean::TypeKind::Typedef => ItemType::Typedef,
113-
clean::TypeKind::Foreign => ItemType::ForeignType,
114-
clean::TypeKind::Macro => ItemType::Macro,
115-
clean::TypeKind::Attr => ItemType::ProcAttribute,
116-
clean::TypeKind::Derive => ItemType::ProcDerive,
105+
clean::TypeKind::Struct => ItemType::Struct,
106+
clean::TypeKind::Union => ItemType::Union,
107+
clean::TypeKind::Enum => ItemType::Enum,
108+
clean::TypeKind::Function => ItemType::Function,
109+
clean::TypeKind::Trait => ItemType::Trait,
110+
clean::TypeKind::Module => ItemType::Module,
111+
clean::TypeKind::Static => ItemType::Static,
112+
clean::TypeKind::Const => ItemType::Constant,
113+
clean::TypeKind::Variant => ItemType::Variant,
114+
clean::TypeKind::Typedef => ItemType::Typedef,
115+
clean::TypeKind::Foreign => ItemType::ForeignType,
116+
clean::TypeKind::Macro => ItemType::Macro,
117+
clean::TypeKind::Attr => ItemType::ProcAttribute,
118+
clean::TypeKind::Derive => ItemType::ProcDerive,
119+
clean::TypeKind::TraitAlias => ItemType::TraitAlias,
117120
}
118121
}
119122
}
@@ -146,6 +149,7 @@ impl ItemType {
146149
ItemType::Existential => "existential",
147150
ItemType::ProcAttribute => "attr",
148151
ItemType::ProcDerive => "derive",
152+
ItemType::TraitAlias => "traitalias",
149153
}
150154
}
151155

@@ -160,6 +164,7 @@ impl ItemType {
160164
ItemType::Primitive |
161165
ItemType::AssociatedType |
162166
ItemType::Existential |
167+
ItemType::TraitAlias |
163168
ItemType::ForeignType => NameSpace::Type,
164169

165170
ItemType::ExternCrate |

src/librustdoc/html/render.rs

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1836,6 +1836,7 @@ struct AllTypes {
18361836
keywords: FxHashSet<ItemEntry>,
18371837
attributes: FxHashSet<ItemEntry>,
18381838
derives: FxHashSet<ItemEntry>,
1839+
trait_aliases: FxHashSet<ItemEntry>,
18391840
}
18401841

18411842
impl AllTypes {
@@ -1856,6 +1857,7 @@ impl AllTypes {
18561857
keywords: new_set(100),
18571858
attributes: new_set(100),
18581859
derives: new_set(100),
1860+
trait_aliases: new_set(100),
18591861
}
18601862
}
18611863

@@ -1879,6 +1881,7 @@ impl AllTypes {
18791881
ItemType::Constant => self.constants.insert(ItemEntry::new(new_url, name)),
18801882
ItemType::ProcAttribute => self.attributes.insert(ItemEntry::new(new_url, name)),
18811883
ItemType::ProcDerive => self.derives.insert(ItemEntry::new(new_url, name)),
1884+
ItemType::TraitAlias => self.trait_aliases.insert(ItemEntry::new(new_url, name)),
18821885
_ => true,
18831886
};
18841887
}
@@ -1922,6 +1925,7 @@ impl fmt::Display for AllTypes {
19221925
print_entries(f, &self.derives, "Derive Macros", "derives")?;
19231926
print_entries(f, &self.functions, "Functions", "functions")?;
19241927
print_entries(f, &self.typedefs, "Typedefs", "typedefs")?;
1928+
print_entries(f, &self.trait_aliases, "Trait Aliases", "trait-aliases")?;
19251929
print_entries(f, &self.existentials, "Existentials", "existentials")?;
19261930
print_entries(f, &self.statics, "Statics", "statics")?;
19271931
print_entries(f, &self.constants, "Constants", "constants")
@@ -2419,6 +2423,7 @@ impl<'a> fmt::Display for Item<'a> {
24192423
clean::ForeignTypeItem => write!(fmt, "Foreign Type ")?,
24202424
clean::KeywordItem(..) => write!(fmt, "Keyword ")?,
24212425
clean::ExistentialItem(..) => write!(fmt, "Existential Type ")?,
2426+
clean::TraitAliasItem(..) => write!(fmt, "Trait Alias ")?,
24222427
_ => {
24232428
// We don't generate pages for any other type.
24242429
unreachable!();
@@ -2457,6 +2462,7 @@ impl<'a> fmt::Display for Item<'a> {
24572462
clean::ForeignTypeItem => item_foreign_type(fmt, self.cx, self.item),
24582463
clean::KeywordItem(ref k) => item_keyword(fmt, self.cx, self.item, k),
24592464
clean::ExistentialItem(ref e, _) => item_existential(fmt, self.cx, self.item, e),
2465+
clean::TraitAliasItem(ref ta) => item_trait_alias(fmt, self.cx, self.item, ta),
24602466
_ => {
24612467
// We don't generate pages for any other type.
24622468
unreachable!();
@@ -3015,23 +3021,17 @@ fn render_impls(cx: &Context, w: &mut fmt::Formatter,
30153021
Ok(())
30163022
}
30173023

3018-
fn bounds(t_bounds: &[clean::GenericBound]) -> String {
3024+
fn bounds(t_bounds: &[clean::GenericBound], trait_alias: bool) -> String {
30193025
let mut bounds = String::new();
3020-
let mut bounds_plain = String::new();
30213026
if !t_bounds.is_empty() {
3022-
if !bounds.is_empty() {
3023-
bounds.push(' ');
3024-
bounds_plain.push(' ');
3027+
if !trait_alias {
3028+
bounds.push_str(": ");
30253029
}
3026-
bounds.push_str(": ");
3027-
bounds_plain.push_str(": ");
30283030
for (i, p) in t_bounds.iter().enumerate() {
30293031
if i > 0 {
30303032
bounds.push_str(" + ");
3031-
bounds_plain.push_str(" + ");
30323033
}
30333034
bounds.push_str(&(*p).to_string());
3034-
bounds_plain.push_str(&format!("{:#}", *p));
30353035
}
30363036
}
30373037
bounds
@@ -3051,7 +3051,7 @@ fn item_trait(
30513051
it: &clean::Item,
30523052
t: &clean::Trait,
30533053
) -> fmt::Result {
3054-
let bounds = bounds(&t.bounds);
3054+
let bounds = bounds(&t.bounds, false);
30553055
let types = t.items.iter().filter(|m| m.is_associated_type()).collect::<Vec<_>>();
30563056
let consts = t.items.iter().filter(|m| m.is_associated_const()).collect::<Vec<_>>();
30573057
let required = t.items.iter().filter(|m| m.is_ty_method()).collect::<Vec<_>>();
@@ -4282,7 +4282,26 @@ fn item_existential(
42824282
it.name.as_ref().unwrap(),
42834283
t.generics,
42844284
where_clause = WhereClause { gens: &t.generics, indent: 0, end_newline: true },
4285-
bounds = bounds(&t.bounds))?;
4285+
bounds = bounds(&t.bounds, false))?;
4286+
4287+
document(w, cx, it)?;
4288+
4289+
// Render any items associated directly to this alias, as otherwise they
4290+
// won't be visible anywhere in the docs. It would be nice to also show
4291+
// associated items from the aliased type (see discussion in #32077), but
4292+
// we need #14072 to make sense of the generics.
4293+
render_assoc_items(w, cx, it, it.def_id, AssocItemRender::All)
4294+
}
4295+
4296+
fn item_trait_alias(w: &mut fmt::Formatter, cx: &Context, it: &clean::Item,
4297+
t: &clean::TraitAlias) -> fmt::Result {
4298+
write!(w, "<pre class='rust trait-alias'>")?;
4299+
render_attributes(w, it)?;
4300+
write!(w, "trait {}{}{} = {};</pre>",
4301+
it.name.as_ref().unwrap(),
4302+
t.generics,
4303+
WhereClause { gens: &t.generics, indent: 0, end_newline: true },
4304+
bounds(&t.bounds, true))?;
42864305

42874306
document(w, cx, it)?;
42884307

@@ -4846,6 +4865,7 @@ fn item_ty_to_strs(ty: &ItemType) -> (&'static str, &'static str) {
48464865
ItemType::Existential => ("existentials", "Existentials"),
48474866
ItemType::ProcAttribute => ("attributes", "Attribute Macros"),
48484867
ItemType::ProcDerive => ("derives", "Derive Macros"),
4868+
ItemType::TraitAlias => ("trait-aliases", "Trait aliases"),
48494869
}
48504870
}
48514871

src/librustdoc/html/static/main.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,8 @@ if (!DOMTokenList.prototype.remove) {
6868
"keyword",
6969
"existential",
7070
"attr",
71-
"derive"];
71+
"derive",
72+
"traitalias"];
7273

7374
var search_input = document.getElementsByClassName("search-input")[0];
7475

@@ -1786,6 +1787,7 @@ if (!DOMTokenList.prototype.remove) {
17861787
block("type", "Type Definitions");
17871788
block("foreigntype", "Foreign Types");
17881789
block("keyword", "Keywords");
1790+
block("traitalias", "Trait Aliases");
17891791
}
17901792

17911793
window.initSidebarItems = initSidebarItems;

src/librustdoc/html/static/themes/dark.css

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ pre {
9494
}
9595
.content .highlighted a, .content .highlighted span { color: #eee !important; }
9696
.content .highlighted.trait { background-color: #013191; }
97+
.content .highlighted.traitalias { background-color: #013191; }
9798
.content .highlighted.mod,
9899
.content .highlighted.externcrate { background-color: #afc6e4; }
99100
.content .highlighted.mod { background-color: #803a1b; }
@@ -128,6 +129,7 @@ pre {
128129
.content span.externcrate,
129130
.content span.mod, .content a.mod, .block a.current.mod { color: #bda000; }
130131
.content span.trait, .content a.trait, .block a.current.trait { color: #b78cf2; }
132+
.content span.traitalias, .content a.traitalias, .block a.current.traitalias { color: #b397da; }
131133
.content span.fn, .content a.fn, .block a.current.fn,
132134
.content span.method, .content a.method, .block a.current.method,
133135
.content span.tymethod, .content a.tymethod, .block a.current.tymethod,

src/librustdoc/html/static/themes/light.css

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ pre {
9696
}
9797
.content .highlighted a, .content .highlighted span { color: #000 !important; }
9898
.content .highlighted.trait { background-color: #c7b6ff; }
99+
.content .highlighted.traitalias { background-color: #c7b6ff; }
99100
.content .highlighted.mod,
100101
.content .highlighted.externcrate { background-color: #afc6e4; }
101102
.content .highlighted.enum { background-color: #b4d1b9; }
@@ -128,6 +129,7 @@ pre {
128129
.content span.externcrate,
129130
.content span.mod, .content a.mod, .block a.current.mod { color: #4d76ae; }
130131
.content span.trait, .content a.trait, .block a.current.trait { color: #7c5af3; }
132+
.content span.traitalias, .content a.traitalias, .block a.current.traitalias { color: #6841f1; }
131133
.content span.fn, .content a.fn, .block a.current.fn,
132134
.content span.method, .content a.method, .block a.current.method,
133135
.content span.tymethod, .content a.tymethod, .block a.current.tymethod,

src/librustdoc/passes/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,7 @@ impl<'a> fold::DocFolder for Stripper<'a> {
224224
| clean::ConstantItem(..)
225225
| clean::UnionItem(..)
226226
| clean::AssociatedConstItem(..)
227+
| clean::TraitAliasItem(..)
227228
| clean::ForeignTypeItem => {
228229
if i.def_id.is_local() {
229230
if !self.access_levels.is_exported(i.def_id) {

0 commit comments

Comments
 (0)