Skip to content

Commit 2416e22

Browse files
committed
Auto merge of #42027 - mjkillough:typedef_assoc_items, r=QuietMisdreavus
Document direct implementations on type aliases. This improves #32077, but is not a complete fix. For a type alias `type NewType = AliasedType`, it will include any `impl NewType` and `impl Trait for NewType` blocks in the documentation for `NewType`. A complete fix would include the implementations from the aliased type in the type alias' documentation, so that users have a complete picture of methods that are available on the alias. However, to do this properly would require a fix for #14072, as the alias may affect the type parameters of the type alias, making the documentation difficult to understand. (That is, for `type Result = std::result::Result<(), ()>` we would ideally show documentation for `impl Result<(), ()>`, rather than generic documentation for `impl<T, E> Result<T, E>`). I think this improvement is worthwhile, as it exposes implementations which are not currently documented by rustdoc. The documentation for the implementations on the aliased type are still accessible by clicking through to the docs for that type. (Although perhaps it's now less obvious to the user that they should click-through to get there).
2 parents 9454dd5 + 2da3501 commit 2416e22

File tree

3 files changed

+58
-2
lines changed

3 files changed

+58
-2
lines changed

src/librustdoc/clean/mod.rs

+3
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,9 @@ impl Item {
311311
pub fn is_ty_method(&self) -> bool {
312312
self.type_() == ItemType::TyMethod
313313
}
314+
pub fn is_typedef(&self) -> bool {
315+
self.type_() == ItemType::Typedef
316+
}
314317
pub fn is_primitive(&self) -> bool {
315318
self.type_() == ItemType::Primitive
316319
}

src/librustdoc/html/render.rs

+20-2
Original file line numberDiff line numberDiff line change
@@ -3082,7 +3082,13 @@ fn item_typedef(w: &mut fmt::Formatter, cx: &Context, it: &clean::Item,
30823082
where_clause = WhereClause { gens: &t.generics, indent: 0, end_newline: true },
30833083
type_ = t.type_)?;
30843084

3085-
document(w, cx, it)
3085+
document(w, cx, it)?;
3086+
3087+
// Render any items associated directly to this alias, as otherwise they
3088+
// won't be visible anywhere in the docs. It would be nice to also show
3089+
// associated items from the aliased type (see discussion in #32077), but
3090+
// we need #14072 to make sense of the generics.
3091+
render_assoc_items(w, cx, it, it.def_id, AssocItemRender::All)
30863092
}
30873093

30883094
impl<'a> fmt::Display for Sidebar<'a> {
@@ -3092,7 +3098,7 @@ impl<'a> fmt::Display for Sidebar<'a> {
30923098
let parentlen = cx.current.len() - if it.is_mod() {1} else {0};
30933099

30943100
if it.is_struct() || it.is_trait() || it.is_primitive() || it.is_union()
3095-
|| it.is_enum() || it.is_mod()
3101+
|| it.is_enum() || it.is_mod() || it.is_typedef()
30963102
{
30973103
write!(fmt, "<p class='location'>")?;
30983104
match it.inner {
@@ -3101,6 +3107,7 @@ impl<'a> fmt::Display for Sidebar<'a> {
31013107
clean::PrimitiveItem(..) => write!(fmt, "Primitive Type ")?,
31023108
clean::UnionItem(..) => write!(fmt, "Union ")?,
31033109
clean::EnumItem(..) => write!(fmt, "Enum ")?,
3110+
clean::TypedefItem(..) => write!(fmt, "Type Definition ")?,
31043111
clean::ModuleItem(..) => if it.is_crate() {
31053112
write!(fmt, "Crate ")?;
31063113
} else {
@@ -3117,6 +3124,7 @@ impl<'a> fmt::Display for Sidebar<'a> {
31173124
clean::PrimitiveItem(ref p) => sidebar_primitive(fmt, it, p)?,
31183125
clean::UnionItem(ref u) => sidebar_union(fmt, it, u)?,
31193126
clean::EnumItem(ref e) => sidebar_enum(fmt, it, e)?,
3127+
clean::TypedefItem(ref t, _) => sidebar_typedef(fmt, it, t)?,
31203128
clean::ModuleItem(ref m) => sidebar_module(fmt, it, &m.items)?,
31213129
_ => (),
31223130
}
@@ -3259,6 +3267,16 @@ fn sidebar_primitive(fmt: &mut fmt::Formatter, it: &clean::Item,
32593267
Ok(())
32603268
}
32613269

3270+
fn sidebar_typedef(fmt: &mut fmt::Formatter, it: &clean::Item,
3271+
_t: &clean::Typedef) -> fmt::Result {
3272+
let sidebar = sidebar_assoc_items(it);
3273+
3274+
if !sidebar.is_empty() {
3275+
write!(fmt, "<div class=\"block items\"><ul>{}</ul></div>", sidebar)?;
3276+
}
3277+
Ok(())
3278+
}
3279+
32623280
fn sidebar_union(fmt: &mut fmt::Formatter, it: &clean::Item,
32633281
u: &clean::Union) -> fmt::Result {
32643282
let mut sidebar = String::new();

src/test/rustdoc/typedef.rs

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
pub trait MyTrait {
12+
fn method_on_mytrait() {}
13+
}
14+
15+
pub struct MyStruct;
16+
17+
impl MyStruct {
18+
pub fn method_on_mystruct() {}
19+
}
20+
21+
// @has typedef/type.MyAlias.html
22+
// @has - '//*[@class="impl"]//code' 'impl MyAlias'
23+
// @has - '//*[@class="impl"]//code' 'impl MyTrait for MyAlias'
24+
// @has - 'Alias docstring'
25+
// @has - '//*[@class="sidebar"]//p[@class="location"]' 'Type Definition MyAlias'
26+
// @has - '//*[@class="sidebar"]//a[@href="#methods"]' 'Methods'
27+
// @has - '//*[@class="sidebar"]//a[@href="#implementations"]' 'Trait Implementations'
28+
/// Alias docstring
29+
pub type MyAlias = MyStruct;
30+
31+
impl MyAlias {
32+
pub fn method_on_myalias() {}
33+
}
34+
35+
impl MyTrait for MyAlias {}

0 commit comments

Comments
 (0)