Skip to content

Add version display for associated consts #55798

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 50 additions & 30 deletions src/librustdoc/html/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3037,15 +3037,14 @@ fn item_trait(
let item_type = m.type_();
let id = cx.derive_id(format!("{}.{}", item_type, name));
let ns_id = cx.derive_id(format!("{}.{}", name, item_type.name_space()));
write!(w, "{extra}<h3 id='{id}' class='method'>\
<span id='{ns_id}' class='invisible'><code>",
write!(w, "{extra}<h3 id='{id}' class='method'><code id='{ns_id}'>",
extra = render_spotlight_traits(m)?,
id = id,
ns_id = ns_id)?;
render_assoc_item(w, m, AssocItemLink::Anchor(Some(&id)), ItemType::Impl)?;
write!(w, "</code>")?;
render_stability_since(w, m, t)?;
write!(w, "</span></h3>")?;
write!(w, "</h3>")?;
document(w, cx, m)?;
Ok(())
}
Expand Down Expand Up @@ -3237,13 +3236,14 @@ fn assoc_type<W: fmt::Write>(w: &mut W, it: &clean::Item,
Ok(())
}

fn render_stability_since_raw<'a>(w: &mut fmt::Formatter,
ver: Option<&'a str>,
containing_ver: Option<&'a str>) -> fmt::Result {
fn render_stability_since_raw<'a, T: fmt::Write>(
w: &mut T,
ver: Option<&'a str>,
containing_ver: Option<&'a str>,
) -> fmt::Result {
if let Some(v) = ver {
if containing_ver != ver && v.len() > 0 {
write!(w, "<div class='since' title='Stable since Rust version {0}'>{0}</div>",
v)?
write!(w, "<div class='since' title='Stable since Rust version {0}'>{0}</div>", v)?
}
}
Ok(())
Expand Down Expand Up @@ -3373,11 +3373,10 @@ fn item_struct(w: &mut fmt::Formatter, cx: &Context, it: &clean::Item,
let ns_id = cx.derive_id(format!("{}.{}",
field.name.as_ref().unwrap(),
ItemType::StructField.name_space()));
write!(w, "<span id=\"{id}\" class=\"{item_type} small-section-header\">
<a href=\"#{id}\" class=\"anchor field\"></a>
<span id=\"{ns_id}\" class='invisible'>
<code>{name}: {ty}</code>
</span></span>",
write!(w, "<span id=\"{id}\" class=\"{item_type} small-section-header\">\
<a href=\"#{id}\" class=\"anchor field\"></a>\
<code id=\"{ns_id}\">{name}: {ty}</code>\
</span>",
item_type = ItemType::StructField,
id = id,
ns_id = ns_id,
Expand Down Expand Up @@ -3509,7 +3508,7 @@ fn item_enum(w: &mut fmt::Formatter, cx: &Context, it: &clean::Item,
ItemType::Variant.name_space()));
write!(w, "<span id=\"{id}\" class=\"variant small-section-header\">\
<a href=\"#{id}\" class=\"anchor field\"></a>\
<span id='{ns_id}' class='invisible'><code>{name}",
<code id='{ns_id}'>{name}",
id = id,
ns_id = ns_id,
name = variant.name.as_ref().unwrap())?;
Expand All @@ -3525,7 +3524,7 @@ fn item_enum(w: &mut fmt::Formatter, cx: &Context, it: &clean::Item,
write!(w, ")")?;
}
}
write!(w, "</code></span></span>")?;
write!(w, "</code></span>")?;
document(w, cx, variant)?;

use clean::{Variant, VariantKind};
Expand All @@ -3552,8 +3551,8 @@ fn item_enum(w: &mut fmt::Formatter, cx: &Context, it: &clean::Item,
ItemType::StructField.name_space()));
write!(w, "<span id=\"{id}\" class=\"variant small-section-header\">\
<a href=\"#{id}\" class=\"anchor field\"></a>\
<span id='{ns_id}' class='invisible'><code>{f}:&nbsp;{t}\
</code></span></span>",
<code id='{ns_id}'>{f}:&nbsp;{t}\
</code></span>",
id = id,
ns_id = ns_id,
f = field.name.as_ref().unwrap(),
Expand Down Expand Up @@ -3998,7 +3997,7 @@ fn render_impl(w: &mut fmt::Formatter, cx: &Context, i: &Impl, link: AssocItemLi
id, i.inner_impl())?;
}
write!(w, "<a href='#{}' class='anchor'></a>", id)?;
write!(w, "</span></td><td><span class='out-of-band'>")?;
write!(w, "</td><td><span class='out-of-band'>")?;
let since = i.impl_item.stability.as_ref().map(|s| &s.since[..]);
if let Some(l) = (Item { item: &i.impl_item, cx: cx }).src_href() {
write!(w, "<div class='ghost'></div>")?;
Expand All @@ -4008,7 +4007,7 @@ fn render_impl(w: &mut fmt::Formatter, cx: &Context, i: &Impl, link: AssocItemLi
} else {
render_stability_since_raw(w, since, outer_version)?;
}
write!(w, "</span></td></tr></tbody></table></h3>")?;
write!(w, "</span></td></tr></tbody></table></span></h3>")?;
if let Some(ref dox) = cx.shared.maybe_collapsed_doc_value(&i.impl_item) {
let mut ids = cx.id_map.borrow_mut();
write!(w, "<div class='docblock'>{}</div>",
Expand Down Expand Up @@ -4044,52 +4043,73 @@ fn render_impl(w: &mut fmt::Formatter, cx: &Context, i: &Impl, link: AssocItemLi
let ns_id = cx.derive_id(format!("{}.{}", name, item_type.name_space()));
write!(w, "<h4 id='{}' class=\"{}{}\">", id, item_type, extra_class)?;
write!(w, "{}", spotlight_decl(decl)?)?;
write!(w, "<span id='{}' class='invisible'>", ns_id)?;
write!(w, "<table class='table-display'><tbody><tr><td><code>")?;
write!(w, "<table id='{}' class='table-display'><tbody><tr><td><code>", ns_id)?;
render_assoc_item(w, item, link.anchor(&id), ItemType::Impl)?;
write!(w, "</code>")?;
if let Some(l) = (Item { cx, item }).src_href() {
write!(w, "</span></td><td><span class='out-of-band'>")?;
write!(w, "</td><td><span class='out-of-band'>")?;
write!(w, "<div class='ghost'></div>")?;
render_stability_since_raw(w, item.stable_since(), outer_version)?;
write!(w, "<a class='srclink' href='{}' title='{}'>[src]</a>",
write!(w, "<a class='srclink' href='{}' title='{}'>[src]</a></span>",
l, "goto source code")?;
} else {
write!(w, "</td><td>")?;
render_stability_since_raw(w, item.stable_since(), outer_version)?;
}
write!(w, "</td></tr></tbody></table></span></h4>")?;
write!(w, "</td></tr></tbody></table></h4>")?;
}
}
clean::TypedefItem(ref tydef, _) => {
let id = cx.derive_id(format!("{}.{}", ItemType::AssociatedType, name));
let ns_id = cx.derive_id(format!("{}.{}", name, item_type.name_space()));
write!(w, "<h4 id='{}' class=\"{}{}\">", id, item_type, extra_class)?;
write!(w, "<span id='{}' class='invisible'><code>", ns_id)?;
write!(w, "<code id='{}'>", ns_id)?;
assoc_type(w, item, &Vec::new(), Some(&tydef.type_), link.anchor(&id))?;
write!(w, "</code></span></h4>\n")?;
write!(w, "</code></h4>")?;
}
clean::AssociatedConstItem(ref ty, ref default) => {
let mut version = String::new();

render_stability_since_raw(&mut version, item.stable_since(), outer_version)?;

let id = cx.derive_id(format!("{}.{}", item_type, name));
let ns_id = cx.derive_id(format!("{}.{}", name, item_type.name_space()));
write!(w, "<h4 id='{}' class=\"{}{}\">", id, item_type, extra_class)?;
write!(w, "<span id='{}' class='invisible'><code>", ns_id)?;
if !version.is_empty() {
write!(w, "<table id='{}' class='table-display'><tbody><tr><td><code>", ns_id)?;
} else {
write!(w, "<code id='{}'>", ns_id)?;
}
assoc_const(w, item, ty, default.as_ref(), link.anchor(&id))?;
if !version.is_empty() {
write!(w, "</code>")?;
}
let src = if let Some(l) = (Item { cx, item }).src_href() {
if !version.is_empty() {
write!(w, "</td><td><span class='out-of-band'>")?;
write!(w, "<div class='ghost'></div>{}", version)?;
}
format!("<a class='srclink' href='{}' title='{}'>[src]</a>",
l, "goto source code")
} else {
if !version.is_empty() {
write!(w, "</td><td>{}", version)?;
}
String::new()
};
write!(w, "</code>{}</span></h4>\n", src)?;
if version.is_empty() {
write!(w, "</code>{}</h4>", src)?;
} else {
write!(w, "{}</span></td></tr></tbody></table></h4>", src)?;
}
}
clean::AssociatedTypeItem(ref bounds, ref default) => {
let id = cx.derive_id(format!("{}.{}", item_type, name));
let ns_id = cx.derive_id(format!("{}.{}", name, item_type.name_space()));
write!(w, "<h4 id='{}' class=\"{}{}\">", id, item_type, extra_class)?;
write!(w, "<span id='{}' class='invisible'><code>", ns_id)?;
write!(w, "<code id='{}'>", ns_id)?;
assoc_type(w, item, bounds, default.as_ref(), link.anchor(&id))?;
write!(w, "</code></span></h4>\n")?;
write!(w, "</code></h4>")?;
}
clean::StrippedItem(..) => return Ok(()),
_ => panic!("can't make docs for trait item with name {:?}", item.name)
Expand Down
2 changes: 1 addition & 1 deletion src/librustdoc/html/static/rustdoc.css
Original file line number Diff line number Diff line change
Expand Up @@ -611,7 +611,7 @@ a {
text-decoration: underline;
}

.invisible > .srclink {
.invisible > .srclink, h4 > code + .srclink {
position: absolute;
top: 0;
right: 0;
Expand Down
26 changes: 26 additions & 0 deletions src/test/rustdoc/assoc-consts-version.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// ignore-tidy-linelength

#![crate_name = "foo"]

#![feature(staged_api)]

#![stable(since="1.1.1", feature="rust1")]

#[stable(since="1.1.1", feature="rust1")]
pub struct SomeStruct;

impl SomeStruct {
// @has 'foo/struct.SomeStruct.html' '//*[@id="SOME_CONST.v"]//div[@class="since"]' '1.1.2'
#[stable(since="1.1.2", feature="rust2")]
pub const SOME_CONST: usize = 0;
}
4 changes: 2 additions & 2 deletions src/test/rustdoc/assoc-types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
// @has assoc_types/trait.Index.html
pub trait Index<I: ?Sized> {
// @has - '//*[@id="associatedtype.Output"]//code' 'type Output: ?Sized'
// @has - '//*[@id="Output.t"]//code' 'type Output: ?Sized'
// @has - '//code[@id="Output.t"]' 'type Output: ?Sized'
type Output: ?Sized;
// @has - '//*[@id="index.v"]//code' 'fn index'
// @has - '//code[@id="index.v"]' 'fn index'
// @has - '//*[@id="tymethod.index"]//code' \
// "fn index<'a>(&'a self, index: I) -> &'a Self::Output"
// @has - '//*[@id="tymethod.index"]//code//a[@href="../assoc_types/trait.Index.html#associatedtype.Output"]' \
Expand Down