Skip to content

Commit 0e3a2c0

Browse files
committed
Linkify associated types and constants
1 parent 1bd8183 commit 0e3a2c0

File tree

2 files changed

+45
-18
lines changed

2 files changed

+45
-18
lines changed

src/librustdoc/html/render.rs

Lines changed: 41 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2021,10 +2021,33 @@ fn item_trait(w: &mut fmt::Formatter, cx: &Context, it: &clean::Item,
20212021
Ok(())
20222022
}
20232023

2024-
fn assoc_const(w: &mut fmt::Formatter, it: &clean::Item,
2025-
ty: &clean::Type, default: Option<&String>)
2026-
-> fmt::Result {
2027-
write!(w, "const {}", it.name.as_ref().unwrap())?;
2024+
fn naive_assoc_href(it: &clean::Item, link: AssocItemLink) -> String {
2025+
use html::item_type::ItemType::*;
2026+
2027+
let name = it.name.as_ref().unwrap();
2028+
let ty = match shortty(it) {
2029+
Typedef | AssociatedType => AssociatedType,
2030+
s@_ => s,
2031+
};
2032+
2033+
let anchor = format!("#{}.{}", ty, name);
2034+
match link {
2035+
AssocItemLink::Anchor => anchor,
2036+
AssocItemLink::GotoSource(did, _) => {
2037+
href(did).map(|p| format!("{}{}", p.0, anchor)).unwrap_or(anchor)
2038+
}
2039+
}
2040+
}
2041+
2042+
fn assoc_const(w: &mut fmt::Formatter,
2043+
it: &clean::Item,
2044+
ty: &clean::Type,
2045+
default: Option<&String>,
2046+
link: AssocItemLink) -> fmt::Result {
2047+
write!(w, "const <a href='{}' class='constant'>{}</a>",
2048+
naive_assoc_href(it, link),
2049+
it.name.as_ref().unwrap())?;
2050+
20282051
write!(w, ": {}", ty)?;
20292052
if let Some(default) = default {
20302053
write!(w, " = {}", default)?;
@@ -2034,13 +2057,15 @@ fn assoc_const(w: &mut fmt::Formatter, it: &clean::Item,
20342057

20352058
fn assoc_type(w: &mut fmt::Formatter, it: &clean::Item,
20362059
bounds: &Vec<clean::TyParamBound>,
2037-
default: &Option<clean::Type>)
2038-
-> fmt::Result {
2039-
write!(w, "type {}", it.name.as_ref().unwrap())?;
2060+
default: Option<&clean::Type>,
2061+
link: AssocItemLink) -> fmt::Result {
2062+
write!(w, "type <a href='{}' class='type'>{}</a>",
2063+
naive_assoc_href(it, link),
2064+
it.name.as_ref().unwrap())?;
20402065
if !bounds.is_empty() {
20412066
write!(w, ": {}", TyParamBounds(bounds))?
20422067
}
2043-
if let Some(ref default) = *default {
2068+
if let Some(default) = default {
20442069
write!(w, " = {}", default)?;
20452070
}
20462071
Ok(())
@@ -2095,6 +2120,7 @@ fn render_assoc_item(w: &mut fmt::Formatter,
20952120
href(did).map(|p| format!("{}#{}.{}", p.0, ty, name)).unwrap_or(anchor)
20962121
}
20972122
};
2123+
// FIXME(#24111): remove when `const_fn` is stabilized
20982124
let vis_constness = match get_unstable_features_setting() {
20992125
UnstableFeatures::Allow => constness,
21002126
_ => hir::Constness::NotConst
@@ -2124,10 +2150,10 @@ fn render_assoc_item(w: &mut fmt::Formatter,
21242150
link)
21252151
}
21262152
clean::AssociatedConstItem(ref ty, ref default) => {
2127-
assoc_const(w, item, ty, default.as_ref())
2153+
assoc_const(w, item, ty, default.as_ref(), link)
21282154
}
21292155
clean::AssociatedTypeItem(ref bounds, ref default) => {
2130-
assoc_type(w, item, bounds, default)
2156+
assoc_type(w, item, bounds, default.as_ref(), link)
21312157
}
21322158
_ => panic!("render_assoc_item called on non-associated-item")
21332159
}
@@ -2487,25 +2513,25 @@ fn render_impl(w: &mut fmt::Formatter, cx: &Context, i: &Impl, link: AssocItemLi
24872513
clean::TypedefItem(ref tydef, _) => {
24882514
let id = derive_id(format!("{}.{}", ItemType::AssociatedType, name));
24892515
write!(w, "<h4 id='{}' class='{}'><code>", id, shortty)?;
2490-
write!(w, "type {} = {}", name, tydef.type_)?;
2516+
assoc_type(w, item, &Vec::new(), Some(&tydef.type_), link)?;
24912517
write!(w, "</code></h4>\n")?;
24922518
}
24932519
clean::AssociatedConstItem(ref ty, ref default) => {
24942520
let id = derive_id(format!("{}.{}", shortty, name));
24952521
write!(w, "<h4 id='{}' class='{}'><code>", id, shortty)?;
2496-
assoc_const(w, item, ty, default.as_ref())?;
2522+
assoc_const(w, item, ty, default.as_ref(), link)?;
24972523
write!(w, "</code></h4>\n")?;
24982524
}
24992525
clean::ConstantItem(ref c) => {
25002526
let id = derive_id(format!("{}.{}", shortty, name));
25012527
write!(w, "<h4 id='{}' class='{}'><code>", id, shortty)?;
2502-
assoc_const(w, item, &c.type_, Some(&c.expr))?;
2528+
assoc_const(w, item, &c.type_, Some(&c.expr), link)?;
25032529
write!(w, "</code></h4>\n")?;
25042530
}
25052531
clean::AssociatedTypeItem(ref bounds, ref default) => {
25062532
let id = derive_id(format!("{}.{}", shortty, name));
25072533
write!(w, "<h4 id='{}' class='{}'><code>", id, shortty)?;
2508-
assoc_type(w, item, bounds, default)?;
2534+
assoc_type(w, item, bounds, default.as_ref(), link)?;
25092535
write!(w, "</code></h4>\n")?;
25102536
}
25112537
_ => panic!("can't make docs for trait item with name {:?}", item.name)
@@ -2545,9 +2571,7 @@ fn render_impl(w: &mut fmt::Formatter, cx: &Context, i: &Impl, link: AssocItemLi
25452571
}
25462572

25472573
// If we've implemented a trait, then also emit documentation for all
2548-
// default methods which weren't overridden in the implementation block.
2549-
// FIXME: this also needs to be done for associated types, whenever defaults
2550-
// for them work.
2574+
// default items which weren't overridden in the implementation block.
25512575
if let Some(did) = i.trait_did() {
25522576
if let Some(t) = cache().traits.get(&did) {
25532577
render_default_items(w, cx, t, &i.impl_, render_header, outer_version)?;

src/test/rustdoc/issue-28478.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@
1414
// @has issue_28478/trait.Bar.html
1515
pub trait Bar {
1616
// @has - '//*[@id="associatedtype.Bar"]' 'type Bar = ()'
17+
// @has - '//*[@href="#associatedtype.Bar"]' 'Bar'
1718
type Bar = ();
18-
1919
// @has - '//*[@id="associatedconstant.Baz"]' 'const Baz: usize = 7'
20+
// @has - '//*[@href="#associatedconstant.Baz"]' 'Baz'
2021
const Baz: usize = 7;
2122
// @has - '//*[@id="tymethod.bar"]' 'fn bar'
2223
fn bar();
@@ -33,6 +34,8 @@ impl Foo {
3334
}
3435

3536
impl Bar for Foo {
37+
// @has - '//*[@href="../issue_28478/trait.Bar.html#associatedtype.Bar"]' 'Bar'
38+
// @has - '//*[@href="../issue_28478/trait.Bar.html#associatedconstant.Baz"]' 'Baz'
3639
// @has - '//*[@href="../issue_28478/trait.Bar.html#tymethod.bar"]' 'bar'
3740
fn bar() {}
3841
// @has - '//*[@href="../issue_28478/trait.Bar.html#method.baz"]' 'baz'

0 commit comments

Comments
 (0)