Skip to content

Commit c02300c

Browse files
Rollup merge of rust-lang#55798 - GuillaumeGomez:version-display-associated-const, r=QuietMisdreavus
Add version display for associated consts Fixes rust-lang#54030. <img width="1440" alt="screenshot 2018-11-08 at 23 57 29" src="https://user-images.githubusercontent.com/3050060/48232648-99decf00-e3b2-11e8-9f41-6bd12a161c7d.png"> r? @QuietMisdreavus
2 parents 09d6ab9 + ca04c63 commit c02300c

File tree

4 files changed

+79
-33
lines changed

4 files changed

+79
-33
lines changed

src/librustdoc/html/render.rs

+50-30
Original file line numberDiff line numberDiff line change
@@ -3037,15 +3037,14 @@ fn item_trait(
30373037
let item_type = m.type_();
30383038
let id = cx.derive_id(format!("{}.{}", item_type, name));
30393039
let ns_id = cx.derive_id(format!("{}.{}", name, item_type.name_space()));
3040-
write!(w, "{extra}<h3 id='{id}' class='method'>\
3041-
<span id='{ns_id}' class='invisible'><code>",
3040+
write!(w, "{extra}<h3 id='{id}' class='method'><code id='{ns_id}'>",
30423041
extra = render_spotlight_traits(m)?,
30433042
id = id,
30443043
ns_id = ns_id)?;
30453044
render_assoc_item(w, m, AssocItemLink::Anchor(Some(&id)), ItemType::Impl)?;
30463045
write!(w, "</code>")?;
30473046
render_stability_since(w, m, t)?;
3048-
write!(w, "</span></h3>")?;
3047+
write!(w, "</h3>")?;
30493048
document(w, cx, m)?;
30503049
Ok(())
30513050
}
@@ -3237,13 +3236,14 @@ fn assoc_type<W: fmt::Write>(w: &mut W, it: &clean::Item,
32373236
Ok(())
32383237
}
32393238

3240-
fn render_stability_since_raw<'a>(w: &mut fmt::Formatter,
3241-
ver: Option<&'a str>,
3242-
containing_ver: Option<&'a str>) -> fmt::Result {
3239+
fn render_stability_since_raw<'a, T: fmt::Write>(
3240+
w: &mut T,
3241+
ver: Option<&'a str>,
3242+
containing_ver: Option<&'a str>,
3243+
) -> fmt::Result {
32433244
if let Some(v) = ver {
32443245
if containing_ver != ver && v.len() > 0 {
3245-
write!(w, "<div class='since' title='Stable since Rust version {0}'>{0}</div>",
3246-
v)?
3246+
write!(w, "<div class='since' title='Stable since Rust version {0}'>{0}</div>", v)?
32473247
}
32483248
}
32493249
Ok(())
@@ -3373,11 +3373,10 @@ fn item_struct(w: &mut fmt::Formatter, cx: &Context, it: &clean::Item,
33733373
let ns_id = cx.derive_id(format!("{}.{}",
33743374
field.name.as_ref().unwrap(),
33753375
ItemType::StructField.name_space()));
3376-
write!(w, "<span id=\"{id}\" class=\"{item_type} small-section-header\">
3377-
<a href=\"#{id}\" class=\"anchor field\"></a>
3378-
<span id=\"{ns_id}\" class='invisible'>
3379-
<code>{name}: {ty}</code>
3380-
</span></span>",
3376+
write!(w, "<span id=\"{id}\" class=\"{item_type} small-section-header\">\
3377+
<a href=\"#{id}\" class=\"anchor field\"></a>\
3378+
<code id=\"{ns_id}\">{name}: {ty}</code>\
3379+
</span>",
33813380
item_type = ItemType::StructField,
33823381
id = id,
33833382
ns_id = ns_id,
@@ -3509,7 +3508,7 @@ fn item_enum(w: &mut fmt::Formatter, cx: &Context, it: &clean::Item,
35093508
ItemType::Variant.name_space()));
35103509
write!(w, "<span id=\"{id}\" class=\"variant small-section-header\">\
35113510
<a href=\"#{id}\" class=\"anchor field\"></a>\
3512-
<span id='{ns_id}' class='invisible'><code>{name}",
3511+
<code id='{ns_id}'>{name}",
35133512
id = id,
35143513
ns_id = ns_id,
35153514
name = variant.name.as_ref().unwrap())?;
@@ -3525,7 +3524,7 @@ fn item_enum(w: &mut fmt::Formatter, cx: &Context, it: &clean::Item,
35253524
write!(w, ")")?;
35263525
}
35273526
}
3528-
write!(w, "</code></span></span>")?;
3527+
write!(w, "</code></span>")?;
35293528
document(w, cx, variant)?;
35303529

35313530
use clean::{Variant, VariantKind};
@@ -3552,8 +3551,8 @@ fn item_enum(w: &mut fmt::Formatter, cx: &Context, it: &clean::Item,
35523551
ItemType::StructField.name_space()));
35533552
write!(w, "<span id=\"{id}\" class=\"variant small-section-header\">\
35543553
<a href=\"#{id}\" class=\"anchor field\"></a>\
3555-
<span id='{ns_id}' class='invisible'><code>{f}:&nbsp;{t}\
3556-
</code></span></span>",
3554+
<code id='{ns_id}'>{f}:&nbsp;{t}\
3555+
</code></span>",
35573556
id = id,
35583557
ns_id = ns_id,
35593558
f = field.name.as_ref().unwrap(),
@@ -3998,7 +3997,7 @@ fn render_impl(w: &mut fmt::Formatter, cx: &Context, i: &Impl, link: AssocItemLi
39983997
id, i.inner_impl())?;
39993998
}
40003999
write!(w, "<a href='#{}' class='anchor'></a>", id)?;
4001-
write!(w, "</span></td><td><span class='out-of-band'>")?;
4000+
write!(w, "</td><td><span class='out-of-band'>")?;
40024001
let since = i.impl_item.stability.as_ref().map(|s| &s.since[..]);
40034002
if let Some(l) = (Item { item: &i.impl_item, cx: cx }).src_href() {
40044003
write!(w, "<div class='ghost'></div>")?;
@@ -4008,7 +4007,7 @@ fn render_impl(w: &mut fmt::Formatter, cx: &Context, i: &Impl, link: AssocItemLi
40084007
} else {
40094008
render_stability_since_raw(w, since, outer_version)?;
40104009
}
4011-
write!(w, "</span></td></tr></tbody></table></h3>")?;
4010+
write!(w, "</span></td></tr></tbody></table></span></h3>")?;
40124011
if let Some(ref dox) = cx.shared.maybe_collapsed_doc_value(&i.impl_item) {
40134012
let mut ids = cx.id_map.borrow_mut();
40144013
write!(w, "<div class='docblock'>{}</div>",
@@ -4044,52 +4043,73 @@ fn render_impl(w: &mut fmt::Formatter, cx: &Context, i: &Impl, link: AssocItemLi
40444043
let ns_id = cx.derive_id(format!("{}.{}", name, item_type.name_space()));
40454044
write!(w, "<h4 id='{}' class=\"{}{}\">", id, item_type, extra_class)?;
40464045
write!(w, "{}", spotlight_decl(decl)?)?;
4047-
write!(w, "<span id='{}' class='invisible'>", ns_id)?;
4048-
write!(w, "<table class='table-display'><tbody><tr><td><code>")?;
4046+
write!(w, "<table id='{}' class='table-display'><tbody><tr><td><code>", ns_id)?;
40494047
render_assoc_item(w, item, link.anchor(&id), ItemType::Impl)?;
40504048
write!(w, "</code>")?;
40514049
if let Some(l) = (Item { cx, item }).src_href() {
4052-
write!(w, "</span></td><td><span class='out-of-band'>")?;
4050+
write!(w, "</td><td><span class='out-of-band'>")?;
40534051
write!(w, "<div class='ghost'></div>")?;
40544052
render_stability_since_raw(w, item.stable_since(), outer_version)?;
4055-
write!(w, "<a class='srclink' href='{}' title='{}'>[src]</a>",
4053+
write!(w, "<a class='srclink' href='{}' title='{}'>[src]</a></span>",
40564054
l, "goto source code")?;
40574055
} else {
40584056
write!(w, "</td><td>")?;
40594057
render_stability_since_raw(w, item.stable_since(), outer_version)?;
40604058
}
4061-
write!(w, "</td></tr></tbody></table></span></h4>")?;
4059+
write!(w, "</td></tr></tbody></table></h4>")?;
40624060
}
40634061
}
40644062
clean::TypedefItem(ref tydef, _) => {
40654063
let id = cx.derive_id(format!("{}.{}", ItemType::AssociatedType, name));
40664064
let ns_id = cx.derive_id(format!("{}.{}", name, item_type.name_space()));
40674065
write!(w, "<h4 id='{}' class=\"{}{}\">", id, item_type, extra_class)?;
4068-
write!(w, "<span id='{}' class='invisible'><code>", ns_id)?;
4066+
write!(w, "<code id='{}'>", ns_id)?;
40694067
assoc_type(w, item, &Vec::new(), Some(&tydef.type_), link.anchor(&id))?;
4070-
write!(w, "</code></span></h4>\n")?;
4068+
write!(w, "</code></h4>")?;
40714069
}
40724070
clean::AssociatedConstItem(ref ty, ref default) => {
4071+
let mut version = String::new();
4072+
4073+
render_stability_since_raw(&mut version, item.stable_since(), outer_version)?;
4074+
40734075
let id = cx.derive_id(format!("{}.{}", item_type, name));
40744076
let ns_id = cx.derive_id(format!("{}.{}", name, item_type.name_space()));
40754077
write!(w, "<h4 id='{}' class=\"{}{}\">", id, item_type, extra_class)?;
4076-
write!(w, "<span id='{}' class='invisible'><code>", ns_id)?;
4078+
if !version.is_empty() {
4079+
write!(w, "<table id='{}' class='table-display'><tbody><tr><td><code>", ns_id)?;
4080+
} else {
4081+
write!(w, "<code id='{}'>", ns_id)?;
4082+
}
40774083
assoc_const(w, item, ty, default.as_ref(), link.anchor(&id))?;
4084+
if !version.is_empty() {
4085+
write!(w, "</code>")?;
4086+
}
40784087
let src = if let Some(l) = (Item { cx, item }).src_href() {
4088+
if !version.is_empty() {
4089+
write!(w, "</td><td><span class='out-of-band'>")?;
4090+
write!(w, "<div class='ghost'></div>{}", version)?;
4091+
}
40794092
format!("<a class='srclink' href='{}' title='{}'>[src]</a>",
40804093
l, "goto source code")
40814094
} else {
4095+
if !version.is_empty() {
4096+
write!(w, "</td><td>{}", version)?;
4097+
}
40824098
String::new()
40834099
};
4084-
write!(w, "</code>{}</span></h4>\n", src)?;
4100+
if version.is_empty() {
4101+
write!(w, "</code>{}</h4>", src)?;
4102+
} else {
4103+
write!(w, "{}</span></td></tr></tbody></table></h4>", src)?;
4104+
}
40854105
}
40864106
clean::AssociatedTypeItem(ref bounds, ref default) => {
40874107
let id = cx.derive_id(format!("{}.{}", item_type, name));
40884108
let ns_id = cx.derive_id(format!("{}.{}", name, item_type.name_space()));
40894109
write!(w, "<h4 id='{}' class=\"{}{}\">", id, item_type, extra_class)?;
4090-
write!(w, "<span id='{}' class='invisible'><code>", ns_id)?;
4110+
write!(w, "<code id='{}'>", ns_id)?;
40914111
assoc_type(w, item, bounds, default.as_ref(), link.anchor(&id))?;
4092-
write!(w, "</code></span></h4>\n")?;
4112+
write!(w, "</code></h4>")?;
40934113
}
40944114
clean::StrippedItem(..) => return Ok(()),
40954115
_ => panic!("can't make docs for trait item with name {:?}", item.name)

src/librustdoc/html/static/rustdoc.css

+1-1
Original file line numberDiff line numberDiff line change
@@ -611,7 +611,7 @@ a {
611611
text-decoration: underline;
612612
}
613613

614-
.invisible > .srclink {
614+
.invisible > .srclink, h4 > code + .srclink {
615615
position: absolute;
616616
top: 0;
617617
right: 0;
+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Copyright 2015 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+
// ignore-tidy-linelength
12+
13+
#![crate_name = "foo"]
14+
15+
#![feature(staged_api)]
16+
17+
#![stable(since="1.1.1", feature="rust1")]
18+
19+
#[stable(since="1.1.1", feature="rust1")]
20+
pub struct SomeStruct;
21+
22+
impl SomeStruct {
23+
// @has 'foo/struct.SomeStruct.html' '//*[@id="SOME_CONST.v"]//div[@class="since"]' '1.1.2'
24+
#[stable(since="1.1.2", feature="rust2")]
25+
pub const SOME_CONST: usize = 0;
26+
}

src/test/rustdoc/assoc-types.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@
1515
// @has assoc_types/trait.Index.html
1616
pub trait Index<I: ?Sized> {
1717
// @has - '//*[@id="associatedtype.Output"]//code' 'type Output: ?Sized'
18-
// @has - '//*[@id="Output.t"]//code' 'type Output: ?Sized'
18+
// @has - '//code[@id="Output.t"]' 'type Output: ?Sized'
1919
type Output: ?Sized;
20-
// @has - '//*[@id="index.v"]//code' 'fn index'
20+
// @has - '//code[@id="index.v"]' 'fn index'
2121
// @has - '//*[@id="tymethod.index"]//code' \
2222
// "fn index<'a>(&'a self, index: I) -> &'a Self::Output"
2323
// @has - '//*[@id="tymethod.index"]//code//a[@href="../assoc_types/trait.Index.html#associatedtype.Output"]' \

0 commit comments

Comments
 (0)